Skip to main content
May 23, 2023 7:19 PM UTC
Question

How to switch peripherals for the same pins in run-time (dynamically)?

I have a board that has a connector. Another board is mounted on top of it. This additional board may require either GPIO, or UART, or SPI on the same signals. But I need to change peripherals and work with pins without recompiling the code, without changing the firmware. It is required to do this at run-time (dynamically). The question is how to achieve this using STM32CubeMX and HAL/LL code? Are there any AppNotes? (I can achieve this by programming registers, no problem, I'm interested in how this is done within the framework of the CubeMx / HAL / LL paradigm).

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
May 23, 2023 7:36 PM UTC

Bit niche so likely something you'll have to think through end-to-end rather than dropping out of the code generator.

Without recompiling, you can partition your initialization code to separate the possible use cases, then selectively initialize sub-components based on options or flags. Perhaps modify behaviour of callbacks or other functions based on the configuration settings.

Literally on-the-fly, you'd need to remap pins via the AFR[x] mux settings. And other MODER settings if the nature of the pin driver needs to change, etc.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
May 23, 2023 8:29 PM UTC

...you can partition your initialization code to separate the possible use cases, then selectively initialize sub-components based on options or flags...

I suppose that I will have to make several sub-projects with configured peripherals and borrow the generated code from them.

Is there a similarity of conditional compilation in CubeMX?

Problem with code re-generation. After all, this will have to interfere with the generated Cube / HAL / LL code, but it is constantly overwritten.

May 23, 2023 8:32 PM UTC

Okay, even if do not change at runtime, but just recompile and have each firmware version. And it's just not easy to do.