AnsweredAssumed Answered

STM32F3 12bit DAC with DMA - issue

Question asked by Laurikainen.Jaakko on Mar 28, 2016
Latest reply on Mar 29, 2016 by Laurikainen.Jaakko
Hello everyone, this is my first post on the forums. I've been following the posts for awhile and now I have myself faced a problem I can't seem to fix.

I'm using a NUCLEO-F334R8 evaluation board and HAL library to develop a software that plays wav-files from SD card using the built-in DAC in STM32. I have got the SPI SD card access working, now I have been trying to get the DAC side working with DMA. My problem is that while the 8-bit conversions are working nicely, the DMA somehow mixes up the data transfer when using 12-bit mode.

I have defined following sample array:
const uint16_t Sine12bit[32] = {510, 610, 700, 790, 840, 870, 980, 1010, 1020,
              1010, 980, 930, 870, 790, 700, 610, 510, 410,
              310, 220, 140, 140, 40, 0, 00, 10, 40, 90, 150, 230,
              320, 41};

Initializing the DMA as follows:
DMAHandle.Init.Instance = DMA1
DMAHandle.Init.Direction = DMA_MEMORY_TO_PERIPH;
DMAHandle.Init.PeriphInc = DMA_PINC_DISABLE;
DMAHandle.Init.MemInc = DMA_MINC_ENABLE;
DMAHandle.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
DMAHandle.Init.MemDataAlignment = DMA_PDATAALIGN_HALFWORD;
DMAHandle.Init.Mode = DMA_CIRCULAR;
DMAHandle.Init.Priority = DMA_PRIORITY_HIGH;

Starting the DAC in DMA mode as follows:
HALRet = HAL_DAC_Start_DMA(&DACHandle, DAC_CHANNEL_1, (uint32_t*)Sine12bit,
                     sizeof (Sine12bit) / sizeof (uint16_t),

Im monitoring the values that DMA is transferring to DAC using HAL_DAC_GetValue(), which is just returning the value of hdac->Instance->DOR1. Problem is that when for example the first sample is transferred (510 == 0x1FE), only 0xFE ends up in DOR1 register, and not the upper 4 bits. In 8-bit mode the 0xFE is visible on DOR1 as 0xFE0 which works correctly.

So far I've tried all combinations of DMA configuration (byte, halfword, word) and the Alignment parameter for the HAL_DAC_Start_DMA(). I just can't get the DMA to correctly set the upper 4 bits in the DAC 12-bit field. I would like to use DAC2 for the purpose but I have also tried DAC1 with similar results.

Any help would be appreciated, I'm starting to pull my hair here. I think I will try to compare the HAL functions to the ones in the peripheral library, there is very few examples available for DMA DAC using the new HAL layer.