cancel
Showing results for 
Search instead for 
Did you mean: 

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

Andy-li
Associate

       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);
      }
    }
  }
}
1 ACCEPTED SOLUTION

Accepted Solutions
Peter BENSCH
ST Employee

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

In order 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
Peter BENSCH
ST Employee

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

In order 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.
Andy-li
Associate

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