Try Embedded

Nucleo-F446RE: a sine wave from the DAC with TIM6 and DMA

Bare-metal C on the STM32F446: TIM6 paces the DAC, DMA feeds it from a table, and a 1 kHz sine appears on PA4 with the CPU doing nothing.

Output · Advanced

In a real product: Audio test-tone generator (Consumer electronics): The timer and DMA send each sample on time, however busy the CPU is, so the tone stays clean.

The STM32F446 has a 12-bit DAC: it puts a real voltage on PA4, not the pulses of PWM. A program could write each sample itself, but its timing would wander whenever it did anything else. Here TIM6 paces the samples and DMA delivers them, so the wave stays steady while the main loop prints and waits. The oscilloscope shows the result.

What you'll learn

  • The DAC's holding register (DHR12R1) and output register (DOR1), and how a trigger moves one to the other
  • A timer's trigger output: MMS in TIM6->CR2 sends TRGO to the DAC at every update
  • Feeding the DAC with circular DMA, on DMA1 stream 5, channel 7
  • Why DMAUDR means a sample came too late, and how a sample rate and a table set the frequency

How it works

With TEN1 set, a value written to DHR12R1 waits there until a trigger moves it to DOR1, which sets the voltage: DOR1 / 4095 * 3.3 V. TSEL1 chooses the trigger, and its reset value 0 is TIM6's TRGO on this chip. DMAEN1 makes each timer trigger also ask the DMA for the next value, so the holding register is ready for the next trigger.

DMA1 stream 5 on channel 7 answers those requests. It copies one 16-bit value from sine[] to DHR12R1 each time, and CIRC starts the table over at its end. TIM6 counts 500 clocks of 16 MHz, 32,000 updates a second. With 32 samples in a cycle, that makes a 1 kHz sine. The output buffer is on (BOFF1 is 0), so the pin can drive a load.

The first trigger puts out whatever DHR12R1 held before the DMA's first value, which is why a DAC fed by DMA starts one sample late. The main loop never touches the wave: it only prints DOR1 and the underrun flag. Press Autoset on the oscilloscope to see the sine and its frequency.

Try this

  • Change TIM6->ARR to 999: the sample rate halves, and so does the sine's frequency.
  • Remove DMA_SxCR_CIRC: the table plays once, the next request goes unanswered, and DMAUDR is set.
  • Replace the DMA with the triangle generator: clear DMAEN1, set DAC_CR_WAVE1_1 and DAC_CR_MAMP1, and write 0 to DHR12R1.
  • Set DAC_CR_BOFF1 to turn the buffer off and read about the output impedance in the reference manual.

main.c

// Bare-metal C on the Nucleo-F446RE: no Arduino core and no HAL. CMSIS names every register.
// TIM6 triggers the DAC at 32 kHz; DMA1 stream 5 feeds it from a table of 32 samples: a 1 kHz sine on PA4.
#include <stdio.h>
#include "stm32f4xx.h"

// One cycle of a sine, centred on mid-scale.
static const uint16_t sine[32] = {
  2048, 2438, 2813, 3159, 3462, 3711, 3896, 4010,
  4048, 4010, 3896, 3711, 3462, 3159, 2813, 2438,
  2048, 1658, 1283, 937, 634, 385, 200, 86,
  48, 86, 200, 385, 634, 937, 1283, 1658,
};

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: USART2 on PA2, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
  while (!(USART2->SR & USART_SR_TXE)) {}
  USART2->DR = ch;
  return ch;
}

static void wave_start(void) {
  RCC->APB1ENR |= RCC_APB1ENR_DACEN | RCC_APB1ENR_TIM6EN;
  RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN;
  GPIOA->MODER |= GPIO_MODER_MODER4;   // PA4: analog

  // DMA1 stream 5, channel 7: the DAC's channel 1 request. 16 bits at a time, round and round the table.
  DMA1_Stream5->PAR = (uint32_t)&DAC->DHR12R1;
  DMA1_Stream5->M0AR = (uint32_t)sine;
  DMA1_Stream5->NDTR = 32;
  DMA1_Stream5->CR = (7u << DMA_SxCR_CHSEL_Pos) | DMA_SxCR_MSIZE_0 | DMA_SxCR_PSIZE_0 | DMA_SxCR_MINC | DMA_SxCR_CIRC
                     | DMA_SxCR_DIR_0 | DMA_SxCR_EN;

  // The DAC: triggered (TSEL1 0 is TIM6's TRGO), asking the DMA at each trigger.
  DAC->CR = DAC_CR_DMAEN1 | DAC_CR_TEN1 | DAC_CR_EN1;

  // TIM6 last: 16 MHz / 500 = 32 kHz, with TRGO on each update.
  TIM6->PSC = 0;
  TIM6->ARR = 499;
  TIM6->CR2 = TIM_CR2_MMS_1;
  TIM6->CR1 = TIM_CR1_CEN;
}

int main(void) {
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
  GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODER2) | GPIO_MODER_MODER2_1;   // PA2: USART2 TX, AF7
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8);
  USART2->BRR = SystemCoreClock / 115200;
  USART2->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;

  wave_start();
  while (1) {
    // The wave needs nothing from here: this only looks at it.
    unsigned value = DAC->DOR1;
    printf("DOR1 %u (%u mV), underrun %u\r\n", value, value * 3300u / 4095u, (DAC->SR & DAC_SR_DMAUDR1) ? 1u : 0u);
    delay_ms(500);
  }
}

Lessons behind it