调整代码工程结构
This commit is contained in:
parent
1ea3ece768
commit
c308aa688b
@ -19,13 +19,13 @@ typedef struct
|
||||
}stream_data_t;
|
||||
|
||||
|
||||
uint32_t stream_init(uint32_t max_size);
|
||||
void frameStreamWrite(uint8_t *buf, uint32_t length);
|
||||
void frameStreamRead(uint8_t *buf, uint32_t length);
|
||||
uint32_t frameStreamRead_Get_index();
|
||||
uint32_t frameStreamRecLen();//已使用缓存字节数
|
||||
uint32_t frameStreamRecEmptyLen();//剩余空间
|
||||
uint8_t ISframeStreamEmpty();
|
||||
uint32_t stream_init(stream_data_t* esp_data_interface,uint32_t max_size);
|
||||
void frameStreamWrite(stream_data_t* esp_data_interface,uint8_t *buf, uint32_t length);
|
||||
void frameStreamRead(stream_data_t* esp_data_interface,uint8_t *buf, uint32_t length);
|
||||
uint32_t frameStreamRead_Get_index(stream_data_t* esp_data_interface);
|
||||
uint32_t frameStreamRecLen(stream_data_t* esp_data_interface);//已使用缓存字节数
|
||||
uint32_t frameStreamRecEmptyLen(stream_data_t* esp_data_interface);//剩余空间
|
||||
uint8_t ISframeStreamEmpty(stream_data_t* esp_data_interface);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@ -1,89 +1,88 @@
|
||||
#include "stream.h"
|
||||
|
||||
stream_data_t esp_data_interface;
|
||||
SemaphoreHandle_t xSemaphore_frameStream_W;
|
||||
SemaphoreHandle_t xSemaphore_frameStream_R;
|
||||
static SemaphoreHandle_t xSemaphore_frameStream_W;
|
||||
static SemaphoreHandle_t xSemaphore_frameStream_R;
|
||||
|
||||
uint32_t stream_init(uint32_t max_size)
|
||||
uint32_t stream_init(stream_data_t* esp_data_interface,uint32_t max_size)
|
||||
{
|
||||
xSemaphore_frameStream_W = xSemaphoreCreateMutex();// 创建互斥型信号量。
|
||||
xSemaphore_frameStream_R = xSemaphoreCreateMutex();// 创建互斥型信号量。
|
||||
esp_data_interface.max_size = max_size;
|
||||
esp_data_interface.buffer = malloc(max_size);
|
||||
if(esp_data_interface.buffer == NULL)
|
||||
esp_data_interface->max_size = max_size;
|
||||
esp_data_interface->buffer = malloc(max_size);
|
||||
if(esp_data_interface->buffer == NULL)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
esp_data_interface.write = esp_data_interface.read = 0;
|
||||
esp_data_interface->write = esp_data_interface->read = 0;
|
||||
return max_size;
|
||||
}
|
||||
|
||||
void frameStreamWrite(uint8_t *buf, uint32_t length)
|
||||
void frameStreamWrite(stream_data_t* esp_data_interface,uint8_t *buf, uint32_t length)
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t *p;
|
||||
xSemaphoreTake( xSemaphore_frameStream_W, portMAX_DELAY );//获得资源的使用权
|
||||
p = &esp_data_interface.buffer[0];
|
||||
p = &esp_data_interface->buffer[0];
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
*(p + (esp_data_interface.write)) = buf[i];
|
||||
esp_data_interface.write = (esp_data_interface.write + 1) % ((esp_data_interface.max_size));
|
||||
*(p + (esp_data_interface->write)) = buf[i];
|
||||
esp_data_interface->write = (esp_data_interface->write + 1) % ((esp_data_interface->max_size));
|
||||
}
|
||||
xSemaphoreGive( xSemaphore_frameStream_W );//释放
|
||||
}
|
||||
|
||||
void frameStreamRead(uint8_t *buf, uint32_t length)
|
||||
void frameStreamRead(stream_data_t* esp_data_interface,uint8_t *buf, uint32_t length)
|
||||
{
|
||||
uint32_t i;
|
||||
uint8_t *p;
|
||||
xSemaphoreTake( xSemaphore_frameStream_R, portMAX_DELAY );//获得资源的使用权
|
||||
p = &esp_data_interface.buffer[0];
|
||||
p = &esp_data_interface->buffer[0];
|
||||
|
||||
for (i = 0; i < length; i++)
|
||||
{
|
||||
buf[i] = *(p + (esp_data_interface.read));
|
||||
esp_data_interface.read = (esp_data_interface.read + 1) % ((esp_data_interface.max_size));
|
||||
buf[i] = *(p + (esp_data_interface->read));
|
||||
esp_data_interface->read = (esp_data_interface->read + 1) % ((esp_data_interface->max_size));
|
||||
}
|
||||
xSemaphoreGive( xSemaphore_frameStream_R );//释放
|
||||
}
|
||||
|
||||
|
||||
uint32_t frameStreamRead_Get_index()
|
||||
uint32_t frameStreamRead_Get_index(stream_data_t* esp_data_interface)
|
||||
{
|
||||
return esp_data_interface.read;
|
||||
return esp_data_interface->read;
|
||||
}
|
||||
|
||||
|
||||
uint32_t frameStreamRecLen()//已使用缓存字节数
|
||||
uint32_t frameStreamRecLen(stream_data_t* esp_data_interface)//已使用缓存字节数
|
||||
{
|
||||
uint32_t v;
|
||||
|
||||
v = esp_data_interface.write + (esp_data_interface.max_size) - esp_data_interface.read;
|
||||
v = esp_data_interface->write + (esp_data_interface->max_size) - esp_data_interface->read;
|
||||
|
||||
while (v >= (esp_data_interface.max_size))
|
||||
while (v >= (esp_data_interface->max_size))
|
||||
{
|
||||
v -= (esp_data_interface.max_size);
|
||||
v -= (esp_data_interface->max_size);
|
||||
}
|
||||
|
||||
return (v);
|
||||
}
|
||||
|
||||
uint32_t frameStreamRecEmptyLen()//剩余空间
|
||||
uint32_t frameStreamRecEmptyLen(stream_data_t* esp_data_interface)//剩余空间
|
||||
{
|
||||
uint32_t v;
|
||||
|
||||
v = esp_data_interface.read + (esp_data_interface.max_size) - esp_data_interface.write;
|
||||
while (v > (esp_data_interface.max_size))
|
||||
v = esp_data_interface->read + (esp_data_interface->max_size) - esp_data_interface->write;
|
||||
while (v > (esp_data_interface->max_size))
|
||||
{
|
||||
v -= (esp_data_interface.max_size);
|
||||
v -= (esp_data_interface->max_size);
|
||||
}
|
||||
return (v);
|
||||
}
|
||||
|
||||
uint8_t ISframeStreamEmpty()
|
||||
uint8_t ISframeStreamEmpty(stream_data_t* esp_data_interface)
|
||||
{
|
||||
if(esp_data_interface.read == esp_data_interface.write)
|
||||
if(esp_data_interface->read == esp_data_interface->write)
|
||||
{
|
||||
return ESP_OK;
|
||||
}else{
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
|
||||
file(GLOB PORT_SRCS "wifi/*.c" )
|
||||
file(GLOB PORT_SRCS "usb_serial_task.c" "wifi/*.c" )
|
||||
|
||||
file(GLOB FILE_SYS_SRCS "file_system/spiffs.c" )
|
||||
|
||||
idf_component_register(SRCS "main.c"
|
||||
idf_component_register(SRCS "main.c" "usb_serial_task"
|
||||
${PORT_SRCS}
|
||||
${FILE_SYS_SRCS}
|
||||
INCLUDE_DIRS "."
|
||||
|
||||
192
main/main.c
192
main/main.c
@ -35,144 +35,15 @@
|
||||
#include "spiffs.h"
|
||||
|
||||
#include "bsp_flash.h"
|
||||
|
||||
#include "stream.h"
|
||||
#include "esp_efuse.h"
|
||||
|
||||
#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 ECHO_TASK_STACK_SIZE (4096)
|
||||
|
||||
#define RD_BUF_SIZE (BUF_SIZE)
|
||||
static QueueHandle_t uart0_queue;
|
||||
#include "public.h"
|
||||
|
||||
#define WRITE_BLOCK 64
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
|
||||
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), 20 / portTICK_PERIOD_MS);
|
||||
|
||||
// Write data back to the USB SERIAL JTAG
|
||||
if (len) {
|
||||
usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
|
||||
// uart_write_bytes(EX_UART_NUM, (const char *) data, len);
|
||||
data[len] = '\0';
|
||||
ESP_LOG_BUFFER_HEXDUMP("Recv str: ", data, len, ESP_LOG_INFO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
uint8_t* load_html2Ram(wifi_html_t html)
|
||||
{
|
||||
@ -190,6 +61,7 @@ uint8_t* load_html2Ram(wifi_html_t html)
|
||||
}
|
||||
|
||||
extern uint8_t* home_html_data;
|
||||
stream_data_t wifi_interface_data;
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
@ -200,11 +72,13 @@ void app_main(void)
|
||||
err = nvs_flash_init(); // 重新初始化
|
||||
}
|
||||
ESP_ERROR_CHECK(err);
|
||||
// ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
|
||||
// init_spiffs();
|
||||
ESPFlash_Init();
|
||||
stream_init(&wifi_interface_data,1024*10);
|
||||
|
||||
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
||||
gpio_config_t config = {
|
||||
.mode = GPIO_MODE_OUTPUT,
|
||||
@ -214,40 +88,24 @@ void app_main(void)
|
||||
gpio_config(&config);
|
||||
gpio_set_level(8,0);
|
||||
// gpio_set_level(18,1);
|
||||
/* 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(EX_UART_NUM, BUF_SIZE * 2, BUF_SIZE * 2, 20, &uart0_queue, 0);
|
||||
// uart_param_config(EX_UART_NUM, &uart_config);
|
||||
xTaskCreate(echo_task, "USB SERIAL JTAG_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
|
||||
//Create a task to handler UART event from ISR
|
||||
// xTaskCreate(uart_event_task, "uart_event_task", 2048*10, NULL, 12, NULL);
|
||||
usb_serial2Uart(NULL);
|
||||
|
||||
// home_html.html_data = load_html2Ram(home_html);
|
||||
// wifi_config_html.html_data = load_html2Ram(wifi_config_html);
|
||||
// upload_ota_html.html_data = load_html2Ram(upload_ota_html);
|
||||
// // favicon_ico.html_data = load_html2Ram(favicon_ico);
|
||||
// // ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
||||
// // ESP_LOGI(TAG,"def_name:%s",Sys_wifi_config.def_AP_Name);
|
||||
// ESP_LOGI(TAG,"sizeof--Sys_wifi_config:%d",sizeof(wifi_Save_info_t));
|
||||
// Read_ESPFlash(0x40,0,&Sys_wifi_config,sizeof(wifi_Save_info_t));
|
||||
// ESP_LOGI(TAG,"wifi_ssid:%s",Sys_wifi_config.wifi_ssid);
|
||||
// ESP_LOGI(TAG,"wifi_password:%s",Sys_wifi_config.wifi_password);
|
||||
// // if(Sys_wifi_config.wifi_mode != 0x55)
|
||||
// {
|
||||
// wifi_init_softap();
|
||||
// }else{
|
||||
// wifi_init_station();
|
||||
// }
|
||||
home_html.html_data = load_html2Ram(home_html);
|
||||
wifi_config_html.html_data = load_html2Ram(wifi_config_html);
|
||||
upload_ota_html.html_data = load_html2Ram(upload_ota_html);
|
||||
// favicon_ico.html_data = load_html2Ram(favicon_ico);
|
||||
// ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
|
||||
// ESP_LOGI(TAG,"def_name:%s",Sys_wifi_config.def_AP_Name);
|
||||
ESP_LOGI(TAG,"sizeof--Sys_wifi_config:%d",sizeof(wifi_Save_info_t));
|
||||
Read_ESPFlash(0x40,0,&Sys_wifi_config,sizeof(wifi_Save_info_t));
|
||||
ESP_LOGI(TAG,"wifi_ssid:%s",Sys_wifi_config.wifi_ssid);
|
||||
ESP_LOGI(TAG,"wifi_password:%s",Sys_wifi_config.wifi_password);
|
||||
if(Sys_wifi_config.wifi_mode != 0x55)
|
||||
{
|
||||
wifi_init_softap();
|
||||
}else{
|
||||
wifi_init_station();
|
||||
}
|
||||
|
||||
// while (1)
|
||||
// {
|
||||
@ -272,7 +130,7 @@ void app_main(void)
|
||||
// xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL);
|
||||
ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size());
|
||||
// Document_feeder_test();
|
||||
#if 1
|
||||
#if 0
|
||||
|
||||
uint8_t* load_buff = NULL;
|
||||
const loader_esp32_config_t esp32c3_config = {
|
||||
@ -345,5 +203,7 @@ void app_main(void)
|
||||
}else{
|
||||
ESP_LOGE(TAG, "connect_to_target initialization failed.");
|
||||
}
|
||||
|
||||
loader_port_esp32_deinit();
|
||||
#endif
|
||||
}
|
||||
|
||||
42
main/public.h
Normal file
42
main/public.h
Normal file
@ -0,0 +1,42 @@
|
||||
#ifndef PUBLIC_H
|
||||
#define PUBLIC_H
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/usb_serial_jtag.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_check.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "driver/uart.h"
|
||||
#include "freertos/event_groups.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "nvs_flash.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/sys.h"
|
||||
#include <lwip/netdb.h>
|
||||
|
||||
#include "esp32_port.h"
|
||||
#include "esp_loader.h"
|
||||
#include "flasher_common.h"
|
||||
#include "driver/uart.h"
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#include "wifi_task.h"
|
||||
#include "spiffs.h"
|
||||
|
||||
#include "bsp_flash.h"
|
||||
|
||||
#include "esp_efuse.h"
|
||||
|
||||
|
||||
extern void usb_serial2Uart(void *pvParameters);
|
||||
|
||||
#endif
|
||||
160
main/usb_serial_task.c
Normal file
160
main/usb_serial_task.c
Normal file
@ -0,0 +1,160 @@
|
||||
#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 (1024)
|
||||
#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), 20 / portTICK_PERIOD_MS);
|
||||
|
||||
// Write data back to the USB SERIAL JTAG
|
||||
if (len) {
|
||||
usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS);
|
||||
// uart_write_bytes(EX_UART_NUM, (const char *) data, len);
|
||||
data[len] = '\0';
|
||||
ESP_LOG_BUFFER_HEXDUMP("Recv str: ", data, len, ESP_LOG_INFO);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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, 10, NULL);
|
||||
//Create a task to handler UART event from ISR
|
||||
xTaskCreate(uart_event_task, "uart_event_task", 2048*10, NULL, 12, NULL);
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user