2022-08-03 10:54 PM
We are currently working with STM32F103VE MCU and we have a dilemma about the order of initialization GPIO pins as alternate function outputs and associated peripherals. The goal is to have full control over the pin states.
We have checked order of initialization done by CubeMX and outcome is that AF GPIOs are firstly initialized and then associated peripherals. But in the reference manual (RM0008 Rev 21) we have found the next info.
It seems that output is not specified and is not under control if GPIO is initialized as AF output before associated peripheral is initialized?
It seems that the peripherals should be initialized first and only then GPIO as AF output?
If one AF output pin is shared between more peripherals, as it is usually, which peripheral exactly has control over the AF output pin? Does it depend on order of peripherals initialization?
E.g. PB13 can be assigned to SPI2_SCK and to TIM1_CH1N.
Thank you for answers!
Nenad
2022-08-03 11:44 PM
Logically thinking, you should first initialize the peripheral, then connect it to the pins, no matter what the microcontroller type is.
With F1 series, the priority of peripherals when taking over the control of pins is fixed internally and cannot be altered in software, which may lead to some interesting quirks. It does not depend on peripheral initialization order.
2022-08-03 11:57 PM
> It seems that the peripherals should be initialized first and only then GPIO as AF output?
Yes. And the generated code does that. Generate a simple example, say SPI2 only and step through the code. MX_GPIO_Init() is called before MX_SPI2_Init(), but it does not touch the SPI pins.
MX_SPI2_Init() calls HAL_SPI_MspInit() which sets the SPI2 pins accordingly.
> If one AF output pin is shared between more peripherals
It's your responsibility to avoid those conflicts. STM32CubeIDE/MX can help you by coloring used pins green. It might be useful/favorable to share pins on F1 series, but I haven't done that.
hth
KnarfB
2022-08-04 12:44 AM
Hi gbm,
Thanks a lot for the answer!
Do you know where we can find any documentation for F1 about mentioned priority of peripherals when taking over the control of pins?
BR
Nenad
2022-08-04 01:02 AM
Hi KnarfB,
Thanks a lot for the answer!
> You are right about the order of calling function, but after calling HAL_SPI_MspInit(), where some pins are initialized as AF outputs, initialization of SPI2 is comming. What is about the AF output pin state between calling HAL_SPI_MspInit() and initialization of SPI2 peripheral?
> It seems that I was not so clear about "shared pins". E.g. PB13 can be assign to SPI2_SCK and TIM1_CH1N as AF outputs. If we are using both mentioned peripherals in firmware, SPI2 and TIM1, but the goal is to use PB13 as SPI2_SCK. Which peripheral will take control over the PB13 and what will happen with PB13 pin state if we call TIM1 initialization before or after SPI2 initialization?