ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
uart_component_zephyr.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
3
4#include <zephyr/drivers/uart.h>
5
7#include "esphome/core/hal.h"
8#include "esphome/core/log.h"
9#include "esphome/core/wake.h"
10
11namespace esphome::uart {
12
13ESPHOME_LOG_TAG(TAG, "uart.zephyr");
14
16 if (this->device_ == nullptr || !device_is_ready(this->device_)) {
17 this->mark_failed(LOG_STR("UART device not ready"));
18 return;
19 }
20 if (!this->configure_()) {
21 this->mark_failed(LOG_STR("UART configuration not supported"));
22 return;
23 }
24 if (this->rx_pin_ != nullptr) {
25 this->rx_storage_ = new uint8_t[this->rx_buffer_size_]; // NOLINT(cppcoreguidelines-owning-memory)
26 ring_buf_init(&this->rx_ring_, this->rx_buffer_size_, this->rx_storage_);
27 uart_irq_callback_user_data_set(this->device_, ZephyrUARTComponent::rx_isr, this);
28 uart_irq_rx_enable(this->device_);
29 }
30}
31
33 struct uart_config config{};
34 config.baudrate = this->baud_rate_;
35 switch (this->parity_) {
37 config.parity = UART_CFG_PARITY_NONE;
38 break;
40 config.parity = UART_CFG_PARITY_EVEN;
41 break;
42 default:
43 ESP_LOGE(TAG, "Parity %s is not supported", LOG_STR_ARG(parity_to_str(this->parity_)));
44 return false;
45 }
46 config.stop_bits = this->stop_bits_ == 2 ? UART_CFG_STOP_BITS_2 : UART_CFG_STOP_BITS_1;
47 config.data_bits = static_cast<uint8_t>(UART_CFG_DATA_BITS_5 + (this->data_bits_ - 5));
48 config.flow_ctrl = UART_CFG_FLOW_CTRL_NONE;
49 int err = uart_configure(this->device_, &config);
50 if (err != 0) {
51 ESP_LOGE(TAG, "uart_configure() failed for %" PRIu32 " baud, %u data bits, parity %s, %u stop bits: %d",
52 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_, err);
53 return false;
54 }
55 return true;
56}
57
58void ZephyrUARTComponent::load_settings(bool dump_config) {
59 if (this->is_failed() || this->device_ == nullptr || !device_is_ready(this->device_) || !this->configure_())
60 return;
61 if (dump_config) {
62 ESP_LOGCONFIG(TAG, "UART bus was reloaded.");
63 this->dump_config();
64 }
65}
66
67void ZephyrUARTComponent::rx_isr(const struct device *device, void *user_data) {
68 auto *self = static_cast<ZephyrUARTComponent *>(user_data);
69 bool received = false;
70 while (uart_irq_update(device) > 0 && uart_irq_rx_ready(device) > 0) {
71 uint8_t *space;
72 uint32_t claimed = ring_buf_put_claim(&self->rx_ring_, &space, self->rx_buffer_size_);
73 if (claimed == 0) {
74 // The buffer is full: drain the FIFO so the interrupt clears, and count what is lost
75 uint8_t discard[16];
76 int dropped = uart_fifo_read(device, discard, sizeof(discard));
77 if (dropped <= 0)
78 break;
79 self->rx_overflow_ = self->rx_overflow_ + dropped;
80 continue;
81 }
82 int got = uart_fifo_read(device, space, claimed);
83 ring_buf_put_finish(&self->rx_ring_, got > 0 ? got : 0);
84 if (got <= 0)
85 break;
86 received = true;
87 }
88 if (received)
90}
91
93 ESP_LOGCONFIG(TAG, "UART Bus:");
94 ESP_LOGCONFIG(TAG, " Device: %s", this->device_ != nullptr ? this->device_->name : "none");
95 LOG_PIN(" TX Pin: ", this->tx_pin_);
96 LOG_PIN(" RX Pin: ", this->rx_pin_);
97 if (this->rx_pin_ != nullptr) {
98 ESP_LOGCONFIG(TAG, " RX Buffer Size: %u", (unsigned) this->rx_buffer_size_);
99 }
100 ESP_LOGCONFIG(TAG,
101 " Baud Rate: %" PRIu32 " baud\n"
102 " Data Bits: %u\n"
103 " Parity: %s\n"
104 " Stop bits: %u",
105 this->baud_rate_, this->data_bits_, LOG_STR_ARG(parity_to_str(this->parity_)), this->stop_bits_);
106 this->check_logger_conflict();
107}
108
109void ZephyrUARTComponent::write_array(const uint8_t *data, size_t len) {
110 if (this->is_failed() || this->device_ == nullptr || this->tx_pin_ == nullptr || len == 0)
111 return;
112 // uart_poll_out() waits for the previous byte, so a long write at a low baud rate outlasts the watchdog
113 for (size_t i = 0; i < len; i++) {
114 uart_poll_out(this->device_, data[i]);
115 App.feed_wdt();
116 }
117 this->last_tx_start_us_ = micros();
118 this->tx_pending_ = true;
119#ifdef USE_UART_DEBUGGER
120 // After the whole array, so that a slow callback does not split it
121 for (size_t i = 0; i < len; i++) {
122 this->debug_callback_.call(UART_DIRECTION_TX, data[i]);
123 }
124#endif
125}
126
128 if (!this->has_peek_) {
129 if (!this->check_read_timeout_())
130 return false;
131 if (ring_buf_get(&this->rx_ring_, &this->peek_buffer_, 1) != 1)
132 return false;
133 this->has_peek_ = true;
134 }
135 *data = this->peek_buffer_;
136 return true;
137}
138
139bool ZephyrUARTComponent::read_array(uint8_t *data, size_t len) {
140 if (len == 0)
141 return true;
142 if (!this->check_read_timeout_(len))
143 return false;
144 size_t offset = 0;
145 if (this->has_peek_) {
146 data[0] = this->peek_buffer_;
147 this->has_peek_ = false;
148 offset = 1;
149 }
150 if (offset < len)
151 ring_buf_get(&this->rx_ring_, data + offset, len - offset);
152#ifdef USE_UART_DEBUGGER
153 for (size_t i = 0; i < len; i++) {
154 this->debug_callback_.call(UART_DIRECTION_RX, data[i]);
155 }
156#endif
157 uint32_t overflow = this->rx_overflow_;
158 if (overflow != this->rx_overflow_reported_) {
159 ESP_LOGW(TAG, "RX buffer full: %" PRIu32 " bytes lost", overflow - this->rx_overflow_reported_);
160 this->rx_overflow_reported_ = overflow;
161 }
162 return true;
163}
164
166 if (this->rx_storage_ == nullptr)
167 return 0;
168 return ring_buf_size_get(&this->rx_ring_) + (this->has_peek_ ? 1 : 0);
169}
170
172 if (!this->tx_pending_)
174 this->tx_pending_ = false;
175 // uart_poll_out() returns once the last byte has started: wait for its start, data, parity and stop bits,
176 // plus one bit
177 const uint32_t bits =
178 1 + this->data_bits_ + (this->parity_ == UART_CONFIG_PARITY_NONE ? 0 : 1) + this->stop_bits_ + 1;
179 const uint32_t frame_us = (bits * 1000000 + this->baud_rate_ - 1) / this->baud_rate_;
180 const uint32_t elapsed = micros() - this->last_tx_start_us_;
181 if (elapsed < frame_us)
182 delayMicroseconds(frame_us - elapsed);
184}
185
186} // namespace esphome::uart
187#endif // USE_ZEPHYR
void feed_wdt()
Feed the task watchdog.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:288
bool check_read_timeout_(size_t len=1)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
void write_array(const uint8_t *data, size_t len) override
void load_settings()
Load the UART settings.
static void rx_isr(const struct device *device, void *user_data)
bool read_array(uint8_t *data, size_t len) override
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
const LogString * parity_to_str(UARTParityOptions parity)
Definition uart.cpp:43
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
@ UART_FLUSH_RESULT_SUCCESS
Confirmed: all bytes left the TX FIFO.
const void size_t len
Definition hal.h:68
void IRAM_ATTR HOT delayMicroseconds(uint32_t us)
Definition hal.cpp:48
uint32_t IRAM_ATTR HOT micros()
Definition hal.cpp:43
Application App
Global storage of Application pointer - only one Application can exist.
void IRAM_ATTR wake_loop_any_context()
IRAM_ATTR entry point for ISR callers — defined in wake_esp8266.cpp.
static void uint32_t
Platform-specific main loop wake primitives.