179 lines
7.2 KiB
C
179 lines
7.2 KiB
C
#include "public.h"
|
|
#include "stream.h"
|
|
|
|
#define EX_UART_NUM UART_NUM_1
|
|
#define PATTERN_CHR_NUM (3) /*!< Set the number of consecutive and identical characters received by receiver which defines a UART pattern*/
|
|
|
|
#define BUF_SIZE (4096)
|
|
#define ECHO_TASK_STACK_SIZE (4096)
|
|
|
|
#define RD_BUF_SIZE (BUF_SIZE)
|
|
static QueueHandle_t uart0_queue;
|
|
|
|
static const char *TAG = "usb_serial";
|
|
|
|
static void echo_task(void *arg)
|
|
{
|
|
// Configure USB SERIAL JTAG
|
|
usb_serial_jtag_driver_config_t usb_serial_jtag_config = {
|
|
.rx_buffer_size = BUF_SIZE,
|
|
.tx_buffer_size = BUF_SIZE,
|
|
};
|
|
|
|
ESP_ERROR_CHECK(usb_serial_jtag_driver_install(&usb_serial_jtag_config));
|
|
ESP_LOGI("usb_serial_jtag echo", "USB_SERIAL_JTAG init done");
|
|
|
|
// Configure a temporary buffer for the incoming data
|
|
uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
|
|
if (data == NULL) {
|
|
ESP_LOGE("usb_serial_jtag echo", "no memory for data");
|
|
return;
|
|
}
|
|
|
|
while (1) {
|
|
|
|
int len = usb_serial_jtag_read_bytes(data, (BUF_SIZE - 1), 200 / portTICK_PERIOD_MS);
|
|
|
|
// Write data back to the USB SERIAL JTAG
|
|
if (len) {
|
|
if(frameStreamRecEmptyLen(&uart_interface_data) < 1024*4)
|
|
{
|
|
// ESP_LOGE(TAG,"frameStream Be almost full:%d",len);
|
|
frameStreamWrite(&uart_interface_data,&data[0],len);
|
|
while (1)
|
|
{
|
|
vTaskDelay(10/ portTICK_PERIOD_MS);
|
|
len = usb_serial_jtag_read_bytes(data, 0, 200 / portTICK_PERIOD_MS);//Ë¢ÐÂÁ÷¿Ø
|
|
if(frameStreamRecEmptyLen(&uart_interface_data) > 1024*6)
|
|
{
|
|
// ESP_LOGE(TAG,"frameStream: %ld",frameStreamRecEmptyLen(&uart_interface_data));
|
|
break;
|
|
}
|
|
}
|
|
}else{
|
|
frameStreamWrite(&uart_interface_data,&data[0],len);
|
|
}
|
|
|
|
// uart_write_bytes(EX_UART_NUM, (const char *) data, len);
|
|
// data[len] = '\0';
|
|
// ESP_LOG_BUFFER_HEXDUMP("Recv str: ", data, 8, ESP_LOG_INFO);
|
|
}else{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
static void uart_event_task(void *pvParameters)
|
|
{
|
|
uart_event_t event;
|
|
size_t buffered_size;
|
|
uint8_t* dtmp = (uint8_t*) malloc(RD_BUF_SIZE);
|
|
for (;;) {
|
|
//Waiting for UART event.
|
|
if (xQueueReceive(uart0_queue, (void *)&event, (TickType_t)portMAX_DELAY)) {
|
|
bzero(dtmp, RD_BUF_SIZE);
|
|
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:
|
|
uart_read_bytes(EX_UART_NUM, dtmp, event.size, portMAX_DELAY);
|
|
ESP_LOGI(TAG, "read_bytes:%d",event.size);
|
|
// uart_write_bytes(EX_UART_NUM, (const char*) dtmp, event.size);
|
|
usb_serial_jtag_write_bytes((const char*) dtmp, event.size, 20 / portTICK_PERIOD_MS);
|
|
// int to_write = event.size;
|
|
// while (to_write > 0) {
|
|
// int written = send(sock, dtmp + (event.size - to_write), to_write, 0);
|
|
// if (written < 0) {
|
|
// ESP_LOGE(TAG, "Error occurred during sending: errno %d", errno);
|
|
// // Failed to retransmit, giving up
|
|
// return;
|
|
// }
|
|
// to_write -= written;
|
|
// }
|
|
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 increasing 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;
|
|
}
|
|
}
|
|
}
|
|
free(dtmp);
|
|
dtmp = NULL;
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
|
|
stream_data_t usb_interface_data;
|
|
void usb_serial2Uart(void *pvParameters)
|
|
{
|
|
stream_init(&usb_interface_data,1024*20);
|
|
/* Configure parameters of an UART driver,
|
|
* communication pins and install the driver */
|
|
uart_config_t uart_config = {
|
|
.baud_rate = 115200,
|
|
.data_bits = UART_DATA_8_BITS,
|
|
.parity = UART_PARITY_DISABLE,
|
|
.stop_bits = UART_STOP_BITS_1,
|
|
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
|
.source_clk = UART_SCLK_DEFAULT,
|
|
};
|
|
ESP_ERROR_CHECK(uart_set_pin(UART_NUM_1, 4, 5, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
|
|
//Install UART driver, and get the queue.
|
|
uart_driver_install(UART_NUM_1, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);
|
|
uart_param_config(UART_NUM_1, &uart_config);
|
|
xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 20, NULL);
|
|
//Create a task to handler UART event from ISR
|
|
xTaskCreate(uart_event_task, "uart_event_task", 2048*10, NULL, 12, NULL);
|
|
|
|
} |