driver(uart):merge branch into v3.0 which fixed three bug related with uart

1. uart fifo reset
    2. uart pattern interrupt
    3. uart buffered_len error.
This commit is contained in:
kooho
2018-01-24 21:27:12 +08:00
parent 2a55629556
commit 3a6be05945
5 changed files with 395 additions and 130 deletions

View File

@@ -7,6 +7,7 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
@@ -29,58 +30,85 @@ static const char *TAG = "uart_events";
*/
#define EX_UART_NUM UART_NUM_0
#define PATTERN_CHR_NUM (3) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/
#define BUF_SIZE (1024)
#define RD_BUF_SIZE (BUF_SIZE)
static QueueHandle_t uart0_queue;
static void uart_event_task(void *pvParameters)
{
uart_event_t event;
size_t buffered_size;
uint8_t *dtmp = (uint8_t *) malloc(BUF_SIZE);
while (1) {
/* Waiting for UART event.
If it happens then print out information what is it */
if (xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
for(;;) {
//Waiting for UART event.
if(xQueueReceive(uart0_queue, (void * )&event, (portTickType)portMAX_DELAY)) {
bzero(dtmp, RD_BUF_SIZE);
ESP_LOGI(TAG, "uart[%d] event:", EX_UART_NUM);
switch (event.type) {
case UART_DATA:
/* Event of UART receiving data
* We'd better handler data event fast, there would be much more data events
* than other types of events.
* If we take too much time on data event, the queue might be full.
* In this example, we don't process data in event, but read data outside.
*/
uart_get_buffered_data_len(EX_UART_NUM, &buffered_size);
ESP_LOGI(TAG, "data, len: %d; buffered len: %d", event.size, buffered_size);
break;
case UART_FIFO_OVF:
ESP_LOGE(TAG, "hw fifo overflow");
// If fifo overflow happened, you should consider adding flow control for your application.
// We can read data out out the buffer, or directly flush the Rx buffer.
uart_flush(EX_UART_NUM);
break;
case UART_BUFFER_FULL:
ESP_LOGE(TAG, "ring buffer full");
// If buffer full happened, you should consider increasing your buffer size
// We can read data out out the buffer, or directly flush the Rx buffer.
uart_flush(EX_UART_NUM);
break;
case UART_BREAK:
ESP_LOGI(TAG, "uart rx break detected");
break;
case UART_PARITY_ERR:
ESP_LOGE(TAG, "uart parity error");
break;
case UART_FRAME_ERR:
ESP_LOGE(TAG, "uart frame error");
break;
case UART_PATTERN_DET:
ESP_LOGI(TAG, "uart pattern detected");
break;
default:
ESP_LOGE(TAG, "not serviced uart event type: %d\n", event.type);
break;
switch(event.type) {
//Event of UART receving data
/*We'd better handler data event fast, there would be much more data events than
other types of events. If we take too much time on data event, the queue might
be full.*/
case UART_DATA:
ESP_LOGI(TAG, "[UART DATA]: %d", event.size);
uart_read_bytes(EX_UART_NUM, dtmp, event.size, portMAX_DELAY);
ESP_LOGI(TAG, "[DATA EVT]:");
uart_write_bytes(EX_UART_NUM, (const char*) dtmp, event.size);
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(EX_UART_NUM);
xQueueReset(uart0_queue);
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(EX_UART_NUM);
xQueueReset(uart0_queue);
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_LOGI(TAG, "uart parity error");
break;
//Event of UART frame error
case UART_FRAME_ERR:
ESP_LOGI(TAG, "uart frame error");
break;
//UART_PATTERN_DET
case UART_PATTERN_DET:
uart_get_buffered_data_len(EX_UART_NUM, &buffered_size);
int pos = uart_pattern_pop_pos(EX_UART_NUM);
ESP_LOGI(TAG, "[UART PATTERN DETECTED] pos: %d, buffered size: %d", pos, buffered_size);
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(EX_UART_NUM);
} else {
uart_read_bytes(EX_UART_NUM, dtmp, pos, 100 / portTICK_PERIOD_MS);
uint8_t pat[PATTERN_CHR_NUM + 1];
memset(pat, 0, sizeof(pat));
uart_read_bytes(EX_UART_NUM, pat, PATTERN_CHR_NUM, 100 / portTICK_PERIOD_MS);
ESP_LOGI(TAG, "read data: %s", dtmp);
ESP_LOGI(TAG, "read pat : %s", pat);
}
break;
//Others
default:
ESP_LOGI(TAG, "uart event type: %d", event.type);
break;
}
}
}
@@ -103,23 +131,19 @@ void app_main()
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(EX_UART_NUM, &uart_config);
// Set UART pins using UART0 default pins i.e. no changes
//Set UART log level
esp_log_level_set(TAG, ESP_LOG_INFO);
//Set UART pins (using UART0 default pins ie no changes.)
uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 10, &uart0_queue, 0);
//Install UART driver, and get the queue.
uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);
// Set uart pattern detection function
uart_enable_pattern_det_intr(EX_UART_NUM, '+', 3, 10000, 10, 10);
//Set uart pattern detect function.
uart_enable_pattern_det_intr(EX_UART_NUM, '+', PATTERN_CHR_NUM, 10000, 10, 10);
//Reset the pattern queue length to record at most 20 pattern positions.
uart_pattern_queue_reset(EX_UART_NUM, 20);
// Create a task to handle uart event from ISR
//Create a task to handler UART event from ISR
xTaskCreate(uart_event_task, "uart_event_task", 2048, NULL, 12, NULL);
// Reserve a buffer and process incoming data
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
while (1) {
int len = uart_read_bytes(EX_UART_NUM, data, BUF_SIZE, 100 / portTICK_RATE_MS);
if (len > 0) {
ESP_LOGI(TAG, "uart read : %d", len);
uart_write_bytes(EX_UART_NUM, (const char *)data, len);
}
}
}