Skip to content

Updated the Reset_Reason code #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions examples/Peripherals/Registers/Reset_reason/Reset_reason.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include <STM32LowPower.h>
#include <low_power.h>

/* Last Reset Reason Sketch
* This sketch will determine what caused the last reset on the STM32 MCU. Most microcontrollers
* have a register dedicated to storing the last reason of the chip, weather being from a
* low power condition, software caused brown-out. Test it by resetting the MCU via the USER button
* low power condition, software caused or brown-out. Test it by resetting the MCU via holding the USER button,
* which triggers the Reset_my_MCU() function or unplug the USB cable and repluggit back. Adjust your
* UART, USER Button pin and registers accordingly. Use the MCU's datasheet and/or stm32yyyxxx.h for reference.
* The code is provided "as is" with no liability.
Expand All @@ -22,22 +25,25 @@ enum reset_reason {
WINDOW_WDG = 1 << 4,
LOW_POWER = 1 << 5,
OPTION_BYTE_LOADER = 1 << 6,
POWER_ON_DOWN = 1 << 7
POWER_ON_DOWN = 1 << 7,
STANDBY = 1 << 8,
WAKEUP = 1 << 9
};

reset_reason last_reset_reason = UNKNOWN_RESET;
reset_reason last_reset_reason = UNKNOWN_RESET; //is initially 0 or unknown
static int default_button_state = LOW;

void Reset_My_MCU() {
// There are a few reset conditions.
// Keep the one you wish to use and comment out the others.
// There are a few reset conditions. Keep the one you wish to use and comment out the others.

// Below is the Software reset condition
// NVIC_SystemReset();

// Below is the Watchdog Timer reset condition
IWatchdog.begin(1000); //1ms tick then reset
while (1)
; // Wait for reset
; // Wait for reset

}

void setup() {
Expand Down Expand Up @@ -69,9 +75,18 @@ void setup() {
#ifdef RCC_CSR_PORRSTF
if (LL_RCC_IsActiveFlag_PORRST()) last_reset_reason = (reset_reason)(last_reset_reason | POWER_ON_DOWN);
#endif
#ifdef RCC_CSR_SBF
if (LL_PWR_IsActiveFlag_SB()) last_reset_reason = (reset_reason)(last_reset_reason | STANDBY);
#endif
#ifdef RCC_CSR_WUF
if (LL_PWR_IsActiveFlag_WU()) last_reset_reason = (reset_reason)(last_reset_reason | WAKEUP);
#endif


// Clear reset flags
LL_RCC_ClearResetFlags();
LL_PWR_ClearFlag_SB();
LL_PWR_ClearFlag_WU();
}

void loop() {
Expand All @@ -83,10 +98,11 @@ void loop() {
if (last_reset_reason & WINDOW_WDG) Serial.println(" - Window Watchdog reset");
if (last_reset_reason & LOW_POWER) Serial.println(" - Low-power reset");
if (last_reset_reason & OPTION_BYTE_LOADER) Serial.println(" - Option byte loader reset");
if (last_reset_reason & NRST_PIN) Serial.println(" - Pin reset (NRST or software)"); //last case so the rest take precedence before issuing NRST
if (last_reset_reason & STANDBY) Serial.println(" - Standby mode reset");
if (last_reset_reason & WAKEUP) Serial.println(" - WakeUp flag reset (Pin or RTC)");
if (last_reset_reason & POWER_ON_DOWN) Serial.println(" - Power on or power down reset");
if (last_reset_reason & NRST_PIN) Serial.println(" - Pin reset (NRST or software)"); //last case so the rest take precedence before issuing NRST
if (last_reset_reason == UNKNOWN_RESET) Serial.println(" - Unknown or no flags set");
last_reset_reason = UNKNOWN_RESET;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be best to kept it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? the point is that the loop will continually show what was the last reset reason. that line will overwrite the reason and display "- Unknown or no flags set".


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