61 lines
2.6 KiB
Python
61 lines
2.6 KiB
Python
|
||
|
||
#/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
|
||
|
||
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)
|