16static const char *
const TAG =
"uart.idf";
19 uart_parity_t parity = UART_PARITY_DISABLE;
21 parity = UART_PARITY_EVEN;
23 parity = UART_PARITY_ODD;
26 uart_word_length_t data_bits;
29 data_bits = UART_DATA_5_BITS;
32 data_bits = UART_DATA_6_BITS;
35 data_bits = UART_DATA_7_BITS;
38 data_bits = UART_DATA_8_BITS;
41 data_bits = UART_DATA_BITS_MAX;
45 uart_config_t uart_config{};
47 uart_config.data_bits = data_bits;
48 uart_config.parity = parity;
49 uart_config.stop_bits = this->
stop_bits_ == 1 ? UART_STOP_BITS_1 : UART_STOP_BITS_2;
50 uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE;
51 uart_config.source_clk = UART_SCLK_DEFAULT;
52 uart_config.rx_flow_ctrl_thresh = 122;
58 static uint8_t next_uart_num = 0;
61 bool logger_uses_hardware_uart =
true;
63#ifdef USE_LOGGER_USB_CDC
66 logger_uses_hardware_uart =
false;
70#ifdef USE_LOGGER_USB_SERIAL_JTAG
73 logger_uses_hardware_uart =
false;
83 if (next_uart_num >= SOC_UART_NUM) {
84 ESP_LOGW(TAG,
"Maximum number of UART components created already");
88 this->
uart_num_ =
static_cast<uart_port_t
>(next_uart_num++);
89 this->
lock_ = xSemaphoreCreateMutex();
91 xSemaphoreTake(this->
lock_, portMAX_DELAY);
95 xSemaphoreGive(this->
lock_);
100 esp_err_t err = uart_param_config(this->
uart_num_, &uart_config);
102 ESP_LOGW(TAG,
"uart_param_config failed: %s", esp_err_to_name(err));
113 invert |= UART_SIGNAL_TXD_INV;
115 invert |= UART_SIGNAL_RXD_INV;
117 err = uart_set_line_inverse(this->
uart_num_, invert);
119 ESP_LOGW(TAG,
"uart_set_line_inverse failed: %s", esp_err_to_name(err));
124 err = uart_set_pin(this->
uart_num_, tx, rx, flow_control, UART_PIN_NO_CHANGE);
126 ESP_LOGW(TAG,
"uart_set_pin failed: %s", esp_err_to_name(err));
131 if (uart_is_driver_installed(this->
uart_num_)) {
134 ESP_LOGW(TAG,
"uart_driver_delete failed: %s", esp_err_to_name(err));
146 ESP_LOGW(TAG,
"uart_driver_install failed: %s", esp_err_to_name(err));
153 ESP_LOGW(TAG,
"uart_set_rx_full_threshold failed: %s", esp_err_to_name(err));
160 ESP_LOGW(TAG,
"uart_set_rx_timeout failed: %s", esp_err_to_name(err));
168 ESP_LOGW(TAG,
"uart_set_mode failed: %s", esp_err_to_name(err));
174 ESP_LOGCONFIG(TAG,
"UART %u was reloaded.", this->
uart_num_);
180 ESP_LOGCONFIG(TAG,
"UART Bus %u:", this->
uart_num_);
184 if (this->
rx_pin_ !=
nullptr) {
186 " RX Buffer Size: %u\n"
187 " RX Full Threshold: %u\n"
192 " Baud Rate: %" PRIu32
" baud\n"
202 esp_err_t err = uart_set_rx_full_threshold(this->
uart_num_, rx_full_threshold);
204 ESP_LOGW(TAG,
"uart_set_rx_full_threshold failed: %s", esp_err_to_name(err));
213 esp_err_t err = uart_set_rx_timeout(this->
uart_num_, rx_timeout);
215 ESP_LOGW(TAG,
"uart_set_rx_timeout failed: %s", esp_err_to_name(err));
223 xSemaphoreTake(this->
lock_, portMAX_DELAY);
225 xSemaphoreGive(this->
lock_);
226#ifdef USE_UART_DEBUGGER
227 for (
size_t i = 0; i <
len; i++) {
236 xSemaphoreTake(this->
lock_, portMAX_DELAY);
240 int len = uart_read_bytes(this->
uart_num_, data, 1, 20 / portTICK_PERIOD_MS);
248 xSemaphoreGive(this->
lock_);
253 size_t length_to_read =
len;
256 xSemaphoreTake(this->
lock_, portMAX_DELAY);
263 if (length_to_read > 0)
264 uart_read_bytes(this->
uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS);
265 xSemaphoreGive(this->
lock_);
266#ifdef USE_UART_DEBUGGER
267 for (
size_t i = 0; i <
len; i++) {
277 xSemaphoreTake(this->
lock_, portMAX_DELAY);
278 uart_get_buffered_data_len(this->
uart_num_, &available);
281 xSemaphoreGive(this->
lock_);
287 ESP_LOGVV(TAG,
" Flushing");
288 xSemaphoreTake(this->
lock_, portMAX_DELAY);
289 uart_wait_tx_done(this->
uart_num_, portMAX_DELAY);
290 xSemaphoreGive(this->
lock_);
BedjetMode mode
BedJet operating mode.
virtual 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
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
QueueHandle_t uart_event_queue_
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
Providing packet encoding functions for exchanging data with a remote host.