cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F3 12bit DAC with DMA - issue

lkaino
Associate II
Posted on March 28, 2016 at 09:59

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 STM 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;
HAL_DMA_Init(&DMAHandle);

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),
DAC_ALIGN_12B_R);

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. #dma-dac-hal
3 REPLIES 3
lkaino
Associate II
Posted on March 28, 2016 at 10:27

Okay I fought with this whole day yesterday. Today with new eyes and new ides I quite quickly spotted the problem after checking the DMA registers.

This row was wrong, I was incorrectly setting the

DMA_PDATAALIGN_HALFWORD

for the MemDataAlignment. This is the correct way:

DMAHandle.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;

lkaino
Associate II
Posted on March 28, 2016 at 20:00

Got the audio playback working, with very poor quality, here's a sample: 

https://www.youtube.com/watch?v=-PYCjDFqgas

I'm converting a 16-bit mono wav file to 12-bit by shifting all samples by >> 4. I also added an amplifier to the output and tested the internal output buffer. Makes no difference to the noise.

Any ideas what am I doing wrong?

lkaino
Associate II
Posted on March 29, 2016 at 08:25

Okay solved this issue also. The 16-bit PCM sample is signed, I thought it is unsigned. So it was an issue in my downsampling logic.

Sorry for flooding the forum with my issues :).