Skip to main content
Najd_Elaoud
Associate II
July 22, 2025
Solved

Run Different Functions Based on GPIO Mode (PA12 as Input or Output) – STM32G0

  • July 22, 2025
  • 1 reply
  • 263 views

Hi everyone,

I'm working on an STM32G070CBTx, and I need to execute two different functions depending on the GPIO mode of pin PA12.

Specifically:

  • If PA12 is configured as input (GPIO_MODE_INPUT), I want to run function1()

  • If PA12 is configured as output push-pull (GPIO_MODE_OUTPUT_PP), I want to run function2()

Is there a way in HAL or LL to check the current mode of PA12 at runtime?
Or do I need to track the mode manually when I configure it?

Any suggestions or best practices for this kind of conditional GPIO-mode-based logic would be appreciated!

Thanks in advance!

Best answer by Sarra.S

Hello @Najd_Elaoud

One way is to visualize the GPIOx_MODER register. This register is responsible for configuring and storing the mode of each GPIO pin. 

SarraS_0-1753197743173.png

Hope that helps!

1 reply

Sarra.SBest answer
ST Employee
July 22, 2025

Hello @Najd_Elaoud

One way is to visualize the GPIOx_MODER register. This register is responsible for configuring and storing the mode of each GPIO pin. 

SarraS_0-1753197743173.png

Hope that helps!

Najd_Elaoud
Associate II
July 23, 2025

Thanks for the suggestion; using the MODER and OTYPER registers did the trick!

Here’s the code I used (based on your advice), and it works perfectly:

// Check PA12 mode (MODER12, bits 25:24)
	 uint32_t PA12_MODE = (GPIOA -> MODER & GPIO_MODER_MODE12) >> GPIO_MODER_MODE12_Pos;
 // Check output type (OT12, bit 12)
 uint32_t PA12_OUTPUT_TYPE = (GPIOA -> OTYPER & GPIO_OTYPER_OT12) >> GPIO_OTYPER_OT12_Pos;

	 if (PA12_MODE == 0X00)		// Input mode
	 {
		 // USE ONLY WHEN PA12 IS SET AS INPUT
	 	 FUNCTION_1();
	 }
	 else if (PA12_MODE == 0X01 && PA12_OUTPUT_TYPE == 0X00)	// Output mode (Push-Pull)
	 {
		 // USE ONLY WHEN PA12 IS SET AS OUTPUT_PP
		 FUNCTION_2();
	 }

This lets me safely switch between the two functions based on the pin mode of PA12, just what I needed.

Really appreciate your help!