2019-10-25 09:07 AM
Hi,
I am a new coder using the STM32L100RC discovery board. As such, my problem is quite simple I believe; I cannot exit Stop mode using an external trigger on the WakeUp Pin PA0. The code works as fallow: the LED on the PC9 pin light up, I enter STOP MODE and once I use the WakeUp command by pushing on the PA0 button, the LED turns off for a few seconds. Now I do know that using a a for loop isn't the right way to wait a few seconds, no need to point this out. I'd be very grateful if someone could explain what I am doing wrong. Here's what I've done so far:
void Button_Initialization (void)
{
RCC_APB2PeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
// Configure PC9 as push-pull output
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitTypeDef GPIO_InitStructA;
GPIO_InitStructA.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructA.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructA.GPIO_Mode = GPIO_Mode_AF;
GPIO_PinAFConfig(GPIOA, GPIO_Pin_0, GPIO_AF_WKUP);
GPIO_Init(GPIOA, &GPIO_InitStructA);
PWR_WakeUpPinCmd(PWR_WakeUpPin_1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructButton;
// GPIO_InitStructButton.GPIO_Pin = ;
GPIO_InitStructButton.GPIO_Speed = GPIO_Speed_400KHz;
GPIO_InitStructButton.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructButton.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA, &GPIO_InitStructButton);
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line = EXTI_Line1;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Event;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_Init( &EXTI_InitStruct);
}
int main(void)
{
Button_Initialization();
while(1)
{
// Turn off LED on PC9
GPIO_SetBits(GPIOC, GPIO_Pin_9);
PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFE);
GPIO_ResetBits(GPIOC, GPIO_Pin_9);
for (int i = 0; i < 1000000; i++)
{
}
}
}
2019-10-28 12:45 PM
First, you don't need to use a wake-up pin to get out of stop mode. Most interrupts will do to get you out of stop mode. Wake-up pins are used to get you out of standby. Therefore, do not put the pin in an alternate function mode. Instead, make the mode GPIO_MODE_IT_FALLING or GPIO_MODE_IT_RISING depending on which way the button sends the pin.
Second, I have woken up from stop mode and never seen your EXTI_InitTypeDef. I am not familiar with it and I think that it is unnecessary.
Also, when I wake from a button press, I use
PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFI);
Rather than
PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFE);
Also, make sure that in your external interrupt handler for the button press, you are clearing the interrupt pin, or else you will never go back to sleep for more than an infinitesimally small amount of time.
Do that by calling the following from the interrupt
void HAL_GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
{
/* EXTI line interrupt detected */
if(__HAL_GPIO_EXTI_GET_IT(GPIO_Pin) != RESET)
{
__HAL_GPIO_EXTI_CLEAR_IT(GPIO_Pin);
HAL_GPIO_EXTI_Callback(GPIO_Pin);
}
}
I also typically reconfigure my clock when coming out of stop mode, but it seems like you aren't doing much clock control here, so that shouldn't make a difference.
In general, always try to start with demo code and go from there. Starting from scratch is difficult with this HAL code base unless you have the datasheet and peripheral specific documents memorized.