2026年4月26日 星期日

Esptool

Esptool 是一個用 Python 開發的 Open Source 跨平台軟體,
用來操作 Espressif SoC (樂鑫 SoC 開發板),
可進行燒錄、設定...等動作,例如:

  • 對 Flash 中儲存的二進位資料進行讀取、寫入、擦除與驗證。
  • 讀取晶片特性及其他相關資料(例如 MAC 位址或 Flash 晶片 ID)。
  • 讀取與寫入一次性可程式化 (OTP、one-time-programmable) 的 eFuse (電子保險絲)。
  • 準備可供燒錄的二進位可執行映像檔。
  • 分析、組譯並合併二進位映像檔。

如果使用 Arduino IDE 進行 ESP32 開發,已裝了 Boards Manager 裡的 ESP32 開發板套件,
則在「%LOCALAPPDATA%\Arduino15\packages\esp32\tools\esptool_py\(esptool版本號)\」路徑底下,
就會有 esptool.exe 執行檔。
C:\Users\xyz\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\5.2.0>dir /B
espefuse.exe
espsecure.exe
esptool.exe
esp_rfc2217_server.exe
LICENSE
README.md


esptool  指令:
C:\Users\xyz\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\5.2.0>esptool -h

 Usage: esptool [OPTIONS] COMMAND [ARGS]...

 esptool v5.2.0 - serial utility for flashing, provisioning, and interacting with
 Espressif SoCs.

╭─ Options ───────────────────────────────────────────────────────────────────────────────╮
│ --chip              -c  [auto|esp8266|esp32|esp32s2|es  Target chip type.               │
│                         p32s3|esp32c3|esp32c2|esp32c6|                                  │
│                         esp32c61|esp32c5|esp32e22|esp3                                  │
│                         2h2|esp32h21|esp32p4|esp32h4|e                                  │
│                         sp32s31]                                                        │
│ --port              -p  SERIAL-PORT                     Serial port device.             │
│ --baud              -b  BAUD-RATE                       Serial port baud rate used when │
│                                                         flashing/reading.               │
│ --port-filter           [TEXT]                          Serial port device filter, can  │
│                                                         be vid=NUMBER, pid=NUMBER,      │
│                                                         name=SUBSTRING,                 │
│                                                         serial=SUBSTRING.               │
│ --before                [default-reset|usb-reset|no-re  Which reset to perform before   │
│                         set|no-reset-no-sync]           connecting to the chip.         │
│ --after             -a  [hard-reset|soft-reset|no-rese  Which reset to perform after    │
│                         t|no-reset-stub|watchdog-reset  operation is finished.          │
│                         ]                                                               │
│ --no-stub                                               Disable launching the flasher   │
│                                                         stub, only talk to ROM          │
│                                                         bootloader. Some features will  │
│                                                         not be available.               │
│ --trace             -t                                  Enable trace-level output of    │
│                                                         esptool interactions.           │
│ --verbose           -v                                  Print all output, disable       │
│                                                         collapsing output stages.       │
│ --silent            -s                                  Silence all output except for   │
│                                                         errors.                         │
│ --override-vddsdio      [1.8V|1.9V|OFF]                 Override ESP32 VDDSDIO internal │
│                                                         voltage regulator (use with     │
│                                                         care).                          │
│ --connect-attempts      INTEGER                         Number of attempts to connect,  │
│                                                         negative or 0 for infinite.     │
│                                                         Default: 7.                     │
│ --help              -h                                  Show this message and exit.     │
╰─────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Basic commands ────────────────────────────────────────────────────────────────────────╮
│ write-flash            Write a binary blob to flash. The address is followed by binary  │
│                        filename, separated by space.                                    │
│ read-flash             Read SPI flash memory content.                                   │
│ erase-flash            Erase the SPI flash memory.                                      │
│ erase-region           Erase a region of the SPI flash memory.                          │
│ read-mac               Print the device MAC address.                                    │
│ flash-id               Print the SPI flash memory manufacturer and device ID.           │
│ elf2image              Create an application image from ELF file                        │
│ image-info             Print information about a firmware image (bootloader or          │
│                        application).                                                    │
│ merge-bin              Merge multiple raw binary files into a single flashable file.    │
│ version                Print esptool version.                                           │
╰─────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Advanced commands ─────────────────────────────────────────────────────────────────────╮
│ verify-flash           Verify a binary blob against the flash memory content.           │
│ load-ram               Download an image to RAM and execute.                            │
│ dump-mem               Dump arbitrary memory to a file.                                 │
│ read-mem               Read arbitrary memory location.                                  │
│ write-mem              Modify or write to arbitrary memory location.                    │
│ read-flash-status      Read SPI flash memory status register.                           │
│ write-flash-status     Write SPI flash memory status register.                          │
│ read-flash-sfdp        Read SPI flash SFDP (Serial Flash Discoverable Parameters).      │
│ get-security-info      Print security information report.                               │
│ chip-id                Print the device chip ID.                                        │
│ run                    Run application code loaded in flash.                            │
╰─────────────────────────────────────────────────────────────────────────────────────────╯


