Merge remote-tracking branch 'origin/fix_logger_loop_disable' into integration

This commit is contained in:
J. Nick Koston
2026-02-20 17:04:06 -06:00
15 changed files with 236 additions and 69 deletions

View File

@@ -2,6 +2,7 @@
#ifdef USE_ZEPHYR
#include <climits>
#include "esphome/core/log.h"
#include <esphome/components/zephyr/reset_reason.h>
#include <zephyr/drivers/hwinfo.h>
#include <hal/nrf_power.h>
#include <cstdint>
@@ -15,16 +16,6 @@ static const char *const TAG = "debug";
constexpr std::uintptr_t MBR_PARAM_PAGE_ADDR = 0xFFC;
constexpr std::uintptr_t MBR_BOOTLOADER_ADDR = 0xFF8;
static size_t append_reset_reason(char *buf, size_t size, size_t pos, bool set, const char *reason) {
if (!set) {
return pos;
}
if (pos > 0) {
pos = buf_append_printf(buf, size, pos, ", ");
}
return buf_append_printf(buf, size, pos, "%s", reason);
}
static inline uint32_t read_mem_u32(uintptr_t addr) {
return *reinterpret_cast<volatile uint32_t *>(addr); // NOLINT(performance-no-int-to-ptr)
}
@@ -57,39 +48,7 @@ static inline uint32_t sd_version_get() {
}
const char *DebugComponent::get_reset_reason_(std::span<char, RESET_REASON_BUFFER_SIZE> buffer) {
char *buf = buffer.data();
const size_t size = RESET_REASON_BUFFER_SIZE;
uint32_t cause;
auto ret = hwinfo_get_reset_cause(&cause);
if (ret) {
ESP_LOGE(TAG, "Unable to get reset cause: %d", ret);
buf[0] = '\0';
return buf;
}
size_t pos = 0;
pos = append_reset_reason(buf, size, pos, cause & RESET_PIN, "External pin");
pos = append_reset_reason(buf, size, pos, cause & RESET_SOFTWARE, "Software reset");
pos = append_reset_reason(buf, size, pos, cause & RESET_BROWNOUT, "Brownout (drop in voltage)");
pos = append_reset_reason(buf, size, pos, cause & RESET_POR, "Power-on reset (POR)");
pos = append_reset_reason(buf, size, pos, cause & RESET_WATCHDOG, "Watchdog timer expiration");
pos = append_reset_reason(buf, size, pos, cause & RESET_DEBUG, "Debug event");
pos = append_reset_reason(buf, size, pos, cause & RESET_SECURITY, "Security violation");
pos = append_reset_reason(buf, size, pos, cause & RESET_LOW_POWER_WAKE, "Waking up from low power mode");
pos = append_reset_reason(buf, size, pos, cause & RESET_CPU_LOCKUP, "CPU lock-up detected");
pos = append_reset_reason(buf, size, pos, cause & RESET_PARITY, "Parity error");
pos = append_reset_reason(buf, size, pos, cause & RESET_PLL, "PLL error");
pos = append_reset_reason(buf, size, pos, cause & RESET_CLOCK, "Clock error");
pos = append_reset_reason(buf, size, pos, cause & RESET_HARDWARE, "Hardware reset");
pos = append_reset_reason(buf, size, pos, cause & RESET_USER, "User reset");
pos = append_reset_reason(buf, size, pos, cause & RESET_TEMPERATURE, "Temperature reset");
// Ensure null termination if nothing was written
if (pos == 0) {
buf[0] = '\0';
}
const char *buf = zephyr::get_reset_reason(buffer);
ESP_LOGD(TAG, "Reset Reason: %s", buf);
return buf;
}

View File

