espressif / esptool

Espressif SoC serial bootloader utility
https://docs.espressif.com/projects/esptool
GNU General Public License v2.0
5.6k stars 1.39k forks source link

Protocol error when trying to flash ESP32-C3 Mini (ESPTOOL-752) #924

Closed ma3oun closed 1 year ago

ma3oun commented 1 year ago

Operating System

Ubuntu 22.04 LTS

Esptool Version

4.5.1

Python Version

Python 3.9.13

Chip Description

ESP32-C3-MINI-1

Device Description

I'm using a custom board, using a design similar to this The ESP32 connects to the PC directly by USB (IO18 and IO19)

Hardware Configuration

I'm using a USB-C port on the board to connect to USB 2.0 or 3.0 standard ports on my PC. The ESP32 interacts with a INA3221 current sensor by I2C.

How is Esptool Run

Terminal (python esptool.py ...) and latest Arduino IDE (lolin C3 board selected)

Full Esptool Command Line that Was Run

python esptool.py -t --before usb_reset --chip esp32c3 write_flash --no-compress 0x00 ~/Arduino/BlinkWithoutDelay/build/esp32.esp32.lolin_c3_mini/BlinkWithoutDelay.ino.bin

Esptool Output

esptool.py v4.5.1
Found 1 serial ports
Serial port /dev/ttyACM0
Connecting...
/dev/ttyACM0 failed to connect: [Errno 71] Protocol error

A fatal error occurred: Could not connect to an Espressif device on any of the 1 available serial ports.

More Information

The PC seems to recognize the ESP32 when the USB cable is plugged in. dmesg output:

[ 8006.453801] usb 1-1: new full-speed USB device number 15 using xhci_hcd
[ 8006.627939] usb 1-1: New USB device found, idVendor=303a, idProduct=1001, bcdDevice= 1.01
[ 8006.627951] usb 1-1: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[ 8006.627955] usb 1-1: Product: USB JTAG/serial debug unit
[ 8006.627958] usb 1-1: Manufacturer: Espressif
[ 8006.627961] usb 1-1: SerialNumber: 64:E8:33:E2:9F:5C
[ 8006.635779] cdc_acm 1-1:1.0: ttyACM0: USB ACM device

Aduino IDE terminal output after selecting the "Lolin C3 mini board":

Traceback (most recent call last):
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool.py", line 34, in <module>

    esptool._main()
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/__init__.py", line 1032, in _main
    main()
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/__init__.py", line 674, in main
    esp = esp or get_default_connected_device(
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/__init__.py", line 921, in get_default_connected_device
    _esp.connect(before, connect_attempts)
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/loader.py", line 640, in connect
    last_error = self._connect_attempt(reset_strategy, mode)
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/loader.py", line 525, in _connect_attempt
    reset_strategy()  # Reset the chip to bootloader (download mode)
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/reset.py", line 100, in __call__
    self._setRTS(False)
  File "/home/toto/.arduino15/packages/esp32/tools/esptool_py/4.5.1/esptool/reset.py", line 40, in _setRTS
    self.port.setRTS(state)
  File "/opt/tools/anaconda3/lib/python3.9/site-packages/serial/serialutil.py", line 600, in setRTS
    self.rts = value
  File "/opt/tools/anaconda3/lib/python3.9/site-packages/serial/serialutil.py", line 463, in rts
    self._update_rts_state()
  File "/opt/tools/anaconda3/lib/python3.9/site-packages/serial/serialposix.py", line 708, in _update_rts_state
    fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_RTS_str)
OSError: [Errno 71] Protocol error
Failed uploading: uploading error: exit status 1

Other Steps to Reproduce

No response

I Have Read the Troubleshooting Guide

ma3oun commented 1 year ago

I have also tried the "no_reset" and "no_reset_no_sync" options without much success. I get "Failed to connect to ESP32-C3: No serial data received"

dobairoland commented 1 year ago

Could you please also share the output of dmesg after attaching the device and trying to flash?

ma3oun commented 1 year ago

I have already given the output of dmesg after attaching the device. When I try to flash it, dmesg outputs nothing more.

radimkarnis commented 1 year ago

Hi @ma3oun, this seems to be an issue with the drivers. Esptool fails due to the host system failing to set the RTS line. Inspecting your backtrace, you can actually see the error originates in pySerial, not esptool. I am not sure how to solve the drivers issue

I have also tried the "no_reset" and "no_reset_no_sync" options without much success. I get "Failed to connect to ESP32-C3: No serial data received"

Using the no_reset option requires you to put the ESP into the download mode manually (by pressing the RESET button while holding down the BOOT button - if your board has these). You can read about the process here.

This could be the workaround to your issue - place the devkit manually into the download mode and then run esptool with the --before no_reset option.

You can see a similar issue being discussed here.

ma3oun commented 1 year ago

Thanks @radimkarnis . Luckily, I can pull-down GPIO9 on my custom board and I was able to flash the board using the before no_reset option, as explained in the link you provided. Now, I can again flash the board directly from Arduino IDE without any custom option. This leads me to think that the problem seems to be in the bootloader.

radimkarnis commented 1 year ago

Now, I can again flash the board directly from Arduino IDE without any custom option.

In that case, the previously flashed code interfered with the USB console in pins IO18 and IO19.

I will update the documentation to mention this, I will leave this ticket open until then. Thanks for the report!