110 lines
4.0 KiB
Python
110 lines
4.0 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)
|
||
|
||
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)
|
||
|
||
def read_flash(chip, port, baud, address, size, output_file):
|
||
cmd = [
|
||
"esptool.py",
|
||
"--chip", chip,
|
||
"--port", port,
|
||
"--baud", str(baud),
|
||
"read_flash",
|
||
hex(address),
|
||
hex(size),
|
||
output_file
|
||
]
|
||
|
||
print("Running command:", " ".join(cmd))
|
||
try:
|
||
subprocess.run(cmd, check=True)
|
||
print(f"✅ Flash 读取成功,保存为:{output_file}")
|
||
except subprocess.CalledProcessError as e:
|
||
print("❌ 读取失败:", e)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
# 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)
|
||
|
||
address = 0x2e1000 # 起始地址
|
||
size = 0x10000 # 读取大小,可以修改
|
||
output_file = r"D:\esp\backup_read_flash.bin"
|
||
|
||
read_flash(chip, port, baud, address, size, output_file) |