@@ -101,6 +101,8 @@ CONF_INITIAL_LEVEL = "initial_level"
CONF_LOGGER_ID = "logger_id"
CONF_RUNTIME_TAG_LEVELS = "runtime_tag_levels"
CONF_TASK_LOG_BUFFER_SIZE = "task_log_buffer_size"
CONF_WAIT_FOR_CDC = "wait_for_cdc"
CONF_EARLY_MESSAGE = "early_message"
UART_SELECTION_ESP32 = {
VARIANT_ESP32: [UART0, UART1, UART2],
@@ -208,6 +210,12 @@ def validate_initial_no_higher_than_global(config):
return config
def validate_wait_for_cdc(config):
if config.get(CONF_WAIT_FOR_CDC) and config.get(CONF_HARDWARE_UART) != USB_CDC:
raise cv.Invalid("wait_for_cdc requires hardware_uart: USB_CDC")
return config
Logger = logger_ns.class_("Logger", cg.Component)
LoggerMessageTrigger = logger_ns.class_(
"LoggerMessageTrigger",
@@ -300,10 +308,18 @@ CONFIG_SCHEMA = cv.All(
cv.SplitDefault(
CONF_ESP8266_STORE_LOG_STRINGS_IN_FLASH, esp8266=True
): cv.All(cv.only_on_esp8266, cv.boolean),
cv.SplitDefault(CONF_WAIT_FOR_CDC, nrf52=False): cv.All(
cv.only_on(PLATFORM_NRF52),
cv.boolean,
),
cv.SplitDefault(CONF_EARLY_MESSAGE, nrf52=False): cv.All(
cv.only_on(PLATFORM_NRF52), cv.boolean
),
}
).extend(cv.COMPONENT_SCHEMA),
validate_local_no_higher_than_global,
validate_initial_no_higher_than_global,
validate_wait_for_cdc,
)
@@ -425,7 +441,14 @@ async def to_code(config):
except cv.Invalid:
pass
if config.get(CONF_WAIT_FOR_CDC):
cg.add_define("USE_LOGGER_WAIT_FOR_CDC")
if config.get(CONF_EARLY_MESSAGE):
cg.add_define("USE_LOGGER_EARLY_MESSAGE")
if CORE.is_nrf52:
# esphome implement own fatal error handler which save PC/LR before reset
zephyr_add_prj_conf("RESET_ON_FATAL_ERROR", False)
zephyr_add_prj_conf("THREAD_LOCAL_STORAGE", True)
if config[CONF_HARDWARE_UART] == UART0:
zephyr_add_overlay("""&uart0 { status = "okay";};""")

View File

