Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ESP32-S2] Not able to monitor device after flashing - device not booting properly #621

Open
italicmew opened this issue Mar 25, 2024 · 21 comments

Comments

@italicmew
Copy link

italicmew commented Mar 25, 2024

I am currently attempting to flash my ESP-32-s2 device using the esp-template and espflash tool. However, I am facing issues in getting the compiled binary to run successfully on my device. My initial problem was with resetting the device, as it was not resetting as expected compared to esptool.py. After several attempts, I discovered that setting -b no-reset was necessary to initiate the flashing process successfully.

Even after flashing, my device does not boot up or display anything. I noticed that when I manually reset the device, the serial ports disappear.

$ cargo generate esp-rs/esp-template
$ CRATE_CC_NO_DEFAULTS=1 cargo build
$ cargo espflash flash -b no-reset --no-stub --monitor

[2024-03-25T22:53:49Z WARN ] Pre-connection option 'NoReset' was selected. Connection may fail if the chip is not in bootloader or flasher stub mode.
[2024-03-25T22:53:49Z INFO ] Detected 2 serial ports
[2024-03-25T22:53:49Z INFO ] Ports which match a known common dev board are highlighted
[2024-03-25T22:53:49Z INFO ] Please select a port
[2024-03-25T22:53:50Z INFO ] Serial port: '/dev/cu.usbmodem01'
[2024-03-25T22:53:50Z INFO ] Connecting...
    Finished dev [optimized + debuginfo] target(s) in 0.23s
Chip type:         esp32s2 (revision v0.0)
Crystal frequency: 40 MHz
Flash size:        4MB
Features:          WiFi, Embedded Flash 4MB, No Embedded PSRAM, ADC and temperature sensor calibration in BLK2 of efuse V1
MAC address:       84:f7:03:e8:6c:5e
App/part. size:    396,576/1,048,576 bytes, 37.82%
[2024-03-25T22:53:50Z INFO ] Segment at address '0x1000' has not changed, skipping write
[2024-03-25T22:53:50Z INFO ] Segment at address '0x8000' has not changed, skipping write
[2024-03-25T22:53:51Z INFO ] Segment at address '0x10000' has not changed, skipping write
[2024-03-25T22:53:51Z INFO ] Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

Error:   × Broken pipe
Click to expand Full logs
 RUST_LOG=debug cargo espflash flash -b no-reset --no-stub --monitor
[2024-03-25T23:07:26Z DEBUG] Flash(
        FlashArgs {
            build_args: BuildArgs {
                bin: None,
                example: None,
                features: None,
                frozen: false,
                locked: false,
                package: None,
                release: false,
                target: None,
                target_dir: None,
                unstable: None,
                flash_config_args: FlashConfigArgs {
                    flash_freq: None,
                    flash_mode: None,
                    flash_size: None,
                },
            },
            connect_args: ConnectArgs {
                after: HardReset,
                baud: None,
                before: NoReset,
                chip: None,
                confirm_port: false,
                list_all_ports: false,
                no_stub: true,
                port: None,
            },
            flash_args: FlashArgs {
                bootloader: None,
                erase_parts: None,
                erase_data_parts: None,
                log_format: Serial,
                min_chip_rev: 0,
                monitor: true,
                monitor_baud: None,
                partition_table: None,
                target_app_partition: None,
                partition_table_offset: None,
                ram: false,
                no_verify: false,
                no_skip: false,
            },
        },
    )
