ESP32 erase_flash failed with “Invalid head of packet”

The first step before installing MicroPython to ESP32 is to erase the flash.

I’ve installed all necessary software like esptool, but the flashing was failing with error:

esptool.py --chip esp32 --port /dev/ttyUSB0 erase_flash                                                                                                         ──(Sat,Jun27)─┘
esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting........_____....._____....._____....._____....._____....._____....._____

A fatal error occurred: Failed to connect to ESP32: Invalid head of packet (0x1B)

The solution to the problem is to pres and hold BOOT button. Then start erase command mentioned above.

After the initial erase I was able to flash there MicroPython:

esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-idf4-20200627-unstable-v1.12-590-g9f911d822.bin

Then it was possible to connect via rshell:

rshell -p /dev/ttyUSB0                                                                                                                                          ──(Sat,Jun27)─┘
Using buffer-size of 32
Connecting to /dev/ttyUSB0 (buffer-size 32)...
Trying to connect to REPL  connected
Testing if ubinascii.unhexlify exists ... Y
Retrieving root directories ... /boot.py/
Setting time ... Jun 27, 2020 20:46:47
Evaluating board_name ... pyboard
Retrieving time epoch ... Jan 01, 2000
Welcome to rshell. Use Control-D (or the exit command) to exit rshell.

/home/georgik/projects/esp32> cd /pyboard/
/pyboard> ls
boot.py