255 lines
9.4 KiB
C
255 lines
9.4 KiB
C
#include "public.h"
|
|
#include "stream.h"
|
|
#include "tinyusb.h"
|
|
#include "tusb_cdc_acm.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 uint8_t buf[CONFIG_TINYUSB_CDC_RX_BUFSIZE + 1];
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
void tinyusb_cdc_rx_callback(int itf, cdcacm_event_t *event)
|
|
{
|
|
/* initialization */
|
|
size_t rx_size = 0;
|
|
|
|
/* read */
|
|
esp_err_t ret = tinyusb_cdcacm_read(itf, buf, CONFIG_TINYUSB_CDC_RX_BUFSIZE, &rx_size);
|
|
if (ret == ESP_OK) {
|
|
ESP_LOGI(TAG, "Data from channel %d:", itf);
|
|
ESP_LOG_BUFFER_HEXDUMP(TAG, buf, rx_size, ESP_LOG_INFO);
|
|
} else {
|
|
ESP_LOGE(TAG, "Read error");
|
|
}
|
|
|
|
/* write back */
|
|
tinyusb_cdcacm_write_queue(itf, buf, rx_size);
|
|
tinyusb_cdcacm_write_flush(itf, 0);
|
|
}
|
|
|
|
void tinyusb_cdc_line_state_changed_callback(int itf, cdcacm_event_t *event)
|
|
{
|
|
int dtr = event->line_state_changed_data.dtr;
|
|
int rts = event->line_state_changed_data.rts;
|
|
ESP_LOGI(TAG, "Line state changed on channel %d: DTR:%d, RTS:%d", itf, dtr, rts);
|
|
}
|
|
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
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{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
#endif
|
|
|
|
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;
|
|
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32S2
|
|
void usb_serial_task(void *pvParameters)
|
|
{
|
|
ESP_LOGI(TAG, "USB initialization");
|
|
const tinyusb_config_t tusb_cfg = {
|
|
.device_descriptor = NULL,
|
|
.string_descriptor = NULL,
|
|
.external_phy = false,
|
|
.configuration_descriptor = NULL,
|
|
};
|
|
|
|
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
|
|
|
|
tinyusb_config_cdcacm_t acm_cfg = {
|
|
.usb_dev = TINYUSB_USBDEV_0,
|
|
.cdc_port = TINYUSB_CDC_ACM_0,
|
|
.rx_unread_buf_sz = 64,
|
|
.callback_rx = &tinyusb_cdc_rx_callback, // the first way to register a callback
|
|
.callback_rx_wanted_char = NULL,
|
|
.callback_line_state_changed = NULL,
|
|
.callback_line_coding_changed = NULL
|
|
};
|
|
|
|
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
|
|
/* the second way to register a callback */
|
|
ESP_ERROR_CHECK(tinyusb_cdcacm_register_callback(
|
|
TINYUSB_CDC_ACM_0,
|
|
CDC_EVENT_LINE_STATE_CHANGED,
|
|
&tinyusb_cdc_line_state_changed_callback));
|
|
ESP_LOGI(TAG, "USB initialization DONE");
|
|
|
|
vTaskDelete(NULL);
|
|
}
|
|
|
|
#endif
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32C3
|
|
void usb_serial2Uart(void *pvParameters)
|
|
{
|
|
// stream_init(&usb_interface_data,1024*20);
|
|
/* Configure parameters of an UART driver,
|
|
* communication pins and install the driver */
|
|
|
|
#if 0
|
|
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);
|
|
//Create a task to handler UART event from ISR
|
|
xTaskCreate(uart_event_task, "uart_event_task", 2048*10, NULL, 12, NULL);
|
|
#endif
|
|
xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 20, NULL);
|
|
}
|
|
#endif |