cancel
Showing results for 
Search instead for 
Did you mean: 

enter/exit STOP mode with one GPIO pin

kokbarnabas
Associate II

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

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
TDK
Guru

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

 

If you feel a post has answered your question, please click "Accept as Solution".

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

 

TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

Great thanks, now its working.

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