3#include <esp8266_peri.h>
9#ifdef USE_UART_WAKE_LOOP_ON_RX
21static constexpr uint32_t SW_SERIAL_EDGE_MODE_MAX_BAUD = 19200;
25static constexpr uint32_t SW_SERIAL_TX_BIT_LOCK_MAX_BAUD = 38400;
28static constexpr uint32_t RX_TICKS_PER_SECOND = 16000000;
31static constexpr uint32_t TX_UNLOCK_MIN_RUN_CYCLES = F_CPU / 1000000 * 30;
33static constexpr uint32_t MAX_TX_LOCK_CYCLES = F_CPU / 1000000 * 200;
35bool ESP8266UartComponent::serial0_in_use =
false;
37bool ESP8266UartComponent::serial0_swapped =
false;
43 config |= UART_PARITY_NONE;
45 config |= UART_PARITY_EVEN;
47 config |= UART_PARITY_ODD;
52 config |= UART_NB_BIT_5;
55 config |= UART_NB_BIT_6;
58 config |= UART_NB_BIT_7;
61 config |= UART_NB_BIT_8;
66 config |= UART_NB_STOP_BIT_1;
68 config |= UART_NB_STOP_BIT_2;
90 setup_pin_if_needed(this->
rx_pin_);
92 setup_pin_if_needed(this->
tx_pin_);
98 SerialConfig config =
static_cast<SerialConfig
>(
get_config());
100#ifdef USE_ESP8266_UART_SERIAL
112 ESP8266UartComponent::serial0_in_use =
true;
113 }
else if (!ESP8266UartComponent::serial0_in_use && (
tx_pin_ ==
nullptr ||
tx_pin_->
get_pin() == 15) &&
125 ESP8266UartComponent::serial0_in_use =
true;
126 ESP8266UartComponent::serial0_swapped =
true;
129#ifdef USE_ESP8266_UART_SERIAL1
144 ESP_LOGCONFIG(TAG,
"Loading UART bus settings");
146 SerialConfig config =
static_cast<SerialConfig
>(
get_config());
149#ifdef USE_ESP8266_UART_SERIAL
151 if (this->
hw_serial_ == &Serial && ESP8266UartComponent::serial0_swapped)
159 ESP_LOGCONFIG(TAG,
"UART bus was reloaded.");
165 ESP_LOGCONFIG(TAG,
"UART Bus:");
166 LOG_PIN(
" TX Pin: ", this->
tx_pin_);
167 LOG_PIN(
" RX Pin: ", this->
rx_pin_);
168 if (this->
rx_pin_ !=
nullptr) {
172 " Baud Rate: %u baud\n"
178 ESP_LOGCONFIG(TAG,
" Using hardware serial interface.");
180 ESP_LOGCONFIG(TAG,
" Using software serial"
181#ifdef USE_UART_WAKE_LOOP_ON_RX
182 "\n Wake on data RX: ENABLED"
185 if (this->
rx_pin_ !=
nullptr) {
186 ESP_LOGCONFIG(TAG,
" RX decoder: %s",
187 this->
baud_rate_ <= SW_SERIAL_EDGE_MODE_MAX_BAUD ? LOG_STR_LITERAL(
"edge")
188 : LOG_STR_LITERAL(
"start bit sampler"));
190 if (this->
tx_pin_ !=
nullptr && this->
baud_rate_ <= SW_SERIAL_TX_BIT_LOCK_MAX_BAUD &&
193 " TX locks interrupts for whole bytes because a software UART receives above %u baud; Wi-Fi "
194 "watchdog resets are possible under load",
195 (
unsigned) SW_SERIAL_EDGE_MODE_MAX_BAUD);
208 ESP_LOGW(TAG,
" You're using the same serial port for logging and the UART component. Please "
209 "disable logging over the serial port by setting logger->baud_rate to 0.");
218 for (
size_t i = 0; i <
len; i++)
221#ifdef USE_UART_DEBUGGER
222 for (
size_t i = 0; i <
len; i++) {
243 for (
size_t i = 0; i <
len; i++)
246#ifdef USE_UART_DEBUGGER
247 for (
size_t i = 0; i <
len; i++) {
267 ESP_LOGVV(TAG,
" Flushing");
277 size_t rx_buffer_size) {
285 if (rx_pin ==
nullptr)
291 const bool edge_mode = baud_rate <= SW_SERIAL_EDGE_MODE_MAX_BAUD;
293 this->
lock_frame_ = baud_rate > SW_SERIAL_TX_BIT_LOCK_MAX_BAUD;
295 if (tx_pin !=
nullptr) {
302 if (rx_pin !=
nullptr) {
310 buffer =
new uint8_t[rx_buffer_size];
313 stop_bits,
buffer, rx_buffer_size);
326#if F_CPU != 160000000L
328 return bit_time << (CPU2X & 1);
336 uint32_t wait = bit_time + bit_time / 3 - 500;
341 rec |= arg->
read_bit_(&wait, start, bit_time) << i;
349 arg->
wait_(&wait, start, bit_time);
351 arg->
wait_(&wait, start, bit_time);
356#ifdef USE_UART_WAKE_LOOP_ON_RX
366 [[maybe_unused]]
const bool wake = arg->
rx_.
on_edge(now, level);
367#ifdef USE_UART_WAKE_LOOP_ON_RX
374#ifdef USE_UART_WAKE_LOOP_ON_RX
386 ESP_LOGE(TAG,
"UART doesn't have TX pins set!");
389 bool parity_bit =
false;
390 bool need_parity_bit =
true;
396 need_parity_bit =
false;
404 if (need_parity_bit) {
405 parity_bit ^= __builtin_parity(frame);
406 frame |=
uint32_t(parity_bit) << num_bits++;
414 std::optional<InterruptLock> frame_lock;
416 frame_lock.emplace();
428 if ((unlock_at >>
pos) & 1)
429 frame_lock.emplace();
431 this->
write_bit_(
true, &wait, start, bit_time);
436 this->
wait_(&wait, start, bit_time);
444 this->
wait_(wait, start, bit_time);
453 this->
wait_(wait, start, bit_time);
void digital_write(bool value)
Helper class to disable interrupts.
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
void detach_interrupt() const
ISRInternalGPIOPin to_isr() const
Stream * get_hw_serial() const
UARTSelection get_uart() const
Get the UART used by the logger.
void rx_finalize_pending_()
UARTParityOptions parity_
void write_bit_(bool bit, uint32_t *wait, const uint32_t &start, uint32_t bit_time)
SoftwareSerialRxDecoder rx_
static void gpio_intr_edge(ESP8266SoftwareSerial *arg)
void ESPHOME_ALWAYS_INLINE rx_sync_()
bool read_bit_(uint32_t *wait, const uint32_t &start, uint32_t bit_time)
ISRInternalGPIOPin rx_pin_
void wait_(uint32_t *wait, const uint32_t &start, uint32_t bit_time)
void setup(InternalGPIOPin *tx_pin, InternalGPIOPin *rx_pin, uint32_t baud_rate, uint8_t stop_bits, uint32_t data_bits, UARTParityOptions parity, size_t rx_buffer_size)
InternalGPIOPin * gpio_tx_pin_
InternalGPIOPin * gpio_rx_pin_
bool locks_whole_frame() const
TX holds interrupts off for each byte up to the edge into its trailing run of 1 bits,...
static uint8_t blocking_rx_count
static void gpio_intr(ESP8266SoftwareSerial *arg)
Above SW_SERIAL_EDGE_MODE_MAX_BAUD; reads the whole byte in the ISR.
void write_byte(uint8_t data)
ISRInternalGPIOPin tx_pin_
size_t available() override
size_t available_for_write() override
void load_settings()
Load the UART settings.
void check_logger_conflict() override
bool peek_byte(uint8_t *data) override
bool read_array(uint8_t *data, size_t len) override
void dump_config() override
HardwareSerial * hw_serial_
UARTFlushResult flush() override
void write_array(const uint8_t *data, size_t len) override
ESP8266SoftwareSerial * sw_serial_
bool finalize_due(uint32_t now) const
void finalize(uint32_t now)
Call with the ISR masked.
size_t buffer_size() const
void setup(uint32_t bit_ticks, uint8_t data_bits, bool parity, uint8_t stop_bits, uint8_t *buffer, size_t buffer_size)
Configure framing and buffer; drops all state. Follow with reset().
bool ESPHOME_ALWAYS_INLINE on_edge(uint32_t now, bool level)
ISR.
uint8_t peek_byte() const
void reset(uint32_t now, bool level)
Forget any partial frame; level is the current line level.
bool ESPHOME_ALWAYS_INLINE push_byte(uint8_t data)
Drops the byte when full; holds buffer_size - 1 bytes.
UARTParityOptions parity_
bool check_read_timeout_(size_t len=1)
InternalGPIOPin * tx_pin_
uint32_t get_baud_rate() const
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
InternalGPIOPin * rx_pin_
struct @66::@67 __attribute__
Wake the main loop task from an ISR. ISR-safe.
@ UART_SELECTION_UART0_SWAP
ESPHOME_ALWAYS_INLINE SoftwareSerialTxFrame software_serial_tx_frame(uint32_t frame, uint32_t num_bits, uint32_t bit_time, uint32_t min_run_cycles, bool find_unlocks)
frame holds num_bits data and parity bits, LSB first. unlock_at stays 0 unless find_unlocks.
ESPHOME_LOG_TAG(TAG, "uart.binary_sensor")
const LogString * parity_to_str(UARTParityOptions parity)
@ UART_CONFIG_PARITY_EVEN
@ UART_CONFIG_PARITY_NONE
UARTFlushResult
Result of a flush() call.
@ UART_FLUSH_RESULT_ASSUMED_SUCCESS
Platform cannot report result; success is assumed.
uint32_t arch_get_cpu_cycle_count()
void wake_loop_threadsafe()
Non-ISR: always inline.
uint32_t IRAM_ATTR HOT micros()
void ESPHOME_ALWAYS_INLINE wake_loop_isrsafe()
ISR-safe: no task_woken arg because ESP8266 has no FreeRTOS. Caller must be IRAM_ATTR.
One software serial TX byte as line levels, one bit per slot.
uint32_t unlock_at
Slots right after an edge into a run of equal bits lasting at least min_run_cycles,...
uint32_t line
The start bit at slot 0, then the frame, then a stop bit that ends the last run.
uint32_t high_from
Slot of the last 0; from the edge after it the line stays high through the stop bits.
Platform-specific main loop wake primitives.