Skip to main content
Explorer II
December 20, 2023
Solved

enter/exit STOP mode with one GPIO pin

  • December 20, 2023
  • 2 replies
  • 2433 views

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

    This topic has been closed for replies.
    Best answer by TDK

    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.

    2 replies

    Super User
    December 21, 2023

    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.

    TDK_0-1703122950074.png

    https://www.st.com/resource/en/reference_manual/rm0091-stm32f0x1stm32f0x2stm32f0x8-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

     

    Explorer II
    December 26, 2023

    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

     

    TDKAnswer
    Super User
    December 26, 2023

    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.

    Explorer II
    December 27, 2023

    Great thanks, now its working.