@@ -260,6 +260,9 @@ void Logger::dump_config() {
ESP_LOGCONFIG(TAG, " Level for '%s': %s", it.first, LOG_STR_ARG(get_log_level_str(it.second)));
}
#endif
#ifdef USE_ZEPHYR
dump_crash_();
#endif
}
void Logger::set_log_level(uint8_t level) {

View File

@@ -317,6 +317,7 @@ class Logger : public Component {
Stream *hw_serial_{nullptr};
#endif
#if defined(USE_ZEPHYR)
void dump_crash_();
const device *uart_dev_{nullptr};
#endif
#if defined(USE_ESP32) || defined(USE_LIBRETINY) || defined(USE_ZEPHYR)

View File

@@ -8,9 +8,30 @@
#include <zephyr/drivers/uart.h>
#include <zephyr/sys/printk.h>
#include <zephyr/usb/usb_device.h>
#ifdef USE_LOGGER_EARLY_MESSAGE
#include <esphome/components/zephyr/reset_reason.h>
#endif
namespace esphome::zephyr_coredump {
__attribute__((weak)) void print_coredump() {}
} // namespace esphome::zephyr_coredump
namespace esphome::logger {
static const uint32_t CRASH_MAGIC = 0xDEADBEEF;
__attribute__((section(".noinit"))) struct {
uint32_t magic;
uint32_t reason;
uint32_t pc;
uint32_t lr;
#if defined(CONFIG_THREAD_NAME)
char thread[CONFIG_THREAD_MAX_NAME_LEN];
#endif
} crash_buf; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
static const char *const TAG = "logger";
#ifdef USE_LOGGER_UART_SELECTION_USB_CDC
@@ -57,10 +78,26 @@ void Logger::pre_setup() {
ESP_LOGE(TAG, "%s is not ready.", LOG_STR_ARG(get_uart_selection_()));
} else {
this->uart_dev_ = uart_dev;
#if defined(USE_LOGGER_WAIT_FOR_CDC) && defined(USE_LOGGER_UART_SELECTION_USB_CDC)
uint32_t dtr = 0;
uint32_t count = (10 * 100); // wait 10 sec for USB CDC to have early logs
while (dtr == 0 && count-- != 0) {
uart_line_ctrl_get(this->uart_dev_, UART_LINE_CTRL_DTR, &dtr);
delay(10);
arch_feed_wdt();
}
#endif
}
}
global_logger = this;
ESP_LOGI(TAG, "Log initialized");
#ifdef USE_LOGGER_EARLY_MESSAGE
char reason_buffer[zephyr::RESET_REASON_BUFFER_SIZE];
const char *reset_reason = zephyr::get_reset_reason(std::span<char, zephyr::RESET_REASON_BUFFER_SIZE>(reason_buffer));
ESP_LOGI(TAG, "Reset reason: %s", reset_reason);
dump_crash_();
zephyr_coredump::print_coredump();
#endif
}
void HOT Logger::write_msg_(const char *msg, uint16_t len) {
@@ -93,6 +130,66 @@ const LogString *Logger::get_uart_selection_() {
}
}
static const uint8_t REASON_BUF_SIZE = 32;
static const char *reason_to_str(unsigned int reason, char *buf) {
switch (reason) {
case K_ERR_CPU_EXCEPTION:
return "CPU exception";
case K_ERR_SPURIOUS_IRQ:
return "Unhandled interrupt";
case K_ERR_STACK_CHK_FAIL:
return "Stack overflow";
case K_ERR_KERNEL_OOPS:
return "Kernel oops";
case K_ERR_KERNEL_PANIC:
return "Kernel panic";
default:
snprintf(buf, REASON_BUF_SIZE, "Unknown error (%u)", reason);
return buf;
}
}
void Logger::dump_crash_() {
ESP_LOGD(TAG, "Crash buffer address %p", &crash_buf);
if (crash_buf.magic == CRASH_MAGIC) {
char reason_buf[REASON_BUF_SIZE];
ESP_LOGE(TAG, "Last crash:");
ESP_LOGE(TAG, "Reason=%s PC=0x%08x LR=0x%08x", reason_to_str(crash_buf.reason, reason_buf), crash_buf.pc,
crash_buf.lr);
#if defined(CONFIG_THREAD_NAME)
ESP_LOGE(TAG, "Thread: %s", crash_buf.thread);
#endif
}
}
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *esf) {
crash_buf.magic = CRASH_MAGIC;
crash_buf.reason = reason;
if (esf) {
crash_buf.pc = esf->basic.pc;
crash_buf.lr = esf->basic.lr;
}
#if defined(CONFIG_THREAD_NAME)
auto thread = k_current_get();
const char *name = k_thread_name_get(thread);
if (name) {
strncpy(crash_buf.thread, name, sizeof(crash_buf.thread) - 1);
crash_buf.thread[sizeof(crash_buf.thread) - 1] = '\0';
} else {
crash_buf.thread[0] = '\0';
}
#endif
arch_restart();
}
} // namespace esphome::logger
extern "C" {
void k_sys_fatal_error_handler(unsigned int reason, const z_arch_esf_t *esf) {
esphome::logger::k_sys_fatal_error_handler(reason, esf);
}
}
#endif

View File

