Enter Standby mode then wake up(only restart) by RTC timer

Dependents:   Check_StandBy_os2 Check_StandBy_os5 Check_StandBy_os6

see /users/kenjiArai/notebook/standby-mode-current-consumption-on-nucleo-f446re/

Files at this revision

API Documentation at this revision

Comitter:
kenjiArai
Date:
Sun Jan 17 03:05:33 2021 +0000
Parent:
0:abaa4fd0c727
Commit message:
added Nucleo-L476RG and DISCO-L475VG-IOT01A

Changed in this revision

WakeUp_STM32.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r abaa4fd0c727 -r bbc6b5bdd75b WakeUp_STM32.cpp
--- a/WakeUp_STM32.cpp	Thu Mar 12 09:27:30 2020 +0000
+++ b/WakeUp_STM32.cpp	Sun Jan 17 03:05:33 2021 +0000
@@ -3,7 +3,7 @@
     Original idea & program
     https://os.mbed.com/users/Sissors/code/WakeUp/
         by Erik
-    
+
     modified version
     https://os.mbed.com/users/kenjiArai/code/WakeUp/
  */
@@ -15,7 +15,7 @@
  *  http://www7b.biglobe.ne.jp/~kenjia/
  *  http://mbed.org/users/kenjiArai/
  *      Created:    September  21st, 2017
- *      Revised:    March      12th, 2020
+ *      Revised:    January    17th, 2021
  */
 
 #if \
@@ -27,7 +27,9 @@
  || defined(TARGET_NUCLEO_F446RE)\
  || defined(TARGET_NUCLEO_L053R8)\
  || defined(TARGET_NUCLEO_L073RZ)\
- || defined(TARGET_NUCLEO_L152RE)
+ || defined(TARGET_NUCLEO_L152RE)\
+ || defined(TARGET_NUCLEO_L476RG)\
+ || defined(TARGET_DISCO_L475VG_IOT01A)
 
 #include "WakeUp.h"
 #include "rtc_api.h"
@@ -38,7 +40,7 @@
 //Only the IRQ number the alarm is connected to differs. Any errors
 //with RTC_IRQn/RTC_Alarm_IRQn in them are related to this
 #if defined(TARGET_M4) || defined(TARGET_M3)
-#define RTC_IRQ     RTC_Alarm_IRQn               
+#define RTC_IRQ     RTC_Alarm_IRQn
 #else
 #define RTC_IRQ     RTC_IRQn
 #endif
@@ -57,13 +59,17 @@
     if (ms == 0) {              //Just disable alarm
         return;
     }
-    
+
     if (!rtc_isenabled()) {     //Make sure RTC is running
         rtc_init();
         wait_us(250);           //The f401 seems to want a delay after init
     }
 
-    PWR->CR |= PWR_CR_DBP;      //Enable power domain
+#if defined(TARGET_NUCLEO_L476RG) || defined(TARGET_DISCO_L475VG_IOT01A)
+    PWR->CR1 |= PWR_CR1_DBP;    //Enable backup domain
+#else
+    PWR->CR |= PWR_CR_DBP;      //Enable backup domain
+#endif
     RTC->WPR = 0xCA;            //Disable RTC write protection
     RTC->WPR = 0x53;
 
@@ -83,7 +89,7 @@
 
     //Get current time
     uint32_t subsecs = RTC->SSR;
-    time_t secs = rtc_read(); 
+    time_t secs = rtc_read();
 
     //Calculate alarm values
     //Subseconds is countdown,
@@ -115,10 +121,14 @@
     //Enable RTC interrupt (use Alarm-A)
     RTC->ALRMAR = alarmreg;
     RTC->ALRMASSR = subsecs | RTC_ALRMASSR_MASKSS;      //Mask no subseconds
-    RTC->CR |= RTC_CR_ALRAE | RTC_CR_ALRAIE;            //Enable Alarm-A  
+    RTC->CR |= RTC_CR_ALRAE | RTC_CR_ALRAIE;            //Enable Alarm-A
 
     RTC->WPR = 0xFF;        //Enable RTC write protection
-    PWR->CR &= ~PWR_CR_DBP; //Disable power domain
+#if defined(TARGET_NUCLEO_L476RG) || defined(TARGET_DISCO_L475VG_IOT01A)
+    PWR->CR1 &= ~PWR_CR1_DBP;   //Disable backup domain
+#else
+    PWR->CR &= ~PWR_CR_DBP;     //Disable backup domain
+#endif
 
     __enable_irq();         //Alarm is set, so irqs can be enabled again
 
@@ -129,26 +139,43 @@
 
 void WakeUp::standby_then_reset(uint32_t ms)
 {
-    if (ms == 0){   // just go to Reset
+    if (ms == 0) {  // just go to Reset
         system_reset();
-    } 
+    }
     set_ms(ms);
+#if defined(TARGET_NUCLEO_L476RG) || defined(TARGET_DISCO_L475VG_IOT01A)
+    __HAL_RCC_PWR_CLK_ENABLE();
+    HAL_PWREx_EnterSHUTDOWNMode();
+#else
     PWR->CR |= PWR_CR_CWUF;
     HAL_PWR_EnterSTANDBYMode();
+#endif
 }
 
 void WakeUp::irq_handler(void)
 {
     //Clear RTC + EXTI interrupt flags
+#if defined(TARGET_NUCLEO_L476RG) || defined(TARGET_DISCO_L475VG_IOT01A)
+    PWR->CR1 |= PWR_CR1_DBP;    // Enable power domain
+#else
     PWR->CR |= PWR_CR_DBP;      // Enable power domain
+#endif
     RTC->ISR &= ~RTC_ISR_ALRAF;
+#if defined(TARGET_NUCLEO_L476RG) || defined(TARGET_DISCO_L475VG_IOT01A)
+    //RTC->CR &= 0x00ff00ff;      // just in case
+#else
     RTC->CR &= 0x00ff00ff;      // just in case
+#endif
     RTC->WPR = 0xCA;            // Disable RTC write protection
     RTC->WPR = 0x53;
-    RTC->CR &= ~(RTC_CR_ALRAE | RTC_CR_ALRAIE); //DisEnable Alarm-A  
+    RTC->CR &= ~(RTC_CR_ALRAE | RTC_CR_ALRAIE); //Disable Alarm-A
     RTC->WPR = 0xFF;            // Enable RTC write protection
-    EXTI->PR = RTC_EXTI_LINE_ALARM_EVENT;  
+    EXTI->PR = RTC_EXTI_LINE_ALARM_EVENT;
+#if defined(TARGET_NUCLEO_L476RG) || defined(TARGET_DISCO_L475VG_IOT01A)
+    PWR->CR1 &= ~PWR_CR1_DBP;   // Disable power domain
+#else
     PWR->CR &= ~PWR_CR_DBP;     // Disable power domain
+#endif
     system_reset();
 }