From 3ec23f1b837959548da8e7ddec4dfd7db7929a6c Mon Sep 17 00:00:00 2001 From: Wangjialin Date: Fri, 4 Nov 2016 12:52:34 +0800 Subject: [PATCH] Modify as Angus's suggestion: 1. Set XXX_TAG static, remove extern XXX_TAG in uart.h/ledc.h/gpio.h 2. I removed uart_set/get_print_port() functions, these functions are not well tested, I removed them for now. 3. Modify some function names for uart_read/write_bytes 4. Modify uart_write_bytes and uart_write_bytes_with_break. --- components/driver/gpio.c | 2 +- components/driver/include/driver/gpio.h | 2 +- components/driver/include/driver/ledc.h | 1 - components/driver/include/driver/uart.h | 98 ++++------ components/driver/ledc.c | 2 +- components/driver/uart.c | 236 ++++++------------------ 6 files changed, 95 insertions(+), 246 deletions(-) diff --git a/components/driver/gpio.c b/components/driver/gpio.c index da0fedb895..62a0e7faa7 100644 --- a/components/driver/gpio.c +++ b/components/driver/gpio.c @@ -20,7 +20,7 @@ #include "soc/soc.h" #include "esp_log.h" -const char* GPIO_TAG = "GPIO"; +static const char* GPIO_TAG = "GPIO"; #define GPIO_CHECK(a, str, ret_val) if (!(a)) { \ ESP_LOGE(GPIO_TAG,"%s:%d (%s):%s\n", __FILE__, __LINE__, __FUNCTION__, str); \ return (ret_val); \ diff --git a/components/driver/include/driver/gpio.h b/components/driver/include/driver/gpio.h index a31c2f64b7..903621f619 100644 --- a/components/driver/include/driver/gpio.h +++ b/components/driver/include/driver/gpio.h @@ -27,7 +27,7 @@ #ifdef __cplusplus extern "C" { #endif -extern const char* GPIO_TAG; + #define GPIO_SEL_0 (BIT(0)) /*!< Pin 0 selected */ #define GPIO_SEL_1 (BIT(1)) /*!< Pin 1 selected */ #define GPIO_SEL_2 (BIT(2)) /*!< Pin 2 selected */ diff --git a/components/driver/include/driver/ledc.h b/components/driver/include/driver/ledc.h index ac29eaf56a..3ab0ebff1e 100644 --- a/components/driver/include/driver/ledc.h +++ b/components/driver/include/driver/ledc.h @@ -26,7 +26,6 @@ extern "C" { #endif -extern const char* LEDC_TAG; #define LEDC_APB_CLK_HZ (APB_CLK_FREQ) #define LEDC_REF_CLK_HZ (1*1000000) diff --git a/components/driver/include/driver/uart.h b/components/driver/include/driver/uart.h index 200d1148cb..ba487d57d5 100644 --- a/components/driver/include/driver/uart.h +++ b/components/driver/include/driver/uart.h @@ -32,11 +32,11 @@ extern "C" { #include "freertos/ringbuf.h" #include -extern const char* UART_TAG; -#define UART_FIFO_LEN (128) //Do not change this, this value describes the length of the gardware FIFO in the ESP32 +#define UART_FIFO_LEN (128) /*< Length of the hardware FIFO buffers */ #define UART_INTR_MASK 0x1ff #define UART_LINE_INV_MASK (0x3f << 19) #define UART_BITRATE_MAX 5000000 +#define UART_PIN_NO_CHANGE (-1) typedef enum { UART_DATA_5_BITS = 0x0, /*!< word length: 5bits*/ @@ -243,6 +243,8 @@ esp_err_t uart_set_line_inverse(uart_port_t uart_no, uint32_t inverse_mask) ; * * @param rx_thresh Threshold of Hardware RX flow control(0 ~ UART_FIFO_LEN) * + * Only when UART_HW_FLOWCTRL_RTS is set , will the rx_thresh value be set. + * * @return * - ESP_OK Success * - ESP_FAIL Parameter error @@ -380,15 +382,19 @@ esp_err_t uart_isr_register(uart_port_t uart_num, uint8_t uart_intr_num, void (* /** * @brief Set UART pin number * + * @note + * Internal signal can be output to multiple GPIO pads + * Only one GPIO pad can connect with input signal + * * @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2 * - * @param tx_io_num UART TX pin GPIO number + * @param tx_io_num UART TX pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin. * - * @param rx_io_num UART RX pin GPIO number + * @param rx_io_num UART RX pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin. * - * @param rts_io_num UART RTS pin GPIO number + * @param rts_io_num UART RTS pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin. * - * @param cts_io_num UART CTS pin GPIO number + * @param cts_io_num UART CTS pin GPIO number, if set to UART_PIN_NO_CHANGE, use the current pin. * * @return * - ESP_OK Success @@ -434,20 +440,20 @@ esp_err_t uart_set_dtr(uart_port_t uart_num, int level); * - ESP_OK Success * - ESP_FAIL Parameter error */ -esp_err_t uart_param_config(uart_port_t uart_num, uart_config_t *uart_config); +esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config); /** * @brief UART interrupt configure * * @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2 * - * @param p_intr_conf UART interrupt settings + * @param intr_conf UART interrupt settings * * @return * - ESP_OK Success * - ESP_FAIL Parameter error */ -esp_err_t uart_intr_config(uart_port_t uart_num, uart_intr_config_t *p_intr_conf); +esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf); /** * @brief Install UART driver. @@ -504,6 +510,9 @@ esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait); /** * @brief Send data to the UART port from a given buffer and length, * This function will not wait for the space in TX FIFO, just fill the TX FIFO and return when the FIFO is full. + * @note + * This function should only be used when UART TX buffer is not enabled. + * * * @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2 * @@ -515,7 +524,7 @@ esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait); * - (-1) Parameter error * - OTHERS(>=0) The number of data that pushed to the TX FIFO */ -int uart_tx_chars(uart_port_t uart_no, char* buffer, uint32_t len); +int uart_tx_chars(uart_port_t uart_no, const char* buffer, uint32_t len); /** * @brief Send data to the UART port from a given buffer and length, @@ -536,7 +545,7 @@ int uart_tx_chars(uart_port_t uart_no, char* buffer, uint32_t len); * - (-1) Parameter error * - OTHERS(>=0) The number of data that pushed to the TX FIFO */ -int uart_tx_all_chars(uart_port_t uart_num, const char* src, size_t size); +int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size); /** * @brief Send data to the UART port from a given buffer and length, @@ -564,20 +573,7 @@ int uart_tx_all_chars(uart_port_t uart_num, const char* src, size_t size); * - OTHERS(>=0) The number of data that pushed to the TX FIFO */ -int uart_tx_all_chars_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len); - -/** -* @brief UART read one char - * - * @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2 - * - * @param ticks_to_wait Timeout, count in RTOS ticks - * - * @return - * - (-1) Error - * - Others return a char data from UART. - */ -int uart_read_char(uart_port_t uart_num, TickType_t ticks_to_wait); +int uart_write_bytes_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len); /** * @brief UART read bytes from UART buffer @@ -608,25 +604,6 @@ int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickTyp */ esp_err_t uart_flush(uart_port_t uart_num); -/** - * @brief Set the serial output port for ets_printf function, not effective for ESP_LOGX macro. - * - * @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2 - * - * @return - * - ESP_OK Success - * - ESP_FAIL Parameter error, or UART driver not installed. - */ -esp_err_t uart_set_print_port(uart_port_t uart_no); - -/** - * @brief Get the current serial port for ets_printf function - * - * - * @return current print port(0: UART0; 1: UART1; 2: UART2) - */ -int uart_get_print_port(void); - /***************************EXAMPLE********************************** * * @@ -658,7 +635,7 @@ int uart_get_print_port(void); * @code{c} * //2. Set UART pin * //set UART pin, not needed if use default pins. - * uart_set_pin(uart_num, -1, -1, 15, 13); + * uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, 15, 13); * @endcode *-----------------------------------------------------------------------------* * @code{c} @@ -671,12 +648,12 @@ int uart_get_print_port(void); * @code{c} * //4. Write data to UART. * char* test_str = "This is a test string.\n" - * uart_tx_all_chars(uart_num, (const char*)test_str, strlen(test_str)); + * uart_write_bytes(uart_num, (const char*)test_str, strlen(test_str)); * @endcode *-----------------------------------------------------------------------------* * @code{c} * //5. Write data to UART, end with a break signal. - * uart_tx_all_chars_with_break(0, "test break\n",strlen("test break\n"), 100); + * uart_write_bytes_with_break(0, "test break\n",strlen("test break\n"), 100); * @endcode *-----------------------------------------------------------------------------* * @code{c} @@ -696,8 +673,6 @@ int uart_get_print_port(void); * uart_param_config(uart_num, &uart_config); * //Set UART1 pins(TX: IO16, RX: IO17, RTS: IO18, CTS: IO19) * uart_set_pin(uart_num, 16, 17, 18, 19); - * //Set UART log level - * esp_log_level_set(UART_TAG, ESP_LOG_ERROR); * //Install UART driver( We don't need an event queue here) * uart_driver_install(uart_num, 1024 * 2, 1024*4, 10, 17, NULL, RINGBUF_TYPE_BYTEBUF); * uint8_t data[1000]; @@ -705,7 +680,7 @@ int uart_get_print_port(void); * //Read data from UART * int len = uart_read_bytes(uart_num, data, sizeof(data), 10); * //Write data back to UART - * uart_tx_all_chars(uart_num, (const char*)data, len); + * uart_write_bytes(uart_num, (const char*)data, len); * } * } * @endcode @@ -715,6 +690,7 @@ int uart_get_print_port(void); * #include "freertos/queue.h" * //A queue to handle UART event. * QueueHandle_t uart0_queue; + * static const char *TAG = "uart_example"; * void uart_task(void *pvParameters) * { * int uart_num = (int)pvParameters; @@ -723,37 +699,37 @@ int uart_get_print_port(void); * for(;;) { * //Waiting for UART event. * if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) { - * ESP_LOGI(UART_TAG, "uart[%d] event:", uart_num); + * ESP_LOGI(TAG, "uart[%d] event:", uart_num); * switch(event.type) { * //Event of UART receving data * case UART_DATA: - * ESP_LOGI(UART_TAG,"data, len: %d\n", event.data.size); + * ESP_LOGI(TAG,"data, len: %d\n", event.data.size); * int len = uart_read_bytes(uart_num, dtmp, event.data.size, 10); - * ESP_LOGI(UART_TAG, "uart read: %d\n", len); + * ESP_LOGI(TAG, "uart read: %d\n", len); * break; * //Event of HW FIFO overflow detected * case UART_FIFO_OVF: - * ESP_LOGI(UART_TAG, "hw fifo overflow\n"); + * ESP_LOGI(TAG, "hw fifo overflow\n"); * break; * //Event of UART ring buffer full * case UART_BUFFER_FULL: - * ESP_LOGI(UART_TAG, "ring buffer full\n"); + * ESP_LOGI(TAG, "ring buffer full\n"); * break; * //Event of UART RX break detected * case UART_BREAK: - * ESP_LOGI(UART_TAG, "uart rx break\n"); + * ESP_LOGI(TAG, "uart rx break\n"); * break; * //Event of UART parity check error * case UART_PARITY_ERR: - * ESP_LOGI(UART_TAG, "uart parity error\n"); + * ESP_LOGI(TAG, "uart parity error\n"); * break; * //Event of UART frame error * case UART_FRAME_ERR: - * ESP_LOGI(UART_TAG, "uart frame error\n"); + * ESP_LOGI(TAG, "uart frame error\n"); * break; * //Others * default: - * ESP_LOGI(UART_TAG, "uart event type: %d\n", event.type); + * ESP_LOGI(TAG, "uart event type: %d\n", event.type); * break; * } * } @@ -775,9 +751,9 @@ int uart_get_print_port(void); * //Set UART parameters * uart_param_config(uart_num, &uart_config); * //Set UART pins,(-1: default pin, no change.) - * uart_set_pin(uart_num, -1, -1, 15, 13); + * uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, 15, 13); * //Set UART log level - * esp_log_level_set(UART_TAG, ESP_LOG_INFO); + * esp_log_level_set(TAG, ESP_LOG_INFO); * //Install UART driver, and get the queue. * uart_driver_install(uart_num, 1024 * 2, 1024*4, 10, 17, &uart0_queue, RINGBUF_TYPE_BYTEBUF); * //Create a task to handler UART event from ISR diff --git a/components/driver/ledc.c b/components/driver/ledc.c index 771c4a17df..b9039cf626 100644 --- a/components/driver/ledc.c +++ b/components/driver/ledc.c @@ -20,7 +20,7 @@ #include "driver/ledc.h" #include "esp_log.h" -const char* LEDC_TAG = "LEDC"; +static const char* LEDC_TAG = "LEDC"; static portMUX_TYPE ledc_spinlock = portMUX_INITIALIZER_UNLOCKED; #define LEDC_CHECK(a, str, ret_val) if (!(a)) { \ ESP_LOGE(LEDC_TAG,"%s:%d (%s):%s\n", __FILE__, __LINE__, __FUNCTION__, str); \ diff --git a/components/driver/uart.c b/components/driver/uart.c index cab05dd0be..a3e0b92b2b 100644 --- a/components/driver/uart.c +++ b/components/driver/uart.c @@ -29,7 +29,7 @@ #include "driver/gpio.h" #include "soc/uart_struct.h" -const char* UART_TAG = "UART"; +static const char* UART_TAG = "UART"; #define UART_CHECK(a, str, ret) if (!(a)) { \ ESP_LOGE(UART_TAG,"%s:%d (%s):%s\n", __FILE__, __LINE__, __FUNCTION__, str); \ return (ret); \ @@ -249,28 +249,19 @@ esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask) esp_err_t uart_enable_rx_intr(uart_port_t uart_num) { - UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); - UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); - SET_PERI_REG_MASK(UART_INT_ENA_REG(uart_num), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA); - UART_EXIT_CRITICAL(&uart_spinlock[uart_num]); + uart_enable_intr_mask(uart_num, UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA); return ESP_OK; } esp_err_t uart_disable_rx_intr(uart_port_t uart_num) { - UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); - UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); - CLEAR_PERI_REG_MASK(UART_INT_ENA_REG(uart_num), UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA); - UART_EXIT_CRITICAL(&uart_spinlock[uart_num]); + uart_disable_intr_mask(uart_num, UART_RXFIFO_FULL_INT_ENA|UART_RXFIFO_TOUT_INT_ENA); return ESP_OK; } esp_err_t uart_disable_tx_intr(uart_port_t uart_num) { - UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); - UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); - UART[uart_num]->int_ena.txfifo_empty = 0; - UART_EXIT_CRITICAL(&uart_spinlock[uart_num]); + uart_disable_intr_mask(uart_num, UART_TXFIFO_EMPTY_INT_ENA); return ESP_OK; } @@ -391,7 +382,7 @@ esp_err_t uart_set_dtr(uart_port_t uart_num, int level) return ESP_OK; } -esp_err_t uart_param_config(uart_port_t uart_num, uart_config_t *uart_config) +esp_err_t uart_param_config(uart_port_t uart_num, const uart_config_t *uart_config) { UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); UART_CHECK((uart_config), "param null\n", ESP_FAIL); @@ -413,25 +404,25 @@ esp_err_t uart_param_config(uart_port_t uart_num, uart_config_t *uart_config) return ESP_OK; } -esp_err_t uart_intr_config(uart_port_t uart_num, uart_intr_config_t *p_intr_conf) +esp_err_t uart_intr_config(uart_port_t uart_num, const uart_intr_config_t *intr_conf) { UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); - UART_CHECK((p_intr_conf), "param null\n", ESP_FAIL); + UART_CHECK((intr_conf), "param null\n", ESP_FAIL); UART_ENTER_CRITICAL(&uart_spinlock[uart_num]); UART[uart_num]->int_clr.val = UART_INTR_MASK; - if(p_intr_conf->intr_enable_mask & UART_RXFIFO_TOUT_INT_ENA_M) { - UART[uart_num]->conf1.rx_tout_thrhd = ((p_intr_conf->rx_timeout_thresh) & UART_RX_TOUT_THRHD_V); + if(intr_conf->intr_enable_mask & UART_RXFIFO_TOUT_INT_ENA_M) { + UART[uart_num]->conf1.rx_tout_thrhd = ((intr_conf->rx_timeout_thresh) & UART_RX_TOUT_THRHD_V); UART[uart_num]->conf1.rx_tout_en = 1; } else { UART[uart_num]->conf1.rx_tout_en = 0; } - if(p_intr_conf->intr_enable_mask & UART_RXFIFO_FULL_INT_ENA_M) { - UART[uart_num]->conf1.rxfifo_full_thrhd = p_intr_conf->rxfifo_full_thresh; + if(intr_conf->intr_enable_mask & UART_RXFIFO_FULL_INT_ENA_M) { + UART[uart_num]->conf1.rxfifo_full_thrhd = intr_conf->rxfifo_full_thresh; } - if(p_intr_conf->intr_enable_mask & UART_TXFIFO_EMPTY_INT_ENA_M) { - UART[uart_num]->conf1.txfifo_empty_thrhd = p_intr_conf->txfifo_empty_intr_thresh; + if(intr_conf->intr_enable_mask & UART_TXFIFO_EMPTY_INT_ENA_M) { + UART[uart_num]->conf1.txfifo_empty_thrhd = intr_conf->txfifo_empty_intr_thresh; } - UART[uart_num]->int_ena.val = p_intr_conf->intr_enable_mask; + UART[uart_num]->int_ena.val = intr_conf->intr_enable_mask; UART_EXIT_CRITICAL(&uart_spinlock[uart_num]); return ESP_FAIL; } @@ -459,8 +450,8 @@ static void IRAM_ATTR uart_rx_intr_handler_default(void *param) if(p_uart->tx_waiting_brk) { return; } - //TX semaphore used in none tx ringbuffer mode. - if(p_uart->tx_waiting_fifo == true && p_uart->tx_buf_size > 0) { + //TX semaphore will only be used when tx_buf_size is zero. + if(p_uart->tx_waiting_fifo == true && p_uart->tx_buf_size == 0) { p_uart->tx_waiting_fifo = false; xSemaphoreGiveFromISR(p_uart->tx_fifo_sem, NULL); } @@ -682,7 +673,7 @@ static esp_err_t uart_set_break(uart_port_t uart_num, int break_num) //Fill UART tx_fifo and return a number, //This function by itself is not thread-safe, always call from within a muxed section. -static int uart_fill_fifo(uart_port_t uart_num, char* buffer, uint32_t len) +static int uart_fill_fifo(uart_port_t uart_num, const char* buffer, uint32_t len) { uint8_t i = 0; uint8_t tx_fifo_cnt = UART[uart_num]->status.txfifo_cnt; @@ -694,7 +685,7 @@ static int uart_fill_fifo(uart_port_t uart_num, char* buffer, uint32_t len) return copy_cnt; } -int uart_tx_chars(uart_port_t uart_num, char* buffer, uint32_t len) +int uart_tx_chars(uart_port_t uart_num, const char* buffer, uint32_t len) { UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", (-1)); UART_CHECK((p_uart_obj[uart_num]), "uart driver error", (-1)); @@ -703,7 +694,7 @@ int uart_tx_chars(uart_port_t uart_num, char* buffer, uint32_t len) return 0; } xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)portMAX_DELAY); - int tx_len = uart_fill_fifo(uart_num, buffer, len); + int tx_len = uart_fill_fifo(uart_num, (const char*) buffer, len); xSemaphoreGive(p_uart_obj[uart_num]->tx_mux); return tx_len; } @@ -713,44 +704,21 @@ static int uart_tx_all(uart_port_t uart_num, const char* src, size_t size, bool if(size == 0) { return 0; } - //lock for uart_tx - xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)portMAX_DELAY); size_t original_size = size; - while(size) { - //semaphore for tx_fifo available - if(pdTRUE == xSemaphoreTake(p_uart_obj[uart_num]->tx_fifo_sem, (portTickType)portMAX_DELAY)) { - size_t sent = uart_fill_fifo(uart_num, (char*) src, size); - if(sent < size) { - p_uart_obj[uart_num]->tx_waiting_fifo = true; - uart_enable_tx_intr(uart_num, 1, UART_EMPTY_THRESH_DEFAULT); - } - size -= sent; - src += sent; - } - } - if(brk_en) { - uart_set_break(uart_num, brk_len); - xSemaphoreTake(p_uart_obj[uart_num]->tx_brk_sem, (portTickType)portMAX_DELAY); - } - xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem); - xSemaphoreGive(p_uart_obj[uart_num]->tx_mux); - return original_size; -} -int uart_tx_all_chars(uart_port_t uart_num, const char* src, size_t size) -{ - UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", (-1)); - UART_CHECK((p_uart_obj[uart_num] != NULL), "uart driver error", (-1)); - UART_CHECK(src, "buffer null", (-1)); - //Push data to TX ring buffer and return, ISR will send the data. + //lock for uart_tx + xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)portMAX_DELAY); if(p_uart_obj[uart_num]->tx_buf_size > 0) { - xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)portMAX_DELAY); int max_size = xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf); - int ori_size = size; int offset = 0; uart_event_t evt; - evt.type = UART_DATA; evt.data.size = size; + evt.data.brk_len = brk_len; + if(brk_en) { + evt.type = UART_DATA_BREAK; + } else { + evt.type = UART_DATA; + } xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void*) &evt, sizeof(uart_event_t), portMAX_DELAY); while(size > 0) { int send_size = size > max_size / 2 ? max_size / 2 : size; @@ -760,86 +728,45 @@ int uart_tx_all_chars(uart_port_t uart_num, const char* src, size_t size) } xSemaphoreGive(p_uart_obj[uart_num]->tx_mux); uart_enable_tx_intr(uart_num, 1, UART_EMPTY_THRESH_DEFAULT); - return ori_size; } else { - //Send data without TX ring buffer, the task will block until all data have been sent out - return uart_tx_all(uart_num, src, size, 0, 0); + while(size) { + //semaphore for tx_fifo available + if(pdTRUE == xSemaphoreTake(p_uart_obj[uart_num]->tx_fifo_sem, (portTickType)portMAX_DELAY)) { + size_t sent = uart_fill_fifo(uart_num, (char*) src, size); + if(sent < size) { + p_uart_obj[uart_num]->tx_waiting_fifo = true; + uart_enable_tx_intr(uart_num, 1, UART_EMPTY_THRESH_DEFAULT); + } + size -= sent; + src += sent; + } + } + if(brk_en) { + uart_set_break(uart_num, brk_len); + xSemaphoreTake(p_uart_obj[uart_num]->tx_brk_sem, (portTickType)portMAX_DELAY); + } + xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem); } + xSemaphoreGive(p_uart_obj[uart_num]->tx_mux); + return original_size; } -int uart_tx_all_chars_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len) +int uart_write_bytes(uart_port_t uart_num, const char* src, size_t size) { UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", (-1)); - UART_CHECK((p_uart_obj[uart_num]), "uart driver error", (-1)); - UART_CHECK((size > 0), "uart size error", (-1)); - UART_CHECK((src), "uart data null", (-1)); - UART_CHECK((brk_len > 0 && brk_len < 256), "break_num error", (-1)); - //Push data to TX ring buffer and return, ISR will send the data. - if(p_uart_obj[uart_num]->tx_buf_size > 0) { - xSemaphoreTake(p_uart_obj[uart_num]->tx_mux, (portTickType)portMAX_DELAY); - int max_size = xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf); - int ori_size = size; - int offset = 0; - uart_event_t evt; - evt.type = UART_DATA_BREAK; - evt.data.size = size; - evt.data.brk_len = brk_len; - xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void*) &evt, sizeof(uart_event_t), portMAX_DELAY); - while(size > 0) { - int send_size = size > max_size / 2 ? max_size / 2 : size; - xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void*) (src + offset), send_size, portMAX_DELAY); - size -= send_size; - offset += send_size; - } - xSemaphoreGive(p_uart_obj[uart_num]->tx_mux); - uart_enable_tx_intr(uart_num, 1, UART_EMPTY_THRESH_DEFAULT); - return ori_size; - } else { - //Send data without TX ring buffer, the task will block until all data have been sent out - return uart_tx_all(uart_num, src, size, 1, brk_len); - } + UART_CHECK((p_uart_obj[uart_num] != NULL), "uart driver error", (-1)); + UART_CHECK(src, "buffer null", (-1)); + return uart_tx_all(uart_num, src, size, 0, 0); } -int uart_read_char(uart_port_t uart_num, TickType_t ticks_to_wait) +int uart_write_bytes_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len) { UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", (-1)); UART_CHECK((p_uart_obj[uart_num]), "uart driver error", (-1)); - uint8_t* data; - size_t size; - int val; - portTickType ticks_end = xTaskGetTickCount() + ticks_to_wait; - if(xSemaphoreTake(p_uart_obj[uart_num]->rx_mux,(portTickType)ticks_to_wait) != pdTRUE) { - return -1; - } - if(p_uart_obj[uart_num]->rx_cur_remain == 0) { - ticks_to_wait = ticks_end - xTaskGetTickCount(); - data = (uint8_t*) xRingbufferReceive(p_uart_obj[uart_num]->rx_ring_buf, &size, (portTickType) ticks_to_wait); - if(data) { - p_uart_obj[uart_num]->rx_head_ptr = data; - p_uart_obj[uart_num]->rx_ptr = data; - p_uart_obj[uart_num]->rx_cur_remain = size; - } else { - xSemaphoreGive(p_uart_obj[uart_num]->rx_mux); - return -1; - } - } - val = *(p_uart_obj[uart_num]->rx_ptr); - p_uart_obj[uart_num]->rx_ptr++; - p_uart_obj[uart_num]->rx_cur_remain--; - if(p_uart_obj[uart_num]->rx_cur_remain == 0) { - vRingbufferReturnItem(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->rx_head_ptr); - p_uart_obj[uart_num]->rx_head_ptr = NULL; - p_uart_obj[uart_num]->rx_ptr = NULL; - if(p_uart_obj[uart_num]->rx_buffer_full_flg) { - BaseType_t res = xRingbufferSend(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->rx_data_buf, p_uart_obj[uart_num]->rx_stash_len, 1); - if(res == pdTRUE) { - p_uart_obj[uart_num]->rx_buffer_full_flg = false; - uart_enable_rx_intr(p_uart_obj[uart_num]->uart_num); - } - } - } - xSemaphoreGive(p_uart_obj[uart_num]->rx_mux); - return val; + UART_CHECK((size > 0), "uart size error", (-1)); + UART_CHECK((src), "uart data null", (-1)); + UART_CHECK((brk_len > 0 && brk_len < 256), "break_num error", (-1)); + return uart_tx_all(uart_num, src, size, 1, brk_len); } int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait) @@ -952,59 +879,6 @@ esp_err_t uart_flush(uart_port_t uart_num) return ESP_OK; } -//----------------------------------- -//Should not enable hw flow control the debug print port. -//Use uart_tx_all_chars() as a thread-safe function to send data. -static int s_uart_print_nport = UART_NUM_0; -static void uart2_write_char(char chr) -{ - uart_tx_all_chars(UART_NUM_2, (const char*)&chr, 1); -} - -static void uart1_write_char(char chr) -{ - uart_tx_all_chars(UART_NUM_1, (const char*)&chr, 1); -} - -static void uart0_write_char(char chr) -{ - uart_tx_all_chars(UART_NUM_0, (const char*)&chr, 1); -} - -static void uart_ignore_char(char chr) -{ - -} - -//Only effective to ets_printf function, not ESP_LOGX macro. -esp_err_t uart_set_print_port(uart_port_t uart_num) -{ - UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); - UART_CHECK((p_uart_obj[uart_num]), "UART driver error", ESP_FAIL); - s_uart_print_nport = uart_num; - switch(s_uart_print_nport) { - case UART_NUM_0: - ets_install_putc1(uart0_write_char); - break; - case UART_NUM_1: - ets_install_putc1(uart1_write_char); - break; - case UART_NUM_2: - ets_install_putc1(uart2_write_char); - break; - case UART_NUM_MAX: - default: - ets_install_putc1(uart_ignore_char); - break; - } - return ESP_OK; -} - -int uart_get_print_port() -{ - return s_uart_print_nport; -} - esp_err_t uart_driver_install(uart_port_t uart_num, int rx_buffer_size, int tx_buffer_size, int queue_size, int uart_intr_num, void* uart_queue) { UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error", ESP_FAIL); -- 2.40.0