@@ -133,12 +133,12 @@ MAX7219_ON_ACTION_SCHEMA = automation.maybe_simple_id(
@automation.register_action(
"max7129digit.invert_off", DisplayInvertAction, MAX7219_OFF_ACTION_SCHEMA
"max7219digit.invert_off", DisplayInvertAction, MAX7219_OFF_ACTION_SCHEMA
)
@automation.register_action(
"max7129digit.invert_on", DisplayInvertAction, MAX7219_ON_ACTION_SCHEMA
"max7219digit.invert_on", DisplayInvertAction, MAX7219_ON_ACTION_SCHEMA
)
async def max7129digit_invert_to_code(config, action_id, template_arg, args):
async def max7219digit_invert_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_state(config[CONF_STATE]))
@@ -146,12 +146,12 @@ async def max7129digit_invert_to_code(config, action_id, template_arg, args):
@automation.register_action(
"max7129digit.turn_off", DisplayVisibilityAction, MAX7219_OFF_ACTION_SCHEMA
"max7219digit.turn_off", DisplayVisibilityAction, MAX7219_OFF_ACTION_SCHEMA
)
@automation.register_action(
"max7129digit.turn_on", DisplayVisibilityAction, MAX7219_ON_ACTION_SCHEMA
"max7219digit.turn_on", DisplayVisibilityAction, MAX7219_ON_ACTION_SCHEMA
)
async def max7129digit_visible_to_code(config, action_id, template_arg, args):
async def max7219digit_visible_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_state(config[CONF_STATE]))
@@ -159,12 +159,12 @@ async def max7129digit_visible_to_code(config, action_id, template_arg, args):
@automation.register_action(
"max7129digit.reverse_off", DisplayReverseAction, MAX7219_OFF_ACTION_SCHEMA
"max7219digit.reverse_off", DisplayReverseAction, MAX7219_OFF_ACTION_SCHEMA
)
@automation.register_action(
"max7129digit.reverse_on", DisplayReverseAction, MAX7219_ON_ACTION_SCHEMA
"max7219digit.reverse_on", DisplayReverseAction, MAX7219_ON_ACTION_SCHEMA
)
async def max7129digit_reverse_to_code(config, action_id, template_arg, args):
async def max7219digit_reverse_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
cg.add(var.set_state(config[CONF_STATE]))
@@ -183,9 +183,9 @@ MAX7219_INTENSITY_SCHEMA = cv.maybe_simple_value(
@automation.register_action(
"max7129digit.intensity", DisplayIntensityAction, MAX7219_INTENSITY_SCHEMA
"max7219digit.intensity", DisplayIntensityAction, MAX7219_INTENSITY_SCHEMA
)
async def max7129digit_intensity_to_code(config, action_id, template_arg, args):
async def max7219digit_intensity_to_code(config, action_id, template_arg, args):
var = cg.new_Pvariable(action_id, template_arg)
await cg.register_parented(var, config[CONF_ID])
template_ = await cg.templatable(config[CONF_INTENSITY], args, cg.uint8)

View File

@@ -266,6 +266,7 @@ async def to_code(config: ConfigType) -> None:
};
"""
)
zephyr_add_prj_conf("REBOOT", True)
@coroutine_with_priority(CoroPriority.DIAGNOSTICS)

View File

@@ -3,8 +3,7 @@
#ifdef USE_ZEPHYR
#include "esphome/core/hal.h"
#include <zephyr/device.h>
namespace esphome {
namespace zephyr {
namespace esphome::zephyr {
class ZephyrGPIOPin : public InternalGPIOPin {
public:
@@ -39,7 +38,6 @@ class ZephyrGPIOPin : public InternalGPIOPin {
bool value_{false};
};
} // namespace zephyr
} // namespace esphome
} // namespace esphome::zephyr
#endif // USE_ZEPHYR

View File

@@ -2,12 +2,10 @@
#ifdef USE_ZEPHYR
namespace esphome {
namespace zephyr {
namespace esphome::zephyr {
void setup_preferences();
} // namespace zephyr
} // namespace esphome
}
#endif

View File

@@ -0,0 +1,63 @@
#include "reset_reason.h"
#if defined(USE_ZEPHYR) && (defined(USE_LOGGER_EARLY_MESSAGE) || defined(USE_DEBUG))
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#include <zephyr/drivers/hwinfo.h>
namespace esphome::zephyr {
static const char *const TAG = "zephyr";
static size_t append_reset_reason(char *buf, size_t size, size_t pos, bool set, const char *reason) {
if (!set) {
return pos;
}
if (pos > 0) {
pos = buf_append_printf(buf, size, pos, ", ");
}
return buf_append_printf(buf, size, pos, "%s", reason);
}
const char *get_reset_reason(std::span<char, RESET_REASON_BUFFER_SIZE> buffer) {
char *buf = buffer.data();
const size_t size = RESET_REASON_BUFFER_SIZE;
uint32_t cause;
auto ret = hwinfo_get_reset_cause(&cause);
if (ret) {
ESP_LOGE(TAG, "Unable to get reset cause: %d", ret);
buf[0] = '\0';
return buf;
}
size_t pos = 0;
if (cause == 0) {
pos = append_reset_reason(buf, size, pos, true, "None");
} else {
pos = append_reset_reason(buf, size, pos, cause & RESET_PIN, "External pin");
pos = append_reset_reason(buf, size, pos, cause & RESET_SOFTWARE, "Software reset");
pos = append_reset_reason(buf, size, pos, cause & RESET_BROWNOUT, "Brownout (drop in voltage)");
pos = append_reset_reason(buf, size, pos, cause & RESET_POR, "Power-on reset (POR)");
pos = append_reset_reason(buf, size, pos, cause & RESET_WATCHDOG, "Watchdog timer expiration");
pos = append_reset_reason(buf, size, pos, cause & RESET_DEBUG, "Debug event");
pos = append_reset_reason(buf, size, pos, cause & RESET_SECURITY, "Security violation");
pos = append_reset_reason(buf, size, pos, cause & RESET_LOW_POWER_WAKE, "Waking up from low power mode");
pos = append_reset_reason(buf, size, pos, cause & RESET_CPU_LOCKUP, "CPU lock-up detected");
pos = append_reset_reason(buf, size, pos, cause & RESET_PARITY, "Parity error");
pos = append_reset_reason(buf, size, pos, cause & RESET_PLL, "PLL error");
pos = append_reset_reason(buf, size, pos, cause & RESET_CLOCK, "Clock error");
pos = append_reset_reason(buf, size, pos, cause & RESET_HARDWARE, "Hardware reset");
pos = append_reset_reason(buf, size, pos, cause & RESET_USER, "User reset");
pos = append_reset_reason(buf, size, pos, cause & RESET_TEMPERATURE, "Temperature reset");
}
// Ensure null termination if nothing was written
if (pos == 0) {
buf[0] = '\0';
}
return buf;
}
} // namespace esphome::zephyr
#endif

View File

@@ -0,0 +1,18 @@
#pragma once
#include "esphome/core/defines.h"
#if defined(USE_ZEPHYR) && (defined(USE_LOGGER_EARLY_MESSAGE) || defined(USE_DEBUG))
#include <cstddef>
#include <span>
namespace esphome::zephyr {
static constexpr size_t RESET_REASON_BUFFER_SIZE = 128;
const char *get_reset_reason(std::span<char, RESET_REASON_BUFFER_SIZE> buffer);
} // namespace esphome::zephyr
#endif

View File

@@ -70,6 +70,7 @@ from esphome.const import (
KEY_TARGET_FRAMEWORK,
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_NRF52,
PLATFORM_RP2040,
SCHEDULER_DONT_RUN,
TYPE_GIT,
@@ -695,6 +696,7 @@ def only_with_framework(
only_on_esp32 = only_on(PLATFORM_ESP32)
only_on_esp8266 = only_on(PLATFORM_ESP8266)
only_on_nrf52 = only_on(PLATFORM_NRF52)
only_on_rp2040 = only_on(PLATFORM_RP2040)
only_with_arduino = only_with_framework(Framework.ARDUINO)

View File

@@ -338,8 +338,10 @@
#ifdef USE_NRF52
#define USE_ESPHOME_TASK_LOG_BUFFER
#define USE_LOGGER_EARLY_MESSAGE
#define USE_LOGGER_UART_SELECTION_USB_CDC
#define USE_LOGGER_USB_CDC
#define USE_LOGGER_WAIT_FOR_CDC
#define USE_NRF52_DFU
#define USE_NRF52_REG0_VOUT 5
#define USE_NRF52_UICR_ERASE

View File

@@ -5,4 +5,6 @@ esphome:
logger:
level: DEBUG
wait_for_cdc: true
early_message: true
task_log_buffer_size: 0

View File

@@ -13,10 +13,10 @@ esphome:
on_boot:
- priority: 100
then:
- max7129digit.invert_off:
- max7129digit.invert_on:
- max7129digit.turn_on:
- max7129digit.turn_off:
- max7129digit.reverse_on:
- max7129digit.reverse_off:
- max7129digit.intensity: 10
- max7219digit.invert_off:
- max7219digit.invert_on:
- max7219digit.turn_on:
- max7219digit.turn_off:
- max7219digit.reverse_on:
- max7219digit.reverse_off:
- max7219digit.intensity: 10