cancel
Showing results for 
Search instead for 
Did you mean: 

STM32CUBEMX code generation and pin configuration clarification

JTurn.2
Senior

I noticed that some of the pins are configured in MX_GPIO_init() while others are not. They seem to have been configured somewhere else, so I'm not sure where to check if my pins were set properly.

Also, some of the pin configurations are not updated after manually changing the pin outs. For example, SPI2_NSS_Pin, which is "#define SPI2_NSS_Pin GPIO_PIN_11" in main.h, is still configured as "GPIO_PIN_11" in MX_GPIO_init() despite the fact that I changed it to another pin in STM32CUBEMX.

That said, I am quite confused on how the code generator works and how the pins are configured. Also, is it better to use stm32cubemx to generate code or would it be easier to manually configure everything from scratch? Thanks for the help

1 ACCEPTED SOLUTION

Accepted Solutions

MX_GPIO_Init initializes non-peripheral pins, such as buttons or EXTI pins or LEDs, or other General Purpose Input Output pins.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
S.Ma
Principal

Hmm.. to know if all the pins you configured are well... configured, go in debug mode and put a breakpoint at the main() loop (if bare metal), inspect HW registers GPIO ports and see once maually that all is well done. You also need to "regenerate code" to make sure the compiled and built code is updated down to the BIN file

TDK
Guru

SPI pins are configured in HAL_SPI_MspInit which is located in stm32xxx_hal_msp.c and is called from within HAL_SPI_Init. Other peripherals are similar.

> That said, I am quite confused on how the code generator works and how the pins are configured. Also, is it better to use stm32cubemx to generate code or would it be easier to manually configure everything from scratch? Thanks for the help

Up to you. Generally the code generator does a good job initializing everything and will be faster than doing it yourself. Opinions vary.

If you feel a post has answered your question, please click "Accept as Solution".

Interesting. I checked the stm32xxx_hal_msp.c file and the pins are configured correctly. So either it is intialised in main.c in MX_GPIO_Init() or stm32xxx_hal_msp.c (as I noticed that some of the pins are intialised there). Then, what does MX_GPIO_Init() do in main.c and how do I know if it is initialised in main.c or stm32xxx_hal_msp.c?

Thank you for your help!

MX_GPIO_Init initializes non-peripheral pins, such as buttons or EXTI pins or LEDs, or other General Purpose Input Output pins.

If you feel a post has answered your question, please click "Accept as Solution".

I see. Thank you for your help!