cancel
Showing results for 
Search instead for 
Did you mean: 

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

Najd_Elaoud
Associate II

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Sarra.S
ST Employee

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!

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
Sarra.S
ST Employee

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!

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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!