cancel
Showing results for 
Search instead for 
Did you mean: 

How fast am I able to switch the alternate function on a given GPIO between a timer PWM output and GPIO during runtime?

Ricardo1
Associate II

I am using an STM32G4. I want to output PWM from a timer channel on one pin (call it pin A) and have a GPIO output on another (pin B). Then I want to be able to switch the functions on the two pins so that pin A is now a GPIO output and pin B is the PWM output. Is this possible to do at runtime? How fast can this be done? I am looking for a switching speed of at least 1kHz.

Additional details: I am driving an H-bridge that takes two PWMs but only actually needs one switching (PWM) at HIGH or LOW and the other switching at out desired frequency (PWM). But we need to be able to change which pin in switching.

1 REPLY 1

Just change the respective bits in given GPIO_MODER. Without any sophistication, a read-modify-write operation which shouldn't take more than half a dozen of machine cycles, at usual clocks well below 100ns.

Beware of atomicity if the same GPIO_MODER is to be manipulated in interrupts, too.

You might be able to achieve the same entirely within the timer, too, by manipulating the respective TIMx_CCER.CCxP/CCxNP bits, in conjunction with the bits determining the "non-active" pins' behaviour, see Output control bits for complementary tim_ocx and tim_ocxn channels with break feature table in the advanced TIM chapter.

JW