diff --git a/components/bsp_flash/CMakeLists.txt b/components/bsp_flash/CMakeLists.txt new file mode 100644 index 0000000..9fb8117 --- /dev/null +++ b/components/bsp_flash/CMakeLists.txt @@ -0,0 +1,6 @@ +idf_component_register(SRCS "bsp_flash.c" + INCLUDE_DIRS "include" + + REQUIRES driver + REQUIRES nvs_flash + ) diff --git a/components/bsp_flash/bsp_flash.c b/components/bsp_flash/bsp_flash.c new file mode 100644 index 0000000..becddf4 --- /dev/null +++ b/components/bsp_flash/bsp_flash.c @@ -0,0 +1,132 @@ +#include "bsp_flash.h" +#include "esp_log.h" + +static const char *TAG = "flash.h"; + +SemaphoreHandle_t xSemaphore_ESPFlash; +void ESPFlash_Init(void) +{ + xSemaphore_ESPFlash = xSemaphoreCreateMutex();// 创建互斥型信号量。 +} + + +uint32_t Read_ESPFlash(uint8_t SubType,uint32_t addr,uint8_t* buf,uint32_t len) +{ + if(len == 0)return 1; + xSemaphoreTake( xSemaphore_ESPFlash, portMAX_DELAY );//获得资源的使用权 + esp_partition_t* res=esp_partition_find_first(0x01,SubType,NULL); + if(res==NULL) + { + ESP_LOGI(TAG,"no partition is found\r\n"); + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return 0; + } + if(esp_partition_read(res,addr, &buf[0], len)!=ESP_OK) + { + ESP_LOGI(TAG,"SubType: 0x%X 0x%lX",SubType,addr); + ESP_LOGI(TAG,"partition read fail\r\n"); + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return 0x0; + } + else + { + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return len; + } + return 0; +} + + + +uint32_t IRAM_ATTR Write_ESPFlash(uint8_t SubType,uint32_t addr,uint8_t* buf,uint32_t len) +{ + ESP_LOGI(TAG,"SubType: %d %lx",SubType,addr); + xSemaphoreTake( xSemaphore_ESPFlash, portMAX_DELAY );//获得资源的使用权 + esp_partition_t* res=esp_partition_find_first(0x01,SubType,NULL); + if(res==NULL) + { + ESP_LOGI(TAG,"no partition is found\r\n"); + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return 0; + } + else + { + //ESP_LOGI(TAG,"partiton info:\r\n name:%s\r\n size:%x\r\n start address:%x\r\n",res->label,res->size,res->address); + } + #if 1 //使能后会先擦再写 + if(esp_partition_erase_range(res,0,4096)!=ESP_OK) + { + ESP_LOGI(TAG,"partition erase fail\r\n"); + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return 0x0; + } + #endif + if(esp_partition_write(res,addr, &buf[0], len)!=ESP_OK) + { + ESP_LOGI(TAG,"partition write fail\r\n"); + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return 0x0; + } + else + { + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return len; + } + return 0; +} + + +uint32_t IRAM_ATTR Erase_ESPFlash(uint8_t SubType,uint32_t addr) +{ + xSemaphoreTake( xSemaphore_ESPFlash, portMAX_DELAY );//获得资源的使用权 + esp_partition_t* res=esp_partition_find_first(0x01,SubType,NULL); + if(res==NULL) + { + //ESP_LOGI(TAG,"no partition is found\r\n"); + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return 0; + } + else + { + // ESP_LOGI(TAG,"partiton info:\r\n name:%s\r\n size:%lx\r\n start address:%lx\r\n",res->label,res->size,res->address); + } + if(esp_partition_erase_range(res,addr,4096)!=ESP_OK) + { + ESP_LOGI(TAG,"partition erase fail\r\n"); + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return 0x0; + }else{ + xSemaphoreGive( xSemaphore_ESPFlash );//释放 + return addr; + } +} + +void test_partiton(void) +{ + uint8_t write_partition_data[16],read_partition_data[20]; + + esp_partition_t* res=esp_partition_find_first(0x01,0x41,NULL); + + if(res==NULL) + { + printf("no partition is found\r\n"); + return; + } + else + { + printf("partiton info:\r\n name:%s\r\n size:%lx\r\n start address:%lx\r\n", + res->label,res->size,res->address); + } + + if(esp_partition_read(res,0, read_partition_data, 16)!=ESP_OK) + { + printf("partition read fail\r\n"); + } + else + { + printf("read partition data from :"); + for(uint8_t i=0;i<16;i++) + printf("%X ",read_partition_data[i]); + printf("\r\n"); + } +} diff --git a/components/bsp_flash/include/bsp_flash.h b/components/bsp_flash/include/bsp_flash.h new file mode 100644 index 0000000..a3790ba --- /dev/null +++ b/components/bsp_flash/include/bsp_flash.h @@ -0,0 +1,28 @@ +#ifndef __BSP_FLASH_H +#define __BSP_FLASH_H + +#pragma once + +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include +#include +#include "esp_check.h" +#include +#include "driver/gpio.h" +#include "driver/uart.h" +#include "esp_partition.h" + + +extern void test_partiton(void); +extern void ESPFlash_Init(void); +extern uint32_t Read_ESPFlash(uint8_t SubType,uint32_t addr,uint8_t* buf,uint32_t len); +extern uint32_t IRAM_ATTR Write_ESPFlash(uint8_t SubType,uint32_t addr,uint8_t* buf,uint32_t len); +extern uint32_t IRAM_ATTR Erase_ESPFlash(uint8_t SubType,uint32_t addr); + + +#endif + diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f8cb5cc..ae21b8d 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,12 +1,10 @@ file(GLOB PORT_SRCS "wifi/*.c" ) -file(GLOB HTML_SRCS "wifi/www/*.html" "wifi/file/favicon.ico") - file(GLOB FILE_SYS_SRCS "file_system/spiffs.c" ) idf_component_register(SRCS "main.c" ${PORT_SRCS} ${FILE_SYS_SRCS} - EMBED_FILES ${HTML_SRCS} + INCLUDE_DIRS "." INCLUDE_DIRS "file_system/" INCLUDE_DIRS "wifi/") diff --git a/main/main.c b/main/main.c index 6d2caec..dab7d6c 100755 --- a/main/main.c +++ b/main/main.c @@ -34,6 +34,7 @@ #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*/ @@ -169,6 +170,23 @@ static void uart_event_task(void *pvParameters) 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) { @@ -182,7 +200,8 @@ void app_main(void) 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, @@ -209,13 +228,16 @@ 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); + // favicon_ico.html_data = load_html2Ram(favicon_ico); // ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); wifi_init_softap(); 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 = { diff --git a/main/wifi/wifi_html.c b/main/wifi/wifi_html.c index 68754c3..eb6a9ee 100644 --- a/main/wifi/wifi_html.c +++ b/main/wifi/wifi_html.c @@ -6,66 +6,55 @@ static const char *TAG = "wifi_html"; +uint8_t* home_html_data; + + + +wifi_html_t home_html = { + .html_name = "home.html", + .html_addr = 0xb84, + .html_size = 4081, +}; + +wifi_html_t wifi_config_html = { + .html_name = "wifi_config.html", + .html_addr = 0x44b9, + .html_size = 6671, +}; + +wifi_html_t favicon_ico = { + .html_name = "favicon.ico", + .html_addr = 0, + .html_size = 106008, +}; + + static esp_err_t index_get_handler(httpd_req_t *req) { - extern const unsigned char wifi_config_start[] asm("_binary_wifi_config_html_start"); - extern const unsigned char wifi_config_end[] asm("_binary_wifi_config_html_end"); - const size_t wifi_config_size = (wifi_config_end - wifi_config_start); - /* Add file upload form and script which on execution sends a POST request to /upload */ httpd_resp_set_type(req,HTTPD_TYPE_TEXT); - httpd_resp_send(req, (const char *)wifi_config_start, wifi_config_size); + httpd_resp_send(req, (const char *)wifi_config_html.html_data, wifi_config_html.html_size); return ESP_OK; } static esp_err_t Home_get_handler(httpd_req_t *req) { - extern const unsigned char home_start[] asm("_binary_home_html_start"); - extern const unsigned char home_end[] asm("_binary_home_html_end"); - const size_t home_size = (home_end - home_start); - /* Add file upload form and script which on execution sends a POST request to /upload */ httpd_resp_set_type(req,HTTPD_TYPE_TEXT); - httpd_resp_send(req, (const char *)home_start, home_size); + + httpd_resp_send(req, (const char*)home_html.html_data, home_html.html_size); return ESP_OK; } -static esp_err_t upgrade_ota_bin(httpd_req_t *req) -{ - extern const unsigned char upload_script_start[] asm("_binary_upload_script_html_start"); - extern const unsigned char upload_script_end[] asm("_binary_upload_script_html_end"); - const size_t upload_script_size = (upload_script_end - upload_script_start); - - /* Add file upload form and script which on execution sends a POST request to /upload */ - httpd_resp_set_type(req,HTTPD_TYPE_TEXT); - httpd_resp_send(req, (const char *)upload_script_start, upload_script_size); - return ESP_OK; -} - -static esp_err_t Save_file_handler(httpd_req_t *req) -{ - - extern const unsigned char file_server_start[] asm("_binary_file_server_html_start"); - extern const unsigned char file_server_end[] asm("_binary_file_server_html_end"); - const size_t file_server_size = (file_server_end - file_server_start); - - /* Add file upload form and script which on execution sends a POST request to /upload */ - httpd_resp_set_type(req,HTTPD_TYPE_TEXT); - httpd_resp_send(req, (const char *)file_server_start, file_server_size); - return ESP_OK; -} /* Handler to respond with an icon file embedded in flash. * Browsers expect to GET website icon at URI /favicon.ico. * This can be overridden by uploading file with same name */ static esp_err_t favicon_get_handler(httpd_req_t *req) { - extern const unsigned char favicon_ico_start[] asm("_binary_favicon_ico_start"); - extern const unsigned char favicon_ico_end[] asm("_binary_favicon_ico_end"); - const size_t favicon_ico_size = (favicon_ico_end - favicon_ico_start); httpd_resp_set_type(req, "image/x-icon"); - httpd_resp_send(req, (const char *)favicon_ico_start, favicon_ico_size); + httpd_resp_send(req, (const char *)favicon_ico.html_data, favicon_ico.html_size); return ESP_OK; } diff --git a/main/wifi/wifi_task.h b/main/wifi/wifi_task.h index f4899a8..1749419 100644 --- a/main/wifi/wifi_task.h +++ b/main/wifi/wifi_task.h @@ -27,6 +27,18 @@ #define MIN(a, b) ((a) < (b) ? (a) : (b)) +typedef struct wifi_html_ +{ + char* html_name; + uint32_t html_addr; + uint32_t html_size; + uint8_t* html_data; +}wifi_html_t; + +extern wifi_html_t home_html; +extern wifi_html_t wifi_config_html; +extern wifi_html_t favicon_ico; + void wifi_init_station(void); void wifi_init_softap(void); esp_err_t start_web_server(void); diff --git a/main/wifi/www/download_html.json b/main/wifi/www/download_html.json new file mode 100644 index 0000000..46e819c --- /dev/null +++ b/main/wifi/www/download_html.json @@ -0,0 +1,38 @@ +[ + { + "url": "/file_server.html", + "size": 2948, + "name": "file_server.html", + "address": "0x0" + }, + { + "url": "/home.html", + "size": 4081, + "name": "home.html", + "address": "0xb84" + }, + { + "url": "/upload_script.html", + "size": 5883, + "name": "upload_script.html", + "address": "0x1b75" + }, + { + "url": "/upload_script_copy.html", + "size": 3046, + "name": "upload_script_copy.html", + "address": "0x3270" + }, + { + "url": "/wifi.html", + "size": 1635, + "name": "wifi.html", + "address": "0x3e56" + }, + { + "url": "/wifi_config.html", + "size": 6671, + "name": "wifi_config.html", + "address": "0x44b9" + } +] \ No newline at end of file diff --git a/main/wifi/www/html_files.bin b/main/wifi/www/html_files.bin new file mode 100644 index 0000000..0cea961 --- /dev/null +++ b/main/wifi/www/html_files.bin @@ -0,0 +1,580 @@ + + + +
+

