2023-12-07 09:44 AM
Hello
In my project I want to use timer4 of STM32G474RET6 MCU. I want to generate single pulses with programmable width on CH2-CH4. These output pulses from MCU should be with programmable delay from input pulse connected to CH1. Pulses on CH2-CH4 should be with negative polarity ('0'). Output pulse must have enable/disable option (when disabled the output from the disabled channel should be '1'). Delay of each pulse from input pulse should be in resolution of uSec's (delay can be also 0 -> immediate output at the same time as input signal). Width of the pulses should be also in uSec's resolution. The output pulses from CH2-CH4 should be always single (if enabled) - 1 output pulse after each input pulse. Each pulse from CH2-CH4 should have own independent delay/width/enable/disable values. Input pulse polarity can be changed dynamically during the work from rising (default) to falling edge.
I think that the basic configuration should be capture (for CH1) and compare modes (for CH2-CH4)?
I tried to define different configurations generated by MX environment integrated in ST32CubeIDE. But it never worked as expected and what is more disturbing that after starting of any channel of CH1 it caused to stack (or crashes) after reset events (like pressing on hardware reset button, generation SW reset like NVIC_SystemReset(); or toggling between flash banks).
1. Can someone provide me any example how to do it (I was unable to find such example based on HAL on evaluation board environment).
2. What can be wrong in my system that causing such reset problems just by enabling channels of timer 4 ?
Thank you.
Alex
Solved! Go to Solution.
2023-12-11 10:05 AM
Hello
After following Jan's suggestion, I read and learned all relevant chapters.
Now one pulse mode is working (based on 2 timers: timer 3 and timer 4).
Attached file with initialization sequence.
Still have problem with reset, but for this opened a new post:
RESET PROBLEMS WITH STM32G474RET6 - STMicroelectronics Community
Thank you very much.
Alex
2023-12-07 10:14 AM - edited 2023-12-07 10:16 AM
> I want
Things don't happen just because of your wish.
You probably can't generate "delayed" pulses on all of those particular 3 channels - you have to use the combined mode for that, which consumes 2 channels per output.
Also, I don't think you'll find a ready made example or click this simply in CubeMX (althtough it might be clickable).
Your best chance is to learn how to do this, step by step.
0. Read the TIM chapter in RM.
1. Learn how to generate PWM. Even if generated by clicking, observe TIM registers content in debugger and check their content against their description in RM
2. Learn how to generate one pulse (see TIMx_CR1.OPM), triggered manually by TIMx_CR.CEN You can try this entirely in debugger with stopped processor (possibly after setting up GPIO for a selected pin), it's fun.
3. Learn how to trigger this pulse from a pin, see mainly TIMx_SMCR. Again it's fun to do in debugger, although you may want again to get the GPIO for input pin set beforehand (it's then more fun to do everything in debugger and then write that down in the form of purely register-based program).
4. Learn, how to use the combined modes and delay the pulses
JW
2023-12-07 10:53 AM
> 2. What can be wrong in my system that causing such reset problems just by enabling channels of timer 4 ?
Why guess? Debug your program systematically and objectively in order to find the solution. Doesn't do much help for you or others to have people guess at the exact cause.
Let's not entertain the notion that just enabling a channel triggers a hard fault in the mcu.
2023-12-07 10:59 AM
Hello
Thank you all for the fast response.
I am new with STM MCU's and must finish the board urgently.
1. From Jan's response it is not clear for me is it possible to do it in capture compare method base on 4 channels of single timer 4? As Jan wrote that it consumes 2 channels per output (that as I understand I don't have??).
2. Maybe it is better for me now to bring a fast solution in a different method (and later after everything is working to debug and look for improvements)? For example, maybe it is better to leave only capture CH1->that will produce interrupt and inside this interrupt manually to change first time state of CH2-CH4 and then to generate time based accurate timer that will produce another interrupt where I manually will change again state of CH2-CH4-> and to do it again and again until all 3 pulses will be produced in a manual way?
3. Regarding crazy behavior around resets - it happened when I probably had some wrong (maybe invalid) settings (produced by clickable MX). As I am completely new with STM currently I don't know how to debug these issues systematically??
Thank you
Alex
2023-12-07 11:28 PM
> 1. From Jan's response it is not clear for me is it possible to do it in capture compare method base on 4 channels of single timer 4? As Jan wrote that it consumes 2 channels per output (that as I understand I don't have??).
Have you read the timer chapter in Reference Manual (RM)? Have you read the article I've linked to?
The "traditional" STM32 timer's channels in Output Compare + on of the PWM modes generate pulses, one edge of which is at the moment of compare (i.e. given by content of TIMx_CCRx), and the other is at the timer's rollover (i.e. given by content of TIMx_ARR). In other words, all four channel's pulses are aligned at one of the edges. In yet another words, you cannot adjust both delay and duration of a pulse, only one of them.
Now in 'G4, an enhanced variant of the timer is used, where you can combine two channels (in so called Combined mode) to allow position of both edges to be adjustable, as long as they are both below ARR. But that takes up two channels. The relevant subchapter in RM tells you, which channels can be combined. When you combine two channels, you get one output from them, so basically you have one less channel to be used. Some timers have 6 channels, but not all of them can be combined. I am not going to find out, which variant of the timer do you have in your STM32 model in the particular timer you've choosen, that's upon you.
As I've said, start with reading the timer chapter in RM, and then experiment with the timer's registers in debugger.
> fast solution
Oh, if you are after fast solutions, then forget about what I've wrtitten and just keep clicking in CubeMX. All I can offer is tears, blood, sweat and toil.
JW
2023-12-11 10:05 AM
Hello
After following Jan's suggestion, I read and learned all relevant chapters.
Now one pulse mode is working (based on 2 timers: timer 3 and timer 4).
Attached file with initialization sequence.
Still have problem with reset, but for this opened a new post:
RESET PROBLEMS WITH STM32G474RET6 - STMicroelectronics Community
Thank you very much.
Alex