Issue with STM8L052R8 GPIO Input Detection Not Reflecting Actual Pin Voltage Changes
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:
- The input voltage at PG2 changes between high and low, but the read input state does not update accordingly.
- 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);
}
}
}
}