ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
uart_component.cpp
Go to the documentation of this file.
1#include "uart_component.h"
3
4#include <algorithm>
5
6namespace esphome::uart {
7
8ESPHOME_LOG_TAG(TAG, "uart");
9
10// Keeps the pacing product in 32 bits up to about 10 Mbaud.
11static constexpr uint32_t MAX_PACE_SPAN_MS = 4000;
12
14 if (this->available() >= len)
15 return true;
16
17 uint32_t start_time = millis();
18 while (this->available() < len) {
19 if (millis() - start_time > 100) {
20 ESP_LOGE(TAG, "Reading from UART timed out at byte %zu!", this->available());
21 return false;
22 }
23 yield();
24 }
25 return true;
26}
27
29 size_t room = this->available_for_write();
30 if (room != SIZE_MAX) {
31 return room;
32 }
33 // A pass woken early writes little.
34 uint32_t span =
35 std::min({App.get_loop_component_start_time() - last_write_ms, App.get_loop_interval(), MAX_PACE_SPAN_MS});
36 // 10 bits per byte on the line.
37 uint32_t paced = this->baud_rate_ / 10 * span / 1000;
38 return std::max<size_t>(1, paced);
39}
40
42 uint8_t bytelength = this->data_bits_ + this->stop_bits_ + 1;
44 bytelength += 1;
45 int32_t val = clamp<int32_t>((this->baud_rate_ / (bytelength * 1000 / time)) - 1, 1, 120);
46 this->set_rx_full_threshold(val);
47}
48
49#ifdef USE_ESP8266
50void UARTComponent::write_array_progmem(const uint8_t *data, size_t len) {
51 uint8_t buf[32];
52 while (len > 0) {
53 const size_t chunk = std::min(len, sizeof(buf));
54 progmem_memcpy(buf, data, chunk);
55 this->write_array(buf, chunk);
56 data += chunk;
57 len -= chunk;
58 }
59}
60#endif
61
62} // namespace esphome::uart
uint32_t get_loop_interval() const
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
size_t paced_write_room(uint32_t last_write_ms)
void write_array_progmem(const uint8_t *data, size_t len)
bool check_read_timeout_(size_t len=1)
void write_array(const std::vector< uint8_t > &data)
void set_rx_full_threshold_ms(uint8_t time)
virtual void set_rx_full_threshold(size_t rx_full_threshold)
virtual size_t available()=0
virtual size_t available_for_write()
void yield(void)
mopeka_std_values val[3]
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
void progmem_memcpy(void *dst, const void *src, size_t len)
Definition hal.h:56
const void size_t len
Definition hal.h:68
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t