mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
Modify UART driver:
1. Add a ring buffer for UART TX. If the buffer size is set to zero, driver will not use a buffer. But we need a task to send data from buffer to fifo. I tried directly copy data in ISR, but the code looked too long for ISR. 2. Modify the format in uart.h
This commit is contained in:
parent
d7ea61734b
commit
8d6b782327
@ -24,175 +24,168 @@ extern "C" {
|
||||
#include "soc/uart_struct.h"
|
||||
#include "esp_err.h"
|
||||
#include "driver/periph_ctrl.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/xtensa_api.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include <esp_types.h>
|
||||
|
||||
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_INTR_MASK 0x1ff
|
||||
#define UART_LINE_INV_MASK (0x3f << 19)
|
||||
#define UART_BITRATE_MAX 5000000
|
||||
|
||||
typedef enum {
|
||||
UART_DATA_5_BITS = 0x0, //word length: 5bits
|
||||
UART_DATA_6_BITS = 0x1, //word length: 6bits
|
||||
UART_DATA_7_BITS = 0x2, //word length: 7bits
|
||||
UART_DATA_8_BITS = 0x3, //word length: 8bits
|
||||
UART_DATA_5_BITS = 0x0, /*!< word length: 5bits*/
|
||||
UART_DATA_6_BITS = 0x1, /*!< word length: 6bits*/
|
||||
UART_DATA_7_BITS = 0x2, /*!< word length: 7bits*/
|
||||
UART_DATA_8_BITS = 0x3, /*!< word length: 8bits*/
|
||||
UART_DATA_MAX_BITS = 0X4,
|
||||
} uart_word_length_t;
|
||||
|
||||
typedef enum {
|
||||
UART_STOP_BITS_1 = 0x1, //stop bit: 1bit
|
||||
UART_STOP_BITS_1_5 = 0x2, //stop bit: 1.5bits
|
||||
UART_STOP_BITS_2 = 0x3, //stop bit: 2bits
|
||||
UART_STOP_BITS_1 = 0x1, /*!< stop bit: 1bit*/
|
||||
UART_STOP_BITS_1_5 = 0x2, /*!< stop bit: 1.5bits*/
|
||||
UART_STOP_BITS_2 = 0x3, /*!< stop bit: 2bits*/
|
||||
UART_STOP_BITS_MAX = 0x4,
|
||||
} uart_stop_bits_t;
|
||||
|
||||
typedef enum {
|
||||
UART_NUM_0 = 0x0, //base address 0x3ff40000
|
||||
UART_NUM_1 = 0x1, //base address 0x3ff50000
|
||||
UART_NUM_2 = 0x2, //base address 0x3ff6E000
|
||||
UART_NUM_0 = 0x0, /*!< UART base address 0x3ff40000*/
|
||||
UART_NUM_1 = 0x1, /*!< UART base address 0x3ff50000*/
|
||||
UART_NUM_2 = 0x2, /*!< UART base address 0x3ff6E000*/
|
||||
UART_NUM_MAX,
|
||||
} uart_port_t;
|
||||
|
||||
typedef enum {
|
||||
UART_PARITY_DISABLE = 0x0, //Disable UART parity
|
||||
UART_PARITY_EVEN = 0x10, //Enable UART even parity
|
||||
UART_PARITY_ODD = 0x11 //Enable UART odd parity
|
||||
UART_PARITY_DISABLE = 0x0, /*!< Disable UART parity*/
|
||||
UART_PARITY_EVEN = 0x10, /*!< Enable UART even parity*/
|
||||
UART_PARITY_ODD = 0x11 /*!< Enable UART odd parity*/
|
||||
} uart_parity_t;
|
||||
|
||||
typedef enum {
|
||||
UART_BITRATE_300 = 300,
|
||||
UART_BITRATE_600 = 600,
|
||||
UART_BITRATE_1200 = 1200,
|
||||
UART_BITRATE_2400 = 2400,
|
||||
UART_BITRATE_4800 = 4800,
|
||||
UART_BITRATE_9600 = 9600,
|
||||
UART_BITRATE_19200 = 19200,
|
||||
UART_BITRATE_38400 = 38400,
|
||||
UART_BITRATE_57600 = 57600,
|
||||
UART_BITRATE_74880 = 74880,
|
||||
UART_BITRATE_115200 = 115200,
|
||||
UART_BITRATE_230400 = 230400,
|
||||
UART_BITRATE_460800 = 460800,
|
||||
UART_BITRATE_921600 = 921600,
|
||||
UART_BITRATE_1843200 = 1843200,
|
||||
UART_BITRATE_3686400 = 3686400,
|
||||
UART_BITRATE_MAX = 5000000,
|
||||
} uart_baudrate_t; //you can set any rate you need in this range
|
||||
|
||||
typedef enum {
|
||||
UART_HW_FLOWCTRL_DISABLE = 0x0, //disable hardware flow control
|
||||
UART_HW_FLOWCTRL_RTS = 0x1, //enable RX hardware flow control (rts)
|
||||
UART_HW_FLOWCTRL_CTS = 0x2, //enable TX hardware flow control (cts)
|
||||
UART_HW_FLOWCTRL_CTS_RTS = 0x3, //enable hardware flow control
|
||||
UART_HW_FLOWCTRL_DISABLE = 0x0, /*!< disable hardware flow control*/
|
||||
UART_HW_FLOWCTRL_RTS = 0x1, /*!< enable RX hardware flow control (rts)*/
|
||||
UART_HW_FLOWCTRL_CTS = 0x2, /*!< enable TX hardware flow control (cts)*/
|
||||
UART_HW_FLOWCTRL_CTS_RTS = 0x3, /*!< enable hardware flow control*/
|
||||
UART_HW_FLOWCTRL_MAX = 0x4,
|
||||
} uart_hw_flowcontrol_t;
|
||||
|
||||
typedef enum {
|
||||
UART_INVERSE_DISABLE = 0x0, //Disable UART wire output inverse
|
||||
UART_INVERSE_RXD = (uint32_t)UART_RXD_INV_M, //UART RXD input inverse
|
||||
UART_INVERSE_CTS = (uint32_t)UART_CTS_INV_M, //UART CTS input inverse
|
||||
UART_INVERSE_TXD = (uint32_t)UART_TXD_INV_M, //UART TXD output inverse
|
||||
UART_INVERSE_RTS = (uint32_t)UART_RTS_INV_M, //UART RTS output inverse
|
||||
UART_INVERSE_DISABLE = 0x0, /*!< Disable UART wire output inverse*/
|
||||
UART_INVERSE_RXD = (uint32_t)UART_RXD_INV_M, /*!< UART RXD input inverse*/
|
||||
UART_INVERSE_CTS = (uint32_t)UART_CTS_INV_M, /*!< UART CTS input inverse*/
|
||||
UART_INVERSE_TXD = (uint32_t)UART_TXD_INV_M, /*!< UART TXD output inverse*/
|
||||
UART_INVERSE_RTS = (uint32_t)UART_RTS_INV_M, /*!< UART RTS output inverse*/
|
||||
} uart_inverse_t;
|
||||
|
||||
typedef struct {
|
||||
uart_baudrate_t baud_rate; //UART baudrate
|
||||
uart_word_length_t data_bits; //UART byte size
|
||||
uart_parity_t parity; //UART parity mode
|
||||
uart_stop_bits_t stop_bits; //UART stop bits
|
||||
uart_hw_flowcontrol_t flow_ctrl; //UART hw flow control mode(cts/rts)
|
||||
uint8_t rx_flow_ctrl_thresh ; //UART hw RTS threshold
|
||||
int baud_rate; /*!< UART baudrate*/
|
||||
uart_word_length_t data_bits; /*!< UART byte size*/
|
||||
uart_parity_t parity; /*!< UART parity mode*/
|
||||
uart_stop_bits_t stop_bits; /*!< UART stop bits*/
|
||||
uart_hw_flowcontrol_t flow_ctrl; /*!< UART hw flow control mode(cts/rts)*/
|
||||
uint8_t rx_flow_ctrl_thresh ; /*!< UART hw RTS threshold*/
|
||||
} uart_config_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t intr_enable_mask; //UART interrupt enable mask, choose from UART_XXXX_INT_ENA_M under UART_INT_ENA_REG(i), connect with bit-or operator
|
||||
uint8_t rx_timeout_thresh; //UART timeout interrupt threshold(unit: time of sending one byte)
|
||||
uint8_t txfifo_empty_intr_thresh; //UART TX empty interrupt threshold.
|
||||
uint8_t rxfifo_full_thresh; //UART RX full interrupt threshold.
|
||||
uint32_t intr_enable_mask; /*!< UART interrupt enable mask, choose from UART_XXXX_INT_ENA_M under UART_INT_ENA_REG(i), connect with bit-or operator*/
|
||||
uint8_t rx_timeout_thresh; /*!< UART timeout interrupt threshold(unit: time of sending one byte)*/
|
||||
uint8_t txfifo_empty_intr_thresh; /*!< UART TX empty interrupt threshold.*/
|
||||
uint8_t rxfifo_full_thresh; /*!< UART RX full interrupt threshold.*/
|
||||
} uart_intr_config_t;
|
||||
|
||||
|
||||
typedef enum {
|
||||
UART_DATA,
|
||||
UART_BREAK,
|
||||
UART_BUFFER_FULL,
|
||||
UART_FIFO_OVF,
|
||||
UART_FRAME_ERR,
|
||||
UART_PARITY_ERR,
|
||||
UART_EVENT_MAX,
|
||||
UART_DATA, /*!< UART data event*/
|
||||
UART_BREAK, /*!< UART break event*/
|
||||
UART_BUFFER_FULL, /*!< UART RX buffer full event*/
|
||||
UART_FIFO_OVF, /*!< UART FIFO overflow event*/
|
||||
UART_FRAME_ERR, /*!< UART RX frame error event*/
|
||||
UART_PARITY_ERR, /*!< UART RX parity event*/
|
||||
UART_DATA_BREAK, /*!< UART TX data and break event*/
|
||||
UART_EVENT_MAX, /*!< UART event max index*/
|
||||
} uart_event_type_t;
|
||||
|
||||
typedef struct {
|
||||
uart_event_type_t type;
|
||||
union {
|
||||
struct {
|
||||
int brk_len;
|
||||
size_t size;
|
||||
uint8_t data[];
|
||||
} data;
|
||||
|
||||
};
|
||||
} uart_event_t;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Set UART data bits.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_word_length_t data_bit : UART data bits
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param data_bit UART data bits
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit);
|
||||
|
||||
/**
|
||||
* @brief Get UART data bits.
|
||||
*
|
||||
* @param uart_port_t uart_no: UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_FAIL : Parameter error
|
||||
* ESP_OK : Success, result will be put in (*data_bit)
|
||||
* @return
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_OK Success, result will be put in (*data_bit)
|
||||
*/
|
||||
esp_err_t uart_get_word_length(uart_port_t uart_num, uart_word_length_t* data_bit);
|
||||
|
||||
/**
|
||||
* @brief Set UART stop bits.
|
||||
*
|
||||
* @param uart_port_t uart_no: UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_stop_bits_t bit_num : UART stop bits
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param bit_num UART stop bits
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Fail
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Fail
|
||||
*/
|
||||
esp_err_t uart_set_stop_bits(uart_port_t uart_no, uart_stop_bits_t bit_num);
|
||||
|
||||
/**
|
||||
* @brief Set UART stop bits.
|
||||
*
|
||||
* @param uart_port_t uart_no: UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_FAIL : Parameter error
|
||||
* ESP_OK : Success, result will be put in (*stop_bit)
|
||||
* @return
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_OK Success, result will be put in (*stop_bit)
|
||||
*/
|
||||
esp_err_t uart_get_stop_bits(uart_port_t uart_num, uart_stop_bits_t* stop_bit);
|
||||
|
||||
/**
|
||||
* @brief Set UART parity.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_parity_t parity_mode : the enum of uart parity configuration
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param parity_mode the enum of uart parity configuration
|
||||
*
|
||||
* @return null
|
||||
* @return
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_OK Success
|
||||
*/
|
||||
esp_err_t uart_set_parity(uart_port_t uart_no, uart_parity_t parity_mode);
|
||||
|
||||
/**
|
||||
* @brief Get UART parity mode.
|
||||
*
|
||||
* @param uart_port_t uart_no: UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_FAIL : Parameter error
|
||||
* ESP_OK : Success, result will be put in (*parity_mode)
|
||||
* @return
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_OK Success, result will be put in (*parity_mode)
|
||||
*
|
||||
*/
|
||||
esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t* parity_mode);
|
||||
@ -200,32 +193,37 @@ esp_err_t uart_get_parity(uart_port_t uart_num, uart_parity_t* parity_mode);
|
||||
/**
|
||||
* @brief Set UART baud rate.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uint32_t baud_rate : UART baud-rate, we can choose one from uart_baudrate_t, or set a value.
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param baud_rate UART baud-rate.
|
||||
*
|
||||
* @return null
|
||||
* @return
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_OK Success
|
||||
*/
|
||||
esp_err_t uart_set_baudrate(uart_port_t uart_no, uint32_t baud_rate);
|
||||
|
||||
/**
|
||||
* @brief Get UART bit-rate.
|
||||
*
|
||||
* @param uart_port_t uart_no: UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no: UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_FAIL : Parameter error
|
||||
* ESP_OK : Success, result will be put in (*baudrate)
|
||||
* @return
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_OK Success, result will be put in (*baudrate)
|
||||
*
|
||||
*/
|
||||
esp_err_t uart_get_baudrate(uart_port_t uart_num, uint32_t* baudrate);
|
||||
|
||||
/**
|
||||
* @brief Set UART line inverse mode
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uint32_t inverse_mask : Choose the wires that need to be inversed
|
||||
* (Should be chosen from uart_inverse_t, combine with OR-OPERATION)
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param inverse_mask Choose the wires that need to be inversed.
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* (inverse_mask should be chosen from uart_inverse_t, combine with OR-OPERATION)
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_set_line_inverse(uart_port_t uart_no, uint32_t inverse_mask) ;
|
||||
|
||||
@ -233,57 +231,65 @@ esp_err_t uart_set_line_inverse(uart_port_t uart_no, uint32_t inverse_mask) ;
|
||||
/**
|
||||
* @brief Set hardware flow control.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_hw_flowcontrol_t flow_ctrl : Hardware flow control mode
|
||||
* @param uint8_t rx_thresh : Threshold of Hardware RX flow control(0 ~ UART_FIFO_LEN)
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param flow_ctrl Hardware flow control mode
|
||||
* @param rx_thresh Threshold of Hardware RX flow control(0 ~ UART_FIFO_LEN)
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_set_hw_flow_ctrl(uart_port_t uart_no, uart_hw_flowcontrol_t flow_ctrl, uint8_t rx_thresh);
|
||||
|
||||
/**
|
||||
* @brief Get hardware flow control mode
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_FAIL : Parameter error
|
||||
* ESP_OK : Success, result will be put in (*flow_ctrl)
|
||||
* @return
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_OK Success, result will be put in (*flow_ctrl)
|
||||
*/
|
||||
esp_err_t uart_get_hw_flow_ctrl(uart_port_t uart_num, uart_hw_flowcontrol_t* flow_ctrl);
|
||||
|
||||
/**
|
||||
* @brief Clear UART interrupt status
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uint32_t clr_mask : Bit mask of the status that to be cleared.
|
||||
* enable_mask should be chosen from the fields of register UART_INT_CLR_REG
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param clr_mask Bit mask of the status that to be cleared.
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* (enable_mask should be chosen from the fields of register UART_INT_CLR_REG)
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_clear_intr_status(uart_port_t uart_num, uint32_t clr_mask);
|
||||
|
||||
/**
|
||||
* @brief Set UART interrupt enable
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uint32_t enable_mask : Bit mask of the enable bits.
|
||||
* enable_mask should be chosen from the fields of register UART_INT_ENA_REG
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param enable_mask Bit mask of the enable bits.
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* (enable_mask should be chosen from the fields of register UART_INT_ENA_REG)
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_enable_intr_mask(uart_port_t uart_num, uint32_t enable_mask);
|
||||
|
||||
/**
|
||||
* @brief Clear UART interrupt enable bits
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uint32_t disable_mask : Bit mask of the disable bits.
|
||||
* Disable_mask should be chosen from the fields of register UART_INT_ENA_REG
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param disable_mask Bit mask of the disable bits.
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* (disable_mask should be chosen from the fields of register UART_INT_ENA_REG)
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask);
|
||||
|
||||
@ -291,42 +297,46 @@ esp_err_t uart_disable_intr_mask(uart_port_t uart_num, uint32_t disable_mask);
|
||||
/**
|
||||
* @brief Enable UART RX interrupt(RX_FULL & RX_TIMEOUT INTERRUPT)
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_enable_rx_intr(uart_port_t uart_num);
|
||||
|
||||
/**
|
||||
* @brief Disable UART RX interrupt(RX_FULL & RX_TIMEOUT INTERRUPT)
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_disable_rx_intr(uart_port_t uart_num);
|
||||
|
||||
/**
|
||||
* @brief Disable UART TX interrupt(RX_FULL & RX_TIMEOUT INTERRUPT)
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_disable_tx_intr(uart_port_t uart_num);
|
||||
|
||||
/**
|
||||
* @brief Enable UART TX interrupt(RX_FULL & RX_TIMEOUT INTERRUPT)
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param int enable : 1: enable; 0: disable
|
||||
* @param int thresh : Threshold of TX interrupt, 0 ~ UART_FIFO_LEN
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param enable 1: enable; 0: disable
|
||||
* @param thresh Threshold of TX interrupt, 0 ~ UART_FIFO_LEN
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh);
|
||||
|
||||
@ -337,29 +347,31 @@ esp_err_t uart_enable_tx_intr(uart_port_t uart_num, int enable, int thresh);
|
||||
* We can find the information of INUM and interrupt level in soc.h.
|
||||
*
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uint8_t uart_intr_num : UART interrupt number,check the info in soc.h, and please refer to core-isa.h for more details
|
||||
* @param void (* fn)(void* ) : Interrupt handler function.
|
||||
* Note that the handler function MUST be defined with attribution of "IRAM_ATTR" for now.
|
||||
* @param void * arg : parameter for handler function
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_intr_num UART interrupt number,check the info in soc.h, and please refer to core-isa.h for more details
|
||||
* @param fn Interrupt handler function.
|
||||
* @attention
|
||||
* The ISR handler function MUST be defined with attribution of "IRAM_ATTR" for now.
|
||||
* @param arg parameter for handler function
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_isr_register(uart_port_t uart_num, uint8_t uart_intr_num, void (*fn)(void*), void * arg);
|
||||
|
||||
/**
|
||||
* @brief Set UART pin number
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param tx_io_num : UART TX pin GPIO number
|
||||
* @param rx_io_num : UART RX pin GPIO number
|
||||
* @param rts_io_num : UART RTS pin GPIO number
|
||||
* @param cts_io_num : UART CTS pin GPIO number
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param tx_io_num UART TX pin GPIO number
|
||||
* @param rx_io_num UART RX pin GPIO number
|
||||
* @param rts_io_num UART RTS pin GPIO number
|
||||
* @param cts_io_num UART CTS pin GPIO number
|
||||
*
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num);
|
||||
|
||||
@ -367,97 +379,107 @@ esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int r
|
||||
* @brief UART set RTS level (before inverse)
|
||||
* UART rx hardware flow control should not be set.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param int level : 1: RTS output low(active)
|
||||
* 0: RTS output high(block)
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param level 1: RTS output low(active); 0: RTS output high(block)
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_set_rts(uart_port_t uart_num, int level);
|
||||
|
||||
/**
|
||||
* @brief UART set DTR level (before inverse)
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param int level : 1: DTR output low
|
||||
* 0: DTR output high
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param level 1: DTR output low; 0: DTR output high
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_set_dtr(uart_port_t uart_num, int level);
|
||||
|
||||
/**
|
||||
* @brief UART parameter configure
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_config_t *uart_config: UART parameter settings
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_config UART parameter settings
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_param_config(uart_port_t uart_num, uart_config_t *uart_config);
|
||||
|
||||
/**
|
||||
* @brief UART interrupt configure
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_intr_config_t *p_intr_conf: UART interrupt settings
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param p_intr_conf UART interrupt settings
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @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);
|
||||
|
||||
/**
|
||||
* @brief Install UART driver.
|
||||
*
|
||||
* UART ISR handler will be attached to the same CPU core that this function is running on.
|
||||
* Users should know that which CPU is running and then pick a INUM that is not used by system.
|
||||
* We can find the information of INUM and interrupt level in soc.h.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param int buffer_size : UART ring buffer size
|
||||
* @param int queue_size : UART event queue size/depth.
|
||||
* @param int uart_intr_num : UART interrupt number,check the info in soc.h, and please refer to core-isa.h for more details
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param rx_buffer_size UART RX ring buffer size
|
||||
* @param tx_buffer_size UART TX ring buffer size, if set to zero, driver will not use TX buffer and TX task.
|
||||
* @param queue_size UART event queue size/depth.
|
||||
* @param uart_intr_num UART interrupt number,check the info in soc.h, and please refer to core-isa.h for more details
|
||||
* @param uart_queue UART event queue handle, if set NULL, driver will not use an event queue.
|
||||
* @param buf_type UART RX ring_buffer type
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_driver_install(uart_port_t uart_num, int buffer_size, int queue_size, int uart_intr_num, void* uart_queue);
|
||||
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, ringbuf_type_t rx_buf_type);
|
||||
|
||||
/**
|
||||
* @brief Uninstall UART driver.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
esp_err_t uart_driver_delete(uart_port_t uart_num);
|
||||
|
||||
/**
|
||||
* @brief Wait UART TX FIFO empty
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param TickType_t ticks_to_wait: Timeout, count in RTOS ticks
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param ticks_to_wait Timeout, count in RTOS ticks
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL : Parameter error
|
||||
* ESP_ERR_TIMEOUT: Timeout
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
* - ESP_ERR_TIMEOUT Timeout
|
||||
*/
|
||||
esp_err_t uart_wait_tx_fifo_empty(uart_port_t uart_num, TickType_t ticks_to_wait);
|
||||
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.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param char* buffer : data buffer address
|
||||
* @param uint32_t len : data length to send
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param buffer data buffer address
|
||||
* @param len data length to send
|
||||
*
|
||||
* @return -1 : Parameter error
|
||||
* OTHERS(>=0): The number of data that pushed to the TX FIFO
|
||||
* @return
|
||||
* - (-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);
|
||||
|
||||
@ -465,12 +487,13 @@ int uart_tx_chars(uart_port_t uart_no, char* buffer, uint32_t len);
|
||||
* @brief Send data to the UART port from a given buffer and length,
|
||||
* This function will not return until all the data have been sent out, or at least pushed into TX FIFO.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param char* src : data buffer address
|
||||
* @param size_t size : data length to send
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param src data buffer address
|
||||
* @param size data length to send
|
||||
*
|
||||
* @return -1 : Parameter error
|
||||
* OTHERS(>=0): The number of data that pushed to the TX FIFO
|
||||
* @return
|
||||
* - (-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);
|
||||
|
||||
@ -478,57 +501,62 @@ int uart_tx_all_chars(uart_port_t uart_num, const char* src, size_t size);
|
||||
* @brief Send data to the UART port from a given buffer and length,
|
||||
* This function will not return until all the data and the break signal have been sent out.
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param char* src : data buffer address
|
||||
* @param size_t size : data length to send
|
||||
* @param int brk_len : break signal length (unit: one bit's time@current_baudrate)
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param src data buffer address
|
||||
* @param size data length to send
|
||||
* @param brk_len break signal length (unit: one bit's time@current_baudrate)
|
||||
*
|
||||
* @return -1 : Parameter error
|
||||
* OTHERS(>=0): The number of data that pushed to the TX FIFO
|
||||
* @return
|
||||
* - (-1) Parameter error
|
||||
* - 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_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param TickType_t ticks_to_wait : Timeout, count in RTOS ticks
|
||||
* @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.
|
||||
* @return
|
||||
* - (-1) Error
|
||||
* - Others return a char data from UART.
|
||||
*/
|
||||
int uart_read_char(uart_port_t uart_num, TickType_t ticks_to_wait);
|
||||
|
||||
/**
|
||||
* @brief UART read bytes from UART buffer
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uint8_t* buf : pointer to the buffer.
|
||||
* @param uint32_t length : data length
|
||||
* @param TickType_t ticks_to_wait: Timeout, count in RTOS ticks
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param buf pointer to the buffer.
|
||||
* @param length data length
|
||||
* @param ticks_to_wait: Timeout, count in RTOS ticks
|
||||
*
|
||||
* @return -1 : Error
|
||||
* Others : return a char data from uart fifo.
|
||||
* @return
|
||||
* - (-1) Error
|
||||
* - Others return a char data from uart fifo.
|
||||
*/
|
||||
int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickType_t ticks_to_wait);
|
||||
|
||||
/**
|
||||
* @brief UART ring buffer flush
|
||||
*
|
||||
* @param uart_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @param uart_no UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
*
|
||||
* @return ESP_OK : Success
|
||||
* ESP_FAIL: Parameter error
|
||||
* @return
|
||||
* - ESP_OK Success
|
||||
* - ESP_FAIL Parameter error
|
||||
*/
|
||||
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_port_t uart_no : UART_NUM_0, UART_NUM_1 or UART_NUM_2
|
||||
* @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.
|
||||
* @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);
|
||||
|
||||
@ -536,16 +564,15 @@ 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;
|
||||
* @return current print port(0: UART0; 1: UART1; 2: UART2)
|
||||
*/
|
||||
int uart_get_print_port();
|
||||
int uart_get_print_port(void);
|
||||
|
||||
/***************************EXAMPLE**********************************
|
||||
*
|
||||
*
|
||||
* ----------------EXAMPLE OF UART SETTING ---------------------
|
||||
* @code{c}
|
||||
* //1. Setup UART
|
||||
* #include "freertos/queue.h"
|
||||
* #define UART_INTR_NUM 17 //choose one interrupt number from soc.h
|
||||
@ -565,28 +592,32 @@ int uart_get_print_port();
|
||||
* uart_driver_install(uart_num, 1024 * 2, 10, UART_INTR_NUM, &uart_queue);//parameters here are just an example
|
||||
* //b2. Setup UART driver(without UART queue)
|
||||
* uart_driver_install(uart_num, 1024 * 2, 10, UART_INTR_NUM, NULL); //parameters here are just an example
|
||||
*
|
||||
*@endcode
|
||||
*-----------------------------------------------------------------------------*
|
||||
* @code{c}
|
||||
* //2. Set UART pin
|
||||
* uart_set_pin(uart_num, -1, -1, 15, 13); //set UART pin, not needed if use default pins.
|
||||
*
|
||||
* @endcode
|
||||
*-----------------------------------------------------------------------------*
|
||||
* @code{c}
|
||||
* //3. Read data from UART.
|
||||
* uint8_t data[128];
|
||||
* int length = 0;
|
||||
* length = uart_read_bytes(uart_num, data, sizeof(data), 100);
|
||||
*
|
||||
* @endcode
|
||||
*-----------------------------------------------------------------------------*
|
||||
* @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));
|
||||
*
|
||||
* @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);
|
||||
*
|
||||
* @endcode
|
||||
*-----------------------------------------------------------------------------*
|
||||
*
|
||||
* @code{c}
|
||||
* //6. an example of echo test with hardware flow control on UART1
|
||||
* void uart_loop_back_test()
|
||||
* {
|
||||
@ -602,49 +633,59 @@ int uart_get_print_port();
|
||||
* uart_param_config(uart_num, &uart_config); //Config UART1 parameters
|
||||
* uart_set_pin(uart_num, 16, 17, 18, 19); //Set UART1 pins(TX: IO16, RX: IO17, RTS: IO18, CTS: IO19)
|
||||
* esp_log_level_set(UART_TAG, ESP_LOG_ERROR); //Set UART log level
|
||||
* uart_driver_install(uart_num, 1024 * 2, 10, 17, NULL); //Install UART driver( We don't need an event queue here)
|
||||
* //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];
|
||||
* while(1) {
|
||||
* int len = uart_read_bytes(uart_num, data, sizeof(data), 10); //Read data from UART
|
||||
* uart_tx_all_chars(uart_num, (const char*)data, len); //Write data back to UART
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* @endcode
|
||||
*-----------------------------------------------------------------------------*
|
||||
* @code{c}
|
||||
* //7. An example of using UART event queue on UART0.
|
||||
*
|
||||
* #include "freertos/queue.h"
|
||||
* QueueHandle_t uart0_queue; //A queue to handle UART event.
|
||||
* //A queue to handle UART event.
|
||||
* QueueHandle_t uart0_queue;
|
||||
* void uart_task(void *pvParameters)
|
||||
* {
|
||||
* int uart_num = (int)pvParameters;
|
||||
* uart_event_t event;
|
||||
* uint8_t dtmp[1000];
|
||||
* for(;;) {
|
||||
* if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) { //Waiting for UART event.
|
||||
* //Waiting for UART event.
|
||||
* if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
|
||||
* ESP_LOGI(UART_TAG, "uart[%d] event:", uart_num);
|
||||
* switch(event.type) {
|
||||
* case UART_DATA: //Event of UART receving data
|
||||
* //Event of UART receving data
|
||||
* case UART_DATA:
|
||||
* ESP_LOGI(UART_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);
|
||||
* break;
|
||||
* case UART_FIFO_OVF: //Event of HW FIFO overflow detected
|
||||
* //Event of HW FIFO overflow detected
|
||||
* case UART_FIFO_OVF:
|
||||
* ESP_LOGI(UART_TAG, "hw fifo overflow\n");
|
||||
* break;
|
||||
* case UART_BUFFER_FULL: //Event of UART ring buffer full
|
||||
* //Event of UART ring buffer full
|
||||
* case UART_BUFFER_FULL:
|
||||
* ESP_LOGI(UART_TAG, "ring buffer full\n");
|
||||
* break;
|
||||
* //Event of UART RX break detected
|
||||
* case UART_BREAK:
|
||||
* ESP_LOGI(UART_TAG, "uart rx break\n"); //Event of UART RX break detected
|
||||
* ESP_LOGI(UART_TAG, "uart rx break\n");
|
||||
* break;
|
||||
* case UART_PARITY_ERR: //Event of UART parity check error
|
||||
* //Event of UART parity check error
|
||||
* case UART_PARITY_ERR:
|
||||
* ESP_LOGI(UART_TAG, "uart parity error\n");
|
||||
* break;
|
||||
* case UART_FRAME_ERR: //Event of UART frame error
|
||||
* //Event of UART frame error
|
||||
* case UART_FRAME_ERR:
|
||||
* ESP_LOGI(UART_TAG, "uart frame error\n");
|
||||
* break;
|
||||
* default: //Others
|
||||
* //Others
|
||||
* default:
|
||||
* ESP_LOGI(UART_TAG, "uart event type: %d\n", event.type);
|
||||
* break;
|
||||
* }
|
||||
@ -667,10 +708,11 @@ int uart_get_print_port();
|
||||
* uart_param_config(uart_num, &uart_config); //Set UART parameters
|
||||
* uart_set_pin(uart_num, -1, -1, 15, 13); //Set UART pins,(-1: default pin, no change.)
|
||||
* esp_log_level_set(UART_TAG, ESP_LOG_INFO); //Set UART log level
|
||||
* uart_driver_install(uart_num, 1024 * 2, 10, 17, &uart0_queue); //Install UART driver, and get the queue.
|
||||
* //Install UART driver, and get the queue.
|
||||
* uart_driver_install(uart_num, 1024 * 2, 1024*4, 10, 17, &uart0_queue, RINGBUF_TYPE_BYTEBUF);
|
||||
* xTaskCreate(uart_task, "uTask", 2048*8, (void*)uart_num, 10, NULL); //Create a task to handler UART event from ISR
|
||||
* }
|
||||
*
|
||||
* @endcode
|
||||
*
|
||||
***************************END OF EXAMPLE**********************************/
|
||||
|
||||
|
@ -34,9 +34,11 @@ const char* UART_TAG = "UART";
|
||||
ESP_LOGE(UART_TAG,"%s:%d (%s):%s\n", __FILE__, __LINE__, __FUNCTION__, str); \
|
||||
return ESP_FAIL; \
|
||||
}
|
||||
#define DEFAULT_EMPTY_THRESH 10
|
||||
#define DEFAULT_FULL_THRESH 120
|
||||
#define DEFAULT_TOUT_THRESH 10
|
||||
#define UART_EMPTY_THRESH_DEFAULT (10)
|
||||
#define UART_FULL_THRESH_DEFAULT (120)
|
||||
#define UART_TOUT_THRESH_DEFAULT (10)
|
||||
#define UART_TX_TASK_DEPTH_DEFAULT (256*2+64)
|
||||
#define UART_TX_TASK_PRIO_DEFAULT (10)
|
||||
#define UART_ENTER_CRITICAL_ISR(mux) portENTER_CRITICAL_ISR(mux)
|
||||
#define UART_EXIT_CRITICAL_ISR(mux) portEXIT_CRITICAL_ISR(mux)
|
||||
#define UART_ENTER_CRITICAL(mux) portENTER_CRITICAL(mux)
|
||||
@ -46,13 +48,19 @@ typedef struct {
|
||||
uart_port_t uart_num;
|
||||
SemaphoreHandle_t tx_fifo_sem;
|
||||
SemaphoreHandle_t tx_mutex;
|
||||
SemaphoreHandle_t tx_buffer_mutex;
|
||||
SemaphoreHandle_t tx_done_sem;
|
||||
SemaphoreHandle_t tx_brk_sem;
|
||||
SemaphoreHandle_t rx_sem;
|
||||
SemaphoreHandle_t rx_mux;
|
||||
QueueHandle_t xQueueUart;
|
||||
int queue_size;
|
||||
int intr_num;
|
||||
RingbufHandle_t ring_buffer;
|
||||
int rx_buf_size;
|
||||
ringbuf_type_t rx_buf_type;
|
||||
RingbufHandle_t rx_ring_buf;
|
||||
int tx_buf_size;
|
||||
RingbufHandle_t tx_ring_buf;
|
||||
TaskHandle_t tx_task_handle;
|
||||
bool buffer_full_flg;
|
||||
bool tx_waiting;
|
||||
int cur_remain;
|
||||
@ -66,20 +74,6 @@ static uart_obj_t *p_uart_obj[UART_NUM_MAX] = {0};
|
||||
static uart_dev_t* UART[UART_NUM_MAX] = {&UART0, &UART1, &UART2};
|
||||
static portMUX_TYPE uart_spinlock[UART_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED, portMUX_INITIALIZER_UNLOCKED};
|
||||
|
||||
//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)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t tx_fifo_cnt = UART[uart_num]->status.txfifo_cnt;
|
||||
uint8_t tx_remain_fifo_cnt = (UART_FIFO_LEN - tx_fifo_cnt);
|
||||
uint8_t copy_cnt = (len >= tx_remain_fifo_cnt ? tx_remain_fifo_cnt : len);
|
||||
for(i = 0; i < copy_cnt; i++) {
|
||||
WRITE_PERI_REG(UART_FIFO_AHB_REG(uart_num), buffer[i]);
|
||||
}
|
||||
return copy_cnt;
|
||||
}
|
||||
|
||||
esp_err_t uart_set_word_length(uart_port_t uart_num, uart_word_length_t data_bit)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
@ -315,11 +309,11 @@ esp_err_t uart_isr_register(uart_port_t uart_num, uint8_t uart_intr_num, void (*
|
||||
//only one GPIO pad can connect with input signal
|
||||
esp_err_t uart_set_pin(uart_port_t uart_num, int tx_io_num, int rx_io_num, int rts_io_num, int cts_io_num)
|
||||
{
|
||||
// UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
// UART_CHECK((tx_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(tx_io_num))), "tx_io_num error");
|
||||
// UART_CHECK((rx_io_num < 0 || (GPIO_IS_VALID_GPIO(rx_io_num))), "rx_io_num error");
|
||||
// UART_CHECK((rts_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(rts_io_num))), "rts_io_num error");
|
||||
// UART_CHECK((cts_io_num < 0 || (GPIO_IS_VALID_GPIO(cts_io_num))), "cts_io_num error");
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
UART_CHECK((tx_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(tx_io_num))), "tx_io_num error");
|
||||
UART_CHECK((rx_io_num < 0 || (GPIO_IS_VALID_GPIO(rx_io_num))), "rx_io_num error");
|
||||
UART_CHECK((rts_io_num < 0 || (GPIO_IS_VALID_OUTPUT_GPIO(rts_io_num))), "rts_io_num error");
|
||||
UART_CHECK((cts_io_num < 0 || (GPIO_IS_VALID_GPIO(cts_io_num))), "cts_io_num error");
|
||||
|
||||
int tx_sig, rx_sig, rts_sig, cts_sig;
|
||||
switch(uart_num) {
|
||||
@ -443,7 +437,6 @@ static void IRAM_ATTR uart_rx_intr_handler_default(void *param)
|
||||
uart_obj_t *p_uart = (uart_obj_t*) param;
|
||||
uint8_t uart_num = p_uart->uart_num;
|
||||
uart_dev_t* uart_reg = UART[uart_num];
|
||||
|
||||
uint8_t buf_idx = 0;
|
||||
uint32_t uart_intr_status = UART[uart_num]->int_st.val;
|
||||
static int rx_fifo_len = 0;
|
||||
@ -478,7 +471,7 @@ static void IRAM_ATTR uart_rx_intr_handler_default(void *param)
|
||||
UART_EXIT_CRITICAL_ISR(&uart_spinlock[uart_num]);
|
||||
uart_event.type = UART_DATA;
|
||||
uart_event.data.size = rx_fifo_len;
|
||||
if(pdFALSE == xRingbufferSendFromISR(p_uart->ring_buffer, p_uart->data_buf, p_uart->data_len, &HPTaskAwoken)) {
|
||||
if(pdFALSE == xRingbufferSendFromISR(p_uart->rx_ring_buf, p_uart->data_buf, p_uart->data_len, &HPTaskAwoken)) {
|
||||
UART_ENTER_CRITICAL_ISR(&uart_spinlock[uart_num]);
|
||||
uart_reg->int_ena.rxfifo_full = 0;
|
||||
uart_reg->int_ena.rxfifo_tout = 0;
|
||||
@ -544,108 +537,7 @@ static void IRAM_ATTR uart_rx_intr_handler_default(void *param)
|
||||
}
|
||||
|
||||
/**************************************************************/
|
||||
esp_err_t uart_driver_install(uart_port_t uart_num, int buffer_size, int queue_size, int uart_intr_num, void* uart_queue)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
if(p_uart_obj[uart_num] == NULL) {
|
||||
ESP_INTR_DISABLE(uart_intr_num);
|
||||
p_uart_obj[uart_num] = (uart_obj_t*) malloc(sizeof(uart_obj_t));
|
||||
if(p_uart_obj[uart_num] == NULL) {
|
||||
ESP_LOGE(UART_TAG, "UART driver malloc error\n");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
p_uart_obj[uart_num]->uart_num = uart_num;
|
||||
p_uart_obj[uart_num]->tx_fifo_sem = xSemaphoreCreateBinary();
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem);
|
||||
p_uart_obj[uart_num]->tx_done_sem = xSemaphoreCreateBinary();
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_done_sem);
|
||||
p_uart_obj[uart_num]->tx_brk_sem = xSemaphoreCreateBinary();
|
||||
|
||||
p_uart_obj[uart_num]->tx_mutex = xSemaphoreCreateMutex();
|
||||
p_uart_obj[uart_num]->rx_sem = xSemaphoreCreateMutex();
|
||||
p_uart_obj[uart_num]->intr_num = uart_intr_num;
|
||||
p_uart_obj[uart_num]->queue_size = queue_size;
|
||||
|
||||
if(uart_queue) {
|
||||
p_uart_obj[uart_num]->xQueueUart = xQueueCreate(queue_size, sizeof(uart_event_t));
|
||||
*((QueueHandle_t*) uart_queue) = p_uart_obj[uart_num]->xQueueUart;
|
||||
ESP_LOGI(UART_TAG, "queue free spaces: %d\n", uxQueueSpacesAvailable(p_uart_obj[uart_num]->xQueueUart));
|
||||
} else {
|
||||
p_uart_obj[uart_num]->xQueueUart = NULL;
|
||||
}
|
||||
p_uart_obj[uart_num]->buffer_full_flg = false;
|
||||
p_uart_obj[uart_num]->tx_waiting = false;
|
||||
p_uart_obj[uart_num]->rd_ptr = NULL;
|
||||
p_uart_obj[uart_num]->cur_remain = 0;
|
||||
p_uart_obj[uart_num]->head_ptr = NULL;
|
||||
p_uart_obj[uart_num]->ring_buffer = xRingbufferCreate(buffer_size, 0);
|
||||
} else {
|
||||
ESP_LOGE(UART_TAG, "UART driver already installed\n");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
uart_isr_register(uart_num, uart_intr_num, uart_rx_intr_handler_default, p_uart_obj[uart_num]);
|
||||
uart_intr_config_t uart_intr = {
|
||||
.intr_enable_mask = UART_RXFIFO_FULL_INT_ENA_M
|
||||
| UART_RXFIFO_TOUT_INT_ENA_M
|
||||
| UART_FRM_ERR_INT_ENA_M
|
||||
| UART_RXFIFO_OVF_INT_ENA_M
|
||||
| UART_BRK_DET_INT_ENA_M,
|
||||
.rxfifo_full_thresh = DEFAULT_FULL_THRESH,
|
||||
.rx_timeout_thresh = DEFAULT_TOUT_THRESH,
|
||||
.txfifo_empty_intr_thresh = DEFAULT_EMPTY_THRESH
|
||||
};
|
||||
uart_intr_config(uart_num, &uart_intr);
|
||||
ESP_INTR_ENABLE(uart_intr_num);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//Make sure no other tasks are still using UART before you call this function
|
||||
esp_err_t uart_driver_delete(uart_port_t uart_num)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
if(p_uart_obj[uart_num] == NULL) {
|
||||
ESP_LOGI(UART_TAG, "ALREADY NULL\n");
|
||||
return ESP_OK;
|
||||
}
|
||||
ESP_INTR_DISABLE(p_uart_obj[uart_num]->intr_num);
|
||||
uart_disable_rx_intr(uart_num);
|
||||
uart_disable_tx_intr(uart_num);
|
||||
uart_isr_register(uart_num, p_uart_obj[uart_num]->intr_num, NULL, NULL);
|
||||
|
||||
if(p_uart_obj[uart_num]->tx_fifo_sem) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_fifo_sem);
|
||||
p_uart_obj[uart_num]->tx_fifo_sem = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_done_sem) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_done_sem);
|
||||
p_uart_obj[uart_num]->tx_done_sem = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_brk_sem) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_brk_sem);
|
||||
p_uart_obj[uart_num]->tx_brk_sem = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_mutex) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_mutex);
|
||||
p_uart_obj[uart_num]->tx_mutex = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->rx_sem) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->rx_sem);
|
||||
p_uart_obj[uart_num]->rx_sem = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->xQueueUart) {
|
||||
vQueueDelete(p_uart_obj[uart_num]->xQueueUart);
|
||||
p_uart_obj[uart_num]->xQueueUart = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->ring_buffer) {
|
||||
vRingbufferDelete(p_uart_obj[uart_num]->ring_buffer);
|
||||
p_uart_obj[uart_num]->ring_buffer = NULL;
|
||||
}
|
||||
free(p_uart_obj[uart_num]);
|
||||
p_uart_obj[uart_num] = NULL;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t uart_wait_tx_fifo_empty(uart_port_t uart_num, TickType_t ticks_to_wait)
|
||||
esp_err_t uart_wait_tx_done(uart_port_t uart_num, TickType_t ticks_to_wait)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
UART_CHECK((p_uart_obj[uart_num]), "uart driver error");
|
||||
@ -657,17 +549,9 @@ esp_err_t uart_wait_tx_fifo_empty(uart_port_t uart_num, TickType_t ticks_to_wait
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
ticks_to_wait = ticks_end - xTaskGetTickCount();
|
||||
//take 1st tx_done_sem
|
||||
res = xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, (portTickType)ticks_to_wait);
|
||||
if(res == pdFALSE) {
|
||||
ESP_LOGE(UART_TAG, "take uart done sem error, should not get here.\n");
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_done_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_mutex);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, 0);
|
||||
ticks_to_wait = ticks_end - xTaskGetTickCount();
|
||||
if(UART[uart_num]->status.txfifo_cnt == 0) {
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_done_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_mutex);
|
||||
return ESP_OK;
|
||||
}
|
||||
@ -676,11 +560,9 @@ esp_err_t uart_wait_tx_fifo_empty(uart_port_t uart_num, TickType_t ticks_to_wait
|
||||
res = xSemaphoreTake(p_uart_obj[uart_num]->tx_done_sem, (portTickType)ticks_to_wait);
|
||||
if(res == pdFALSE) {
|
||||
uart_disable_intr_mask(uart_num, UART_TX_DONE_INT_ENA_M);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_done_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_mutex);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_done_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_mutex);
|
||||
return ESP_OK;
|
||||
}
|
||||
@ -696,6 +578,20 @@ static esp_err_t uart_set_break(uart_port_t uart_num, int break_num)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//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)
|
||||
{
|
||||
uint8_t i = 0;
|
||||
uint8_t tx_fifo_cnt = UART[uart_num]->status.txfifo_cnt;
|
||||
uint8_t tx_remain_fifo_cnt = (UART_FIFO_LEN - tx_fifo_cnt);
|
||||
uint8_t copy_cnt = (len >= tx_remain_fifo_cnt ? tx_remain_fifo_cnt : len);
|
||||
for(i = 0; i < copy_cnt; i++) {
|
||||
WRITE_PERI_REG(UART_FIFO_AHB_REG(uart_num), buffer[i]);
|
||||
}
|
||||
return copy_cnt;
|
||||
}
|
||||
|
||||
int uart_tx_chars(uart_port_t uart_num, char* buffer, uint32_t len)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
@ -727,7 +623,7 @@ static int uart_tx_all(uart_port_t uart_num, const char* src, size_t size, bool
|
||||
size_t sent = uart_fill_fifo(uart_num, (char*) src, size);
|
||||
if(sent < size) {
|
||||
p_uart_obj[uart_num]->tx_waiting = true;
|
||||
uart_enable_tx_intr(uart_num, 1, DEFAULT_EMPTY_THRESH);
|
||||
uart_enable_tx_intr(uart_num, 1, UART_EMPTY_THRESH_DEFAULT);
|
||||
}
|
||||
size -= sent;
|
||||
src += sent;
|
||||
@ -742,12 +638,55 @@ static int uart_tx_all(uart_port_t uart_num, const char* src, size_t size, bool
|
||||
return original_size;
|
||||
}
|
||||
|
||||
static void uart_tx_task(void* arg)
|
||||
{
|
||||
uart_obj_t* p_uart = (uart_obj_t*) arg;
|
||||
size_t size;
|
||||
uart_event_t evt;
|
||||
for(;;) {
|
||||
char* data = (char*) xRingbufferReceive(p_uart->tx_ring_buf, &size, portMAX_DELAY);
|
||||
if(data == NULL) {
|
||||
continue;
|
||||
}
|
||||
memcpy(&evt, data, sizeof(evt));
|
||||
if(evt.type == UART_DATA) {
|
||||
uart_tx_all(p_uart->uart_num, (const char*) data + sizeof(uart_event_t), evt.data.size, 0, 0);
|
||||
} else if(evt.type == UART_DATA_BREAK) {
|
||||
uart_tx_all(p_uart->uart_num, (const char*) data + sizeof(uart_event_t), evt.data.size, 1, evt.data.brk_len);
|
||||
}
|
||||
vRingbufferReturnItem(p_uart->tx_ring_buf, data);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
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");
|
||||
UART_CHECK((p_uart_obj[uart_num]), "uart driver error");
|
||||
UART_CHECK((p_uart_obj[uart_num] != NULL), "uart driver error");
|
||||
UART_CHECK(src, "buffer null");
|
||||
if(p_uart_obj[uart_num]->tx_buf_size > 0) {
|
||||
if(xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf) > (size + sizeof(uart_event_t))) {
|
||||
uart_event_t *evt = (uart_event_t*) malloc(sizeof(uart_event_t) + size);
|
||||
if(evt == NULL) {
|
||||
ESP_LOGE(UART_TAG, "UART EVT MALLOC ERROR\n");
|
||||
return -1;
|
||||
}
|
||||
xSemaphoreTake(p_uart_obj[uart_num]->tx_buffer_mutex, (portTickType)portMAX_DELAY);
|
||||
evt->type = UART_DATA;
|
||||
evt->data.size = size;
|
||||
memcpy(evt->data.data, src, size);
|
||||
xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void*) evt, sizeof(uart_event_t) + size, portMAX_DELAY);
|
||||
free(evt);
|
||||
evt = NULL;
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_buffer_mutex);
|
||||
return size;
|
||||
} else {
|
||||
ESP_LOGW(UART_TAG, "UART TX BUFFER TOO SMALL[0], SEND DIRECTLY\n");
|
||||
return uart_tx_all(uart_num, src, size, 0, 0);
|
||||
}
|
||||
} else {
|
||||
return uart_tx_all(uart_num, src, size, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
int uart_tx_all_chars_with_break(uart_port_t uart_num, const char* src, size_t size, int brk_len)
|
||||
@ -757,7 +696,29 @@ int uart_tx_all_chars_with_break(uart_port_t uart_num, const char* src, size_t s
|
||||
UART_CHECK((size > 0), "uart size error");
|
||||
UART_CHECK((src), "uart data null");
|
||||
UART_CHECK((brk_len > 0 && brk_len < 256), "break_num error");
|
||||
if(p_uart_obj[uart_num]->tx_buf_size > 0) {
|
||||
if(xRingbufferGetMaxItemSize(p_uart_obj[uart_num]->tx_ring_buf) > (size)) {
|
||||
uart_event_t *evt = (uart_event_t*) malloc(sizeof(uart_event_t) + size);
|
||||
if(evt == NULL) {
|
||||
return -1;
|
||||
}
|
||||
xSemaphoreTake(p_uart_obj[uart_num]->tx_buffer_mutex, (portTickType)portMAX_DELAY);
|
||||
evt->type = UART_DATA_BREAK;
|
||||
evt->data.size = size;
|
||||
evt->data.brk_len = brk_len;
|
||||
memcpy(evt->data.data, src, size);
|
||||
xRingbufferSend(p_uart_obj[uart_num]->tx_ring_buf, (void*) evt, sizeof(uart_event_t) + size, portMAX_DELAY);
|
||||
free(evt);
|
||||
evt = NULL;
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_buffer_mutex);
|
||||
return size;
|
||||
} else {
|
||||
ESP_LOGW(UART_TAG, "UART TX BUFFER TOO SMALL[1], SEND DIRECTLY\n");
|
||||
return uart_tx_all(uart_num, src, size, 1, brk_len);
|
||||
}
|
||||
} else {
|
||||
return uart_tx_all(uart_num, src, size, 1, brk_len);
|
||||
}
|
||||
}
|
||||
|
||||
int uart_read_char(uart_port_t uart_num, TickType_t ticks_to_wait)
|
||||
@ -768,18 +729,18 @@ int uart_read_char(uart_port_t uart_num, TickType_t ticks_to_wait)
|
||||
size_t size;
|
||||
int val;
|
||||
portTickType ticks_end = xTaskGetTickCount() + ticks_to_wait;
|
||||
if(xSemaphoreTake(p_uart_obj[uart_num]->rx_sem,(portTickType)ticks_to_wait) != pdTRUE) {
|
||||
if(xSemaphoreTake(p_uart_obj[uart_num]->rx_mux,(portTickType)ticks_to_wait) != pdTRUE) {
|
||||
return -1;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->cur_remain == 0) {
|
||||
ticks_to_wait = ticks_end - xTaskGetTickCount();
|
||||
data = (uint8_t*) xRingbufferReceive(p_uart_obj[uart_num]->ring_buffer, &size, (portTickType) ticks_to_wait);
|
||||
data = (uint8_t*) xRingbufferReceive(p_uart_obj[uart_num]->rx_ring_buf, &size, (portTickType) ticks_to_wait);
|
||||
if(data) {
|
||||
p_uart_obj[uart_num]->head_ptr = data;
|
||||
p_uart_obj[uart_num]->rd_ptr = data;
|
||||
p_uart_obj[uart_num]->cur_remain = size;
|
||||
} else {
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
@ -787,18 +748,18 @@ int uart_read_char(uart_port_t uart_num, TickType_t ticks_to_wait)
|
||||
p_uart_obj[uart_num]->rd_ptr++;
|
||||
p_uart_obj[uart_num]->cur_remain--;
|
||||
if(p_uart_obj[uart_num]->cur_remain == 0) {
|
||||
vRingbufferReturnItem(p_uart_obj[uart_num]->ring_buffer, p_uart_obj[uart_num]->head_ptr);
|
||||
vRingbufferReturnItem(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->head_ptr);
|
||||
p_uart_obj[uart_num]->head_ptr = NULL;
|
||||
p_uart_obj[uart_num]->rd_ptr = NULL;
|
||||
if(p_uart_obj[uart_num]->buffer_full_flg) {
|
||||
BaseType_t res = xRingbufferSend(p_uart_obj[uart_num]->ring_buffer, p_uart_obj[uart_num]->data_buf, p_uart_obj[uart_num]->data_len, 1);
|
||||
BaseType_t res = xRingbufferSend(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->data_buf, p_uart_obj[uart_num]->data_len, 1);
|
||||
if(res == pdTRUE) {
|
||||
p_uart_obj[uart_num]->buffer_full_flg = false;
|
||||
uart_enable_rx_intr(p_uart_obj[uart_num]->uart_num);
|
||||
}
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
|
||||
return val;
|
||||
}
|
||||
|
||||
@ -807,23 +768,22 @@ int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickTyp
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
UART_CHECK((buf), "uart_num error");
|
||||
UART_CHECK((p_uart_obj[uart_num]), "uart driver error");
|
||||
|
||||
uint8_t* data = NULL;
|
||||
size_t size;
|
||||
size_t copy_len = 0;
|
||||
int len_tmp;
|
||||
if(xSemaphoreTake(p_uart_obj[uart_num]->rx_sem,(portTickType)ticks_to_wait) != pdTRUE) {
|
||||
if(xSemaphoreTake(p_uart_obj[uart_num]->rx_mux,(portTickType)ticks_to_wait) != pdTRUE) {
|
||||
return -1;
|
||||
}
|
||||
while(length) {
|
||||
if(p_uart_obj[uart_num]->cur_remain == 0) {
|
||||
data = (uint8_t*) xRingbufferReceive(p_uart_obj[uart_num]->ring_buffer, &size, (portTickType) ticks_to_wait);
|
||||
data = (uint8_t*) xRingbufferReceive(p_uart_obj[uart_num]->rx_ring_buf, &size, (portTickType) ticks_to_wait);
|
||||
if(data) {
|
||||
p_uart_obj[uart_num]->head_ptr = data;
|
||||
p_uart_obj[uart_num]->rd_ptr = data;
|
||||
p_uart_obj[uart_num]->cur_remain = size;
|
||||
} else {
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
|
||||
return copy_len;
|
||||
}
|
||||
}
|
||||
@ -838,11 +798,11 @@ int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickTyp
|
||||
copy_len += len_tmp;
|
||||
length -= len_tmp;
|
||||
if(p_uart_obj[uart_num]->cur_remain == 0) {
|
||||
vRingbufferReturnItem(p_uart_obj[uart_num]->ring_buffer, p_uart_obj[uart_num]->head_ptr);
|
||||
vRingbufferReturnItem(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->head_ptr);
|
||||
p_uart_obj[uart_num]->head_ptr = NULL;
|
||||
p_uart_obj[uart_num]->rd_ptr = NULL;
|
||||
if(p_uart_obj[uart_num]->buffer_full_flg) {
|
||||
BaseType_t res = xRingbufferSend(p_uart_obj[uart_num]->ring_buffer, p_uart_obj[uart_num]->data_buf, p_uart_obj[uart_num]->data_len, 1);
|
||||
BaseType_t res = xRingbufferSend(p_uart_obj[uart_num]->rx_ring_buf, p_uart_obj[uart_num]->data_buf, p_uart_obj[uart_num]->data_len, 1);
|
||||
if(res == pdTRUE) {
|
||||
p_uart_obj[uart_num]->buffer_full_flg = false;
|
||||
uart_enable_rx_intr(p_uart_obj[uart_num]->uart_num);
|
||||
@ -850,7 +810,7 @@ int uart_read_bytes(uart_port_t uart_num, uint8_t* buf, uint32_t length, TickTyp
|
||||
}
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_sem);
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->rx_mux);
|
||||
return copy_len;
|
||||
}
|
||||
|
||||
@ -858,30 +818,38 @@ esp_err_t uart_flush(uart_port_t uart_num)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
UART_CHECK((p_uart_obj[uart_num]), "uart driver error");
|
||||
|
||||
uart_obj_t* p_uart = p_uart_obj[uart_num];
|
||||
uint8_t* data;
|
||||
size_t size;
|
||||
//rx sem protect the ring buffer read related functions
|
||||
xSemaphoreTake(p_uart->rx_sem, (portTickType)portMAX_DELAY);
|
||||
xSemaphoreTake(p_uart->rx_mux, (portTickType)portMAX_DELAY);
|
||||
while(true) {
|
||||
if(p_uart->head_ptr) {
|
||||
vRingbufferReturnItem(p_uart->ring_buffer, p_uart->head_ptr);
|
||||
vRingbufferReturnItem(p_uart->rx_ring_buf, p_uart->head_ptr);
|
||||
p_uart->rd_ptr = NULL;
|
||||
p_uart->cur_remain = 0;
|
||||
p_uart->head_ptr = NULL;
|
||||
}
|
||||
data = (uint8_t*) xRingbufferReceive(p_uart->ring_buffer, &size, (portTickType) 0);
|
||||
data = (uint8_t*) xRingbufferReceive(p_uart->rx_ring_buf, &size, (portTickType) 0);
|
||||
if(data == NULL) {
|
||||
break;
|
||||
}
|
||||
vRingbufferReturnItem(p_uart->ring_buffer, data);
|
||||
vRingbufferReturnItem(p_uart->rx_ring_buf, data);
|
||||
}
|
||||
p_uart->rd_ptr = NULL;
|
||||
p_uart->cur_remain = 0;
|
||||
p_uart->head_ptr = NULL;
|
||||
xSemaphoreGive(p_uart->rx_sem);
|
||||
uart_wait_tx_fifo_empty(uart_num, portMAX_DELAY);
|
||||
xSemaphoreGive(p_uart->rx_mux);
|
||||
xSemaphoreTake(p_uart->tx_mutex, (portTickType)portMAX_DELAY);
|
||||
do {
|
||||
data = (uint8_t*) xRingbufferReceive(p_uart->tx_ring_buf, &size, (portTickType) 0);
|
||||
if(data == NULL) {
|
||||
break;
|
||||
}
|
||||
vRingbufferReturnItem(p_uart->rx_ring_buf, data);
|
||||
} while(1);
|
||||
xSemaphoreGive(p_uart->tx_mutex);
|
||||
uart_wait_tx_done(uart_num, portMAX_DELAY);
|
||||
uart_reset_fifo(uart_num);
|
||||
return ESP_OK;
|
||||
}
|
||||
@ -915,7 +883,6 @@ esp_err_t uart_set_print_port(uart_port_t uart_num)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
UART_CHECK((p_uart_obj[uart_num]), "UART driver error");
|
||||
|
||||
s_uart_print_nport = uart_num;
|
||||
switch(s_uart_print_nport) {
|
||||
case UART_NUM_0:
|
||||
@ -940,3 +907,127 @@ 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, ringbuf_type_t rx_buf_type)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
UART_CHECK((rx_buffer_size > 0), "uart rx buffer length error\n");
|
||||
if(p_uart_obj[uart_num] == NULL) {
|
||||
ESP_INTR_DISABLE(uart_intr_num);
|
||||
p_uart_obj[uart_num] = (uart_obj_t*) malloc(sizeof(uart_obj_t));
|
||||
if(p_uart_obj[uart_num] == NULL) {
|
||||
ESP_LOGE(UART_TAG, "UART driver malloc error\n");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
p_uart_obj[uart_num]->uart_num = uart_num;
|
||||
p_uart_obj[uart_num]->tx_fifo_sem = xSemaphoreCreateBinary();
|
||||
xSemaphoreGive(p_uart_obj[uart_num]->tx_fifo_sem);
|
||||
p_uart_obj[uart_num]->tx_done_sem = xSemaphoreCreateBinary();
|
||||
p_uart_obj[uart_num]->tx_brk_sem = xSemaphoreCreateBinary();
|
||||
p_uart_obj[uart_num]->tx_mutex = xSemaphoreCreateMutex();
|
||||
p_uart_obj[uart_num]->tx_buffer_mutex = xSemaphoreCreateMutex();
|
||||
p_uart_obj[uart_num]->rx_mux = xSemaphoreCreateMutex();
|
||||
p_uart_obj[uart_num]->intr_num = uart_intr_num;
|
||||
p_uart_obj[uart_num]->queue_size = queue_size;
|
||||
|
||||
if(uart_queue) {
|
||||
p_uart_obj[uart_num]->xQueueUart = xQueueCreate(queue_size, sizeof(uart_event_t));
|
||||
*((QueueHandle_t*) uart_queue) = p_uart_obj[uart_num]->xQueueUart;
|
||||
ESP_LOGI(UART_TAG, "queue free spaces: %d\n", uxQueueSpacesAvailable(p_uart_obj[uart_num]->xQueueUart));
|
||||
} else {
|
||||
p_uart_obj[uart_num]->xQueueUart = NULL;
|
||||
}
|
||||
p_uart_obj[uart_num]->buffer_full_flg = false;
|
||||
p_uart_obj[uart_num]->tx_waiting = false;
|
||||
p_uart_obj[uart_num]->rd_ptr = NULL;
|
||||
p_uart_obj[uart_num]->cur_remain = 0;
|
||||
p_uart_obj[uart_num]->head_ptr = NULL;
|
||||
p_uart_obj[uart_num]->rx_buf_type = rx_buf_type;
|
||||
p_uart_obj[uart_num]->rx_ring_buf = xRingbufferCreate(rx_buffer_size, rx_buf_type);
|
||||
if(tx_buffer_size > 0) {
|
||||
p_uart_obj[uart_num]->tx_ring_buf = xRingbufferCreate(tx_buffer_size, RINGBUF_TYPE_NOSPLIT);
|
||||
p_uart_obj[uart_num]->tx_buf_size = tx_buffer_size;
|
||||
xTaskCreate(uart_tx_task, "uart_tx_task", UART_TX_TASK_DEPTH_DEFAULT, (void*)p_uart_obj[uart_num], UART_TX_TASK_PRIO_DEFAULT, &p_uart_obj[uart_num]->tx_task_handle);
|
||||
|
||||
} else {
|
||||
p_uart_obj[uart_num]->tx_ring_buf = NULL;
|
||||
p_uart_obj[uart_num]->tx_buf_size = 0;
|
||||
p_uart_obj[uart_num]->tx_task_handle = NULL;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGE(UART_TAG, "UART driver already installed\n");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
uart_isr_register(uart_num, uart_intr_num, uart_rx_intr_handler_default, p_uart_obj[uart_num]);
|
||||
uart_intr_config_t uart_intr = {
|
||||
.intr_enable_mask = UART_RXFIFO_FULL_INT_ENA_M
|
||||
| UART_RXFIFO_TOUT_INT_ENA_M
|
||||
| UART_FRM_ERR_INT_ENA_M
|
||||
| UART_RXFIFO_OVF_INT_ENA_M
|
||||
| UART_BRK_DET_INT_ENA_M,
|
||||
.rxfifo_full_thresh = UART_FULL_THRESH_DEFAULT,
|
||||
.rx_timeout_thresh = UART_TOUT_THRESH_DEFAULT,
|
||||
.txfifo_empty_intr_thresh = UART_EMPTY_THRESH_DEFAULT
|
||||
};
|
||||
uart_intr_config(uart_num, &uart_intr);
|
||||
ESP_INTR_ENABLE(uart_intr_num);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
//Make sure no other tasks are still using UART before you call this function
|
||||
esp_err_t uart_driver_delete(uart_port_t uart_num)
|
||||
{
|
||||
UART_CHECK((uart_num < UART_NUM_MAX), "uart_num error");
|
||||
if(p_uart_obj[uart_num] == NULL) {
|
||||
ESP_LOGI(UART_TAG, "ALREADY NULL\n");
|
||||
return ESP_OK;
|
||||
}
|
||||
ESP_INTR_DISABLE(p_uart_obj[uart_num]->intr_num);
|
||||
uart_disable_rx_intr(uart_num);
|
||||
uart_disable_tx_intr(uart_num);
|
||||
uart_isr_register(uart_num, p_uart_obj[uart_num]->intr_num, NULL, NULL);
|
||||
|
||||
if(p_uart_obj[uart_num]->tx_task_handle) {
|
||||
vTaskDelete(p_uart_obj[uart_num]->tx_task_handle);
|
||||
p_uart_obj[uart_num]->tx_task_handle = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_fifo_sem) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_fifo_sem);
|
||||
p_uart_obj[uart_num]->tx_fifo_sem = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_done_sem) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_done_sem);
|
||||
p_uart_obj[uart_num]->tx_done_sem = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_brk_sem) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_brk_sem);
|
||||
p_uart_obj[uart_num]->tx_brk_sem = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_mutex) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_mutex);
|
||||
p_uart_obj[uart_num]->tx_mutex = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_buffer_mutex) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->tx_buffer_mutex);
|
||||
p_uart_obj[uart_num]->tx_buffer_mutex = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->rx_mux) {
|
||||
vSemaphoreDelete(p_uart_obj[uart_num]->rx_mux);
|
||||
p_uart_obj[uart_num]->rx_mux = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->xQueueUart) {
|
||||
vQueueDelete(p_uart_obj[uart_num]->xQueueUart);
|
||||
p_uart_obj[uart_num]->xQueueUart = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->rx_ring_buf) {
|
||||
vRingbufferDelete(p_uart_obj[uart_num]->rx_ring_buf);
|
||||
p_uart_obj[uart_num]->rx_ring_buf = NULL;
|
||||
}
|
||||
if(p_uart_obj[uart_num]->tx_ring_buf) {
|
||||
vRingbufferDelete(p_uart_obj[uart_num]->tx_ring_buf);
|
||||
p_uart_obj[uart_num]->tx_ring_buf = NULL;
|
||||
}
|
||||
|
||||
free(p_uart_obj[uart_num]);
|
||||
p_uart_obj[uart_num] = NULL;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user