diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index a268188..15179d0 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -1,7 +1,7 @@ /* 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. @@ -9,6 +9,7 @@ #include "stm32yyxx_ll_rcc.h" #include "IWatchdog.h" +// #include "STM32LowPower.h" #define USER_BTN_PIN USER_BTN // Adjust this for your board @@ -22,15 +23,20 @@ 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 WakeUp reset condition (needs STM32LowPower.h library) + // LowPower.shutdown(1000); + // Below is the Software reset condition // NVIC_SystemReset(); @@ -69,9 +75,26 @@ void setup() { #ifdef RCC_CSR_PORRSTF if (LL_RCC_IsActiveFlag_PORRST()) last_reset_reason = (reset_reason)(last_reset_reason | POWER_ON_DOWN); #endif +#if defined(PWR_CSR_SBF) || defined(PWR_SR_SBF) || defined(PWR_SR1_SBF) || defined(PWR_CSR1_SBF) || defined(PWR_PMSR_SBF) + if (LL_PWR_IsActiveFlag_SB()) last_reset_reason = (reset_reason)(last_reset_reason | STANDBY); +#endif +#if defined(PWR_CSR_WUF) + if (LL_PWR_IsActiveFlag_WU()) last_reset_reason = (reset_reason)(last_reset_reason | WAKEUP); +#endif + - // Clear reset flags + // Clear internal reset flags after they were captured LL_RCC_ClearResetFlags(); +#if defined(PWR_SCR_CSBF) || defined(PWR_CR1_CSBF) || defined(PWR_PMCR_CSSF) || defined(PWR_SR_CSSF) +#if defined(STM32U0xx) + LL_PWR_ClearFlag_CSB(); +#else + LL_PWR_ClearFlag_SB(); +#endif +#endif +#if defined(PWR_CSR_WUF) + LL_PWR_ClearFlag_WU(); +#endif } void loop() { @@ -83,10 +106,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; // Trigger software reset on button press if (digitalRead(USER_BTN_PIN) != default_button_state) { diff --git a/library.properties b/library.properties index 8d175d9..be51ffb 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=STM32duino Examples -version=1.2.6 +version=1.2.7 author=several maintainer=stm32duino sentence=Provides several examples for the Arduino core for STM32 MCUs.