Using timers on STM32 to control waveform generation - Page 1
Forum section: electronics / general

author: Gibson486
author_rank: Frequent Contributor
timestamp: January 07, 2019, 09:07:41 pm
content: I am trying to control the DAC outputs with the internal trigger of the DAC of an stm32f429. I wanted the trigger to be in synch with a timer as it cycles through the DMA buffer.I initially set up a timer (call it timer 1) to go to another timer (call it timer 2), which is a one shot.The plan was to control to use of timer 1 as sort of trigger pulse. Once enabled, timer 1 would trigger timer 2 to output the number of pulses needed to cycle through the DMA buffer once.I tried using the repetition counter, but that does not seem to do anything. I would be expecting to see my generated waveform (just a sawtooth for now) with a flat line in between, but all I can really do is just slow down or speed up the sawtooth wave with out the flat line in between.I could just control the triggers by just having another pin control the external trigger DAC pin, but I want to avoid hardware changes.I guess, in short, I just want to make n pulses to the internal DAC when it is triggered. Just doing the regular software update through the API is too slow (the process of writing to the DAC through non DMA is not fast enough).Maybe this is just a dumb way to do this? The only other way I can think of is monitoring the the counter register and throwing out an interrupt to let me know it has cycled through, but that seems a little more taunting.

author: ogden
author_rank: Super Contributor
timestamp: January 07, 2019, 09:29:18 pm
content: Always check examples ofstandard peripherals libraryfirst. This timeDAC_SignalsGenerationexample will answer most of your questions

author: Gibson486
author_rank: Frequent Contributor
timestamp: January 07, 2019, 10:10:06 pm
content: Thanks, but I think that example just free runs the timer until you tell it to stop (unless DAC_DeInit() does something special, but i cannot find that function). I am using a circular buffer and I want control the iterations through the buffer. I could just move to the non circular mode, but wouldn't that mean I have to reload the buffer every single time?

author: mark03
author_rank: Frequent Contributor
timestamp: January 07, 2019, 10:27:17 pm
content: When using the repetition counter, you also have to set a bit that makes the counter a "one shot" instead of automatically reloading. I think. It's been a while.

author: ogden
author_rank: Super Contributor
timestamp: January 07, 2019, 11:08:51 pm
content: Quote from: Gibson486 on January 07, 2019, 10:10:06 pmFirst you want to read user's guide of both Timer and DMA peripherals. Then most likely you shall read ST appnote AN4776 "General-purpose timer cookbook" (search for it, look for .pdf). Look for keywords "DMA burst mode" in it. Other option could be timer gating, but I dunno it is possible on stm32 or not. Regarding timer gating/chaining you shall do your research (internet search) yourself. There's example "TIM_DMABurst" having fancy naming. could be it.

author: Gibson486
author_rank: Frequent Contributor
timestamp: January 08, 2019, 04:13:44 pm
content: Thanks! I was looking at the section after that. I will look into burst mode. I was hoping to do this by just starting and stopping a timer, but this may suffice.

author: Gibson486
author_rank: Frequent Contributor
timestamp: January 08, 2019, 04:18:03 pm
content: Quote from: mark03 on January 07, 2019, 10:27:17 pmI am using cubemx, so i would think it would do that for me. Then again, CubeMX does a lot of weird things. Like still having to manually enable PWM in your code even though you checked it off in cubemx.

author: newbrain
author_rank: Super Contributor
timestamp: January 10, 2019, 07:16:22 pm
content: For a complete description on how to use timers to do what you want, checkthis threadwhere a very similar question was answered and discussed.One of the main point is that the slave timer must be an advanced-control one, while for the master a general-purpose one suffices, e.g., TIM1 as slave and TIM2 as master.

author: mark03
author_rank: Frequent Contributor
timestamp: January 11, 2019, 02:08:04 am
content: Quote from: Gibson486 on January 08, 2019, 04:18:03 pmNot to beat a dead horse, but...this is the reason why many of us refuse to use CubeMX beyond, perhaps, the basic configuring of pins and clocks. If something fails to work, you have no way of knowing whether it was a bug in the vendor software, or some magic incantation you failed to recite exactly word-for-word. Whereas, if you wrote the driver yourself and it doesn't work, you're in a better position to debug it. I realize there are valid reasons for using Cube, but I can't help wondering why its "market share" is so high.