1+ #include < STM32LowPower.h>
2+ #include < low_power.h>
3+
14/* Last Reset Reason Sketch
25* This sketch will determine what caused the last reset on the STM32 MCU. Most microcontrollers
36* have a register dedicated to storing the last reason of the chip, weather being from a
4- * low power condition, software caused brown-out. Test it by resetting the MCU via the USER button
7+ * low power condition, software caused or brown-out. Test it by resetting the MCU via holding the USER button,
58* which triggers the Reset_my_MCU() function or unplug the USB cable and repluggit back. Adjust your
69* UART, USER Button pin and registers accordingly. Use the MCU's datasheet and/or stm32yyyxxx.h for reference.
710* The code is provided "as is" with no liability.
@@ -22,15 +25,17 @@ enum reset_reason {
2225 WINDOW_WDG = 1 << 4 ,
2326 LOW_POWER = 1 << 5 ,
2427 OPTION_BYTE_LOADER = 1 << 6 ,
25- POWER_ON_DOWN = 1 << 7
28+ POWER_ON_DOWN = 1 << 7 ,
29+ STANDBY = 1 << 8 ,
30+ WAKEUP = 1 << 9
2631};
2732
28- reset_reason last_reset_reason = UNKNOWN_RESET;
33+ reset_reason last_reset_reason = UNKNOWN_RESET; // is initially 0 or unknown
2934static int default_button_state = LOW;
3035
3136void Reset_My_MCU () {
32- // There are a few reset conditions.
33- // Keep the one you wish to use and comment out the others.
37+ // There are a few reset conditions. Keep the one you wish to use and comment out the others.
38+
3439 // Below is the Software reset condition
3540 // NVIC_SystemReset();
3641
@@ -69,9 +74,26 @@ void setup() {
6974#ifdef RCC_CSR_PORRSTF
7075 if (LL_RCC_IsActiveFlag_PORRST ()) last_reset_reason = (reset_reason)(last_reset_reason | POWER_ON_DOWN);
7176#endif
77+ #if defined(PWR_CSR_SBF) || defined(PWR_SR_SBF) || defined(PWR_SR1_SBF) || defined(PWR_CSR1_SBF) || defined(PWR_PMSR_SBF)
78+ if (LL_PWR_IsActiveFlag_SB ()) last_reset_reason = (reset_reason)(last_reset_reason | STANDBY);
79+ #endif
80+ #if defined(PWR_CSR_WUF)
81+ if (LL_PWR_IsActiveFlag_WU ()) last_reset_reason = (reset_reason)(last_reset_reason | WAKEUP);
82+ #endif
83+
7284
7385 // Clear reset flags
7486 LL_RCC_ClearResetFlags ();
87+ #if defined(PWR_SCR_CSBF) || defined(PWR_CR1_CSBF) || defined(PWR_PMCR_CSSF) || defined(PWR_SR_CSSF)
88+ #if defined(STM32U0xx)
89+ LL_PWR_ClearFlag_CSB ();
90+ #else
91+ LL_PWR_ClearFlag_SB ();
92+ #endif
93+ #endif
94+ #if defined(PWR_CSR_WUF)
95+ LL_PWR_ClearFlag_WU ();
96+ #endif
7597}
7698
7799void loop () {
@@ -83,10 +105,11 @@ void loop() {
83105 if (last_reset_reason & WINDOW_WDG) Serial.println (" - Window Watchdog reset" );
84106 if (last_reset_reason & LOW_POWER) Serial.println (" - Low-power reset" );
85107 if (last_reset_reason & OPTION_BYTE_LOADER) Serial.println (" - Option byte loader reset" );
86- if (last_reset_reason & NRST_PIN) Serial.println (" - Pin reset (NRST or software)" ); // last case so the rest take precedence before issuing NRST
108+ if (last_reset_reason & STANDBY) Serial.println (" - Standby mode reset" );
109+ if (last_reset_reason & WAKEUP) Serial.println (" - WakeUp flag reset (Pin or RTC)" );
87110 if (last_reset_reason & POWER_ON_DOWN) Serial.println (" - Power on or power down reset" );
111+ if (last_reset_reason & NRST_PIN) Serial.println (" - Pin reset (NRST or software)" ); // last case so the rest take precedence before issuing NRST
88112 if (last_reset_reason == UNKNOWN_RESET) Serial.println (" - Unknown or no flags set" );
89- last_reset_reason = UNKNOWN_RESET;
90113
91114 // Trigger software reset on button press
92115 if (digitalRead (USER_BTN_PIN) != default_button_state) {
0 commit comments