Re: DMA example for UART communication for ESP32
Posted: Sat Feb 17, 2018 6:36 pm
If the baud is 115200 then the tout interrupt will come approx 90us after end of frame.
Hi,WiFive wrote:If the baud is 115200 then the tout interrupt will come approx 90us after end of frame.
Hi,WiFive wrote:If you are using uart_read_bytes with nonzero wait time the task should unblock when tout interrupt pushes bytes to ringbuffer
Code: Select all
uart_config_t uart_config = {
.baud_rate = baud_rate,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = hw_flow,
.rx_flow_ctrl_thresh = 122,
};
vTaskDelay(10 / portTICK_RATE_MS);
uart_status = uart_param_config(uart_num, &uart_config);
if(uart_status == ESP_OK)
{
DEBUG_LOGI(UART,TAG1,"UART %d configured successfully...",uart_num);
if(hw_flow==GW_UART_HW_FLOWCTRL_DISABLE)
{
uart_status=uart_set_pin(uart_num,tx ,rx,UART_PIN_NO_CHANGE,UART_PIN_NO_CHANGE );
}
else if(hw_flow==GW_UART_HW_FLOWCTRL_RTS)
{
uart_status=uart_set_pin(uart_num,tx ,rx,rts,UART_PIN_NO_CHANGE );
}
else if(hw_flow==GW_UART_HW_FLOWCTRL_CTS)
{
uart_status=uart_set_pin(uart_num,tx ,rx,UART_PIN_NO_CHANGE,cts);
}
else
{
uart_status=uart_set_pin(uart_num,tx ,rx,rts,cts);
}
}
else
{
DEBUG_LOGI(UART,TAG1, "ERROR : UART%d failed to configure...",uart_num);
return false;
}
if(uart_status==ESP_OK)
{
uart_status=uart_driver_install(uart_num, 1024 * 2, 1024*4, 128, &uart_queue[uart_num], 0);
uart_flush(uart_num);
}
else
{
DEBUG_LOGE(UART,TAG1,"UART%d set pin error",uart_num);
return false;
}
Code: Select all
xTaskCreate(uart0_task, "uart_0", UART_TASK_SIZE, (void*)uart_num, UART_TASK_PRIO, &uart_handler[GW_UART0]);
Code: Select all
void uart0_task(void *arg)
{
int uart_num = (int)arg;
uart_event_t event;
//int len;
uart_data[uart_num]=(uint8_t*)malloc(PACK_SIZE);
if(uart_data[uart_num] == NULL)
{
DEBUG_LOGE(UART,TAG1,"Memory allocation failed");
vTaskDelete(NULL);
}
uart_ring_buf[uart_num]=(uint8_t*)malloc(RING_BUFF_SIZE);
if(uart_ring_buf[uart_num]==NULL)
{
free(uart_data[uart_num]);
DEBUG_LOGE(UART,TAG1,"Memory allocation failed");
vTaskDelete(NULL);
}
if(rb_init(&rb_uart[uart_num],uart_ring_buf[uart_num],RING_BUFF_SIZE,1)==-1)
{
DEBUG_LOGE(UART,TAG1,"Ring buffer init failed");
free(uart_data[uart_num]);
free(uart_ring_buf[uart_num]);
vTaskDelete(NULL);
}
DEBUG_LOGI(UART,TAG1,"***UART0 Task Started***");
for(;;) {
#ifdef CONFIG_ESP32
//Waiting for UART0 event.
if(xQueueReceive(uart_queue[0], (void * )&event, 100)) {
int len; // Not used in the code
// DEBUG_LOGI(UART,TAG1, "uart[%d] event:", uart_num);
switch(event.type) {
//Event of UART0 receving data
case UART_DATA:
memset(uart_data[uart_num],0,PACK_SIZE);
len = uart_read_bytes(uart_num, uart_data[uart_num], event.size, 10);
DEBUG_LOGD(UART,TAG1,"UART0 RECEIVED DATA LENGTH:%d, DATA:'%s'",event.size,uart_data[uart_num]);
rb_write(&rb_uart[uart_num],uart_data[uart_num],len);
break;
//Event of HW FIFO overflow detected
case UART_FIFO_OVF:
DEBUG_LOGE(UART,TAG1, " uart_0 hw fifo overflow\n");
break;
//Event of UART ring buffer full
case UART_BUFFER_FULL:
DEBUG_LOGE(UART,TAG1, " uart_0 ring buffer full\n");
break;
//Event of UART RX break detected
case UART_BREAK:
DEBUG_LOGE(UART,TAG1, "uart_0 rx break\n");
break;
//Event of UART parity check error
case UART_PARITY_ERR:
DEBUG_LOGE(UART,TAG1, "uart_0 parity error\n");
break;
//Event of UART frame error
case UART_FRAME_ERR:
DEBUG_LOGE(UART,TAG1, "uart_0 frame error\n");
break;
//Others
default:
DEBUG_LOGI(UART,TAG1, "uart_0 event type: %d\n", event.type);
break;
}
}
#endif
#ifdef CONFIG_NUVOTON
vTaskDelay(100 / portTICK_RATE_MS);
#endif
}
free(uart_data[uart_num]);
free(uart_ring_buf[uart_num]);
vTaskDelete(NULL);
}
HiWiFive wrote:Yes tout interrupt generates UART_DATA event. Other than changing the wait timeouts not really. But you may want to ask do you need to use event queue and double buffered data or can you just do like this https://github.com/espressif/esp-idf/tr ... _rxtxtasks
Hello,