ESPHome 2025.10.0-dev
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
7#include <zephyr/device.h>
8#include <zephyr/drivers/uart.h>
9#include <zephyr/usb/usb_device.h>
10
11namespace esphome::logger {
12
13static const char *const TAG = "logger";
14
15#ifdef USE_LOGGER_USB_CDC
16void Logger::loop() {
17 if (this->uart_ != UART_SELECTION_USB_CDC || nullptr == this->uart_dev_) {
18 return;
19 }
20 static bool opened = false;
21 uint32_t dtr = 0;
22 uart_line_ctrl_get(this->uart_dev_, UART_LINE_CTRL_DTR, &dtr);
23
24 /* Poll if the DTR flag was set, optional */
25 if (opened == dtr) {
26 return;
27 }
28
29 if (!opened) {
31 }
32 opened = !opened;
33}
34#endif
35
36void Logger::pre_setup() {
37 if (this->baud_rate_ > 0) {
38 static const struct device *uart_dev = nullptr;
39 switch (this->uart_) {
41 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart0));
42 break;
44 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
45 break;
46#ifdef USE_LOGGER_USB_CDC
48 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
49 if (device_is_ready(uart_dev)) {
50 usb_enable(nullptr);
51 }
52 break;
53#endif
54 }
55 if (!device_is_ready(uart_dev)) {
56 ESP_LOGE(TAG, "%s is not ready.", LOG_STR_ARG(get_uart_selection_()));
57 } else {
58 this->uart_dev_ = uart_dev;
59 }
60 }
61 global_logger = this;
62 ESP_LOGI(TAG, "Log initialized");
63}
64
65void HOT Logger::write_msg_(const char *msg) {
66#ifdef CONFIG_PRINTK
67 printk("%s\n", msg);
68#endif
69 if (nullptr == this->uart_dev_) {
70 return;
71 }
72 while (*msg) {
73 uart_poll_out(this->uart_dev_, *msg);
74 ++msg;
75 }
76 uart_poll_out(this->uart_dev_, '\n');
77}
78
79const LogString *Logger::get_uart_selection_() {
80 switch (this->uart_) {
82 return LOG_STR("UART0");
84 return LOG_STR("UART1");
85#ifdef USE_LOGGER_USB_CDC
87 return LOG_STR("USB_CDC");
88#endif
89 default:
90 return LOG_STR("UNKNOWN");
91 }
92}
93
94} // namespace esphome::logger
95
96#endif
UARTSelection uart_
Definition logger.h:263
const LogString * get_uart_selection_()
void loop() override
Definition logger.cpp:177
void pre_setup()
Set up this component.
void write_msg_(const char *msg)
const device * uart_dev_
Definition logger.h:237
@ UART_SELECTION_USB_CDC
Definition logger.h:79
@ UART_SELECTION_UART0
Definition logger.h:70
@ UART_SELECTION_UART1
Definition logger.h:74
Logger * global_logger
Definition logger.cpp:288
Application App
Global storage of Application pointer - only one Application can exist.