rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
stm32_reset_cause.cpp
Go to the documentation of this file.
1/**
2 * @file stm32_reset_cause.cpp
3 * @brief Get Reset Cause for STM32 MCUs
4 *
5 * @date Dec 10, 2023
6 * @author Andrey Belomutskiy, (c) 2012-2023
7 * @author andreika <prometheus.pcb@gmail.com>
8 */
9
10#include "pch.h"
11
12#if EFI_PROD_CODE
13#include "mpu_util.h"
14#endif /* EFI_PROD_CODE */
15
16#ifdef STM32H7XX
17// Reset Status Register flags for H7
18#define BORRSTF RCC_RSR_BORRSTF
19#define PINRSTF RCC_RSR_PINRSTF
20#define PORRSTF RCC_RSR_PORRSTF
21#define SFTRSTF RCC_RSR_SFTRSTF
22#define IWDGRSTF RCC_RSR_IWDG1RSTF
23#define WWDGRSTF RCC_RSR_WWDG1RSTF
24#define LPWRRSTF RCC_RSR_LPWRRSTF
25#else
26// Control/Status Register flags for F4/F7
27#define BORRSTF RCC_CSR_BORRSTF
28#define PINRSTF RCC_CSR_PINRSTF
29#define PORRSTF RCC_CSR_PORRSTF
30#define SFTRSTF RCC_CSR_SFTRSTF
31#define IWDGRSTF RCC_CSR_IWDGRSTF
32#define WWDGRSTF RCC_CSR_WWDGRSTF
33#define LPWRRSTF RCC_CSR_LPWRRSTF
34#endif // STM32H7XX
35
37#ifdef STM32H7XX
38 uint32_t cause = RCC->RSR; // Read the Reset Status Register
39 // keep reset reason visible to main app
40#ifndef EFI_BOOTLOADER
41 // Clear reset flags for future reset detection
42 RCC->RSR |= RCC_RSR_RMVF;
43#endif
44#else
45 uint32_t cause = RCC->CSR; // Read the Control/Status Register
46 // keep reset reason visible to main app
47#ifndef EFI_BOOTLOADER
48 // Clear reset flags for future reset detection
49 RCC->CSR |= RCC_CSR_RMVF;
50#endif
51#endif
52
53 if (cause & BORRSTF) {
54 return Reset_Cause_BOR;
55 } else if (cause & PORRSTF) {
56 return Reset_Cause_POR;
57 } else if (cause & SFTRSTF) {
59 } else if (cause & IWDGRSTF) {
61 } else if (cause & WWDGRSTF) {
63 } else if (cause & LPWRRSTF) {
65 } else if (cause & PINRSTF) {
66 // See STM32 datasheet "Simplified diagram of the reset circuit"
67 // All internal resets happens through driving NRST pin low
68 // This cause rise of PINRSTF bit in CSR register
70 }
72}
73
74// we need to read the reset cause on the early stage, before we setup the rest of MCU hardware
75static const volatile Reset_Cause_t resetCause = readMCUResetCause();
76
80
81const char *getMCUResetCause(Reset_Cause_t cause) {
82 switch (cause) {
84 return "Independent hardware watchdog";
86 return "Window watchdog";
88 return "NVIC_SystemReset or by debugger";
90 return "Reset from NRST pin";
91 case Reset_Cause_POR:
92 return "Power on/power-down reset";
94 return "Reset after illegal Stop, Standby or Shutdown mode entry";
95 case Reset_Cause_BOR:
96 return "BOR reset";
98 return "Firewall reset";
100 return "Option byte load reset";
101 default:
102 return "Unknown";
103 }
104}
105
Reset_Cause_t
Definition mpu_util.h:104
@ Reset_Cause_WWatchdog
Definition mpu_util.h:107
@ Reset_Cause_Firewall
Definition mpu_util.h:113
@ Reset_Cause_Soft_Reset
Definition mpu_util.h:108
@ Reset_Cause_Illegal_Mode
Definition mpu_util.h:111
@ Reset_Cause_NRST_Pin
Definition mpu_util.h:109
@ Reset_Cause_Unknown
Definition mpu_util.h:105
@ Reset_Cause_POR
Definition mpu_util.h:110
@ Reset_Cause_IWatchdog
Definition mpu_util.h:106
@ Reset_Cause_BOR
Definition mpu_util.h:112
@ Reset_Cause_Option_Byte
Definition mpu_util.h:114
Reset_Cause_t getMCUResetCause()
static Reset_Cause_t readMCUResetCause()
static const volatile Reset_Cause_t resetCause