查看 Flash 記憶體相關資訊:
我的開發板接在 com5,所以指定 -p com5
C:\Users\xyz\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\5.2.0>esptool -p com5 flash-id
esptool v5.2.0
Connected to ESP32 on com5:
Chip type:          ESP32-D0WD-V3 (revision v3.1)
Features:           Wi-Fi, BT, Dual Core + LP Core, 240MHz, Vref calibration in eFuse, Coding Scheme None
Crystal frequency:  40MHz
MAC:                **:**:**:**:**:**

Stub flasher running.

Flash Memory Information:
=========================
Manufacturer: 5e
Device: 4016
Detected flash size: 4MB
Flash voltage set by a strapping pin: 3.3V

Hard resetting via RTS pin...


如果開著 Arduino IDE 時出現以下存取被拒訊息,可能是指定的 PORT (COM5) 被 Arduino IDE 佔住連線,可切換 Arduino IDE 目前使用的 PORT 或關掉 Arduino IDE 後,再執行指令。
C:\Users\xyz\AppData\Local\Arduino15\packages\esp32\tools\esptool_py\5.2.0>esptool -p com5 flash-id
esptool v5.2.0
Serial port com5:

A fatal error occurred: Could not open com5, the port is busy or doesn't exist.
(could not open port 'com5': PermissionError(13, '存取被拒。', None, 5))



若要用 Python  執行,需先裝 esptool 套件:
>uv venv
>uv pip install esptool


指定 chip 型號下指令,會逐一檢查連接埠:
>uv run esptool -c esp32 flash-id
esptool v5.2.0
Found 6 serial ports...
Serial port COM7:
COM7 failed to connect: Could not open COM7, the port is busy or doesn't exist.
(could not open port 'COM7': OSError(22, '信號等待逾時。', None, 121))

Serial port COM6:
Connecting...
直到找到符合的 chip
>uv run esptool -c esp32 flash-id
esptool v5.2.0
Connected to ESP32 on COM5:
Chip type:          ESP32-D0WD-V3 (revision v3.1)
Features:           Wi-Fi, BT, Dual Core + LP Core, 240MHz, Vref calibration in eFuse, Coding Scheme None
Crystal frequency:  40MHz
MAC:                **:**:**:**:**:**

Stub flasher running.

Flash Memory Information:
=========================
Manufacturer: 5e
Device: 4016
Detected flash size: 4MB
Flash voltage set by a strapping pin: 3.3V

Hard resetting via RTS pin...


讀取 Flash  分割區資訊:
以下指令執行後,會在目錄下匯出產生 partition_table.bin 檔案。
0x8000:分割表資料開始處
0xC00:分割表長度(最多可以儲存95 個分割表項目)
>uv run esptool --port COM5 read-flash 0x8000 0xC00 partition_table.bin
esptool v5.2.0
Connected to ESP32 on COM5:
Chip type:          ESP32-D0WD-V3 (revision v3.1)
Features:           Wi-Fi, BT, Dual Core + LP Core, 240MHz, Vref calibration in eFuse, Coding Scheme None
Crystal frequency:  40MHz
MAC:                **:**:**:**:**:**

Stub flasher running.

Configuring flash size...
Read 3072 bytes from 0x00008000 in 0.3 seconds (84.6 kbit/s) to 'partition_table.bin'.

Hard resetting via RTS pin...
partition_table.bin 檔案可用 gen_esp32part.py 轉換成 CSV 檔案
gen_esp32part.py 下載連結
https://github.com/espressif/esp-idf/blob/master/components/partition_table/gen_esp32part.py
執行以下指令
>uv run gen_esp32part.py partition_table.bin partition_table.csv
Parsing binary partition input...
Verifying table...
執行後,產生 partition_table.csv 內容如下
(分割區說明可參考 在 Arduino IDE 查看 ESP32 的 RAM 和 Flash 資訊)





參考:


沒有留言:

張貼留言