16 static 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 #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0) 52 uart_config.source_clk = UART_SCLK_DEFAULT;
54 uart_config.source_clk = UART_SCLK_APB;
56 uart_config.rx_flow_ctrl_thresh = 122;
62 static uint8_t next_uart_num = 0;
65 bool logger_uses_hardware_uart =
true;
67 #ifdef USE_LOGGER_USB_CDC 70 logger_uses_hardware_uart =
false;
72 #endif // USE_LOGGER_USB_CDC 74 #ifdef USE_LOGGER_USB_SERIAL_JTAG 77 logger_uses_hardware_uart =
false;
79 #endif // USE_LOGGER_USB_SERIAL_JTAG 87 if (next_uart_num >= UART_NUM_MAX) {
88 ESP_LOGW(TAG,
"Maximum number of UART components created already.");
92 this->
uart_num_ =
static_cast<uart_port_t
>(next_uart_num++);
93 ESP_LOGCONFIG(TAG,
"Setting up UART %u...", this->
uart_num_);
95 this->
lock_ = xSemaphoreCreateMutex();
97 xSemaphoreTake(this->
lock_, portMAX_DELAY);
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));
112 invert |= UART_SIGNAL_TXD_INV;
114 invert |= UART_SIGNAL_RXD_INV;
116 err = uart_set_line_inverse(this->
uart_num_, invert);
118 ESP_LOGW(TAG,
"uart_set_line_inverse failed: %s", esp_err_to_name(err));
123 err = uart_set_pin(this->
uart_num_, tx, rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
125 ESP_LOGW(TAG,
"uart_set_pin failed: %s", esp_err_to_name(err));
137 ESP_LOGW(TAG,
"uart_driver_install failed: %s", esp_err_to_name(err));
142 xSemaphoreGive(this->
lock_);
147 esp_err_t err = uart_param_config(this->
uart_num_, &uart_config);
149 ESP_LOGW(TAG,
"uart_param_config failed: %s", esp_err_to_name(err));
152 }
else if (dump_config) {
153 ESP_LOGCONFIG(TAG,
"UART %u was reloaded.", this->
uart_num_);
159 ESP_LOGCONFIG(TAG,
"UART Bus %u:", this->
uart_num_);
162 if (this->
rx_pin_ !=
nullptr) {
165 ESP_LOGCONFIG(TAG,
" Baud Rate: %" PRIu32
" baud", this->
baud_rate_);
166 ESP_LOGCONFIG(TAG,
" Data Bits: %u", this->
data_bits_);
168 ESP_LOGCONFIG(TAG,
" Stop bits: %u", this->
stop_bits_);
173 xSemaphoreTake(this->
lock_, portMAX_DELAY);
174 uart_write_bytes(this->
uart_num_, data, len);
175 xSemaphoreGive(this->
lock_);
176 #ifdef USE_UART_DEBUGGER 177 for (
size_t i = 0; i <
len; i++) {
186 xSemaphoreTake(this->
lock_, portMAX_DELAY);
190 int len = uart_read_bytes(this->
uart_num_, data, 1, 20 / portTICK_PERIOD_MS);
198 xSemaphoreGive(this->
lock_);
203 size_t length_to_read =
len;
206 xSemaphoreTake(this->
lock_, portMAX_DELAY);
213 if (length_to_read > 0)
214 uart_read_bytes(this->
uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS);
215 xSemaphoreGive(this->
lock_);
216 #ifdef USE_UART_DEBUGGER 217 for (
size_t i = 0; i <
len; i++) {
227 xSemaphoreTake(this->
lock_, portMAX_DELAY);
228 uart_get_buffered_data_len(this->
uart_num_, &available);
231 xSemaphoreGive(this->
lock_);
237 ESP_LOGVV(TAG,
" Flushing...");
238 xSemaphoreTake(this->
lock_, portMAX_DELAY);
239 uart_wait_tx_done(this->
uart_num_, portMAX_DELAY);
240 xSemaphoreGive(this->
lock_);
void dump_config() override
uint32_t get_baud_rate() const
InternalGPIOPin * tx_pin_
UARTParityOptions parity_
QueueHandle_t uart_event_queue_
bool read_array(uint8_t *data, size_t len) override
virtual uint8_t get_pin() const =0
InternalGPIOPin * rx_pin_
uart_config_t get_config_()
bool peek_byte(uint8_t *data) override
void write_array(const uint8_t *data, size_t len) override
void check_logger_conflict() override
virtual void mark_failed()
Mark this component as failed.
Implementation of SPI Controller mode.
bool check_read_timeout_(size_t len=1)
CallbackManager< void(UARTDirection, uint8_t)> debug_callback_
const LogString * parity_to_str(UARTParityOptions parity)
virtual bool is_inverted() const =0
void load_settings() override