cancel
Showing results for 
Search instead for 
Did you mean: 

Does STM32 MCUs support runtime change of GPIO mapping between alternative functions?

Aprui.1
Associate

Hi,

We would like to make a design where we have several LEDs grouped to banks (3 LEDs per bank and 3 banks thus 9 LEDs total)

In each bank of 3 LEDs, only one of the LEDs would be driven with PWM (brigthness control) and other would be either ON / OFF (i.e pin configured to standard output pin). The PWM controlled LED can change within the LED bank. Thus totally there are 9 LEDs that can be driven with PWM but in any instant only 3 LEDs would be driven with PWM.

Question: For example STM32G030C6 MCU has TIM3_CH1 signal that can be routed to three different pins (PA6, PC6 and PB4) to generate PWM output. Also TIM3_CH2 and TIM14_CH1 can be routed to atleast three different GPIOs.

If I will connect one LED bank (three LEDs) to pins PA6, PC6 and PB4, will I be able to change the TIM3_CH1 signal routing during runtime code execution between those mentioned GPIOs or are the routing fixed and changeable only during boot? 

Example: If I want to change the brightness of first LED (on PA6 pin) the TIM3_CH1 signal would be routed to PA6. PC6 and PB4 would be standard output signals (high or low).

At some point I would want to change the brightness of the second LED so I would route the TIM3_CH1 signal to PC6 pin. PA6 and PB4 would be standard output signals (high or low).

And same could happen to the third LED on PB4 also.

4 REPLIES 4
Danish1
Lead II

Short answer: Yes.

Caveat: If you are using the HAL or similar libraries, you might find a momentary glitch during the call to make the change. For LEDs this is unlikely to be a problem. But where the pins control other hardware this might be unacceptable.

You would be *much* better off directly writing to the appropriate registers. And they are all fully documented in the Reference Manual for your stm32. IMHO this is much better than the HAL calls, where you have to guess how to use them from the examples.

Hope this helps,

Danish

Piranha
Chief II

The "during boot" also happens during runtime... 😉

Aprui.1
Associate

Thank you for your quick responses!

... unless you use the GPIO lock feature (see GPIOx_LCKR register).

JW