6#ifdef USE_CRASH_HANDLER
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
17#ifdef USE_BOOTLOADER_ADAFRUIT
24#if KERNEL_VERSION_NUMBER >= 0x030700
35#if defined(CONFIG_THREAD_NAME)
36 char thread[CONFIG_THREAD_MAX_NAME_LEN];
42#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
47 static bool opened =
false;
49 uart_line_ctrl_get(this->
uart_dev_, UART_LINE_CTRL_DTR, &dtr);
59#ifdef USE_CRASH_HANDLER
74 static const struct device *uart_dev =
nullptr;
75 switch (this->
uart_) {
77 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart0));
80 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
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)) {
93 if (device_is_ready(uart_dev)) {
95#if defined(USE_LOGGER_WAIT_FOR_CDC) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
97 int32_t
count = (10 * 100);
98 while (dtr == 0 &&
count-- > 0) {
99 uart_line_ctrl_get(this->
uart_dev_, UART_LINE_CTRL_DTR, &dtr);
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);
115#ifdef USE_CRASH_HANDLER
116#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
132 printk(
"%.*s",
static_cast<int>(
len), msg);
137 for (uint16_t i = 0; i <
len; ++i) {
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");
153 return LOG_STR(
"UNKNOWN");
157static const uint8_t REASON_BUF_SIZE = 32;
159static const char *reason_to_str(
unsigned int reason,
char *buf) {
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";
172 snprintf(buf, REASON_BUF_SIZE,
"Unknown error (%u)", reason);
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;
182static constexpr uint32_t ADAFRUIT_BOOTLOADER_MIN_VERSION =
183 (ADAFRUIT_BOOTLOADER_MIN_MAJOR << 16) | (ADAFRUIT_BOOTLOADER_MIN_MINOR << 8) | ADAFRUIT_BOOTLOADER_MIN_PATCH;
187 const uint32_t version = NRF_TIMER2->CC[0];
188 if (version >= ADAFRUIT_BOOTLOADER_MIN_VERSION)
191 char version_buf[
sizeof(
"255.255.255")] =
"unknown";
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));
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);
203 ESP_LOGD(TAG,
"Crash buffer address %p", &
crash_buf);
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,
209#if defined(CONFIG_THREAD_NAME)
210 ESP_LOGE(TAG,
"Thread: %s",
crash_buf.thread);
212 int32_t
count = (2 * 100);
213 while (
count-- > 0) {
227#if defined(CONFIG_THREAD_NAME)
228 auto thread = k_current_get();
229 const char *name = k_thread_name_get(thread);
240#ifdef USE_CRASH_HANDLER
uint32_t get_config_hash()
Get the config hash as a 32-bit integer.
void schedule_dump_config()
void write_msg_(const char *msg, uint16_t len)
const LogString * get_uart_selection_()
void pre_setup()
Set up this component.
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
void k_sys_fatal_error_handler(unsigned int reason, const esphome::logger::FatalErrorEsf *esf)
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)
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.
void print_coredump()
A log reader is present: serial at boot, USB CDC when the port opens, an API or MQTT client subscribi...
constexpr const char * CRASH_LOG_TAG
The tag the dump is logged under, so a client can check the level in force for it.
void stop_coredump()
The reader went away (the USB port was closed); the print stops and the dump stays for the next one.
const char * get_reset_reason(std::span< char, RESET_REASON_BUFFER_SIZE > buffer)
void HOT delay(uint32_t ms)
Application App
Global storage of Application pointer - only one Application can exist.