Skip to content

Commit 523b71e

Browse files
alexceltare2fpistm
andcommitted
feat: add 2 interesting examples
Signed-off-by: George Popa <george.popa@metasphere.co.uk> Co-authored-by: Frederic Pillon <frederic.pillon@st.com>
1 parent 33f94a1 commit 523b71e

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* UID Retrieve sketch
2+
* UID (Universal Identifier) is a ID that's etched to each MCU at factory release
3+
* so it's uniquely identifiable. This can help traceability and addressing devices
4+
* without having to craft a database yourself. This sketch retrieves UID, MAC, Device
5+
* and Revision ID of each MCU. Refer to the relevant datasheet to know where are these
6+
* values are stored in the registers.
7+
* The code is provided "as is" with no liability.
8+
*/
9+
10+
void setup() {
11+
Serial.begin(115200);
12+
while (!Serial)
13+
; // Wait for Serial to be ready
14+
15+
Serial.printf("%s Device Identifiers:\n", BOARD_NAME);
16+
17+
// Unique Device ID (96 bits / 12 bytes)
18+
uint32_t uid0 = HAL_GetUIDw0();
19+
uint32_t uid1 = HAL_GetUIDw1();
20+
uint32_t uid2 = HAL_GetUIDw2();
21+
22+
Serial.print("UID: ");
23+
Serial.print(uid2, HEX);
24+
Serial.print("-");
25+
Serial.print(uid1, HEX);
26+
Serial.print("-");
27+
Serial.println(uid0, HEX);
28+
29+
// MAC Address: typically stored in UID for STM32U series
30+
// Use the lower 6 bytes of the 96-bit UID (commonly used)
31+
uint8_t mac[6] = {
32+
(uint8_t)(uid0 >> 0),
33+
(uint8_t)(uid0 >> 8),
34+
(uint8_t)(uid0 >> 16),
35+
(uint8_t)(uid1 >> 0),
36+
(uint8_t)(uid1 >> 8),
37+
(uint8_t)(uid1 >> 16)
38+
};
39+
40+
Serial.print("MAC Address: ");
41+
for (int i = 0; i < 6; i++) {
42+
if (mac[i] < 0x10) Serial.print("0");
43+
Serial.print(mac[i], HEX);
44+
if (i < 5) Serial.print(":");
45+
}
46+
Serial.println();
47+
Serial.printf("Device ID: 0x%x\n", HAL_GetDEVID());
48+
Serial.printf("Revision ID: 0x%x\n", HAL_GetREVID());
49+
}
50+
51+
void loop() {
52+
// Nothing here
53+
}

0 commit comments

Comments
 (0)