Skip to content

Commit d8b16d5

Browse files
committed
fixed some mistakes, typos and added 2 more flags
1 parent 841d9f2 commit d8b16d5

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

examples/Peripherals/Registers/Reset_reason/Reset_reason.ino

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
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,22 +25,25 @@ 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
2934
static int default_button_state = LOW;
3035

3136
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+
3439
// Below is the Software reset condition
3540
// NVIC_SystemReset();
3641

3742
// Below is the Watchdog Timer reset condition
3843
IWatchdog.begin(1000); //1ms tick then reset
3944
while (1)
40-
; // Wait for reset
45+
; // Wait for reset
46+
4147
}
4248

4349
void setup() {
@@ -69,9 +75,18 @@ void setup() {
6975
#ifdef RCC_CSR_PORRSTF
7076
if (LL_RCC_IsActiveFlag_PORRST()) last_reset_reason = (reset_reason)(last_reset_reason | POWER_ON_DOWN);
7177
#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+
7285

7386
// Clear reset flags
7487
LL_RCC_ClearResetFlags();
88+
LL_PWR_ClearFlag_SB();
89+
LL_PWR_ClearFlag_WU();
7590
}
7691

7792
void loop() {
@@ -83,10 +98,11 @@ void loop() {
8398
if (last_reset_reason & WINDOW_WDG) Serial.println(" - Window Watchdog reset");
8499
if (last_reset_reason & LOW_POWER) Serial.println(" - Low-power reset");
85100
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)");
87103
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
88105
if (last_reset_reason == UNKNOWN_RESET) Serial.println(" - Unknown or no flags set");
89-
last_reset_reason = UNKNOWN_RESET;
90106

91107
// Trigger software reset on button press
92108
if (digitalRead(USER_BTN_PIN) != default_button_state) {

0 commit comments

Comments
 (0)