/* * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ #include #include #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 #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" #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; 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) { uint8_t* buff; if(html.html_data != NULL) { return html.html_data; }else{ buff = malloc(html.html_size + 10); Read_ESPFlash(0x41,html.html_addr,&buff[0],html.html_size); // ESP_LOGI(TAG,"html:%s",buff); return buff; } return NULL; } extern uint8_t* home_html_data; void app_main(void) { esp_err_t err = nvs_flash_init(); if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); // 擦除 NVS 分区 err = nvs_flash_init(); // 重新初始化 } ESP_ERROR_CHECK(err); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); init_spiffs(); ESPFlash_Init(); ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size()); gpio_config_t config = { .mode = GPIO_MODE_OUTPUT, .pin_bit_mask = 1ULL << 8 | 1ULL << 7, .pull_up_en = 1 }; gpio_config(&config); gpio_set_level(8,0); gpio_set_level(7,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); home_html.html_data = load_html2Ram(home_html); wifi_config_html.html_data = load_html2Ram(wifi_config_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(); } vTaskDelay(1000/ portTICK_PERIOD_MS); start_web_server(); // xTaskCreate(tcp_server_task, "tcp_server", 4096, (void*)AF_INET, 5, NULL); ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size()); #if 0 const loader_esp32_config_t config = { .baud_rate = 115200, .uart_port = UART_NUM_1, .uart_rx_pin = GPIO_NUM_5, .uart_tx_pin = GPIO_NUM_4, .reset_trigger_pin = GPIO_NUM_2, .gpio0_trigger_pin = GPIO_NUM_1, }; if (loader_port_esp32_init(&config) != ESP_LOADER_SUCCESS) { ESP_LOGE(TAG, "serial initialization failed."); return; } if (connect_to_target(230400) == ESP_LOADER_SUCCESS) { ESP_LOGI(TAG, "connect_to_target initialization success."); }else{ ESP_LOGE(TAG, "connect_to_target initialization failed."); } #endif }