Nucleo-L552ZE-Q: gap-free ADC capture with DMA double-buffer mode
Bare-metal C on the STM32L552 (Cortex-M33 at 110 MHz): a timer paces the ADC, the DMA fills two buffers in turn through DMAMUX, and the CPU reads whichever…
Sensors · Advanced
In a real product: Vibration logger on a pump (Industrial): Samples keep arriving at a fixed rate while the code works on the last block, so no sample is lost and none is read half-written.
Capturing a signal without gaps is harder than it looks. If one buffer is being filled while your code reads it, the two get mixed up. The usual answer is two buffers: the hardware fills one while the code works on the other, then they swap. The STM32L5's DMA does the swapping itself. This project samples a potentiometer 1,000 times a second, in blocks of eight, and prints the average of each finished block.
What you'll learn
- Starting the STM32L5 from its 4 MHz MSI clock, and what port G's separate supply means for Serial
- Waking the ADC: deep power-down, the regulator, calibration, then enable
- Pacing conversions with a timer's TRGO instead of a software start
- Routing a request through DMAMUX to a DMA channel
- Double-buffer mode:
DBM,CM0AR,CM1AR, andCTsaying which buffer is in use
How it works
TIM6 counts at 10 kHz and overflows every 10 counts. With MMS at 010 its update becomes TRGO, and the ADC's EXTSEL value 13 picks TIM6_TRGO, so each overflow starts one conversion. The clock here is the chip's default, so the timer's prescaler is sized from SystemCoreClock.
Each result raises a DMA request. On this chip a request does not go straight to a channel: DMAMUX1_Channel0->CCR chooses which request DMA1's channel 1 takes, and 5 is ADC1. On Serial's side, PG7 belongs to port G, which has its own supply: until PWR->CR2 IOSV is set, PG2 to PG15 do nothing at all.
With DBM set, the channel runs for ever. It fills CM0AR for CNDTR samples, sets TCIF1, toggles CT, and goes on with CM1AR. So when the transfer-complete interrupt fires, CT names the buffer now being filled and the other one is complete. The handler only remembers which that is; the main loop does the printing, so a slow serial port never holds up an interrupt.
The ADC has OVRMOD set, so a late read never blocks a new result, and DMACFG set so its requests carry on past the end of a buffer.
Try this
- Turn the potentiometer while it runs and watch the averages follow.
- Change
TIM6->ARRto sample ten times faster. How long does a block take now? - Print the two buffers' first samples too, to see them taking turns.
- Clear
DMA_CCR_DBMandDMA_CCR_CIRC: one block is captured and then the channel stops. - Print
SCB->CPUIDand compare with the Cortex-M4 and Cortex-M7 boards. This chip has the same FPU instructions but no bit-banding.
main.c
// Bare-metal C on the Nucleo-L552ZE-Q: no Arduino core and no HAL. CMSIS names every register.
// TIM6 paces ADC1; DMA1 channel 1 fills two buffers in turn. The CPU reads the one that has finished.
#include <stdio.h>
#include "stm32l5xx.h"
#define BLOCK 8
static uint16_t buffers[2][BLOCK];
static volatile int finished = -1; // the block ready to read, or -1
void DMA1_Channel1_IRQHandler(void) {
DMA1->IFCR = DMA_IFCR_CTCIF1;
// CT names the buffer now being filled, so the other one is complete.
finished = (DMA1_Channel1->CCR & DMA_CCR_CT) ? 0 : 1;
}
// printf sends each character here: LPUART1 on PG7, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
while (!(LPUART1->ISR & USART_ISR_TXE)) {}
LPUART1->TDR = ch;
return ch;
}
static void serial(void) {
RCC->APB1ENR1 |= RCC_APB1ENR1_PWREN;
PWR->CR2 |= PWR_CR2_IOSV; // PG2-PG15 run from VDDIO2, off at reset
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOGEN;
RCC->APB1ENR2 |= RCC_APB1ENR2_LPUART1EN;
GPIOG->MODER = (GPIOG->MODER & ~GPIO_MODER_MODE7) | GPIO_MODER_MODE7_1;
GPIOG->AFR[0] = (GPIOG->AFR[0] & ~(0xFu << 28)) | (8u << 28); // PG7: AF8, LPUART1 TX
LPUART1->BRR = (uint32_t)(256ull * SystemCoreClock / 115200); // 256 x the clock over the baud rate
LPUART1->CR1 = USART_CR1_TE | USART_CR1_UE;
}
static void adc_on(void) {
RCC->AHB2ENR |= RCC_AHB2ENR_ADCEN;
RCC->CCIPR1 |= 3u << RCC_CCIPR1_ADCSEL_Pos; // the ADC on the system clock
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
ADC1->CR &= ~ADC_CR_DEEPPWD;
ADC1->CR |= ADC_CR_ADVREGEN;
for (volatile int i = 0; i < 200; i++) {} // the regulator's start-up time
ADC1->CR |= ADC_CR_ADCAL;
while (ADC1->CR & ADC_CR_ADCAL) {}
ADC1->CR |= ADC_CR_ADEN;
while (!(ADC1->ISR & ADC_ISR_ADRDY)) {}
ADC1->SMPR1 = 0;
ADC1->SMPR2 = 0;
ADC1->SMPR1 |= 7u << ADC_SMPR1_SMP8_Pos; // channel 8 (PA3, A0), the longest sample time
ADC1->SQR1 = 8u << ADC_SQR1_SQ1_Pos; // one conversion: channel 8
ADC1->CFGR = ADC_CFGR_DMAEN | ADC_CFGR_DMACFG | ADC_CFGR_OVRMOD | (1u << ADC_CFGR_EXTEN_Pos) | (13u << ADC_CFGR_EXTSEL_Pos);
}
static void dma_on(void) {
RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN | RCC_AHB1ENR_DMAMUX1EN;
DMAMUX1_Channel0->CCR = 5; // DMA1 channel 1 takes ADC1's request
DMA1_Channel1->CPAR = (uint32_t)&ADC1->DR;
DMA1_Channel1->CM0AR = (uint32_t)buffers[0];
DMA1_Channel1->CM1AR = (uint32_t)buffers[1];
DMA1_Channel1->CNDTR = BLOCK;
DMA1_Channel1->CCR = DMA_CCR_DBM | DMA_CCR_CIRC | DMA_CCR_MINC | DMA_CCR_PSIZE_0 | DMA_CCR_MSIZE_0 | DMA_CCR_TCIE | DMA_CCR_EN;
NVIC_EnableIRQ(DMA1_Channel1_IRQn);
}
int main(void) {
serial();
adc_on();
dma_on();
ADC1->CR |= ADC_CR_ADSTART; // armed: it waits for the trigger
printf("sampling at 1 kHz in blocks of %d\r\n", BLOCK);
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM6EN;
TIM6->PSC = SystemCoreClock / 10000 - 1; // 10 kHz
TIM6->ARR = 9; // an update every millisecond
TIM6->CR2 = 2u << TIM_CR2_MMS_Pos; // the update is TRGO
TIM6->CR1 = TIM_CR1_CEN;
int shown = 0;
while (shown < 40) {
if (finished < 0) { __WFI(); continue; }
int which = finished;
finished = -1;
uint32_t sum = 0;
for (int i = 0; i < BLOCK; i++) sum += buffers[which][i];
printf("buffer %d: average %lu\r\n", which, (unsigned long)(sum / BLOCK));
shown++;
}
TIM6->CR1 = 0;
printf("done\r\n");
while (1) __WFI();
}