|
| 1 | +/* Last Reset Reason Sketch |
| 2 | +* This sketch will determine what caused the last reset on the STM32 MCU. Most microcontrollers |
| 3 | +* 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 |
| 5 | +* which triggers the Reset_my_MCU() function or unplug the USB cable and repluggit back. Adjust your |
| 6 | +* UART, USER Button pin and registers accordingly. Use the MCU's datasheet and/or stm32yyyxxx.h for reference. |
| 7 | +* The code is provided "as is" with no liability. |
| 8 | +*/ |
| 9 | + |
| 10 | +#include "stm32yyxx_ll_rcc.h" |
| 11 | +#include "IWatchdog.h" |
| 12 | + |
| 13 | +#define USER_BTN_PIN USER_BTN // Adjust this for your board |
| 14 | + |
| 15 | +// Enumerator for combining reset flag bits into one byte then display them |
| 16 | +enum reset_reason { |
| 17 | + UNKNOWN_RESET = 0, |
| 18 | + BROWN_OUT = 1 << 0, |
| 19 | + NRST_PIN = 1 << 1, |
| 20 | + SOFTWARE_RST = 1 << 2, |
| 21 | + INDEPENDENT_WDG = 1 << 3, |
| 22 | + WINDOW_WDG = 1 << 4, |
| 23 | + LOW_POWER = 1 << 5, |
| 24 | + OPTION_BYTE_LOADER = 1 << 6, |
| 25 | + POWER_ON_DOWN = 1 << 7 |
| 26 | +}; |
| 27 | + |
| 28 | +reset_reason last_reset_reason = UNKNOWN_RESET; |
| 29 | +static int default_button_state = LOW; |
| 30 | + |
| 31 | +void Reset_My_MCU() { |
| 32 | + // There are a few reset conditions. |
| 33 | + // Keep the one you wish to use and comment out the others. |
| 34 | + // Below is the Software reset condition |
| 35 | + // NVIC_SystemReset(); |
| 36 | + |
| 37 | + // Below is the Watchdog Timer reset condition |
| 38 | + IWatchdog.begin(1000); //1ms tick then reset |
| 39 | + while (1) |
| 40 | + ; // Wait for reset |
| 41 | +} |
| 42 | + |
| 43 | +void setup() { |
| 44 | + pinMode(USER_BTN_PIN, INPUT); |
| 45 | + default_button_state = digitalRead(USER_BTN_PIN); |
| 46 | + Serial.begin(115200); |
| 47 | + while (!Serial) |
| 48 | + ; // Wait for Serial |
| 49 | + |
| 50 | +#ifdef RCC_CSR_BORRSTF |
| 51 | + if (LL_RCC_IsActiveFlag_BORRST()) last_reset_reason = (reset_reason)(last_reset_reason | BROWN_OUT); |
| 52 | +#endif |
| 53 | + if (LL_RCC_IsActiveFlag_PINRST()) last_reset_reason = (reset_reason)(last_reset_reason | NRST_PIN); |
| 54 | + if (LL_RCC_IsActiveFlag_SFTRST()) last_reset_reason = (reset_reason)(last_reset_reason | SOFTWARE_RST); |
| 55 | +#if defined(RCC_RSR_IWDG1RSTF) |
| 56 | + if (LL_RCC_IsActiveFlag_IWDG1RST()) last_reset_reason = (reset_reason)(last_reset_reason | INDEPENDENT_WDG); |
| 57 | +#else |
| 58 | + if (LL_RCC_IsActiveFlag_IWDGRST()) last_reset_reason = (reset_reason)(last_reset_reason | INDEPENDENT_WDG); |
| 59 | +#endif |
| 60 | +#if defined(RCC_RSR_WWDG1RSTF) |
| 61 | + if (LL_RCC_IsActiveFlag_WWDG1RST()) last_reset_reason = (reset_reason)(last_reset_reason | WINDOW_WDG); |
| 62 | +#else |
| 63 | + if (LL_RCC_IsActiveFlag_WWDGRST()) last_reset_reason = (reset_reason)(last_reset_reason | WINDOW_WDG); |
| 64 | +#endif |
| 65 | + if (LL_RCC_IsActiveFlag_LPWRRST()) last_reset_reason = (reset_reason)(last_reset_reason | LOW_POWER); |
| 66 | +#if defined(RCC_CSR_OBLRSTF) || defined(RCC_CSR2_OBLRSTF) |
| 67 | + if (LL_RCC_IsActiveFlag_OBLRST()) last_reset_reason = (reset_reason)(last_reset_reason | OPTION_BYTE_LOADER); |
| 68 | +#endif |
| 69 | +#ifdef RCC_CSR_PORRSTF |
| 70 | + if (LL_RCC_IsActiveFlag_PORRST()) last_reset_reason = (reset_reason)(last_reset_reason | POWER_ON_DOWN); |
| 71 | +#endif |
| 72 | + |
| 73 | + // Clear reset flags |
| 74 | + LL_RCC_ClearResetFlags(); |
| 75 | +} |
| 76 | + |
| 77 | +void loop() { |
| 78 | + Serial.println("Last reset reason:"); |
| 79 | + |
| 80 | + if (last_reset_reason & BROWN_OUT) Serial.println(" - Brown-out reset"); |
| 81 | + if (last_reset_reason & SOFTWARE_RST) Serial.println(" - Software reset"); |
| 82 | + if (last_reset_reason & INDEPENDENT_WDG) Serial.println(" - Independent Watchdog reset"); |
| 83 | + if (last_reset_reason & WINDOW_WDG) Serial.println(" - Window Watchdog reset"); |
| 84 | + if (last_reset_reason & LOW_POWER) Serial.println(" - Low-power reset"); |
| 85 | + 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 |
| 87 | + if (last_reset_reason & POWER_ON_DOWN) Serial.println(" - Power on or power down reset"); |
| 88 | + if (last_reset_reason == UNKNOWN_RESET) Serial.println(" - Unknown or no flags set"); |
| 89 | + last_reset_reason = UNKNOWN_RESET; |
| 90 | + |
| 91 | + // Trigger software reset on button press |
| 92 | + if (digitalRead(USER_BTN_PIN) != default_button_state) { |
| 93 | + Serial.println("Button pressed → Triggering reset..."); |
| 94 | + delay(300); // Debounce |
| 95 | + Reset_My_MCU(); |
| 96 | + } |
| 97 | + |
| 98 | + delay(1000); |
| 99 | +} |
0 commit comments