ESPHome 2026.3.0-dev
Loading...
Searching...
No Matches
reset_reason.cpp
Go to the documentation of this file.
1#include "reset_reason.h"
2
3#if defined(USE_ZEPHYR) && (defined(USE_LOGGER_EARLY_MESSAGE) || defined(USE_DEBUG))
4#include "esphome/core/log.h"
6#include <zephyr/drivers/hwinfo.h>
7
8namespace esphome::zephyr {
9
10static const char *const TAG = "zephyr";
11
12static size_t append_reset_reason(char *buf, size_t size, size_t pos, bool set, const char *reason) {
13 if (!set) {
14 return pos;
15 }
16 if (pos > 0) {
17 pos = buf_append_printf(buf, size, pos, ", ");
18 }
19 return buf_append_printf(buf, size, pos, "%s", reason);
20}
21
22const char *get_reset_reason(std::span<char, RESET_REASON_BUFFER_SIZE> buffer) {
23 char *buf = buffer.data();
24 const size_t size = RESET_REASON_BUFFER_SIZE;
25
26 uint32_t cause;
27 auto ret = hwinfo_get_reset_cause(&cause);
28 if (ret) {
29 ESP_LOGE(TAG, "Unable to get reset cause: %d", ret);
30 buf[0] = '\0';
31 return buf;
32 }
33 size_t pos = 0;
34
35 if (cause == 0) {
36 pos = append_reset_reason(buf, size, pos, true, "None");
37 } else {
38 pos = append_reset_reason(buf, size, pos, cause & RESET_PIN, "External pin");
39 pos = append_reset_reason(buf, size, pos, cause & RESET_SOFTWARE, "Software reset");
40 pos = append_reset_reason(buf, size, pos, cause & RESET_BROWNOUT, "Brownout (drop in voltage)");
41 pos = append_reset_reason(buf, size, pos, cause & RESET_POR, "Power-on reset (POR)");
42 pos = append_reset_reason(buf, size, pos, cause & RESET_WATCHDOG, "Watchdog timer expiration");
43 pos = append_reset_reason(buf, size, pos, cause & RESET_DEBUG, "Debug event");
44 pos = append_reset_reason(buf, size, pos, cause & RESET_SECURITY, "Security violation");
45 pos = append_reset_reason(buf, size, pos, cause & RESET_LOW_POWER_WAKE, "Waking up from low power mode");
46 pos = append_reset_reason(buf, size, pos, cause & RESET_CPU_LOCKUP, "CPU lock-up detected");
47 pos = append_reset_reason(buf, size, pos, cause & RESET_PARITY, "Parity error");
48 pos = append_reset_reason(buf, size, pos, cause & RESET_PLL, "PLL error");
49 pos = append_reset_reason(buf, size, pos, cause & RESET_CLOCK, "Clock error");
50 pos = append_reset_reason(buf, size, pos, cause & RESET_HARDWARE, "Hardware reset");
51 pos = append_reset_reason(buf, size, pos, cause & RESET_USER, "User reset");
52 pos = append_reset_reason(buf, size, pos, cause & RESET_TEMPERATURE, "Temperature reset");
53 }
54
55 // Ensure null termination if nothing was written
56 if (pos == 0) {
57 buf[0] = '\0';
58 }
59 return buf;
60}
61} // namespace esphome::zephyr
62
63#endif
const char *const TAG
Definition spi.cpp:7
const char * get_reset_reason(std::span< char, RESET_REASON_BUFFER_SIZE > buffer)
size_t size
Definition helpers.h:854
size_t size_t pos
Definition helpers.h:854