2025-08-18 7:18 PM - last edited on 2025-08-19 2:10 AM by Peter BENSCH
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);
}
}
}
}
Solved! Go to Solution.
2025-08-19 2:20 AM
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
2025-08-19 2:20 AM
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
2025-08-19 6:52 PM
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