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;
67 if (next_uart_num >= UART_NUM_MAX) {
68 ESP_LOGW(TAG,
"Maximum number of UART components created already.");
73 ESP_LOGCONFIG(TAG,
"Setting up UART %u...", this->
uart_num_);
75 this->
lock_ = xSemaphoreCreateMutex();
77 xSemaphoreTake(this->
lock_, portMAX_DELAY);
80 esp_err_t err = uart_param_config(this->
uart_num_, &uart_config);
82 ESP_LOGW(TAG,
"uart_param_config failed: %s", esp_err_to_name(err));
92 invert |= UART_SIGNAL_TXD_INV;
94 invert |= UART_SIGNAL_RXD_INV;
96 err = uart_set_line_inverse(this->
uart_num_, invert);
98 ESP_LOGW(TAG,
"uart_set_line_inverse failed: %s", esp_err_to_name(err));
103 err = uart_set_pin(this->
uart_num_, tx, rx, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
105 ESP_LOGW(TAG,
"uart_set_pin failed: %s", esp_err_to_name(err));
117 ESP_LOGW(TAG,
"uart_driver_install failed: %s", esp_err_to_name(err));
122 xSemaphoreGive(this->
lock_);
127 esp_err_t err = uart_param_config(this->
uart_num_, &uart_config);
129 ESP_LOGW(TAG,
"uart_param_config failed: %s", esp_err_to_name(err));
132 }
else if (dump_config) {
133 ESP_LOGCONFIG(TAG,
"UART %u was reloaded.", this->
uart_num_);
139 ESP_LOGCONFIG(TAG,
"UART Bus %u:", this->
uart_num_);
142 if (this->
rx_pin_ !=
nullptr) {
145 ESP_LOGCONFIG(TAG,
" Baud Rate: %" PRIu32
" baud", this->
baud_rate_);
146 ESP_LOGCONFIG(TAG,
" Data Bits: %u", this->
data_bits_);
148 ESP_LOGCONFIG(TAG,
" Stop bits: %u", this->
stop_bits_);
153 xSemaphoreTake(this->
lock_, portMAX_DELAY);
154 uart_write_bytes(this->
uart_num_, data, len);
155 xSemaphoreGive(this->
lock_);
156 #ifdef USE_UART_DEBUGGER 157 for (
size_t i = 0; i <
len; i++) {
166 xSemaphoreTake(this->
lock_, portMAX_DELAY);
170 int len = uart_read_bytes(this->
uart_num_, data, 1, 20 / portTICK_PERIOD_MS);
178 xSemaphoreGive(this->
lock_);
183 size_t length_to_read =
len;
186 xSemaphoreTake(this->
lock_, portMAX_DELAY);
193 if (length_to_read > 0)
194 uart_read_bytes(this->
uart_num_, data, length_to_read, 20 / portTICK_PERIOD_MS);
195 xSemaphoreGive(this->
lock_);
196 #ifdef USE_UART_DEBUGGER 197 for (
size_t i = 0; i <
len; i++) {
207 xSemaphoreTake(this->
lock_, portMAX_DELAY);
208 uart_get_buffered_data_len(this->
uart_num_, &available);
211 xSemaphoreGive(this->
lock_);
217 ESP_LOGVV(TAG,
" Flushing...");
218 xSemaphoreTake(this->
lock_, portMAX_DELAY);
219 uart_wait_tx_done(this->
uart_num_, portMAX_DELAY);
220 xSemaphoreGive(this->
lock_);
void dump_config() override
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.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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