esp-rs / espflash

Serial flasher utility for Espressif SoCs and modules based on esptool.py
Apache License 2.0
474 stars 116 forks source link

Autodetect flash size and speed and configure automatically #146

Closed MabezDev closed 2 years ago

MabezDev commented 2 years ago

Building on top of https://github.com/esp-rs/espflash/pull/141, we could detect and automatically update the image header.

JurajSadel commented 2 years ago

This should be already implemented.

JuliDi commented 1 year ago

There seems to be an issue with this detection (in version 1.7.0 at least).

If I use espflash directly, it correctly determines the flash size of 4 MB and the program, which is around 1 MB, uses up around 25% of the flash.

$ cargo run                                    
    Finished dev [optimized + debuginfo] target(s) in 0.15s
     Running `espflash --monitor target/riscv32imc-esp-espidf/debug/esp_pid`
New version of espflash is available: v2.0.0-rc.2

Detected 2 serial ports. Ports which match a known common dev board are highlighted.

Serial port: /dev/cu.usbmodem4201
Connecting...

Chip type:         ESP32-C3 (revision 3)
Crystal frequency: 40MHz
Flash size:        4MB
Features:          WiFi
MAC address:       34:b4:72:80:1d:e0
App/part. size:    992624/4128768 bytes, 24.04%

If I use cargo espflash on the same device however, it determines the flash size correctly but then my almost 1 MB program uses up 95% of the flash. If I add some more std bloat, I easily get beyond 1 MB and will refuse to flash.

cargo espflash --monitor /dev/cu.usbmodem4201                   
New version of cargo-espflash is available: v2.0.0-rc.2

Serial port: /dev/cu.usbmodem4201
Connecting...

    Finished dev [optimized + debuginfo] target(s) in 0.61s
Chip type:         ESP32-C3 (revision 3)
Crystal frequency: 40MHz
Flash size:        4MB
Features:          WiFi
MAC address:       34:b4:72:80:1d:e0
App/part. size:    992624/1048576 bytes, 94.66%
JuliDi commented 1 year ago

To follow up on the previously mentioned "refuse to flash". If I turn off optimization in debug build, it easily exceeds 1 MB.

cargo espflash --monitor /dev/cu.usbmodem4201
New version of cargo-espflash is available: v2.0.0-rc.2

Serial port: /dev/cu.usbmodem4201
Connecting...

    Finished dev [unoptimized + debuginfo] target(s) in 0.72s
Chip type:         ESP32-C3 (revision 3)
Crystal frequency: 40MHz
Flash size:        4MB
Features:          WiFi
MAC address:       34:b4:72:80:1d:e0
App/part. size:    1748400/1048576 bytes, 166.74%
Error: 
  × Supplied elf image is too big and doesn't fit configured app partition

using espflash directly it works, but the code will panic a few lines into main()...

cargo run       
    Finished dev [unoptimized + debuginfo] target(s) in 0.18s
     Running `espflash --monitor target/riscv32imc-esp-espidf/debug/esp_pid`
New version of espflash is available: v2.0.0-rc.2

Detected 2 serial ports. Ports which match a known common dev board are highlighted.

Serial port: /dev/cu.usbmodem4201
Connecting...

Chip type:         ESP32-C3 (revision 3)
Crystal frequency: 40MHz
Flash size:        4MB
Features:          WiFi
MAC address:       34:b4:72:80:1d:e0
App/part. size:    1748400/4128768 bytes, 42.35%
eddwinpaz commented 6 months ago

If I try,

esp32-app on  master [?] is 📦 v0.1.0 via 🦀 v1.76.0-nightly on ☁️  (us-east-2) 
❯ cargo run           
    Finished dev [optimized + debuginfo] target(s) in 0.13s
     Running `espflash flash --monitor target/xtensa-esp32-espidf/debug/esp32-app`
[2024-03-04T04:19:27Z INFO ] 🚀 A new version of espflash is available: v3.0.0-rc.1
[2024-03-04T04:19:27Z INFO ] Serial port: '/dev/ttyACM0'
[2024-03-04T04:19:27Z INFO ] Connecting...
[2024-03-04T04:19:27Z INFO ] Using flash stub
Chip type:         esp32 (revision v3.0)
Crystal frequency: 40MHz
Flash size:        4MB
Features:          WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC address:       ****************
App/part. size:    1,114,176/4,128,768 bytes, 26.99%
[00:00:01] [========================================]      17/17      0x1000                                                                                                          
[00:00:00] [========================================]       1/1       0x8000                                                                                                          
[00:01:04] [========================================]     675/675     0x10000                                                                                                         [2024-03-04T04:20:34Z INFO ] Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:7104
0x3fff0030 - phy_chan_target_power
    at ??:??
load:0x40078000,len:15576
0x40078000 - phy_chan_target_power
    at ??:??
load:0x40080400,len:4
0x40080400 - _init
    at ??:??
ho 8 tail 4 room 4
load:0x40080404,len:3876
0x40080404 - _init
    at ??:??
entry 0x4008064c
0x4008064c - _init
    at ??:??
