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,22 +25,25 @@ 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
37
42
// Below is the Watchdog Timer reset condition
38
43
IWatchdog.begin (1000 ); // 1ms tick then reset
39
44
while (1 )
40
- ; // Wait for reset
45
+ ; // Wait for reset
46
+
41
47
}
42
48
43
49
void setup () {
@@ -69,9 +75,18 @@ void setup() {
69
75
#ifdef RCC_CSR_PORRSTF
70
76
if (LL_RCC_IsActiveFlag_PORRST ()) last_reset_reason = (reset_reason)(last_reset_reason | POWER_ON_DOWN);
71
77
#endif
78
+ #ifdef RCC_CSR_SBF
79
+ if (LL_PWR_IsActiveFlag_SB ()) last_reset_reason = (reset_reason)(last_reset_reason | STANDBY);
80
+ #endif
81
+ #ifdef RCC_CSR_WUF
82
+ if (LL_PWR_IsActiveFlag_WU ()) last_reset_reason = (reset_reason)(last_reset_reason | WAKEUP);
83
+ #endif
84
+
72
85
73
86
// Clear reset flags
74
87
LL_RCC_ClearResetFlags ();
88
+ LL_PWR_ClearFlag_SB ();
89
+ LL_PWR_ClearFlag_WU ();
75
90
}
76
91
77
92
void loop () {
@@ -83,10 +98,11 @@ void loop() {
83
98
if (last_reset_reason & WINDOW_WDG) Serial.println (" - Window Watchdog reset" );
84
99
if (last_reset_reason & LOW_POWER) Serial.println (" - Low-power reset" );
85
100
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
101
+ if (last_reset_reason & STANDBY) Serial.println (" - Standby mode reset" );
102
+ if (last_reset_reason & WAKEUP) Serial.println (" - WakeUp flag reset (Pin or RTC)" );
87
103
if (last_reset_reason & POWER_ON_DOWN) Serial.println (" - Power on or power down reset" );
104
+ if (last_reset_reason & NRST_PIN) Serial.println (" - Pin reset (NRST or software)" ); // last case so the rest take precedence before issuing NRST
88
105
if (last_reset_reason == UNKNOWN_RESET) Serial.println (" - Unknown or no flags set" );
89
- last_reset_reason = UNKNOWN_RESET;
90
106
91
107
// Trigger software reset on button press
92
108
if (digitalRead (USER_BTN_PIN) != default_button_state) {
0 commit comments