Skip to main content
Explorer
August 19, 2025
Solved

Issue with STM8L052R8 GPIO Input Detection Not Reflecting Actual Pin Voltage Changes

  • August 19, 2025
  • 2 replies
  • 2055 views

       STM8L experts, I need your help!

       Our company’s legacy product uses the STM8L052R8 microcontroller. Currently, an important customer requires additional functionality that involves using a GPIO pin to detect the state of an external switch.

       We developed test code in IAR and selected GPIOG_PIN2 as an input, configuring it in  floating input mode or pull-up input mode. The PG2 input pin is pulled up to VCC with a  4.7KΩ resistor and can be pulled down to GND through a switch, allowing us to apply either 3.3V (high) or 0V (low) to the pin. However, during testing, we observed the following issues:

  1. The input voltage at PG2 changes between high and low, but the read input state does not update accordingly.
  2. During IAR debugging:
    • The PG_DDR register shows DDR2 = 0, confirming PG2 is in input mode.
    • The PG_IDR register shows IDR2 = 0 or 1, but it does not change in response to the input voltage.

    3.Behavior when debugging starts:

    • If PG2 input voltage is 3.3V, IDR2 = 1.
    • If PG2 input voltage is 0V, IDR2 = 0.

Additional Note:

        We searched for relevant information on the Internet,and some people suggested enabling the GPIO clock using:

CLK_PeripheralClockConfig(CLK_Peripheral_GPIOG, ENABLE);

However, in the stm8l15x_clk.h header file, the first parameter of this function only includes enumerations like: CLK_Peripheral_TIM1 \ CLK_Peripheral_USART1 \ CLK_Peripheral_SPI1 ,There is no option for GPIO .

        At first, I wondered if STM8L keeps GPIO clocks off by default to save power, but I couldn’t find any evidence in the datasheets to confirm this.

       The test implementation is minimal in code volume, detailed in the attached appendix.

 

Appendix: Test Code:

void LED1_Init ()
{
 GPIO_Init (GPIOC, GPIO_Pin_0, GPIO_Mode_Out_PP_Low_Fast);
}
void KEY_Init ()
{
 GPIO_Init (GPIOG, GPIO_Pin_2, GPIO_Mode_In_FL_No_IT);
}

void main ()
{
 CLK_SYSCLKDivConfig (CLK_SYSCLKDiv_1);
 LED1_Init ();
 KEY_Init ();
 while (1)
 {
 if (GPIO_ReadInputDataBit (GPIOG, GPIO_Pin_2) == SET)
 {
 delay (10);
 if (GPIO_ReadInputDataBit (GPIOG, GPIO_Pin_2) == SET)
 {
 delay (10);
 GPIO_ResetBits (GPIOC, GPIO_Pin_0);
 }
 }
 else
 {
 delay (10);
 if (GPIO_ReadInputDataBit (GPIOG, GPIO_Pin_2) == RESET)
 {
 GPIO_SetBits (GPIOC, GPIO_Pin_0);
 }
 }
 }
}
    This topic has been closed for replies.
    Best answer by Peter BENSCH

    Welcome @Andy-li, to the community!

    Your test code touches on an old problem: depending on the compiler, the test for the assignment is not compiled as desired, which was discussed in this old thread, for example (see the posts by @Tesla DeLorean there).

    If you want to continue using the libraries without making any changes and to avoid such trouble, you should change the tests as suggested from

    if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) == SET)
    if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) == RESET)

    to

    if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) // SET
    if (!GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) // RESET

     Hope that helps?

    Regards
    /Peter

    2 replies

    Technical Moderator
    August 19, 2025

    Welcome @Andy-li, to the community!

    Your test code touches on an old problem: depending on the compiler, the test for the assignment is not compiled as desired, which was discussed in this old thread, for example (see the posts by @Tesla DeLorean there).

    If you want to continue using the libraries without making any changes and to avoid such trouble, you should change the tests as suggested from

    if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) == SET)
    if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) == RESET)

    to

    if (GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) // SET
    if (!GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_2) // RESET

     Hope that helps?

    Regards
    /Peter

    Andy-liAuthor
    Explorer
    August 20, 2025

    My deepest gratitude, Professor Peter. Your expertise rivals even the most renowned scholars of Hogwarts!☺️

    You’re an absolute legend!​​ :party_popper:

    Your guidance not only solved my issue but also taught me critical nuances about STM8L’s GPIO configuration that I’d never find in the datasheet. 

    Thanks!

    Andy