1
+ #include < STM32LowPower.h>
2
+ #include < low_power.h>
3
+
1
4
/* Last Reset Reason Sketch
2
5
* This sketch will determine what caused the last reset on the STM32 MCU. Most microcontrollers
3
6
* 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,
5
8
* which triggers the Reset_my_MCU() function or unplug the USB cable and repluggit back. Adjust your
6
9
* UART, USER Button pin and registers accordingly. Use the MCU's datasheet and/or stm32yyyxxx.h for reference.
7
10
* The code is provided "as is" with no liability.
@@ -22,15 +25,17 @@ enum reset_reason {
22
25
WINDOW_WDG = 1 << 4 ,
23
26
LOW_POWER = 1 << 5 ,
24
27
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
26
31
};
27
32
28
- reset_reason last_reset_reason = UNKNOWN_RESET;
33
+ reset_reason last_reset_reason = UNKNOWN_RESET; // is initially 0 or unknown
29
34
static int default_button_state = LOW;
30
35
31
36
void 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
+
34
39
// Below is the Software reset condition
35
40
// NVIC_SystemReset();
36
41
@@ -69,9 +74,26 @@ void setup() {
69
74
#ifdef RCC_CSR_PORRSTF
70
75
if (LL_RCC_IsActiveFlag_PORRST ()) last_reset_reason = (reset_reason)(last_reset_reason | POWER_ON_DOWN);
71
76
#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
+
72
84
73
85
// Clear reset flags
74
86
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
75
97
}
76
98
77
99
void loop () {
@@ -83,10 +105,11 @@ void loop() {
83
105
if (last_reset_reason & WINDOW_WDG) Serial.println (" - Window Watchdog reset" );
84
106
if (last_reset_reason & LOW_POWER) Serial.println (" - Low-power reset" );
85
107
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)" );
87
110
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
88
112
if (last_reset_reason == UNKNOWN_RESET) Serial.println (" - Unknown or no flags set" );
89
- last_reset_reason = UNKNOWN_RESET;
90
113
91
114
// Trigger software reset on button press
92
115
if (digitalRead (USER_BTN_PIN) != default_button_state) {
0 commit comments