-
-
Save mzimmers/9ed4eafbfe14d6ce275f7017dba748d3 to your computer and use it in GitHub Desktop.
esp32 uart handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
UartMgr::UartMgr() | |
{ | |
esp_err_t err; | |
// load the configuration. | |
err = uart_param_config(CD_UART_NBR, &m_uartConfig); | |
//Set UART pins. | |
err = uart_set_pin(CD_UART_NBR, | |
CD_UART_TX_PIN, // tx | |
CD_UART_RX_PIN, // rx | |
UART_PIN_NO_CHANGE, // rts | |
UART_PIN_NO_CHANGE); //cts | |
//Install UART driver, and get the queue. | |
err = uart_driver_install(CD_UART_NBR, BUF_SIZE, BUF_SIZE, 20, &m_uartQueue, 0); | |
} | |
void UartMgr::uartEventTask(TaskParams *params) | |
{ | |
uart_event_t event; | |
size_t bufSize; | |
int pos; | |
size_t nbrBytesRead = 0; | |
int nbrBytesWritten = 0; | |
m_params = params; | |
uint8_t ioBuff[BUF_SIZE]; | |
char logBuff[BUF_SIZE]; | |
Message msg(m_params); | |
string s1; | |
for (;;) | |
{ | |
// Waiting for UART event. | |
if (xQueueReceive(m_uartQueue, (void *) &event, (portTickType)portMAX_DELAY)) | |
{ | |
//bzero(ioBuff, BUF_SIZE); | |
ESP_LOGI(TAG, "uart[%d] event:", CD_UART_NBR); | |
switch(event.type) | |
{ | |
// Event of UART receving data; need to handle fast. | |
case UART_DATA: | |
// ESP_LOGI(TAG, "[UART DATA]: %d", event.size); | |
uart_get_buffered_data_len(CD_UART_NBR, &bufSize); | |
// ESP_LOGI(TAG, "%d bytes in SIO input buffer.", bufSize); | |
nbrBytesRead = (size_t) uart_read_bytes(CD_UART_NBR, ioBuff, bufSize, portMAX_DELAY); | |
ESP_LOGI(TAG, "%d bytes read in from SIO input buffer.", nbrBytesRead); | |
ESP_LOG_BUFFER_HEXDUMP(TAG, ioBuff, bufSize, ESP_LOG_INFO); | |
uart_get_buffered_data_len(CD_UART_NBR, &bufSize); | |
nbrBytesWritten = uart_write_bytes(CD_UART_NBR, "ACK", 3); | |
// ESP_LOGI(TAG, "nbrBytesRead = %d. nbrBytesWritten = %d.", nbrBytesRead, nbrBytesWritten); | |
// ioBuff[nbrBytesRead] = '\0'; | |
// snprintf(logBuff, BUF_SIZE, "Received %d bytes %s via SIO.", nbrBytesRead, ioBuff); | |
// s1 = logBuff; | |
// msg.log(TAG, s1); | |
break; | |
// Event of HW FIFO overflow detected | |
case UART_FIFO_OVF: | |
ESP_LOGI(TAG, "hw fifo overflow"); | |
// If fifo overflow happened, you should consider adding flow control for your application. | |
// The ISR has already reset the rx FIFO, | |
// As an example, we directly flush the rx buffer here in order to read more data. | |
uart_flush_input(CD_UART_NBR); | |
xQueueReset(m_uartQueue); | |
break; | |
// Event of UART ring buffer full | |
case UART_BUFFER_FULL: | |
ESP_LOGI(TAG, "ring buffer full"); | |
// If buffer full happened, you should consider encreasing your buffer size | |
// As an example, we directly flush the rx buffer here in order to read more data. | |
uart_flush_input(CD_UART_NBR); | |
xQueueReset(m_uartQueue); | |
break; | |
// Event of UART RX break detected | |
case UART_BREAK: | |
ESP_LOGI(TAG, "uart rx break"); | |
break; | |
// Event of UART parity check error | |
case UART_PARITY_ERR: | |
ESP_LOGE(TAG, "uart parity error"); | |
break; | |
// Event of UART frame error | |
case UART_FRAME_ERR: | |
ESP_LOGE(TAG, "uart frame error"); | |
break; | |
// UART_PATTERN_DET | |
case UART_PATTERN_DET: | |
uart_get_buffered_data_len(CD_UART_NBR, &bufSize); | |
pos = uart_pattern_pop_pos(CD_UART_NBR); | |
ESP_LOGI(TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", pos, bufSize); | |
if (pos == -1) | |
{ | |
// There used to be a UART_PATTERN_DET event, but the pattern position queue is full so that it can not | |
// record the position. We should set a larger queue size. | |
// As an example, we directly flush the rx buffer here. | |
uart_flush_input(CD_UART_NBR); | |
} | |
else | |
{ | |
uart_read_bytes(CD_UART_NBR, ioBuff, (unsigned) pos, 100 / portTICK_PERIOD_MS); | |
uint8_t pat[PATTERN_CHR_NUM + 1]; | |
memset(pat, 0, sizeof(pat)); | |
uart_read_bytes(CD_UART_NBR, pat, PATTERN_CHR_NUM, 100 / portTICK_PERIOD_MS); | |
ESP_LOGI(TAG, "read data: %s", ioBuff); | |
ESP_LOGI(TAG, "read pat : %s", pat); | |
} | |
break; | |
// Others | |
default: | |
ESP_LOGI(TAG, "uart event type: %d", event.type); | |
break; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define CD_UART_NBR UART_NUM_1 | |
#define CD_UART_TX_PIN (10) | |
#define CD_UART_RX_PIN (9) | |
#define PATTERN_CHR_NUM (33) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/ | |
#define BUF_SIZE (512) | |
#define RD_BUF_SIZE (BUF_SIZE) | |
extern "C" | |
{ | |
void uartTaskWrapper(void *data) __attribute__ ((noreturn)); | |
class UartMgr | |
{ | |
private: | |
QueueHandle_t m_uartQueue = nullptr; | |
TaskParams *m_params; | |
const uart_config_t m_uartConfig = | |
{ | |
115200, // uart_config.baud_rate | |
UART_DATA_8_BITS, // uart_config.data_bits | |
UART_PARITY_DISABLE, // uart_config.parity | |
UART_STOP_BITS_1, // uart_config.stop_bits | |
UART_HW_FLOWCTRL_DISABLE, // uart_config.flow_ctrl | |
122, // uart_config.rx_flow_ctrl_thresh | |
true // uart_config.use_ref_tick | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment