From 663827d6a21b35d907b91472c0d6f68ba5ff04c8 Mon Sep 17 00:00:00 2001 From: Yu-886 <2860280851@qq.com> Date: Fri, 21 Mar 2025 14:29:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=84=B1=E6=9C=BA=E7=83=A7?= =?UTF-8?q?=E5=BD=95=E5=99=A8=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/main.c | 120 +++++++++++++++++++++++++++++++++------------- tools/esptools.py | 28 ++++++++++- 2 files changed, 114 insertions(+), 34 deletions(-) diff --git a/main/main.c b/main/main.c index e762aa9..120de07 100755 --- a/main/main.c +++ b/main/main.c @@ -72,7 +72,7 @@ static void echo_task(void *arg) // Write data back to the USB SERIAL JTAG if (len) { - // usb_serial_jtag_write_bytes((const char *) data, len, 20 / portTICK_PERIOD_MS); + 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); @@ -196,10 +196,10 @@ 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(); + // init_spiffs(); ESPFlash_Init(); ESP_LOGI(TAG, "mem free:%ld",esp_get_free_heap_size()); gpio_config_t config = { @@ -209,7 +209,7 @@ void app_main(void) }; gpio_config(&config); gpio_set_level(8,0); - gpio_set_level(7,1); + // gpio_set_level(18,1); /* Configure parameters of an UART driver, * communication pins and install the driver */ uart_config_t uart_config = { @@ -228,47 +228,103 @@ void app_main(void) //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); - 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) + // { + // vTaskDelay(1000/ portTICK_PERIOD_MS); + // } - vTaskDelay(1000/ portTICK_PERIOD_MS); - start_web_server(); + // 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()); + // Document_feeder_test(); + #if 1 - #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, + uint8_t* load_buff = NULL; + const loader_esp32_config_t esp32c3_config = { + .baud_rate = 1152000, + .uart_port = UART_NUM_0, + .uart_rx_pin = GPIO_NUM_20, + .uart_tx_pin = GPIO_NUM_21, + .reset_trigger_pin = GPIO_NUM_9, + .gpio0_trigger_pin = GPIO_NUM_2, }; - if (loader_port_esp32_init(&config) != ESP_LOADER_SUCCESS) { + if (loader_port_esp32_init(&esp32c3_config) != ESP_LOADER_SUCCESS) { ESP_LOGE(TAG, "serial initialization failed."); return; } - if (connect_to_target(230400) == ESP_LOADER_SUCCESS) { + if (connect_to_target(1152000) == ESP_LOADER_SUCCESS) { ESP_LOGI(TAG, "connect_to_target initialization success."); + if(esp_get_free_heap_size() >200*1024) + { + load_buff = malloc(WRITE_BLOCK*1024+24); + if(load_buff == NULL) + { + return; + } + } + + if(esp_loader_get_target() == ESP32S3_CHIP) + { + ESP_LOGI(TAG,"Find ESP32S3"); + ESP_LOGI(TAG, "Loading bin..."); + // flash_binary() + uint32_t bin_size = 749504; + uint32_t write_addr = 0; + while (bin_size) + { + if(bin_size > WRITE_BLOCK*1024) + { + if(Read_ESPFlash(0x45,write_addr,&load_buff[0],WRITE_BLOCK*1024) == WRITE_BLOCK*1024) + { + flash_binary(&load_buff[0],WRITE_BLOCK*1024,write_addr); + write_addr+=WRITE_BLOCK*1024; + bin_size -= WRITE_BLOCK*1024; + ESP_LOGI(TAG,"Load : %ld --- %ld",write_addr,bin_size); + } + + }else{ + Read_ESPFlash(0x45,write_addr,&load_buff[0],bin_size); + flash_binary(&load_buff[0],bin_size,write_addr); + write_addr += bin_size; + bin_size = 0; + ESP_LOGI(TAG,"Load : %ld --- %ld",write_addr,bin_size); + break; + } + } + ESP_LOGI(TAG, "Loading bin success"); + + ESP_LOGI(TAG, "Done!"); + esp_loader_reset_target(); + + // Delay for skipping the boot message of the targets + vTaskDelay(500 / portTICK_PERIOD_MS); + + // Forward slave's serial output + ESP_LOGI(TAG, "********************************************"); + ESP_LOGI(TAG, "*** Logs below are print from slave .... ***"); + ESP_LOGI(TAG, "********************************************"); + } }else{ ESP_LOGE(TAG, "connect_to_target initialization failed."); } diff --git a/tools/esptools.py b/tools/esptools.py index 1d3cd7a..e555dd8 100644 --- a/tools/esptools.py +++ b/tools/esptools.py @@ -55,6 +55,30 @@ def write_flash(directory, base_address=0x2D1000, port="COM11", baud=460800): print("Flashing command:", flash_cmd) esptool.main(flash_cmd) +def flash_file(chip, port, baud, address, file_path): + """使用 esptool.main 烧录指定文件到 ESP32""" + if not os.path.exists(file_path): + print(f"文件未找到: {file_path}") + return + + flash_cmd = [ + "--chip", "esp32c3", + "--port", port, + "--baud", str(baud), + "write_flash", "--force", "-z", hex(address), file_path + ] + + print("正在烧录,命令:", flash_cmd) + esptool.main(flash_cmd) + if __name__ == "__main__": - directory = r"D:\\esp\\WorkSpace\\esp32c3-usb_serial\\main\\wifi\\www" - write_flash(directory) + # directory = r"D:\\esp\\WorkSpace\\esp32c3-usb_serial\\main\\wifi\\www" + # write_flash(directory) + + chip = "esp32c3" + port = "COM11" + baud = 460800 + address = 0x2e1000 # 修改为你的烧录地址 + file_path = r"D:\esp\WorkSpace\esp32c3-usb_serial\tools\target_esp32s3.bin" # 修改为你的文件路径 + + flash_file(chip, port, baud, address, file_path) \ No newline at end of file