9#include "driver/gpio.h"
10#include "soc/gpio_num.h"
12#ifdef USE_UART_WAKE_LOOP_ON_RX
22static const char *
const TAG =
"uart.idf";
25 uart_parity_t parity = UART_PARITY_DISABLE;
27 parity = UART_PARITY_EVEN;
29 parity = UART_PARITY_ODD;
32 uart_word_length_t data_bits;
35 data_bits = UART_DATA_5_BITS;
38 data_bits = UART_DATA_6_BITS;
41 data_bits = UART_DATA_7_BITS;
44 data_bits = UART_DATA_8_BITS;
47 data_bits = UART_DATA_BITS_MAX;
51 uart_config_t uart_config{};
53 uart_config.data_bits = data_bits;
54 uart_config.parity = parity;
55 uart_config.stop_bits = this->
stop_bits_ == 1 ? UART_STOP_BITS_1 : UART_STOP_BITS_2;
56 uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
57 uart_config.source_clk = UART_SCLK_DEFAULT;
58 uart_config.rx_flow_ctrl_thresh = 122;
64 static uint8_t next_uart_num = 0;
67 bool logger_uses_hardware_uart =
true;
69#ifdef USE_LOGGER_USB_CDC
72 logger_uses_hardware_uart =
false;
76#ifdef USE_LOGGER_USB_SERIAL_JTAG
79 logger_uses_hardware_uart =
false;
89 if (next_uart_num >= SOC_UART_NUM) {
90 ESP_LOGW(TAG,
"Maximum number of UART components created already");
94 this->
uart_num_ =
static_cast<uart_port_t
>(next_uart_num++);
96#if (SOC_UART_LP_NUM >= 1)
97 size_t fifo_len = ((this->
uart_num_ < SOC_UART_HP_NUM) ? SOC_UART_FIFO_LEN : SOC_LP_UART_FIFO_LEN);
99 size_t fifo_len = SOC_UART_FIFO_LEN;
102 ESP_LOGW(TAG,
"rx_buffer_size is too small, must be greater than %zu", fifo_len);
112 if (uart_is_driver_installed(this->
uart_num_)) {
113 err = uart_driver_delete(this->
uart_num_);
115 ESP_LOGW(TAG,
"uart_driver_delete failed: %s", esp_err_to_name(err));
120 err = uart_driver_install(this->
uart_num_,
129 ESP_LOGW(TAG,
"uart_driver_install failed: %s", esp_err_to_name(err));
144 setup_pin_if_needed(this->
rx_pin_);
146 setup_pin_if_needed(this->
tx_pin_);
155 invert |= UART_SIGNAL_TXD_INV;
158 invert |= UART_SIGNAL_RXD_INV;
161 invert |= UART_SIGNAL_RTS_INV;
164 err = uart_set_line_inverse(this->
uart_num_, invert);
166 ESP_LOGW(TAG,
"uart_set_line_inverse failed: %s", esp_err_to_name(err));
171 err = uart_set_pin(this->
uart_num_, tx, rx, flow_control, UART_PIN_NO_CHANGE);
173 ESP_LOGW(TAG,
"uart_set_pin failed: %s", esp_err_to_name(err));
180 ESP_LOGW(TAG,
"uart_set_rx_full_threshold failed: %s", esp_err_to_name(err));
187 ESP_LOGW(TAG,
"uart_set_rx_timeout failed: %s", esp_err_to_name(err));
195 ESP_LOGW(TAG,
"uart_set_mode failed: %s", esp_err_to_name(err));
201 err = uart_param_config(this->
uart_num_, &uart_config);
203 ESP_LOGW(TAG,
"uart_param_config failed: %s", esp_err_to_name(err));
208#ifdef USE_UART_WAKE_LOOP_ON_RX
216 ESP_LOGCONFIG(TAG,
"Reloaded UART %u", this->
uart_num_);
222 ESP_LOGCONFIG(TAG,
"UART Bus %u:", this->
uart_num_);
223 LOG_PIN(
" TX Pin: ", this->
tx_pin_);
224 LOG_PIN(
" RX Pin: ", this->
rx_pin_);
226 if (this->
rx_pin_ !=
nullptr) {
228 " RX Buffer Size: %u\n"
229 " RX Full Threshold: %u\n"
234 " Baud Rate: %" PRIu32
" baud\n"
238#ifdef USE_UART_WAKE_LOOP_ON_RX
239 "\n Wake on data RX: ENABLED"
248 esp_err_t err = uart_set_rx_full_threshold(this->
uart_num_, rx_full_threshold);
250 ESP_LOGW(TAG,
"uart_set_rx_full_threshold failed: %s", esp_err_to_name(err));
259 esp_err_t err = uart_set_rx_timeout(this->
uart_num_, rx_timeout);
261 ESP_LOGW(TAG,
"uart_set_rx_timeout failed: %s", esp_err_to_name(err));
269 int32_t write_len = uart_write_bytes(this->
uart_num_, data,
len);
270 if (write_len != (int32_t)
len) {
271 ESP_LOGW(TAG,
"uart_write_bytes failed: %d != %zu", write_len,
len);
274#ifdef USE_UART_DEBUGGER
275 for (
size_t i = 0; i <
len; i++) {
287 int len = uart_read_bytes(this->
uart_num_, data, 1, 20 / portTICK_PERIOD_MS);
299 size_t length_to_read =
len;
300 int32_t read_len = 0;
309 if (length_to_read > 0)
310 read_len = uart_read_bytes(this->
uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS);
311#ifdef USE_UART_DEBUGGER
312 for (
size_t i = 0; i <
len; i++) {
316 return read_len == (int32_t) length_to_read;
323 err = uart_get_buffered_data_len(this->
uart_num_, &available);
326 ESP_LOGW(TAG,
"uart_get_buffered_data_len failed: %s", esp_err_to_name(err));
336 ESP_LOGVV(TAG,
" Flushing");
337 uart_wait_tx_done(this->
uart_num_, portMAX_DELAY);
342#ifdef USE_UART_WAKE_LOOP_ON_RX
346 BaseType_t *task_woken) {
347 if (uart_select_notif == UART_SELECT_READ_NOTIF) {
BedjetMode mode
BedJet operating mode.
static void IRAM_ATTR wake_loop_isrsafe(int *px_higher_priority_task_woken)
Wake the main event loop from an ISR.
void mark_failed()
Mark this component as failed.
virtual uint8_t get_pin() const =0
virtual bool is_inverted() const =0
void dump_config() override
void set_rx_timeout(size_t rx_timeout) override
uart_config_t get_config_()
void check_logger_conflict() override
bool peek_byte(uint8_t *data) override
size_t available() override
void write_array(const uint8_t *data, size_t len) override
void set_rx_full_threshold(size_t rx_full_threshold) override
bool read_array(uint8_t *data, size_t len) override
static void uart_rx_isr_callback(uart_port_t uart_num, uart_select_notif_t uart_select_notif, BaseType_t *task_woken)
void load_settings() override
UARTParityOptions parity_
bool check_read_timeout_(size_t len=1)
InternalGPIOPin * tx_pin_
uint32_t get_baud_rate() const
InternalGPIOPin * flow_control_pin_
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
size_t rx_full_threshold_
InternalGPIOPin * rx_pin_
@ UART_SELECTION_USB_SERIAL_JTAG
const LogString * parity_to_str(UARTParityOptions parity)
@ UART_CONFIG_PARITY_EVEN