2025-07-22 7:58 AM
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!
Solved! Go to Solution.
2025-07-22 8:23 AM - edited 2025-07-22 8:23 AM
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.
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.
2025-07-22 8:23 AM - edited 2025-07-22 8:23 AM
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.
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.
2025-07-23 2:57 AM
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!