2025-07-05 2:37 AM
Hello,
I'm attempting to make my MCU shift between STOP and RUN mode using a button. When I press the button the CPU stops, however, upon pressing again it fails to start the CPU. I know this form the LED indicators.
The strange part is that when I place a debug point, or anything debug related, the CPU wakes and immediately and runs the ISR as if it were waiting for the stop mode to resume.
Is there some setting I need to enable to get the GPIOs to funtion in stop0?
I have had a look at other forum posts and MX examples but have not seen anything, or I have missed it.
My ISR:
void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == Button_Pin && powerDownState == 0)
{
powerDownState = 1;
HAL_GPIO_WritePin(Green_LED_GPIO_Port,Green_LED_Pin, GPIO_PIN_RESET); // Enable pull-up on button
HAL_SuspendTick();
HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);
}
else
{
HAL_ResumeTick();
powerDownState = 0;
HAL_GPIO_WritePin(Green_LED_GPIO_Port, Green_LED_Pin, GPIO_PIN_SET);
}
}
GPIO_Init:
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(Green_LED_GPIO_Port, Green_LED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(Button_Drive_GPIO_Port, Button_Drive_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : Green_LED_Pin */
GPIO_InitStruct.Pin = Green_LED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Green_LED_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : Button_Pin */
GPIO_InitStruct.Pin = Button_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(Button_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : Button_Drive_Pin */
GPIO_InitStruct.Pin = Button_Drive_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(Button_Drive_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI2_3_IRQn, 3, 0);
HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
}
Cheers,
Michael
2025-07-05 6:32 AM
Don't enter stop mode within an interrupt. The interrupt can't pre-empt itself.
Set a flag and handle going to sleep within the main loop.
2025-07-05 6:49 AM
Use STOP in ISR callback is possible only with good knowledge and prepare registers state ... As @TDK write never doit this way...