cancel
Showing results for 
Search instead for 
Did you mean: 

STM8L DAC+DMA+TIM4

digitalcomfort
Associate
Posted on October 23, 2012 at 13:02

hello, i'm trying to play raw pcm (22050 Hz, 8 bit unsigned, mono) from sd card to DAC through DMA, with ring buffer.

everything works perfectly when i'm using TIM2 as interrupt source (at 22050 Hz) and DAC's software trigger.

but i really need to use DMA because of resource-critical timer's interrupt. so, i used stm8l-discovery's wavegenerator example - initialization code copypasted almost unchanged, except of using DAC's circular mode - i don't use it because of playing data stream, not periodic waveform.

http://pastebin.com/RnHaUqLr

i'm filling ring buffer in eternal cycle in main() when the half_buf flag equals ''1''. i reset the flag after reading sd sector (512 bytes) and setting the flag in DMA channel 2-3 interrupt when counter i==3 and half transfer interrupt flag is set.

http://pastebin.com/qQFJAVE4

i had to split dac_buf[512] to for 4 parts - dac_buf[4][128] because DMA can't serve more than 255 blocks at once. every time in DMA interrupt transfer complete flag is set, i disable DMA channel 3, do i++ (upto 3), move to memory0 new address (&dac_buf[i]), enable DMA.

http://pastebin.com/yTVCkQLp

looks like it should work, but it doesn't, just producing scary alien sounds with some periodic noise block near sd reading frequency.

reading function is still ok - i traced storing values step-by-step, it's filling dac_buf with exactly the same as on sd card, and in the same order.

address of dac_buf's current block looks ok too - incrementing by 128 and resetting to begin of buffer.

i suspect it doesn't work because of switching off and on DMA - some jitter, stopping TIM4, etc, but i'm not sure.

does anybody have correct source of with playing stream to DAC through DMA? any help, please - i don't have ideas anymore.

#dac #tim4 #stm8l #dma
1 REPLY 1
lowpowermcu
Associate II
Posted on December 25, 2012 at 12:31

Hi,

You can look at demo source code:

http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/FIRMWARE/stm8l1528-eval_fw.zip