ESPHome 2025.9.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
15void Logger::loop() {
16#ifdef USE_LOGGER_USB_CDC
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#endif
34 this->process_messages_();
35}
36
37void Logger::pre_setup() {
38 if (this->baud_rate_ > 0) {
39 static const struct device *uart_dev = nullptr;
40 switch (this->uart_) {
42 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart0));
43 break;
45 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(uart1));
46 break;
47#ifdef USE_LOGGER_USB_CDC
49 uart_dev = DEVICE_DT_GET_OR_NULL(DT_NODELABEL(cdc_acm_uart0));
50 if (device_is_ready(uart_dev)) {
51 usb_enable(nullptr);
52 }
53 break;
54#endif
55 }
56 if (!device_is_ready(uart_dev)) {
57 ESP_LOGE(TAG, "%s is not ready.", get_uart_selection_());
58 } else {
59 this->uart_dev_ = uart_dev;
60 }
61 }
62 global_logger = this;
63 ESP_LOGI(TAG, "Log initialized");
64}
65
66void HOT Logger::write_msg_(const char *msg) {
67#ifdef CONFIG_PRINTK
68 printk("%s\n", msg);
69#endif
70 if (nullptr == this->uart_dev_) {
71 return;
72 }
73 while (*msg) {
74 uart_poll_out(this->uart_dev_, *msg);
75 ++msg;
76 }
77 uart_poll_out(this->uart_dev_, '\n');
78}
79
80const char *const UART_SELECTIONS[] = {"UART0", "UART1", "USB_CDC"};
81
82const char *Logger::get_uart_selection_() { return UART_SELECTIONS[this->uart_]; }
83
84} // namespace esphome::logger
85
86#endif
UARTSelection uart_
Definition logger.h:267
const char * get_uart_selection_()
void loop() override
Definition logger.cpp:178
void pre_setup()
Set up this component.
void write_msg_(const char *msg)
const device * uart_dev_
Definition logger.h:239
const char *const UART_SELECTIONS[]
@ 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:283
Application App
Global storage of Application pointer - only one Application can exist.