How to distinguish which event caused the mcu to exit Standby / Sleep mode

1

My config: STM32F407VGT, ide STM32CubeIDE.

I put MCU in standby mode and there is two ways to MCU leave standby state. First way is to connect PA0 pin with 3.3v and second way is to wait for RTC timer to count to the specified value. Both ways works fine.

Now my question is How I can distinguish events? I find this in datasheet

Bit 8 EWUP: Enable WKUP pin

This bit is set and cleared by software.

0: WKUP pin is used for general purpose I/O. An event on the WKUP pin does not wakeup the device from Standby mode. 1: WKUP pin is used for wakeup from Standby mode and forced in input pull down configuration (rising edge on WKUP pin wakes-up the system from Standby mode). Note: This bit is reset by a system reset.

With this code I tried to come up with an idea

  if(PWR_CSR_EWUP == 0x00000100) // PA0 is used to leave standby mode
  {
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
      HAL_Delay(2500);
  }
  else  // mcu leave standby mode because RTC timer count desired value
  {
      HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
      HAL_Delay(2500);
  }
  Enter_StandbyMode();

For some reason I always got that PWR_CSR_EWUP isn't equal with 0x00000100 even if I leave standby mode by connecting PA0 with 3.3V. So the conclusion is that I always get it else state ( green led (PD14) is always turned on after mcu left standby mode).

c
embedded
sleep
stm32
standby
asked on Stack Overflow Jan 30, 2020 by subavet995 • edited Jun 20, 2020 by Community

1 Answer

2

This looks as if you have to correct two things:

  • you must read the register PWR_CSR, not the bitmask PWR_CSR_EWUP

  • you shouldn't check for being equal (because the 32-bit register holds 6 more bits which indicate other stuff), but mask out the bit you need and check the result: ((PWR_CSR & PWR_CSR_EWUP) == PWR_CSR_EWUP)

Of course, you can also check STM32CubeF4 HAL library for a function that delivers the information you want. When you look at its implementation, you should find something like this (but I admit I haven't checked...).

answered on Stack Overflow Apr 7, 2020 by HelpingHand

User contributions licensed under CC BY-SA 3.0