I (31) boot: ESP-IDF v5.1-beta1-378-gea5e0ff298-dirt 2nd stage bootloader
I (31) boot: compile time Jun  7 2023 07:48:23
I (33) boot: Multicore bootloader
I (37) boot: chip revision: v3.0
I (41) boot.esp32: SPI Speed      : 40MHz
I (46) boot.esp32: SPI Mode       : DIO
I (50) boot.esp32: SPI Flash Size : 4MB
I (55) boot: Enabling RNG early entropy source...
I (60) boot: Partition Table:
I (64) boot: ## Label            Usage          Type ST Offset   Length
I (71) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (79) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (86) boot:  2 factory          factory app      00 00 00010000 003f0000
I (94) boot: End of partition table
I (98) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=3ade4h (241124) map
I (193) esp_image: segment 1: paddr=0004ae0c vaddr=3ffb0000 size=039f0h ( 14832) load
I (200) esp_image: segment 2: paddr=0004e804 vaddr=40080000 size=01814h (  6164) load
I (202) esp_image: segment 3: paddr=00050020 vaddr=400d0020 size=bcd10h (773392) map
I (487) esp_image: segment 4: paddr=0010cd38 vaddr=40081814 size=132dch ( 78556) load
I (530) boot: Loaded app from partition at offset 0x10000
I (530) boot: Disabling RNG early entropy source...
I (542) cpu_start: Multicore app
I (542) cpu_start: Pro cpu up.
I (542) cpu_start: Starting app cpu, entry point is 0x40082060
0x40082060 - call_start_cpu1
    at /home/eddwinpaz/Desktop/rust-esp32/esp32-app/.embuild/espressif/esp-idf/v5.1.2/components/esp_system/port/cpu_start.c:157
I (0) cpu_start: App cpu up.
I (560) cpu_start: Pro cpu start user code
I (560) cpu_start: cpu freq: 160000000 Hz
I (560) cpu_start: Application information:
I (565) cpu_start: Project name:     libespidf
I (570) cpu_start: App version:      1
I (574) cpu_start: Compile time:     Mar  3 2024 22:32:19
I (580) cpu_start: ELF file SHA256:  0000000000000000...
I (586) cpu_start: ESP-IDF:          v5.1.2
I (591) cpu_start: Min chip rev:     v0.0
I (596) cpu_start: Max chip rev:     v3.99 
I (601) cpu_start: Chip rev:         v3.0
I (606) heap_init: Initializing. RAM available for dynamic allocation:
I (613) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (619) heap_init: At 3FFB7EC8 len 00028138 (160 KiB): DRAM
I (625) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (631) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (638) heap_init: At 40094AF0 len 0000B510 (45 KiB): IRAM
I (646) spi_flash: detected chip: generic
I (649) spi_flash: flash io: dio
W (653) pcnt(legacy): legacy driver is deprecated, please migrate to `driver/pulse_cnt.h`
W (661) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (671) app_start: Starting scheduler on CPU0
I (675) app_start: Starting scheduler on CPU1
I (675) main_task: Started on CPU0
I (685) main_task: Calling app_main()
I (685) gpio: GPIO[2]| InputEn: 0| OutputEn: 0| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0 
I (745) wifi:wifi driver task: 3ffbfd60, prio:23, stack:6656, core=0
I (765) wifi:wifi firmware version: 91b9630
I (765) wifi:wifi certification version: v7.0
I (765) wifi:config NVS flash: enabled
I (765) wifi:config nano formating: disabled
I (775) wifi:Init data frame dynamic rx buffer num: 32
I (775) wifi:Init static rx mgmt buffer num: 10
I (775) wifi:Init management short buffer num: 32
I (785) wifi:Init dynamic tx buffer num: 32
I (785) wifi:Init static rx buffer size: 1600
I (795) wifi:Init static rx buffer num: 10
I (795) wifi:Init dynamic rx buffer num: 32
I (805) wifi_init: rx ba win: 6
I (805) wifi_init: tcpip mbox: 32
I (805) wifi_init: udp mbox: 6
I (815) wifi_init: tcp mbox: 6
I (815) wifi_init: tcp tx win: 5744
I (815) wifi_init: tcp rx win: 5744
I (825) wifi_init: tcp mss: 1440
I (825) wifi_init: WiFi IRAM OP enabled
I (835) wifi_init: WiFi RX IRAM OP enabled
I (845) phy_init: phy_version 4780,16b31a7,Sep 22 2023,20:42:16
I (925) wifi:mode : sta (**************)
I (925) wifi:enable tsf
Wifi Started
Wifi connected

it runs the application with no issues. but when I try to flash it using

esp32-app on  master [?] is 📦 v0.1.0 via 🦀 v1.76.0-nightly on ☁️  (us-east-2) took 5s 
❯ cargo espflash flash                                           
[2024-03-04T04:18:57Z INFO ] 🚀 A new version of cargo-espflash is available: v3.0.0-rc.1
[2024-03-04T04:18:57Z INFO ] Serial port: '/dev/ttyACM0'
[2024-03-04T04:18:57Z INFO ] Connecting...
[2024-03-04T04:18:57Z INFO ] Using flash stub
    Finished dev [optimized + debuginfo] target(s) in 0.12s
Chip type:         esp32 (revision v3.0)
Crystal frequency: 40MHz
Flash size:        4MB
Features:          WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC address:       ******************
Bootloader:        /home/eddwinpaz/Desktop/rust-esp32/esp32-app/target/xtensa-esp32-espidf/debug/build/esp-idf-sys-fa702bc195fd0183/out/build/bootloader/bootloader.bin
Partition table:   /home/eddwinpaz/Desktop/rust-esp32/esp32-app/target/xtensa-esp32-espidf/debug/build/esp-idf-sys-fa702bc195fd0183/out/build/partition_table/partition-table.bin
Error: espflash::image_too_big (link)

  × Supplied ELF image of 1114176B is too big, and doesn't fit configured app partition of 1048576B
  help: Reduce the size of the binary or increase the size of the app partition.

it blames that the espflash image is too big.

SergioGasquez commented 6 months ago

When using cargo-espflash you use the esp-idf partition table, while when using espflash, you use the espflash generated partition table, see: https://github.com/esp-rs/espflash/tree/main/cargo-espflash#bootloader-and-partition-table