Try Embedded

Nucleo-G071RB: read a potentiometer with ADC, DMA and DMAMUX

Bare-metal C on the STM32G071: bring the G0's ADC up (regulator, calibration, ready flag) and let DMA, routed by DMAMUX, keep eight readings in memory.

Sensors · Intermediate

In a real product: Dimmer knob on a low-power controller (Smart home): The ADC and DMA keep the knob's reading up to date while the CPU does other work.

The Nucleo-G071RB carries an STM32G071RB: an Arm Cortex-M0+ with 128 KB of flash and 36 KB of RAM. Its ADC has a start-up sequence the older families do not, and its DMA channels have no fixed requests: a DMAMUX in front of them connects any peripheral's request to any channel. This program reads a potentiometer on A0 eight times over and over, with DMA copying every result into memory, and prints the average.

What you'll learn

  • The G0's ADC start-up: its voltage regulator (ADVREGEN), calibration (ADCAL), then ADEN and waiting for ADRDY
  • Choosing channels with CHSELR and waiting for CCRDY, which says the ADC has taken the change
  • Routing the ADC's request to a DMA channel through DMAMUX (DMAMUX1_Channel0->CCR)
  • Circular DMA (CIRC) with the ADC's circular DMA mode (DMACFG), so the buffer never stops filling

How it works

After reset the G071 runs from its 16 MHz HSI16 oscillator, and every pin is in analog mode already, so PA0 (A0) needs no set-up. The ADC does: its regulator must be switched on and given about 20 us to settle, then a calibration measures the converter's offset, which is stored and used from then on.

CHSELR bit 0 picks channel 0. Each change to it makes the ADC check the configuration, and CCRDY in ISR says it is done. SMPR gives the input a long sampling time. In CFGR1, CONT starts the next conversion as soon as one ends, DMAEN asks the DMA to take each result and DMACFG keeps asking after the DMA wraps.

DMAMUX channel 0 feeds DMA1 channel 1. Writing 5, the ADC's request number on the G0, into its CCR connects them. DMA1 channel 1 then copies ADC1->DR into samples[], eight 16-bit results, and starts over (CIRC). The main loop only reads memory: it averages the eight samples and prints the result with the voltage.

Try this

  • Turn the knob and watch the reading follow, with no ADC register read in the main loop.
  • Remove the DMAMUX1_Channel0->CCR line: the DMA channel gets no request and the samples stay 0.
  • Remove DMACFG: the DMA fills the buffer once, then the ADC stops asking and the readings freeze.

main.c

// Bare-metal C on the Nucleo-G071RB: no Arduino core and no HAL. CMSIS names every register.
// ADC1 converts A0 (PA0) continuously; DMA1 channel 1, fed by DMAMUX, keeps eight readings in samples[].
#include <stdio.h>
#include "stm32g0xx.h"

#define SAMPLES 8
static volatile uint16_t samples[SAMPLES];   // written by DMA
static volatile uint32_t ticks;              // milliseconds since SysTick started

void SysTick_Handler(void) {
  ticks++;
}

static void delay_ms(uint32_t ms) {
  uint32_t start = ticks;
  while ((uint32_t)(ticks - start) < ms) {}
}

// printf sends each character here: LPUART1 on PA2, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
  while (!(LPUART1->ISR & USART_ISR_TXE_TXFNF)) {}
  LPUART1->TDR = ch;
  return ch;
}

static void adc_dma_start(void) {
  RCC->APBENR2 |= RCC_APBENR2_ADCEN;
  RCC->AHBENR |= RCC_AHBENR_DMA1EN;

  // The ADC: regulator, calibration, channel 0, a long sampling time, continuous conversions with circular DMA.
  ADC1->CR |= ADC_CR_ADVREGEN;
  delay_ms(1);                                  // the regulator needs about 20 us
  ADC1->CR |= ADC_CR_ADCAL;
  while (ADC1->CR & ADC_CR_ADCAL) {}
  ADC1->CHSELR = ADC_CHSELR_CHSEL0;
  while (!(ADC1->ISR & ADC_ISR_CCRDY)) {}       // the ADC has taken the channel
  ADC1->ISR = ADC_ISR_CCRDY;
  ADC1->SMPR = 7u << ADC_SMPR_SMP1_Pos;         // 160.5 cycles
  ADC1->CFGR1 = ADC_CFGR1_CONT | ADC_CFGR1_DMAEN | ADC_CFGR1_DMACFG;

  // DMAMUX channel 0 (DMA1 channel 1) takes the ADC's request, number 5.
  DMAMUX1_Channel0->CCR = 5;
  DMA1_Channel1->CPAR = (uint32_t)&ADC1->DR;
  DMA1_Channel1->CMAR = (uint32_t)samples;
  DMA1_Channel1->CNDTR = SAMPLES;
  DMA1_Channel1->CCR = DMA_CCR_MSIZE_0 | DMA_CCR_PSIZE_0 | DMA_CCR_MINC | DMA_CCR_CIRC | DMA_CCR_EN;

  ADC1->ISR = ADC_ISR_ADRDY;
  ADC1->CR |= ADC_CR_ADEN;
  while (!(ADC1->ISR & ADC_ISR_ADRDY)) {}
  ADC1->CR |= ADC_CR_ADSTART;
}

int main(void) {
  RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
  RCC->APBENR1 |= RCC_APBENR1_LPUART1EN;
  GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODE2) | GPIO_MODER_MODE2_1;   // PA2: LPUART1 TX
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (6u << 8);              // AF6
  LPUART1->BRR = (uint32_t)(256ull * SystemCoreClock / 115200);            // 16 MHz HSI16 after reset
  LPUART1->CR1 = USART_CR1_TE | USART_CR1_UE;

  SysTick->LOAD = SystemCoreClock / 1000 - 1;   // 1 ms
  SysTick->VAL = 0;
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

  adc_dma_start();
  delay_ms(1);                                  // the first eight conversions take about 0.1 ms
  while (1) {
    uint32_t sum = 0;
    for (int i = 0; i < SAMPLES; i++) sum += samples[i];
    uint32_t reading = sum / SAMPLES;
    printf("pot %lu (%lu mV)\r\n", (unsigned long)reading, (unsigned long)(reading * 3300 / 4095));
    delay_ms(250);
  }
}

Lessons behind it