2025-08-18 7:18 PM
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:
3.Behavior when debugging starts:
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); }
}
}
}