cancel
Showing results for 
Search instead for 
Did you mean: 

What is the correct initialization order of GPIOs as alternate function output and associated peripherals in order to have full control over the state of the output pins?

NMiti.3
Associate II

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.

0693W00000QMvTDQA1.png 

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.

0693W00000QMvUQQA1.png 

Thank you for answers!

Nenad

4 REPLIES 4
gbm
Lead III

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.

KnarfB
Principal III

> 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

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

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?