[2024-03-25T23:07:26Z DEBUG] connecting to crates.io:443 at [2600:9000:2610:ac00:c:7ed3:240:93a1]:443
[2024-03-25T23:07:26Z DEBUG] No cached session for DnsName("crates.io")
[2024-03-25T23:07:26Z DEBUG] Not resuming any session
[2024-03-25T23:07:26Z DEBUG] Using ciphersuite TLS13_AES_128_GCM_SHA256
[2024-03-25T23:07:26Z DEBUG] Not resuming
[2024-03-25T23:07:26Z DEBUG] TLS1.3 encrypted extensions: [ServerNameAck]
[2024-03-25T23:07:26Z DEBUG] ALPN protocol is None
[2024-03-25T23:07:26Z DEBUG] created stream: Stream(RustlsStream)
[2024-03-25T23:07:26Z DEBUG] sending request GET https://crates.io/api/v1/crates/cargo-espflash/versions
[2024-03-25T23:07:26Z DEBUG] writing prelude: GET /api/v1/crates/cargo-espflash/versions HTTP/1.1
    Host: crates.io
    User-Agent: ureq/2.9.6
    Accept: */*
    accept-encoding: gzip
[2024-03-25T23:07:27Z DEBUG] Chunked body in response
[2024-03-25T23:07:27Z DEBUG] response 200 to GET https://crates.io/api/v1/crates/cargo-espflash/versions
[2024-03-25T23:07:27Z DEBUG] dropping stream: Stream(RustlsStream)
[2024-03-25T23:07:27Z DEBUG] Config: Config {
        baudrate: None,
        bootloader: None,
        connection: Connection {
            serial: None,
        },
        partition_table: None,
        usb_device: [
            UsbDevice {
                vid: 12346,
                pid: 2,
            },
        ],
        save_path: "/Users/tiburcio/Library/Application Support/rs.esp.espflash/espflash.toml",
    }
[2024-03-25T23:07:27Z WARN ] Pre-connection option 'NoReset' was selected. Connection may fail if the chip is not in bootloader or flasher stub mode.
[2024-03-25T23:07:27Z INFO ] Detected 2 serial ports
[2024-03-25T23:07:27Z INFO ] Ports which match a known common dev board are highlighted
[2024-03-25T23:07:27Z INFO ] Please select a port
[2024-03-25T23:07:29Z INFO ] Serial port: '/dev/cu.usbmodem01'
[2024-03-25T23:07:29Z INFO ] Connecting...
[2024-03-25T23:07:29Z DEBUG] Writing command: Sync
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1073745920 }
[2024-03-25T23:07:29Z DEBUG] Attempting flash enable with: SpiAttachParams { clk: 0, q: 0, d: 0, hd: 0, cs: 0 }
[2024-03-25T23:07:29Z DEBUG] Writing command: SpiAttach { spi_params: SpiAttachParams { clk: 0, q: 0, d: 0, hd: 0, cs: 0 } }
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1061167128 }
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1061167136 }
[2024-03-25T23:07:29Z DEBUG] Writing command: WriteReg { address: 1061167128, value: 2415919104, mask: None }
[2024-03-25T23:07:29Z DEBUG] Writing command: WriteReg { address: 1061167136, value: 1879048351, mask: None }
[2024-03-25T23:07:29Z DEBUG] Writing command: WriteReg { address: 1061167144, value: 23, mask: None }
[2024-03-25T23:07:29Z DEBUG] Writing command: WriteReg { address: 1061167192, value: 0, mask: None }
[2024-03-25T23:07:29Z DEBUG] Writing command: WriteReg { address: 1061167104, value: 262144, mask: None }
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1061167128 }
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1061167192 }
[2024-03-25T23:07:29Z DEBUG] Writing command: WriteReg { address: 1061167128, value: 2147483840, mask: None }
[2024-03-25T23:07:29Z DEBUG] Writing command: WriteReg { address: 1061167136, value: 1879048192, mask: None }
[2024-03-25T23:07:29Z DEBUG] Flash detect OK!
[2024-03-25T23:07:29Z DEBUG] Writing command: SpiSetParams { spi_params: SpiSetParams { fl_id: 0, total_size: 4194304, block_size: 65536, sector_size: 4096, page_size: 256, status_mask: 65535 } }
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:29Z DEBUG] Writing command: ReadReg { address: 1061265492 }
[2024-03-25T23:07:29Z DEBUG] Writing command: SpiAttach { spi_params: SpiAttachParams { clk: 0, q: 0, d: 0, hd: 0, cs: 0 } }
    Finished dev [optimized + debuginfo] target(s) in 0.33s
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265492 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265516 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265476 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265480 }
Chip type:         esp32s2 (revision v0.0)
Crystal frequency: 40 MHz
Flash size:        4MB
Features:          WiFi, Embedded Flash 4MB, No Embedded PSRAM, ADC and temperature sensor calibration in BLK2 of efuse V1
MAC address:       84:f7:03:e8:6c:5e
[2024-03-25T23:07:30Z DEBUG] Writing command: SpiAttach { spi_params: SpiAttachParams { clk: 0, q: 0, d: 0, hd: 0, cs: 0 } }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265488 }
[2024-03-25T23:07:30Z DEBUG] Writing command: ReadReg { address: 1061265492 }
App/part. size:    396,576/1,048,576 bytes, 37.82%
[2024-03-25T23:07:30Z DEBUG] Writing command: FlashMd5 { offset: 4096, size: 21584 }
[2024-03-25T23:07:30Z INFO ] Segment at address '0x1000' has not changed, skipping write
[2024-03-25T23:07:30Z DEBUG] Writing command: FlashMd5 { offset: 32768, size: 3072 }
[2024-03-25T23:07:30Z INFO ] Segment at address '0x8000' has not changed, skipping write
[2024-03-25T23:07:30Z DEBUG] Writing command: FlashMd5 { offset: 65536, size: 396576 }
[2024-03-25T23:07:31Z INFO ] Segment at address '0x10000' has not changed, skipping write
[2024-03-25T23:07:31Z DEBUG] Using HardReset reset strategy
[2024-03-25T23:07:31Z INFO ] Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit
Click to expand esptool.py LOGS that are working

esptool.py --chip esp32s2 -p /dev/cu.usbmodem01 -b 460800 --before=no_reset --after=no_reset write_flash --flash_mode dio --flash_freq 80m --flash_size 4MB 0x1000 bootloader/bootloader.bin 0x10000 salmon.bin 0x8000 partition_table/partition-table.bin
esptool.py v4.7.dev1
Serial port /dev/cu.usbmodem01
WARNING: Pre-connection option "no_reset" was selected. Connection may fail if the chip is not in bootloader or flasher stub mode.
Connecting...
Chip is ESP32-S2FH4 (revision v0.0)
Features: WiFi, Embedded Flash 4MB, No Embedded PSRAM, ADC and temperature sensor calibration in BLK2 of efuse V1
Crystal is 40MHz
MAC: 84:f7:03:e8:6c:5e
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00006fff...
Flash will be erased from 0x00010000 to 0x00047fff...
Flash will be erased from 0x00008000 to 0x00008fff...
Compressed 21888 bytes to 13918...
Writing at 0x00001000... (14 %)
Writing at 0x00002526... (28 %)
Writing at 0x00003181... (42 %)
Writing at 0x00003cd1... (57 %)
Writing at 0x00004740... (71 %)
Writing at 0x0000510d... (85 %)
Writing at 0x00005c33... (100 %)
Wrote 21888 bytes (13918 compressed) at 0x00001000 in 0.4 seconds (effective 460.3 kbit/s)...
Hash of data verified.
Compressed 227296 bytes to 125420...
Writing at 0x00010000... (1 %)
Writing at 0x0001118b... (3 %)
Writing at 0x000127d3... (4 %)
Writing at 0x00014109... (6 %)
Writing at 0x00015826... (8 %)
Writing at 0x000167e3... (9 %)
Writing at 0x00017d0b... (11 %)
Writing at 0x000190ea... (12 %)
Writing at 0x0001af34... (14 %)
Writing at 0x0001ca0d... (16 %)
Writing at 0x0001e202... (17 %)
Writing at 0x00020074... (19 %)
Writing at 0x00020bf2... (20 %)
Writing at 0x0002173a... (22 %)
Writing at 0x00022163... (24 %)
Writing at 0x00022bd0... (25 %)
Writing at 0x000235c3... (27 %)
Writing at 0x0002434f... (29 %)
Writing at 0x00024da2... (30 %)
Writing at 0x00025a50... (32 %)
Writing at 0x00026520... (33 %)
Writing at 0x00027245... (35 %)
Writing at 0x00027e1b... (37 %)
Writing at 0x00028951... (38 %)
Writing at 0x000293db... (40 %)
Writing at 0x00029f7d... (41 %)
Writing at 0x0002ab38... (43 %)
Writing at 0x0002b5be... (45 %)
Writing at 0x0002c1a5... (46 %)
Writing at 0x0002cc40... (48 %)
Writing at 0x0002d68f... (50 %)
Writing at 0x0002e173... (51 %)
Writing at 0x0002ed9e... (53 %)
Writing at 0x000303de... (54 %)
Writing at 0x00031005... (56 %)
Writing at 0x00031a3f... (58 %)
Writing at 0x0003248c... (59 %)
Writing at 0x0003324c... (61 %)
Writing at 0x000345b0... (62 %)
Writing at 0x00035a77... (64 %)
Writing at 0x000376fa... (66 %)
Writing at 0x00038314... (67 %)
Writing at 0x00038e78... (69 %)
Writing at 0x00039e22... (70 %)
Writing at 0x0003a9cd... (72 %)
Writing at 0x0003b457... (74 %)
Writing at 0x0003c066... (75 %)
Writing at 0x0003cb09... (77 %)
Writing at 0x0003d6a6... (79 %)
Writing at 0x0003e256... (80 %)
Writing at 0x0003ee2d... (82 %)
Writing at 0x0003f9d6... (83 %)
Writing at 0x000404c5... (85 %)
Writing at 0x00040ffa... (87 %)
Writing at 0x00041b1d... (88 %)
Writing at 0x000426de... (90 %)
Writing at 0x000432b6... (91 %)
Writing at 0x0004439b... (93 %)
Writing at 0x000451bb... (95 %)
Writing at 0x00045b1c... (96 %)
Writing at 0x000466fb... (98 %)
Writing at 0x0004742a... (100 %)
Wrote 227296 bytes (125420 compressed) at 0x00010000 in 1.9 seconds (effective 944.5 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 103...
Writing at 0x00008000... (100 %)
Wrote 3072 bytes (103 compressed) at 0x00008000 in 0.1 seconds (effective 388.7 kbit/s)...
Hash of data verified.

Leaving...
Staying in bootloader.
Done

Os: MacOS
espflash Version: 3.0.1-dev

@italicmew
Copy link
Author

Running the same thing on a normal esp-32 seems to work fine with exactly same setup.

ESP32 flash logs

cargo espflash flash --monitor
[2024-03-25T23:32:46Z INFO ] Detected 2 serial ports
[2024-03-25T23:32:46Z INFO ] Ports which match a known common dev board are highlighted
[2024-03-25T23:32:46Z INFO ] Please select a port
[2024-03-25T23:32:48Z INFO ] Serial port: '/dev/cu.usbserial-0001'
[2024-03-25T23:32:48Z INFO ] Connecting...
[2024-03-25T23:32:48Z INFO ] Using flash stub
    Finished dev [optimized + debuginfo] target(s) in 0.28s
Chip type:         esp32 (revision v1.0)
Crystal frequency: 40 MHz
Flash size:        4MB
Features:          WiFi, BT, Dual Core, Coding Scheme None
MAC address:       24:0a:c4:12:81:08
App/part. size:    412,432/4,128,768 bytes, 9.99%
[2024-03-25T23:32:50Z INFO ] Segment at address '0x1000' has not changed, skipping write
[2024-03-25T23:32:50Z INFO ] Segment at address '0x8000' has not changed, skipping write
[2024-03-25T23:32:50Z INFO ] Segment at address '0x10000' has not changed, skipping write
[2024-03-25T23:32:51Z INFO ] Flashing has completed!
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

ets Jun  8 2016 00:22:57

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 - esp_wifi::HEAP_DATA
    at ??:??
load:0x40078000,len:15576
load:0x40080400,len:4
ho 8 tail 4 room 4
load:0x40080404,len:3876
entry 0x4008064c
0x4008064c - core::ptr::read_volatile
    at /Users/fabricio/.rustup/toolchains/esp/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:1583
I (30) boot: ESP-IDF v5.1-beta1-378-gea5e0ff298-dirt 2nd stage bootloader
I (30) boot: compile time Jun  7 2023 07:48:23
I (32) boot: Multicore bootloader
I (37) boot: chip revision: v1.0
I (40) boot.esp32: SPI Speed      : 40MHz
I (45) boot.esp32: SPI Mode       : DIO
I (50) boot.esp32: SPI Flash Size : 4MB
I (54) boot: Enabling RNG early entropy source...
I (60) boot: Partition Table:
I (63) boot: ## Label            Usage          Type ST Offset   Length
I (70) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (78) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (85) boot:  2 factory          factory app      00 00 00010000 003f0000
I (93) boot: End of partition table
I (97) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=11d4ch ( 73036) map
I (132) esp_image: segment 1: paddr=00021d74 vaddr=3ffb0000 size=00e10h (  3600) load
I (134) esp_image: segment 2: paddr=00022b8c vaddr=3ffb8d24 size=00190h (   400) load
I (138) esp_image: segment 3: paddr=00022d24 vaddr=40080000 size=0cc7ch ( 52348) load
I (168) esp_image: segment 4: paddr=0002f9a8 vaddr=00000000 size=00670h (  1648) 
I (169) esp_image: segment 5: paddr=00030020 vaddr=400d0020 size=44ac8h (281288) map
I (281) boot: Loaded app from partition at offset 0x10000
I (281) boot: Disabling RNG early entropy source...
INFO - Logger is setup
Hello world!
INFO - esp-wifi configuration Config { rx_queue_size: 5, tx_queue_size: 3, static_rx_buf_num: 10, dynamic_rx_buf_num: 32, static_tx_buf_num: 0, dynamic_tx_buf_num: 32, ampdu_rx_enable: 0, ampdu_tx_enable: 0, amsdu_tx_enable: 0, rx_ba_win: 6, max_burst_size: 1, country_code: "CN", country_code_operating_class: 0, mtu: 1492, heap_size: 65536, tick_rate_hz: 50, listen_interval: 3, beacon_timeout: 6, ap_beacon_timeout: 300, failure_retry_cnt: 1, scan_method: 0 }
Loop...
Loop...
Loop...
Loop...

@SergioGasquez
Copy link
Member

  • Just to confirm, you are pressing Ctrl + R after the flashing has completed, right?
  • esptool is only flashing, and espflash also flashes successfully.
    • esptool is using --before=no_reset --after=no_reset
  • Have you tried to use espflash monitor?
    • Might also be useful to try the -b no-reset-no-sync flag

@italicmew
Copy link
Author

Hey @SergioGasquez, thanks for the quick reply!

1 - In the attempt mentioned above, I pressed Ctrl + R, waited briefly, manually reset the board using the RESET button, and then attempted to use espflash monitor.
2 - I tried exactly the same configuration as esptool is using, and the same thing happened: espflash flash -B 115200 --no-stub -a no-reset -b no-reset -c esp32s2 -m dio -f 80mhz -s 4mb
3 - Yes, I attempted that after manually resetting the board, but no serial file descriptor was detected. The board seemed to vanish, requiring me to manually enter in bootloader mode again.

@Vollbrecht
Copy link

I am not sure how its done in the no_std template, but the s2 is a bit special - e.g it can use usb-otg or usb-cdc-acm, but not both at the same time. So if you are flashing it and your binary doesn't setup usb-otg and your are not falling back to manually enabling usb-cdc the board just simply will not be there after it is started.

@Vollbrecht
Copy link

on esp-idf-template based project you need to set usb-cdc manually to be used via a sdkconfig but not sure how that all works on the no_std site.

@italicmew
Copy link
Author

@Vollbrecht Are you referring to this esp usb doc, right?

I was able to make the esp-idf-template work setting CONFIG_ESP_CONSOLE_USB_CDC=y in the sdkconfig.defaults file.

cargo espflash monitor -b no-reset --no-stub
[2024-03-26T20:46:24Z WARN ] Pre-connection option 'NoReset' was selected. Connection may fail if the chip is not in bootloader or flasher stub mode.
[2024-03-26T20:46:24Z INFO ] Detected 2 serial ports
[2024-03-26T20:46:24Z INFO ] Ports which match a known common dev board are highlighted
[2024-03-26T20:46:24Z INFO ] Please select a port
[2024-03-26T20:46:26Z INFO ] Serial port: '/dev/tty.usbmodem12301'
[2024-03-26T20:46:26Z INFO ] Connecting...
Commands:
    CTRL+R    Reset chip
    CTRL+C    Exit

I (38) boot: ESP-IDF v5.1.3 2nd stage bootloader
I (38) boot: compile time Mar 26 2024 17:34:50
I (38) boot: chip revision: v0.0
I (39) boot.esp32s2: SPI Speed      : 40MHz
I (39) boot.esp32s2: SPI Mode       : DIO
I (39) boot.esp32s2: SPI Flash Size : 4MB
I (39) boot: Enabling RNG early entropy source...
I (39) boot: Partition Table:
I (39) boot: ## Label            Usage          Type ST Offset   Length
I (40) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (42) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (43) boot:  2 factory          factory app      00 00 00010000 00100000
I (44) boot: End of partition table
I (44) esp_image: segment 0: paddr=00010020 vaddr=3f000020 size=1e060h (122976) map
I (77) esp_image: segment 1: paddr=0002e088 vaddr=3ffbe3d0 size=01894h (  6292) load
I (80) esp_image: segment 2: paddr=0002f924 vaddr=40024000 size=006f4h (  1780) load
I (81) esp_image: segment 3: paddr=00030020 vaddr=40080020 size=46250h (287312) map
I (158) esp_image: segment 4: paddr=00076278 vaddr=400246f4 size=09cdch ( 40156) load
I (176) boot: Loaded app from partition at offset 0x10000
I (177) boot: Disabling RNG early entropy source...
I (227) main_task: Started on CPU0
I (227) main_task: Calling app_main()
I (2227) blink_idf: Hello, world!
I (4227) blink_idf: Hello, world!

I am still unable to make this work with the no_std template. Do you have any code that I could review to understand how we can resolve this problem? I noticed your comments on similar issues in other repositories and I'm wondering if I can help with a solution.

@Vollbrecht
Copy link

I assume that not initiating the usb-cdc in some form on the no_std binary may be the problem here, though my knowledge is limited here as i am not actively using the no_std hal. Maybe someone else could tell if they implement this correctly here.

@SergioGasquez
Copy link
Member

We have an usb cdc example in esp-hal, see https://github.com/esp-rs/esp-hal/blob/main/examples/src/bin/usb_serial.rs

@italicmew
Copy link
Author

italicmew commented Mar 29, 2024

After spending some time on this, I believe I've grasped the core of the issue. The USB-CDC initialization occurs in the bootloader if CONFIG_ESP_CONSOLE_USB_CDC is set, as seen here. It's possible that the espflash bootloader was compiled without this configuration enabled.

Moreover, the flash stub provided doesn't seem to function as expected either, likely due to the absence of USB OTG support.

The example provided by @SergioGasquez seems to work for what is intended. However, it does not initialize the console itself, and I don't see any logs after reset, I still not able to see bootloader nor application logs.
Also, when flashed this example onto my esp32s2, the USB serial file descriptor appears on my macOS, but unfortunately, espflash monitor fails to communicate with it, leading to crashes.

$ cargo espflash monitor -b no-reset
[2024-03-29T18:52:45Z WARN ] Pre-connection option 'NoReset' was selected. Connection may fail if the chip is not in bootloader or flasher stub mode.
[2024-03-29T18:52:45Z INFO ] Detected 2 serial ports
[2024-03-29T18:52:45Z INFO ] Ports which match a known common dev board are highlighted
[2024-03-29T18:52:45Z INFO ] Please select a port
✔ Remember this serial port for future use? · no
[2024-03-29T18:52:48Z INFO ] Serial port: '/dev/cu.usbmodem12301'
[2024-03-29T18:52:48Z INFO ] Connecting...
[2024-03-29T18:52:48Z DEBUG] Writing command: Sync
Error:   × Main thread panicked.
  ├─▶ at /Users/xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/espflash-3.0.0/src/connection/mod.rs:363:30
  ╰─▶ called `Result::unwrap()` on an `Err` value: ParseIntError { kind: InvalidDigit }
  help: set the `RUST_BACKTRACE=1` environment variable to display a backtrace.

It appears that idf.py monitor is functioning correctly as it echoes back what I send in uppercase. However, it seems to be a separate issue unrelated to the current discussion.

@italicmew
Copy link
Author

Alright, I've decided to take a more proactive approach and have connected a USB-Serial adapter to the UART0 pins. As expected, all the console logs that were meant to be redirected to USB are present.

Moreover, when I flash a C esp-idf example with USB CDC enabled, all the console logs are successfully redirected to USB.

Now, here's my idea: What if I flash the bootloader generated in the C esp-idf project into my Rust esp-idf project using the esp flash --bootloader argument?

I attempted this approach using the following command:

cargo espflash flash -b no-reset --bootloader my_custom_bootloader/bootloader.bin --no-stub -m dio -s 4mb -f 80mhz

However, I couldn't get it to work as expected. The first-stage bootloader fails to the new bootloader; it simply does not print any logs in UART0 related to second stage bootloader, not creates any new USB file descriptor. Here's the output I'm getting from UART0:

<RESET>

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe61d0,len:0x1808
load:0x4004b000,len:0x4
load:0x4004b004,len:0xb10
load:0x4004f000,len:0x3200
entry 0x4004b1dc

<JUNK BEING PRINT...>

Here is the output of the esp-idf C project flashed using esptool:

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x9 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe61d0,len:0x1808
load:0x4004b000,len:0x4
load:0x4004b004,len:0xb10
load:0x4004f000,len:0x3200
entry 0x4004b1dc

<no junk>
<new usb file descriptor available>

Here is the output of rust project flashed with the espflash bootloader:

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x9 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:2
load:0x3ffe6108,len:0x17dc
load:0x4004b000,len:0x4
load:0x4004b004,len:0xab4
load:0x4004f000,len:0x3160
entry 0x4004b1c4
I (26) boot: ESP-IDF v5.1-beta1-378-gea5e0ff298-dirt 2nd stage bootloader
I (26) boot: compile time Jun  7 2023 08:05:03
I (28) boot: chip revision: v0.0
I (31) boot.esp32s2: SPI Speed      : 40MHz
I (36) boot.esp32s2: SPI Mode       : DIO
I (41) boot.esp32s2: SPI Flash Size : 4MB
I (46) boot: Enabling RNG early entropy source...
I (51) boot: Partition Table:
I (55) boot: ## Label            Usage          Type ST Offset   Length
I (62) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (69) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (77) boot:  2 factory          factory app      00 00 00010000 00100000
I (84) boot: End of partition table
I (89) esp_image: segment 0: paddr=00010020 vaddr=3f000020 size=03804h ( 14340) map
I (101) esp_image: segment 1: paddr=0001382c vaddr=3ffb3444 size=00008h (     8) load
I (106) esp_image: segment 2: paddr=0001383c vaddr=40022000 size=01444h (  5188) load
I (116) esp_image: segment 3: paddr=00014c88 vaddr=00000000 size=0b390h ( 45968) 
I (134) esp_image: segment 4: paddr=00020020 vaddr=40080020 size=05260h ( 21088) map
I (141) boot: Loaded app from partition at offset 0x10000
I (141) boot: Disabling RNG early entropy source...
INFO - Logger is setup
Hello world!

@italicmew
Copy link
Author

@SergioGasquez Is there any special steps I need to take to compile the bootloader from esp-idf? https://github.com/esp-rs/espflash/tree/main/espflash/resources - you mentioned that it's compiled by hand

@Vollbrecht
Copy link

Vollbrecht commented Mar 31, 2024

If you are using the esp-idf-template and build a hello-world, you can find that a corresponding bootloader is build and put out inside the output directory. So your output contains 3 files. The bootloader.bin the partition-table.bin and the hello-world elf file

@Vollbrecht
Copy link

cargo espflash has special code inside it so it can automatically pick up that file, while direct usage of espflash cannot directly locate that file and you manually need to tell espflash where it can be found.

@italicmew
Copy link
Author

@Vollbrecht Yup, I'm using the esp-idf-template - I compiled and generated the bootloader.bin and instructed espflash to use it with --bootloader path/to/esp-idf-template/../bootloader.bin. But that failed, see my previous comment. Should I also use the partition table created by the esp-idf-template? I didn't use that.

@italicmew
Copy link
Author

italicmew commented Mar 31, 2024

Same result as before with this command:

cargo espflash flash -b no-reset --bootloader /Users/tiburcio/Documents/workspace/esp-idf-template/build/bootloader/bootloader.bin --no-stub -m dio -s 4mb -f 80mhz --partition-table /Users/tiburcio/Documents/workspace/esp-idf-template/build/partition_table/partition-table.bin

Output from UART0:

<RESET>

ESP-ROM:esp32s2-rc4-20191025
Build:Oct 25 2019
rst:0x1 (POWERON),boot:0x8 (SPI_FAST_FLASH_BOOT)
SPIWP:0xee
mode:DIO, clock div:1
load:0x3ffe61d0,len:0x1808
load:0x4004b000,len:0x4
load:0x4004b004,len:0xb10
load:0x4004f000,len:0x3200
entry 0x4004b1dc

<JUNK BEING PRINT...>
<NO USB SERIAL>

@SergioGasquez
Copy link
Member

I think you should use --no-stub as the current stubs do not support USB-OTG (esp-rs/esp-flasher-stub#32) as you mentioned

@italicmew
Copy link
Author

italicmew commented Apr 2, 2024

I've been consistently using --no-stub in all my espflash flash commands, I'm aware of that issue with the stub. However, the challenge is the inability to access console output via USB on my developer board, which directly connects its USB to the ESP32-S2 chip.

To address this, I believe flashing a bootloader compiled with CONFIG_ESP_CONSOLE_USB_CDC=y, passing it using --bootloader to espflash, could resolve the issue. Unfortunately, I was not able to do it.

I'm not sure whether this is an issue or not. Is it expected behavior for console output to always redirect to the serial used for flashing the device? This seems to be the default behavior of esp-idf(flashtool), if I'm not mistaken. Perhaps we could enhance the granularity of espflash arguments to enable selection of an ESP32-S2 target that utilizes USB-OTG. For that we would need a new esp32bootloader-withusb.bin and more targets to be configured, which does not sounds as good solution.

@italicmew
Copy link
Author

italicmew commented Apr 2, 2024

Side note: Quick search on google shows a mixture of devBoards. Most of my results are boards without usb-serial converts. (search for esp32-s2)

Screenshot 2024-04-02 at 20 25 50

@SergioGasquez
Copy link
Member

When using cargo-espflash you dont need to point at the bootloader generated in the output folder as this is the default behavior. The --bootloader arg is useful for espflash or when not using the generated one in cargo-espflash.

This seems to be the default behavior of esp-idf(flashtool), if I'm not mistaken.

Could you verify this, please?

Perhaps we could enhance the granularity of espflash arguments to enable selection of an ESP32-S2 target that utilizes USB-OTG. For that we would need a new esp32bootloader-withusb.bin and more targets to be configured, which does not sounds as good solution.

I am not sure if I like the idea of adding more bootloaders, which result in a more difficult maintenance and are not really useful for most users.

@jath03
Copy link

jath03 commented Apr 28, 2024

I have been experiencing this issue while trying to flash the Flipper Zero Wi-Fi Module. I have been able to flash using espflash flash -b no-reset --no-stub, but logging hasn't been working when I try a no_std example like this one. The missing piece, I think is in esp-println, where, for esp32s2, it just writes data over UART.

What I would need to do is add a feature in the esp-println crate to use USB CDC and then somehow implement that. The espressif docs mention a USB CDC implementation in the ROM code. Does anyone know where I could find more information about the ROM code and the functions that might be there for USB CDC?

@daagaak
Copy link

daagaak commented May 18, 2024

I am not sure if I like the idea of adding more bootloaders, which result in a more difficult maintenance and are not really useful for most users.

I'm a little puzzled by this stance. I have three different "hobby" ESP32-S2 based dev boards (Adafruit ESP32-S2, Feather-S2 and an Adafruit MagTag) that all rely on USB CDC for their serial connection. So those boards are ergonomically hard to use since I moved up to espflash 3.

(Oh, and a Lolin ESP32-S2 based board too. I think it's a D2?)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

5 participants