Reputation: 25
I have an array of 256 elements that I want to set the DAC to:
HAL_DAC_Start_DMA(&hdac, DAC_CHANNEL_2, (uint32_t *)array, 256, DAC_ALIGN_12B_R);
DMA is triggered by TIM2, 256 timer cycles take approximately 1 ms. Now I want to store 128 samples of ADC values in the buffer when the DAC is in the 100th element of the array (it has been reloaded 100 times with TIM2). This procedure will be repeated for each DAC cycle. I am trying two timers in slave mode, but the reading of the CNT register in the while loop is random. Another idea was to use interrupts, but I can't synchronize this process.
How to synchronize this? Picture show the idea enter image description here
Upvotes: 0
Views: 297
Reputation: 1213
Which STM32?
Your description is not entirely clear, but my interpretation (including the image) is, that at the moment when 100th DMA sample is output, you want to start collecting ADC samples, and you want to collect 128 ADC samples with ADC running fairly quickly, probably at the similar sampling rate than DAC (so that the 128th ADC sample is taken sooner than the 100+156th DAC sample is output, according to the picture you've given).
There are several ways to achieve this. Maybe the simplest is to start ADC in Continuous mode and simply leave it running freely. Then, at the 100th DAC sample, just start a DMA transferring ADC results from ADC_DR to a buffer in memory, with NDTR set to 128; the transfer-complete interrupt of DMA will the mark the point where you have all the ADC samples collected.
To start DMA at the 100th DAC sample, you can set up a timer TIMx other than TIM2 as a slave to TIM2, setting TIM2 TRGO upon Update, and setting TIMx slave-mode controller to External clock mode 1. So, TIMx will count the number of TIM2 Updates (i.e. number of DAC samples). Set some TIMx channel to Compare (no output needed) and set the respective TIMx_CCRx ("pulse") to 100 and enable interrupt from that channel. You can then start the DMA transferring from ADC in that interrupt.
JW
Upvotes: 0