From f7cdb357eb49d50cb7023be0c9dedbb342a182ab Mon Sep 17 00:00:00 2001 From: George Popa Date: Thu, 31 Jul 2025 13:57:28 +0100 Subject: [PATCH 1/8] fix: some mistakes/typos and added 2 more flags Signed-off-by: George Popa Co-authored-by: Frederic Pillon --- .../Registers/Reset_reason/Reset_reason.ino | 37 +++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index a268188..c6e9356 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -1,7 +1,10 @@ +#include +#include + /* 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. @@ -22,15 +25,17 @@ 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(); @@ -69,9 +74,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 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 +105,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) { From be0e55e56878a524a72cf939bd389b72a4916425 Mon Sep 17 00:00:00 2001 From: George Popa Date: Wed, 3 Sep 2025 17:45:43 +0100 Subject: [PATCH 2/8] fix: commented out potential non-included library --- .../Registers/Reset_reason/Reset_reason.ino | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index c6e9356..c2424fa 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -1,6 +1,3 @@ -#include -#include - /* 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 @@ -12,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 @@ -36,18 +34,25 @@ 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. + //Bellow is the WakeUp reset condition (needs STM32LowPower.h library) + //LowPower.shutdown(1000); + // Below is the Software reset condition - // NVIC_SystemReset(); + //NVIC_SystemReset(); // Below is the Watchdog Timer reset condition IWatchdog.begin(1000); //1ms tick then reset while (1) ; // Wait for reset + + } void setup() { pinMode(USER_BTN_PIN, INPUT); default_button_state = digitalRead(USER_BTN_PIN); + //Serial.setRx(PA3); + //Serial.setTx(PA2); Serial.begin(115200); while (!Serial) ; // Wait for Serial @@ -82,7 +87,7 @@ void setup() { #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) From bf1340939717d2846a9032b3e8109eb74c987d17 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 4 Sep 2025 10:19:55 +0100 Subject: [PATCH 3/8] Update examples/Peripherals/Registers/Reset_reason/Reset_reason.ino Co-authored-by: Frederic Pillon Signed-off-by: Alex --- examples/Peripherals/Registers/Reset_reason/Reset_reason.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index c2424fa..2653fd7 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -34,7 +34,7 @@ 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. - //Bellow is the WakeUp reset condition (needs STM32LowPower.h library) + // Below is the WakeUp reset condition (needs STM32LowPower.h library) //LowPower.shutdown(1000); // Below is the Software reset condition From 52fb3eab67c0d746036a53ca0559f24cc5b6e7c8 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 4 Sep 2025 10:20:03 +0100 Subject: [PATCH 4/8] Update examples/Peripherals/Registers/Reset_reason/Reset_reason.ino Co-authored-by: Frederic Pillon Signed-off-by: Alex --- examples/Peripherals/Registers/Reset_reason/Reset_reason.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index 2653fd7..b4f6792 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -9,7 +9,7 @@ #include "stm32yyxx_ll_rcc.h" #include "IWatchdog.h" -//#include "STM32LowPower.h" +// #include "STM32LowPower.h" #define USER_BTN_PIN USER_BTN // Adjust this for your board From e7b1025cfe782b6ad354d6acba75f8d87419e10c Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 4 Sep 2025 10:20:10 +0100 Subject: [PATCH 5/8] Update examples/Peripherals/Registers/Reset_reason/Reset_reason.ino Co-authored-by: Frederic Pillon Signed-off-by: Alex --- examples/Peripherals/Registers/Reset_reason/Reset_reason.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index b4f6792..92f166b 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -35,7 +35,7 @@ void Reset_My_MCU() { // 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); + // LowPower.shutdown(1000); // Below is the Software reset condition //NVIC_SystemReset(); From 57a24df802391128621d54c1087f26bfefecc3f6 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 4 Sep 2025 10:20:18 +0100 Subject: [PATCH 6/8] Update examples/Peripherals/Registers/Reset_reason/Reset_reason.ino Co-authored-by: Frederic Pillon Signed-off-by: Alex --- examples/Peripherals/Registers/Reset_reason/Reset_reason.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index 92f166b..a391a25 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -38,7 +38,7 @@ void Reset_My_MCU() { // LowPower.shutdown(1000); // Below is the Software reset condition - //NVIC_SystemReset(); + // NVIC_SystemReset(); // Below is the Watchdog Timer reset condition IWatchdog.begin(1000); //1ms tick then reset From 515b67ea855cc792497c03bb5214342fd4c1a1b5 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 4 Sep 2025 10:20:25 +0100 Subject: [PATCH 7/8] Update examples/Peripherals/Registers/Reset_reason/Reset_reason.ino Co-authored-by: Frederic Pillon Signed-off-by: Alex --- examples/Peripherals/Registers/Reset_reason/Reset_reason.ino | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index a391a25..bedd910 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -51,8 +51,6 @@ void Reset_My_MCU() { void setup() { pinMode(USER_BTN_PIN, INPUT); default_button_state = digitalRead(USER_BTN_PIN); - //Serial.setRx(PA3); - //Serial.setTx(PA2); Serial.begin(115200); while (!Serial) ; // Wait for Serial From d353c7a13b0fd15cd83037e165c356e55fa302be Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 4 Sep 2025 11:20:58 +0100 Subject: [PATCH 8/8] Update examples/Peripherals/Registers/Reset_reason/Reset_reason.ino Co-authored-by: Frederic Pillon Signed-off-by: Alex --- examples/Peripherals/Registers/Reset_reason/Reset_reason.ino | 2 -- 1 file changed, 2 deletions(-) diff --git a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino index bedd910..15179d0 100644 --- a/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino +++ b/examples/Peripherals/Registers/Reset_reason/Reset_reason.ino @@ -44,8 +44,6 @@ void Reset_My_MCU() { IWatchdog.begin(1000); //1ms tick then reset while (1) ; // Wait for reset - - } void setup() {