cancel
Showing results for 
Search instead for 
Did you mean: 

How to read state of SYS WKUP pin ?

freeflyer
Senior

I am using the  Nucleo-L433RC-P board and have configured pin PC13 as SYS WKUP (for the blue top button).

How can I read the state of this pin when the processor wakes up, because HAL_GPIO_ReadPin will not work.

I am sure I used a function or macro to read the state of this pin before, but can't remember how I did it.

I need to distinguish between an RTC wake or a pin wake from shutdown.

I use wake markers stored in an RTC backup register to detetc an RTC wake.  I write to the backup register just before calling shutdown in the function SCH_mcuSleep....

void SCH_mcuSleep (void){
	/* The Following Wakeup sequence is highly recommended prior to each Standby mode entry
	    mainly  when using more than one wakeup source this is to not miss any wakeup event.
	    - Disable all used wakeup sources,
	    - Clear all related wakeup flags,
	    - Re-enable all used wakeup sources,
	    - Enter the shutdown mode.
	 */
	// write value to RTC backup register to indicate mcu was in sleep
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, WAKE_FROM_SLEEP_RTC);

	// Disable all used wakeup sources
	HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
	HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, RTC_WAKE_SEC, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);

	// Clear all related wakeup flags
	__HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
	
Enable_PC13_Wakeup();

	// Call sleep  (note that the terms 'sleep' and 'powerdown' are only used for readability, both call shutdown mode)
	HAL_PWREx_EnterSHUTDOWNMode();
}

 

Then in main I call this function for when the processor wakes...

 

void MAN_checkWake (void)
{
// read wake marker from RTC backup register to determine if woken from sleep or shutdown
wake_marker = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR2);
if (wake_marker == WAKE_FROM_SLEEP_RTC)
{
	// woken from sleep (RTC wake)
	// check if elapsed time is greater then shutdown time
	if (elapsedSeconds >= RTC_SHUTDOWN_S)
	{
		SCH_mcuShutdown();
	}

}
else if (wake_marker == WAKE_FROM_SHUTDOWN_WKUP)
{
	// woken from shutdown (power switch pressed during shutdown)
	wakeConfigReset(); // reset time and date and set default reference pressure
}
else
{
	// woken from reset
	wakeConfigReset();	// reset time and date and set default reference pressure
	// store value in RTC backup register to indicate mcu was reset
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, WAKE_FROM_RESET);
}
}

 

There is a seperate function to shutdown that only uses pin wake (does not require RTC wake)...

 

void SCH_mcuShutdown (void){
	// write value to RTC backup register to indicate mcu was in shutdown
	HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, WAKE_FROM_SHUTDOWN_WKUP);

	// disable RTC wakeup
	HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);

	// enable pin wakeup
	Enable_PC13_Wakeup();

	// Call power down (note that the terms 'sleep' and 'powerdown' are only used for readability, both call shutdown mode)
	HAL_PWREx_EnterSHUTDOWNMode();
}

 

3 REPLIES 3
TDK
Super User

The pin needs to be initialized as an input pin before HAL_GPIO_ReadPin will read its state. By default, the pin is in analog mode.

If a wakeup pin caused the wake up, a flag is set in RCC->SR1. This is latching--it doesn't reflect current state of the pin.

TDK_0-1754682863020.png

 

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

Thanks TDK

How do I read the  RCC->SR1 register ?  Is there a macro or function ?

You can just read it and look for the corresponding bit:

 

if (RCC->SR1 & RCC_SR1_WUF1) {

    ...

 

Or you can use the READ_REG macro:

if (READ_REG(RCC->SR1, RCC_SR1_WUF1)) {

    ...

 

Of if you prefer HAL:

if (__HAL_PWR_GET_FLAG(PWR_FLAG_WUF1)) {
    ...

 

All equivalent.

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