2023-12-20 03:34 PM
Hello everybody,
I want to use the STM32F042G6U6 MCU with one button on PA6 pin if I press the button it should toggle between STOP mode and run. Is this possible at all?
How can I do this? I was able to enter STOP mode or exit from it but I cant toggle it in the same interrupt.
And how does exactly this routine works? When exiting from STOP mode what happens, where is the program start from?
Thanks in advance for the help,
Kok Barnabás
Solved! Go to Solution.
2023-12-26 03:22 PM
An interrupt cannot pre-empt itself. Go to stop mode from the main thread, not from within an interrupt. Set a flag in the interrupt, check it in the main thread and go to sleep if it's set, after clearing it.
2023-12-20 05:43 PM - edited 2023-12-20 05:43 PM
You can set up an interrupt on PA6 in your program to go into stop mode, and use the same interrupt to wake it up from stop mode. Note that buttons typically chatter during pressing/releasing and you will need some method of software debouncing.
When the processor wakes up from stop mode, it continues from where it left off.
The reference manual discusses this in detail.
2023-12-26 01:47 PM
Here is what I doing:
the stop_mode_flag is a global variable.
at the init:
stop_mode_flag=0;
in the while:
stop_mode_flag=1;
HAL_Delay(500);
sprintf(tx_buff,"something\n");
HAL_UART_Transmit(&huart1, tx_buff, 100, 1000);
in the callback:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(stop_mode_flag==1)
{
stop_mode_flag=0;
HAL_PWR_EnterSTOPMode(0, PWR_STOPENTRY_WFI);
}
}
I thougt this should work if I press the button its enter to stop mode and if I press again it exit from stop mode. But I can only enter with this code cant exit. How can I achive the describen behaviour?
Tanks
Kok Barnabás
2023-12-26 03:22 PM
An interrupt cannot pre-empt itself. Go to stop mode from the main thread, not from within an interrupt. Set a flag in the interrupt, check it in the main thread and go to sleep if it's set, after clearing it.
2023-12-26 04:08 PM
Great thanks, now its working.
2024-01-03 12:32 PM
Can You please give me a deeper description of what is happening in the code that I write?
I will make it another way, don't want to use it like this, I'm just curious and want to understand this more.
I tried this code but instead of going to stop mode I used UART to print something out. in that way it worked as I thought, why not when using stop mode?
Thank You