8#include <zephyr/drivers/hwinfo.h>
9#include <zephyr/kernel.h>
10#include <hal/nrf_power.h>
12#include <zephyr/storage/flash_map.h>
14#define BOOTLOADER_VERSION_REGISTER NRF_TIMER2->CC[0]
22static inline uint32_t read_mem_u32(uintptr_t addr) {
24 return *
reinterpret_cast<volatile uint32_t *
>(addr);
27static inline uint8_t read_mem_u8(uintptr_t addr) {
29 return *
reinterpret_cast<volatile uint8_t *
>(addr);
39static inline bool is_sd_present() {
48static inline uint32_t sd_version_get() {
57 ESP_LOGD(TAG,
"Reset Reason: %s", buf);
68static void fa_cb(
const struct flash_area *fa,
void *user_data) {
69#if CONFIG_FLASH_MAP_LABELS
70 const char *fa_label = flash_area_label(fa);
72 if (fa_label ==
nullptr) {
75 ESP_LOGCONFIG(TAG,
"%2d 0x%0*" PRIxPTR
" %-26s %-24.24s 0x%-10x 0x%-12x", (
int) fa->fa_id,
76 sizeof(uintptr_t) * 2, (uintptr_t) fa->fa_dev, fa->fa_dev->name, fa_label, (
uint32_t) fa->fa_off,
79 ESP_LOGCONFIG(TAG,
"%2d 0x%0*" PRIxPTR
" %-26s 0x%-10x 0x%-12x", (
int) fa->fa_id,
sizeof(uintptr_t) * 2,
80 (uintptr_t) fa->fa_dev, fa->fa_dev->name, (
uint32_t) fa->fa_off, fa->fa_size);
85#if CONFIG_FLASH_MAP_LABELS
86 ESP_LOGCONFIG(TAG,
"ID | Device | Device Name "
87 "| Label | Offset | Size");
88 ESP_LOGCONFIG(TAG,
"--------------------------------------------"
89 "-----------------------------------------------");
91 ESP_LOGCONFIG(TAG,
"ID | Device | Device Name "
93 ESP_LOGCONFIG(TAG,
"-----------------------------------------"
94 "------------------------------");
96 flash_area_foreach(fa_cb,
nullptr);
99#ifdef ESPHOME_LOG_HAS_VERBOSE
103#define NRF_PERIPH_ENABLED(periph, reg) \
104 YESNO(((reg)->ENABLE & periph##_ENABLE_ENABLE_Msk) == (periph##_ENABLE_ENABLE_Enabled << periph##_ENABLE_ENABLE_Pos))
107static void log_peripherals_info() {
109 ESP_LOGV(TAG,
"Peripherals status:");
110 ESP_LOGV(TAG,
" USBD: %-3s| UARTE0: %-3s| UARTE1: %-3s| UART0: %-3s",
111 NRF_PERIPH_ENABLED(USBD, NRF_USBD), NRF_PERIPH_ENABLED(UARTE, NRF_UARTE0),
112 NRF_PERIPH_ENABLED(UARTE, NRF_UARTE1), NRF_PERIPH_ENABLED(UART, NRF_UART0));
113 ESP_LOGV(TAG,
" TWIS0: %-3s| TWIS1: %-3s| TWIM0: %-3s| TWIM1: %-3s",
114 NRF_PERIPH_ENABLED(TWIS, NRF_TWIS0), NRF_PERIPH_ENABLED(TWIS, NRF_TWIS1),
115 NRF_PERIPH_ENABLED(TWIM, NRF_TWIM0), NRF_PERIPH_ENABLED(TWIM, NRF_TWIM1));
116 ESP_LOGV(TAG,
" TWI0: %-3s| TWI1: %-3s| COMP: %-3s| CCM: %-3s",
117 NRF_PERIPH_ENABLED(TWI, NRF_TWI0), NRF_PERIPH_ENABLED(TWI, NRF_TWI1), NRF_PERIPH_ENABLED(COMP, NRF_COMP),
118 NRF_PERIPH_ENABLED(CCM, NRF_CCM));
119 ESP_LOGV(TAG,
" PDM: %-3s| SPIS0: %-3s| SPIS1: %-3s| SPIS2: %-3s",
120 NRF_PERIPH_ENABLED(PDM, NRF_PDM), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS0), NRF_PERIPH_ENABLED(SPIS, NRF_SPIS1),
121 NRF_PERIPH_ENABLED(SPIS, NRF_SPIS2));
122 ESP_LOGV(TAG,
" SPIM0: %-3s| SPIM1: %-3s| SPIM2: %-3s| SPIM3: %-3s",
123 NRF_PERIPH_ENABLED(SPIM, NRF_SPIM0), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM1),
124 NRF_PERIPH_ENABLED(SPIM, NRF_SPIM2), NRF_PERIPH_ENABLED(SPIM, NRF_SPIM3));
125 ESP_LOGV(TAG,
" SPI0: %-3s| SPI1: %-3s| SPI2: %-3s| SAADC: %-3s",
126 NRF_PERIPH_ENABLED(SPI, NRF_SPI0), NRF_PERIPH_ENABLED(SPI, NRF_SPI1), NRF_PERIPH_ENABLED(SPI, NRF_SPI2),
127 NRF_PERIPH_ENABLED(SAADC, NRF_SAADC));
128 ESP_LOGV(TAG,
" QSPI: %-3s| QDEC: %-3s| LPCOMP: %-3s| I2S: %-3s",
129 NRF_PERIPH_ENABLED(QSPI, NRF_QSPI), NRF_PERIPH_ENABLED(QDEC, NRF_QDEC),
130 NRF_PERIPH_ENABLED(LPCOMP, NRF_LPCOMP), NRF_PERIPH_ENABLED(I2S, NRF_I2S));
131 ESP_LOGV(TAG,
" PWM0: %-3s| PWM1: %-3s| PWM2: %-3s| PWM3: %-3s",
132 NRF_PERIPH_ENABLED(PWM, NRF_PWM0), NRF_PERIPH_ENABLED(PWM, NRF_PWM1), NRF_PERIPH_ENABLED(PWM, NRF_PWM2),
133 NRF_PERIPH_ENABLED(PWM, NRF_PWM3));
134 ESP_LOGV(TAG,
" AAR: %-3s| QSPI deep power-down:%-3s| CRYPTOCELL: %-3s", NRF_PERIPH_ENABLED(AAR, NRF_AAR),
135 YESNO((NRF_QSPI->IFCONFIG0 & QSPI_IFCONFIG0_DPMENABLE_Msk) ==
136 (QSPI_IFCONFIG0_DPMENABLE_Enable << QSPI_IFCONFIG0_DPMENABLE_Pos)),
137 YESNO((NRF_CRYPTOCELL->ENABLE & CRYPTOCELL_ENABLE_ENABLE_Msk) ==
138 (CRYPTOCELL_ENABLE_ENABLE_Enabled << CRYPTOCELL_ENABLE_ENABLE_Pos)));
141#undef NRF_PERIPH_ENABLED
146 case (UICR_REGOUT0_VOUT_DEFAULT):
147 return "1.8V (default)";
148 case (UICR_REGOUT0_VOUT_1V8):
150 case (UICR_REGOUT0_VOUT_2V1):
152 case (UICR_REGOUT0_VOUT_2V4):
154 case (UICR_REGOUT0_VOUT_2V7):
156 case (UICR_REGOUT0_VOUT_3V0):
158 case (UICR_REGOUT0_VOUT_3V3):
167 constexpr size_t size = DEVICE_INFO_BUFFER_SIZE;
168 char *buf =
buffer.data();
172 auto regstatus = nrf_power_mainregstatus_get(NRF_POWER);
173 const char *supply_status = (regstatus == NRF_POWER_MAINREGSTATUS_NORMAL) ?
"Normal voltage." :
"High voltage.";
174 ESP_LOGD(TAG,
"Main supply status: %s", supply_status);
179 if (nrf_power_mainregstatus_get(NRF_POWER) == NRF_POWER_MAINREGSTATUS_HIGH) {
180 const char *reg0_type = nrf_power_dcdcen_vddh_get(NRF_POWER) ?
"DC/DC" :
"LDO";
181 const char *reg0_voltage = regout0_to_str((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) >> UICR_REGOUT0_VOUT_Pos);
182 ESP_LOGD(TAG,
"Regulator stage 0: %s, %s", reg0_type, reg0_voltage);
187#ifdef USE_NRF52_REG0_VOUT
188 if ((NRF_UICR->REGOUT0 & UICR_REGOUT0_VOUT_Msk) >> UICR_REGOUT0_VOUT_Pos != USE_NRF52_REG0_VOUT) {
189 ESP_LOGE(TAG,
"Regulator stage 0: expected %s", regout0_to_str(USE_NRF52_REG0_VOUT));
193 ESP_LOGD(TAG,
"Regulator stage 0: disabled");
198 const char *reg1_type = nrf_power_dcdcen_get(NRF_POWER) ?
"DC/DC" :
"LDO";
199 ESP_LOGD(TAG,
"Regulator stage 1: %s", reg1_type);
204 const char *usb_state;
205 if (nrf_power_usbregstatus_vbusdet_get(NRF_POWER)) {
206 if (nrf_power_usbregstatus_outrdy_get(NRF_POWER)) {
209 usb_state =
"connected (regulator is not ready)";
212 usb_state =
"disconnected";
214 ESP_LOGD(TAG,
"USB power state: %s", usb_state);
220 nrf_power_pof_thr_t pof_thr = nrf_power_pofcon_get(NRF_POWER, &enabled);
222 const char *pof_voltage;
224 case POWER_POFCON_THRESHOLD_V17:
225 pof_voltage =
"1.7V";
227 case POWER_POFCON_THRESHOLD_V18:
228 pof_voltage =
"1.8V";
230 case POWER_POFCON_THRESHOLD_V19:
231 pof_voltage =
"1.9V";
233 case POWER_POFCON_THRESHOLD_V20:
234 pof_voltage =
"2.0V";
236 case POWER_POFCON_THRESHOLD_V21:
237 pof_voltage =
"2.1V";
239 case POWER_POFCON_THRESHOLD_V22:
240 pof_voltage =
"2.2V";
242 case POWER_POFCON_THRESHOLD_V23:
243 pof_voltage =
"2.3V";
245 case POWER_POFCON_THRESHOLD_V24:
246 pof_voltage =
"2.4V";
248 case POWER_POFCON_THRESHOLD_V25:
249 pof_voltage =
"2.5V";
251 case POWER_POFCON_THRESHOLD_V26:
252 pof_voltage =
"2.6V";
254 case POWER_POFCON_THRESHOLD_V27:
255 pof_voltage =
"2.7V";
257 case POWER_POFCON_THRESHOLD_V28:
258 pof_voltage =
"2.8V";
261 pof_voltage =
"???V";
265 if (nrf_power_mainregstatus_get(NRF_POWER) == NRF_POWER_MAINREGSTATUS_HIGH) {
266 const char *vddh_voltage;
267 switch (nrf_power_pofcon_vddh_get(NRF_POWER)) {
268 case NRF_POWER_POFTHRVDDH_V27:
269 vddh_voltage =
"2.7V";
271 case NRF_POWER_POFTHRVDDH_V28:
272 vddh_voltage =
"2.8V";
274 case NRF_POWER_POFTHRVDDH_V29:
275 vddh_voltage =
"2.9V";
277 case NRF_POWER_POFTHRVDDH_V30:
278 vddh_voltage =
"3.0V";
280 case NRF_POWER_POFTHRVDDH_V31:
281 vddh_voltage =
"3.1V";
283 case NRF_POWER_POFTHRVDDH_V32:
284 vddh_voltage =
"3.2V";
286 case NRF_POWER_POFTHRVDDH_V33:
287 vddh_voltage =
"3.3V";
289 case NRF_POWER_POFTHRVDDH_V34:
290 vddh_voltage =
"3.4V";
292 case NRF_POWER_POFTHRVDDH_V35:
293 vddh_voltage =
"3.5V";
295 case NRF_POWER_POFTHRVDDH_V36:
296 vddh_voltage =
"3.6V";
298 case NRF_POWER_POFTHRVDDH_V37:
299 vddh_voltage =
"3.7V";
301 case NRF_POWER_POFTHRVDDH_V38:
302 vddh_voltage =
"3.8V";
304 case NRF_POWER_POFTHRVDDH_V39:
305 vddh_voltage =
"3.9V";
307 case NRF_POWER_POFTHRVDDH_V40:
308 vddh_voltage =
"4.0V";
310 case NRF_POWER_POFTHRVDDH_V41:
311 vddh_voltage =
"4.1V";
313 case NRF_POWER_POFTHRVDDH_V42:
314 vddh_voltage =
"4.2V";
317 vddh_voltage =
"???V";
320 ESP_LOGD(TAG,
"Power-fail comparator: %s, VDDH: %s", pof_voltage, vddh_voltage);
326 ESP_LOGD(TAG,
"Power-fail comparator: %s", pof_voltage);
331 ESP_LOGD(TAG,
"Power-fail comparator: disabled");
335 auto package = [](uint32_t value) {
338 return "QIxx - 7x7 73-pin aQFN";
340 return "QFxx - 6x6 48-pin QFN";
342 return "CKxx - 3.544 x 3.607 WLCSP";
346 return "Unspecified";
349 char mac_pretty[MAC_ADDRESS_PRETTY_BUFFER_SIZE];
353 " Code page size: %u, code size: %u, device id: 0x%08x%08x\n"
354 " Encryption root: 0x%08x%08x%08x%08x, Identity Root: 0x%08x%08x%08x%08x\n"
355 " Device address type: %s, address: %s\n"
356 " Part code: nRF%x, version: %c%c%c%c, package: %s\n"
357 " RAM: %ukB, Flash: %ukB, production test: %sdone",
358 NRF_FICR->CODEPAGESIZE, NRF_FICR->CODESIZE, NRF_FICR->DEVICEID[1], NRF_FICR->DEVICEID[0], NRF_FICR->ER[0],
359 NRF_FICR->ER[1], NRF_FICR->ER[2], NRF_FICR->ER[3], NRF_FICR->IR[0], NRF_FICR->IR[1], NRF_FICR->IR[2],
360 NRF_FICR->IR[3], (NRF_FICR->DEVICEADDRTYPE & 0x1 ?
"Random" :
"Public"), mac_pretty, NRF_FICR->INFO.PART,
361 NRF_FICR->INFO.VARIANT >> 24 & 0xFF, NRF_FICR->INFO.VARIANT >> 16 & 0xFF, NRF_FICR->INFO.VARIANT >> 8 & 0xFF,
362 NRF_FICR->INFO.VARIANT & 0xFF, package(NRF_FICR->INFO.PACKAGE), NRF_FICR->INFO.RAM, NRF_FICR->INFO.FLASH,
363 (NRF_FICR->PRODTEST[0] == 0xBB42319F ?
"" :
"not "));
364 bool n_reset_enabled = NRF_UICR->PSELRESET[0] == NRF_UICR->PSELRESET[1] &&
365 (NRF_UICR->PSELRESET[0] & UICR_PSELRESET_CONNECT_Msk) == UICR_PSELRESET_CONNECT_Connected
366 << UICR_PSELRESET_CONNECT_Pos;
368 TAG,
" GPIO as NFC pins: %s, GPIO as nRESET pin: %s",
369 YESNO((NRF_UICR->NFCPINS & UICR_NFCPINS_PROTECT_Msk) == (UICR_NFCPINS_PROTECT_NFC << UICR_NFCPINS_PROTECT_Pos)),
370 YESNO(n_reset_enabled));
371 if (n_reset_enabled) {
372 uint8_t port = (NRF_UICR->PSELRESET[0] & UICR_PSELRESET_PORT_Msk) >> UICR_PSELRESET_PORT_Pos;
373 uint8_t pin = (NRF_UICR->PSELRESET[0] & UICR_PSELRESET_PIN_Msk) >> UICR_PSELRESET_PIN_Pos;
374 ESP_LOGD(TAG,
" nRESET port P%u.%02u", port, pin);
376#ifdef USE_BOOTLOADER_MCUBOOT
377 ESP_LOGD(TAG,
" Bootloader: mcuboot");
379 ESP_LOGD(TAG,
" Bootloader: Adafruit, version %u.%u.%u", (BOOTLOADER_VERSION_REGISTER >> 16) & 0xFF,
380 (BOOTLOADER_VERSION_REGISTER >> 8) & 0xFF, BOOTLOADER_VERSION_REGISTER & 0xFF);
381 ESP_LOGD(TAG,
" MBR bootloader addr 0x%08x, UICR bootloader addr 0x%08x", read_mem_u32(
MBR_BOOTLOADER_ADDR),
383 ESP_LOGD(TAG,
" MBR param page addr 0x%08x, UICR param page addr 0x%08x", read_mem_u32(
MBR_PARAM_PAGE_ADDR),
385 if (is_sd_present()) {
387 uint32_t const sd_version = sd_version_get();
390 ver[0] = sd_version / 1000000;
391 ver[1] = (sd_version - ver[0] * 1000000) / 1000;
392 ver[2] = (sd_version - ver[0] * 1000000 - ver[1] * 1000);
394 ESP_LOGD(TAG,
" SoftDevice: S%u %u.%u.%u", sd_id, ver[0], ver[1], ver[2]);
395#ifdef USE_SOFTDEVICE_ID
396#ifdef USE_SOFTDEVICE_VERSION
397 if (USE_SOFTDEVICE_ID != sd_id || USE_SOFTDEVICE_VERSION != ver[0]) {
398 ESP_LOGE(TAG,
"Built for SoftDevice S%u %u.x.y. It may crash due to mismatch of bootloader version.",
399 USE_SOFTDEVICE_ID, USE_SOFTDEVICE_VERSION);
402 if (USE_SOFTDEVICE_ID != sd_id) {
403 ESP_LOGE(TAG,
"Built for SoftDevice S%u. It may crash due to mismatch of bootloader version.", USE_SOFTDEVICE_ID);
410 char uicr_buf[13 * 9];
413 for (
size_t i = 0; i <
size; i++) {
414 len = buf_append_printf(uicr_buf,
sizeof(uicr_buf),
len, i > 0 ?
" %08" PRIX32 :
"%08" PRIX32, data[i]);
418 ESP_LOGD(TAG,
" NRFFW %s", uicr(NRF_UICR->NRFFW, 13));
419 ESP_LOGD(TAG,
" NRFHW %s", uicr(NRF_UICR->NRFHW, 12));
420#ifdef ESPHOME_LOG_HAS_VERBOSE
421 log_peripherals_info();
427#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
428static void log_thread_usage(
const struct k_thread *thread,
void *user_data) {
429 const auto cpu_cycles = *
static_cast<const uint64_t *
>(user_data);
430 auto *tid =
const_cast<k_tid_t
>(thread);
431 k_thread_runtime_stats_t stats;
432 if (k_thread_runtime_stats_get(tid, &stats) != 0)
435 const char *name = k_thread_name_get(tid);
436 ESP_LOGV(TAG,
" %-16s prio %3d %-10s %3u%% peak %" PRIu32
" avg %" PRIu32, name !=
nullptr ? name :
"?",
437 thread->base.prio, k_thread_state_str(tid,
state,
sizeof(
state)),
438 static_cast<unsigned>(stats.execution_cycles * 100 / cpu_cycles),
439 static_cast<uint32_t>(k_cyc_to_us_floor64(stats.peak_cycles)),
440 static_cast<uint32_t>(k_cyc_to_us_floor64(stats.average_cycles)));
444void DebugComponent::update_platform_() {
445#ifdef CONFIG_SCHED_THREAD_USAGE_ANALYSIS
446 k_thread_runtime_stats_t cpu;
447 if (k_thread_runtime_stats_all_get(&cpu) != 0 || cpu.execution_cycles == 0)
449 ESP_LOGV(TAG,
"Thread CPU usage since boot (peak and average us per time slice):");
451 k_thread_foreach_unlocked(log_thread_usage, &cpu.execution_cycles);
Heap-allocating helper functions.
const char * get_wakeup_cause_(std::span< char, WAKEUP_CAUSE_BUFFER_SIZE > buffer)
void log_partition_info_()
Logs information about the device's partition table.
size_t get_device_info_(std::span< char, DEVICE_INFO_BUFFER_SIZE > buffer, size_t pos)
uint32_t get_free_heap_()
const char * get_reset_reason_(std::span< char, RESET_REASON_BUFFER_SIZE > buffer)
constexpr std::uintptr_t MBR_PARAM_PAGE_ADDR
constexpr uintptr_t SD_ID_OFFSET
ESPHOME_LOG_TAG(TAG, "debug")
constexpr std::uintptr_t MBR_BOOTLOADER_ADDR
constexpr uintptr_t MBR_SIZE
constexpr uintptr_t SOFTDEVICE_INFO_STRUCT_OFFSET
constexpr uintptr_t SD_VERSION_OFFSET
constexpr uint32_t SD_MAGIC_NUMBER
std::span< const uint8_t > data
const char * get_reset_reason(std::span< char, RESET_REASON_BUFFER_SIZE > buffer)
size_t buf_append_str(char *buf, size_t size, size_t pos, const char *str)
Safely append a string to buffer, returning new position (capped at size).
const char * get_mac_address_pretty_into_buffer(std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buf)
Get the device MAC address into the given buffer, in colon-separated uppercase hex notation.