ESP32 File Server

+
+ + + + + + + + + + +
+ + + +
+ + + + + +
+
+ + + + + + + 在线配置 + + + + +
+

ESP32 Web 在 线 工 具

+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+
+
+
+ + +
+
+ +
+
+ + + + + + + + 在线配置 + + + +
+

ESP32S2 OTA Bin Server

+ + + + + + + + + + + + + + + + + + + + + +
+
+
+ +
+
+
+
+ + + + + + + 在线OTA更新 + +
+

ESP32S2 OTA Bin Server

+
+ + + + + + + + + + +
+ + + +
+ + + + + +
+
+ + + + + + +

wifi 密码配置

+
+ + +
+ + +
+ +
+
+ + + + + + + 在线配置 + + + + +
+

ESP32 WIFI 配网

+
+
+ +
+
+ +
+
+ +
+ + +
+ + +
+
+
+ + + + + + \ No newline at end of file diff --git a/tools/download_html.json b/tools/download_html.json new file mode 100644 index 0000000..e69de29 diff --git a/tools/esptools.py b/tools/esptools.py index c90f11a..1d3cd7a 100644 --- a/tools/esptools.py +++ b/tools/esptools.py @@ -1,14 +1,60 @@ -import esptool #/Users/yu/.espressif/python_env/idf5.3_py3.9_env/bin/python /Users/yu/esp/v5.3.2/esp-idf/components/esptool_py/esptool/esptool.py -p /dev/tty.wchusbserial54CE0416791 -b 460800 --before default_reset --after hard_reset --chip esp32s3 write_flash --flash_mode dio --flash_freq 80m --flash_size 4MB 0x0 bootloader/bootloader.bin 0x10000 usb_serial_jtag_echo.bin 0x8000 partition_table/partition-table.bin 0xd000 ota_data_initial.bin +import os +import json +import esptool -esptool.main([ - "--chip", "esp32c3", - "--port", "/dev/tty.usbmodem11401", - "--baud", "460800", - "write_flash", - "-z", - "0x2d1000", "/Users/yu/esp/esp32c3-usb_serial/main/wifi/www/wifi_config.html" -]) \ No newline at end of file +def get_files(directory, extra_file=None): + """获取指定目录下的所有 HTML 文件及其大小,并优先添加额外文件(如 favicon.ico)""" + files = [] + if extra_file and os.path.exists(extra_file): + filesize = os.path.getsize(extra_file) + files.append((os.path.basename(extra_file), extra_file, filesize)) + + for filename in sorted(os.listdir(directory)): + if filename.endswith(".html"): + filepath = os.path.join(directory, filename) + filesize = os.path.getsize(filepath) + files.append((filename, filepath, filesize)) + return files + +def create_bin_file(directory, files, output_bin="html_files.bin"): + """将 favicon.ico 和所有 HTML 文件按烧录顺序拼接成一个 .bin 文件""" + bin_path = os.path.join(directory, output_bin) + current_address = 0x0 + json_data = [] + + with open(bin_path, "wb") as bin_file: + for filename, filepath, filesize in files: + with open(filepath, "rb") as f: + file_data = f.read() + bin_file.write(file_data) + json_data.append({ + "url": f"/{filename}", + "size": filesize, + "name": filename, + "address": hex(current_address) + }) + current_address += filesize + + with open(os.path.join(directory, "download_html.json"), "w", encoding="utf-8") as json_file: + json.dump(json_data, json_file, indent=4, ensure_ascii=False) + + print(f"Generated bin file: {bin_path}") + return bin_path + +def write_flash(directory, base_address=0x2D1000, port="COM11", baud=460800): + extra_file = os.path.join(os.path.dirname(directory), "file", "favicon.ico") + files = get_files(directory) + bin_file = create_bin_file(directory, files) + + flash_cmd = ["--chip", "esp32c3", "--port", port, "--baud", str(baud), "write_flash", "-z", hex(base_address), bin_file] + + print("Flashing command:", flash_cmd) + esptool.main(flash_cmd) + +if __name__ == "__main__": + directory = r"D:\\esp\\WorkSpace\\esp32c3-usb_serial\\main\\wifi\\www" + write_flash(directory)