DAC driven by DMA on Nucleo L476RG (works in polling mode, but I can't seem to figure out DMA).
I have tried to set up the second example from "Mastering STM32" in the DAC chapter using STM32CubeMX. I'm struggling a bit with DMA in general as well. The first (non-DMA) example seemed to work fine for me in making the LED breathe. I then modified the STM32CubeMX project to try it out with DMA, and got stuck. I was hoping that there was something simple that I missed (I did start the timer ...).
I have overridden the PA5 pin for DAC1_OUT_2.
I have used TIM6, which is connected to APB1 at 80 MHz (I assume).
pseudo-code-y setup for ease of reading?
TIM6.Prescaler = 8192 - 1
TIM6.CounterMode = Up
TIM6.CounterPeriod = 8192 - 1
TIM6.Auto-ReloadPreload = Disable
TIM6.EventTriggerSelection = Update Event
I also set up the DMA for TIM6 with the "ADD" button. I set it to be circular, peripheral not incremented, memory *is* incremented, transfer is half-word to half-word, and is DMA1_Channel3.
For DAC1, I have set Channel 1 to disabled, Channel 2 to connect only to the external pin (PA5, which is connected to the on-board LED).
DAC1.Out2.OutputBuffer = Enable
DAC1.Out2.Trigger = Timer 6 Trigger Out event
DAC1.Out2.WaveGenerationMode = Disable
DAC1.Out2.UserTrimming = Factory
DAC1.Out2.SampleAndHold = Disable
The only C code I wrote was in main, so I'll put that here, it is all before the infinite loop.
/* USER CODE BEGIN 2 */
#define SAMPLES 200
#define PI 3.14159f
uint16_t IV[SAMPLES];
for (uint16_t i = 0; i < SAMPLES; ++i) {
uint16_t value = (uint16_t) rint(2048 * (sinf(2*PI*i/SAMPLES) + 1));
IV[i] = value < 4096 ? value : 4095;
}
HAL_DAC_Init(&hdac1);
HAL_TIM_Base_Start(&htim6);
HAL_DAC_Start_DMA(&hdac1, DAC_CHANNEL_2, (uint32_t *) IV, SAMPLES, DAC_ALIGN_12B_R); /* USER CODE END 2 *//* USER CODE BEGIN 2 */
