ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
logger_zephyr.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
2
4#include "esphome/core/log.h"
5#include "logger.h"
6#ifdef USE_CRASH_HANDLER
8#endif
9
10#include <zephyr/device.h>
11#include <zephyr/drivers/uart.h>
12#include <zephyr/sys/printk.h>
13#include <zephyr/usb/usb_device.h>
14#ifdef USE_LOGGER_EARLY_MESSAGE
16#endif
17#ifdef USE_BOOTLOADER_ADAFRUIT
18#include <nrf.h>
19#endif
20
21namespace esphome::logger {
22
23// Zephyr 3.7 renamed z_arch_esf_t to struct arch_esf; the old name was later removed.
24#if KERNEL_VERSION_NUMBER >= 0x030700
25using FatalErrorEsf = ::arch_esf;
26#else
27using FatalErrorEsf = z_arch_esf_t;
28#endif
29
30__attribute__((section(".noinit"))) struct {
31 uint32_t magic;
32 uint32_t reason;
35#if defined(CONFIG_THREAD_NAME)
36 char thread[CONFIG_THREAD_MAX_NAME_LEN];
37#endif
38} crash_buf; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
39
40ESPHOME_LOG_TAG(TAG, "logger");
41
42#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
44 if (this->uart_ != UART_SELECTION_USB_CDC || this->uart_dev_ == nullptr) {
45 return;
46 }
47 static bool opened = false;
48 uint32_t dtr = 0;
49 uart_line_ctrl_get(this->uart_dev_, UART_LINE_CTRL_DTR, &dtr);
50
51 /* Poll if the DTR flag was set, optional */
52 if (opened == dtr) {
53 return;
54 }
55
56 if (!opened) {
58 }
59#ifdef USE_CRASH_HANDLER
60 // The new reader also gets a coredump stored by the last crash; a closed port keeps it for the next one
61 if (!opened) {
63 } else {
65 }
66#endif
67 opened = !opened;
68}
69#endif
70
71void Logger::pre_setup() {
72#ifdef CONFIG_SERIAL
73 if (this->baud_rate_ > 0) {
74 static const struct device *uart_dev = nullptr;
75 switch (this->uart_) {
76 case UART_SELECTION_UART0: // NOLINT(bugprone-branch-clone)
77 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart0));
78 break;
80 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
81 break;
82#ifdef USE_LOGGER_USB_CDC
84#ifdef CONFIG_USB_DEVICE_STACK
85 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
86 if (device_is_ready(uart_dev)) {
87 usb_enable(nullptr);
88 }
89#endif
90 break;
91#endif
92 }
93 if (device_is_ready(uart_dev)) {
94 this->uart_dev_ = uart_dev;
95#if defined(USE_LOGGER_WAIT_FOR_CDC) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
96 uint32_t dtr = 0;
97 int32_t count = (10 * 100); // wait 10 sec for USB CDC to have early logs
98 while (dtr == 0 && count-- > 0) {
99 uart_line_ctrl_get(this->uart_dev_, UART_LINE_CTRL_DTR, &dtr);
100 delay(10);
102 }
103#endif
104 }
105 }
106#endif
107 global_logger = this;
108 ESP_LOGI(TAG, "Log initialized");
109#ifdef USE_LOGGER_EARLY_MESSAGE
110 char reason_buffer[zephyr::RESET_REASON_BUFFER_SIZE];
111 const char *reset_reason = zephyr::get_reset_reason(std::span<char, zephyr::RESET_REASON_BUFFER_SIZE>(reason_buffer));
112 ESP_LOGI(TAG, "Reset reason: %s", reset_reason);
113 dump_crash_();
114#endif
115#ifdef USE_CRASH_HANDLER
116#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
117 // Nobody reads a USB port this early; cdc_loop_() reports the dump once the port is opened
118 if (this->uart_ == UART_SELECTION_USB_CDC)
119 return;
120#endif
121 // Without a UART (baud_rate: 0, or one that did not come up) nothing reaches the console
122 if (this->uart_dev_ != nullptr)
124#endif
125}
126
127void HOT Logger::write_msg_(const char *msg, uint16_t len) {
128 // Single write with newline already in buffer (added by caller)
129#ifdef CONFIG_PRINTK
130 // Requires the debug component and an active SWD connection.
131 // It is used for pyocd rtt -t nrf52840
132 printk("%.*s", static_cast<int>(len), msg);
133#endif
134 if (this->uart_dev_ == nullptr) {
135 return;
136 }
137 for (uint16_t i = 0; i < len; ++i) {
138 uart_poll_out(this->uart_dev_, msg[i]);
139 }
140}
141
142const LogString *Logger::get_uart_selection_() {
143 switch (this->uart_) {
145 return LOG_STR("UART0");
147 return LOG_STR("UART1");
148#ifdef USE_LOGGER_USB_CDC
150 return LOG_STR("USB_CDC");
151#endif
152 default:
153 return LOG_STR("UNKNOWN");
154 }
155}
156
157static const uint8_t REASON_BUF_SIZE = 32;
158
159static const char *reason_to_str(unsigned int reason, char *buf) {
160 switch (reason) {
161 case K_ERR_CPU_EXCEPTION:
162 return "CPU exception";
163 case K_ERR_SPURIOUS_IRQ:
164 return "Unhandled interrupt";
165 case K_ERR_STACK_CHK_FAIL:
166 return "Stack overflow";
167 case K_ERR_KERNEL_OOPS:
168 return "Kernel oops";
169 case K_ERR_KERNEL_PANIC:
170 return "Kernel panic";
171 default:
172 snprintf(buf, REASON_BUF_SIZE, "Unknown error (%u)", reason);
173 return buf;
174 }
175}
176
177#ifdef USE_BOOTLOADER_ADAFRUIT
178static constexpr unsigned ADAFRUIT_BOOTLOADER_MIN_MAJOR = 0;
179static constexpr unsigned ADAFRUIT_BOOTLOADER_MIN_MINOR = 11;
180static constexpr unsigned ADAFRUIT_BOOTLOADER_MIN_PATCH = 0;
181// Adafruit_nRF52_Bootloader passes its version to the app as 0x00MMmmpp (major, minor, patch)
182static constexpr uint32_t ADAFRUIT_BOOTLOADER_MIN_VERSION =
183 (ADAFRUIT_BOOTLOADER_MIN_MAJOR << 16) | (ADAFRUIT_BOOTLOADER_MIN_MINOR << 8) | ADAFRUIT_BOOTLOADER_MIN_PATCH;
184
186 // NOLINTNEXTLINE(clang-analyzer-core.FixedAddressDereference) -- NRF_TIMER2 is MMIO at a fixed address
187 const uint32_t version = NRF_TIMER2->CC[0];
188 if (version >= ADAFRUIT_BOOTLOADER_MIN_VERSION)
189 return;
190 // Releases too old to pass their version leave the register at its reset value
191 char version_buf[sizeof("255.255.255")] = "unknown";
192 if (version != 0) {
193 snprintf(version_buf, sizeof(version_buf), "%u.%u.%u", static_cast<unsigned>((version >> 16) & 0xFF),
194 static_cast<unsigned>((version >> 8) & 0xFF), static_cast<unsigned>(version & 0xFF));
195 }
196 ESP_LOGW(TAG,
197 "Adafruit_nRF52_Bootloader version %s has known stability issues. Please update it to %u.%u.%u or newer.",
198 version_buf, ADAFRUIT_BOOTLOADER_MIN_MAJOR, ADAFRUIT_BOOTLOADER_MIN_MINOR, ADAFRUIT_BOOTLOADER_MIN_PATCH);
199}
200#endif
201
203 ESP_LOGD(TAG, "Crash buffer address %p", &crash_buf);
204 if (crash_buf.magic == App.get_config_hash()) {
205 char reason_buf[REASON_BUF_SIZE];
206 ESP_LOGE(TAG, "Last crash:");
207 ESP_LOGE(TAG, "Reason=%s PC=0x%08x LR=0x%08x", reason_to_str(crash_buf.reason, reason_buf), crash_buf.pc,
208 crash_buf.lr);
209#if defined(CONFIG_THREAD_NAME)
210 ESP_LOGE(TAG, "Thread: %s", crash_buf.thread);
211#endif
212 int32_t count = (2 * 100); // wait 2 sec to give a chance to print crash
213 while (count-- > 0) {
214 delay(10);
216 }
217 }
218}
219
220void k_sys_fatal_error_handler(unsigned int reason, const FatalErrorEsf *esf) {
221 crash_buf.magic = App.get_config_hash();
222 crash_buf.reason = reason;
223 if (esf) {
224 crash_buf.pc = esf->basic.pc;
225 crash_buf.lr = esf->basic.lr;
226 }
227#if defined(CONFIG_THREAD_NAME)
228 auto thread = k_current_get();
229 const char *name = k_thread_name_get(thread);
230 if (name) {
231 strncpy(crash_buf.thread, name, sizeof(crash_buf.thread) - 1);
232 crash_buf.thread[sizeof(crash_buf.thread) - 1] = '\0';
233 } else {
234 crash_buf.thread[0] = '\0';
235 }
236#endif
237 arch_restart();
238}
239
240#ifdef USE_CRASH_HANDLER
242void log_crash_to_client() {
243 // A level that drops the lines would erase the dump unseen; keep it for a client that can see it
244 if (global_logger->get_log_level(zephyr_coredump::CRASH_LOG_TAG) < ESPHOME_LOG_LEVEL_ERROR)
245 return;
247}
248#endif
249
250} // namespace esphome::logger
251
252extern "C" {
253
257}
258
259#endif
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
UARTSelection uart_
Definition logger.h:396
void write_msg_(const char *msg, uint16_t len)
Definition logger_esp32.h:9
const LogString * get_uart_selection_()
uint8_t get_log_level()
Definition logger.h:187
void pre_setup()
Set up this component.
const device * uart_dev_
Definition logger.h:354
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
Definition main_task.h:45
unsigned count
Definition gif.cpp:51
void k_sys_fatal_error_handler(unsigned int reason, const esphome::logger::FatalErrorEsf *esf)
::arch_esf FatalErrorEsf
bool crash_log_pending()
Whether a crash record from before this boot is waiting to be reported.
void k_sys_fatal_error_handler(unsigned int reason, const FatalErrorEsf *esf)
@ UART_SELECTION_USB_CDC
Definition logger.h:121
@ UART_SELECTION_UART0
Definition logger.h:112
@ UART_SELECTION_UART1
Definition logger.h:116
Logger * global_logger
Definition logger.cpp:396
void log_crash_to_client()
Log the previous crash for a client that just subscribed to the logs.
ESPHOME_LOG_TAG(TAG, "logger")
bool coredump_pending()
Whether a dump from before this boot is waiting to be printed.
Definition coredump.cpp:35
void print_coredump()
A log reader is present: serial at boot, USB CDC when the port opens, an API or MQTT client subscribi...
Definition coredump.cpp:37
constexpr const char * CRASH_LOG_TAG
The tag the dump is logged under, so a client can check the level in force for it.
Definition coredump.h:7
void stop_coredump()
The reader went away (the USB port was closed); the print stops and the dump stays for the next one.
Definition coredump.cpp:43
const char * get_reset_reason(std::span< char, RESET_REASON_BUFFER_SIZE > buffer)
const void size_t len
Definition hal.h:68
void arch_feed_wdt()
Definition hal.cpp:115
void HOT delay(uint32_t ms)
Definition hal.cpp:85
void arch_restart()
Definition hal.cpp:36
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t