Flashing#

Flashing is the process of installing MicroPython on a microcontroller. The exact instructions vary. Below are the steps for installing MicroPython on an ESP32.

The first step in flashing MicroPython to a microcontroller is to find its port. The %discover --all magic reports this information:

%discover --all
/dev/ttyUSB0
  hwid         USB VID:PID=10C4:EA60 SER=0163A3DA LOCATION=1-1.3
  manufacturer Silicon Labs
  product      CP2104 USB to UART Bridge Controller
  interface    CP2104 USB to UART Bridge Controller

/dev/ttyAMA2
  hwid         fe201800.serial

/dev/ttyAMA1
  hwid         fe201600.serial

/dev/ttyAMA0
  hwid         fe201000.serial

A controller is installed at port /dev/ttyUSB0. The exact port name varies, and may even change if you unplug and replug the device.

ESP32#

Micropython.org has compiled interpreter binaries for select architectures. For ESP32, choose “Generic ESP32 module”, unless a more specific version is available. Update the firmware and port variables in the script below, then execute.

You can also build (customized) MicroPython firmware with ide49.

%%bash 

# set the correct port (%discover -all)
port=/dev/ttyUSB0

# find the latest firmware at https://micropython.org/download/esp32/
firmware=https://micropython.org/resources/firmware/esp32-20220117-v1.18.bin

# download
curl -O $firmware
    
# install (flash)
esptool.py -p $port erase_flash
esptool.py -p $port -b 460800 write_flash -z 0x1000 `basename $firmware`

# optionally delete the downloaded firmware
rm `basename $firmware`
Hide code cell output
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 1518k  100 1518k    0     0   393k      0  0:00:03  0:00:03 --:--:--  393k
esptool.py v3.2
Serial port /dev/ttyUSB0
Connecting......
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting......
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, Coding Scheme None
Crystal is 40MHz
MAC: 30:ae:a4:28:39:f0
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 11.2s
Hard resetting via RTS pin...
esptool.py v3.2
Serial port /dev/ttyUSB0
Connecting.......
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting.......
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, Coding Scheme None
Crystal is 40MHz
MAC: 30:ae:a4:28:39:f0
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x0017cfff...
Compressed 1555136 bytes to 1022998...
Writing at 0x00001000... (1 %)
Writing at 0x00011746... (3 %)
Writing at 0x00019ebe... (4 %)
Writing at 0x00021f00... (6 %)
Writing at 0x000292a7... (7 %)
Writing at 0x00032798... (9 %)
Writing at 0x0003cbac... (11 %)
Writing at 0x00046e05... (12 %)
Writing at 0x0004f85b... (14 %)
Writing at 0x00054e45... (15 %)
Writing at 0x00059ee0... (17 %)
Writing at 0x0005f0ff... (19 %)
Writing at 0x00064281... (20 %)
Writing at 0x00069344... (22 %)
Writing at 0x0006e416... (23 %)
Writing at 0x00072f94... (25 %)
Writing at 0x00077d93... (26 %)
Writing at 0x0007e702... (28 %)
Writing at 0x00083f6a... (30 %)
Writing at 0x00089558... (31 %)
Writing at 0x0008eca6... (33 %)
Writing at 0x000949a2... (34 %)
Writing at 0x00099d06... (36 %)
Writing at 0x0009f3d5... (38 %)
Writing at 0x000a522e... (39 %)
Writing at 0x000ab516... (41 %)
Writing at 0x000b125c... (42 %)
Writing at 0x000b64be... (44 %)
Writing at 0x000bbe59... (46 %)
Writing at 0x000c1bcb... (47 %)
Writing at 0x000c7347... (49 %)
Writing at 0x000cc367... (50 %)
Writing at 0x000d1724... (52 %)
Writing at 0x000d6606... (53 %)
Writing at 0x000db921... (55 %)
Writing at 0x000e0e41... (57 %)
Writing at 0x000e659a... (58 %)
Writing at 0x000ec105... (60 %)
Writing at 0x000f17ba... (61 %)
Writing at 0x000f651c... (63 %)
Writing at 0x000fb981... (65 %)
Writing at 0x0010103f... (66 %)
Writing at 0x0010619e... (68 %)
Writing at 0x0010b3c2... (69 %)
Writing at 0x00110b3d... (71 %)
Writing at 0x00115d19... (73 %)
Writing at 0x0011b22b... (74 %)
Writing at 0x0012056f... (76 %)
Writing at 0x00125cd6... (77 %)
Writing at 0x0012b102... (79 %)
Writing at 0x00130775... (80 %)
Writing at 0x00136069... (82 %)
Writing at 0x0013bc6c... (84 %)
Writing at 0x00141a1d... (85 %)
Writing at 0x00146bb4... (87 %)
Writing at 0x0014c88c... (88 %)
Writing at 0x00154179... (90 %)
Writing at 0x0015d497... (92 %)
Writing at 0x00163417... (93 %)
Writing at 0x00168c91... (95 %)
Writing at 0x0016e70d... (96 %)
Writing at 0x00173e28... (98 %)
Writing at 0x001796da... (100 %)
Wrote 1555136 bytes (1022998 compressed) at 0x00001000 in 25.1 seconds (effective 495.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...

The device is now available:

%discover
24:0a:c4:13:ad:14  serial:///dev/ttyUSB0  
%connect 24:0a:c4:12:87:7c
import sys
print(sys.implementation)
Connected to 24:0a:c4:12:87:7c @ serial:///dev/ttyUSB0
(name='micropython', version=(1, 16, 0), mpy=10757)

To flash firmware from a local file, update the port and firmware variables below, then run the cell.

%%bash 

# local firmware
port=/dev/ttyUSB0
firmware=~/micropython.bin
    
esptool.py -p $port erase_flash
esptool.py -p $port -b 460800 write_flash -z 0x1000 $firmware
Hide code cell output
esptool.py v3.2
Serial port /dev/ttyUSB0
Connecting.....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting.....
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, Coding Scheme None
Crystal is 40MHz
MAC: 30:ae:a4:30:80:14
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 4.6s
Hard resetting via RTS pin...
esptool.py v3.2
Serial port /dev/ttyUSB0
Connecting.....
Detecting chip type... Unsupported detection protocol, switching and trying again...
Connecting....
Detecting chip type... ESP32
Chip is ESP32-D0WDQ6 (revision 1)
Features: WiFi, BT, Dual Core, Coding Scheme None
Crystal is 40MHz
MAC: 30:ae:a4:30:80:14
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Flash will be erased from 0x00001000 to 0x00164fff...
Compressed 1457072 bytes to 966988...
Writing at 0x00001000... (1 %)
Writing at 0x000099b2... (3 %)
Writing at 0x00012347... (5 %)
Writing at 0x0001939e... (6 %)
Writing at 0x00022327... (8 %)
Writing at 0x0002da89... (10 %)
Writing at 0x0003815e... (11 %)
Writing at 0x0004271e... (13 %)
Writing at 0x00047db8... (15 %)
Writing at 0x0004ce55... (16 %)
Writing at 0x00051f55... (18 %)
Writing at 0x00056f23... (20 %)
Writing at 0x0005be0e... (21 %)
Writing at 0x00060a45... (23 %)
Writing at 0x000673cd... (25 %)
Writing at 0x0006c8a7... (26 %)
Writing at 0x00071cf6... (28 %)
Writing at 0x00076c98... (30 %)
Writing at 0x0007c2f9... (31 %)
Writing at 0x000822b1... (33 %)
Writing at 0x00087444... (35 %)
Writing at 0x0008ccb1... (36 %)
Writing at 0x00092a2b... (38 %)
Writing at 0x00098ddc... (40 %)
Writing at 0x0009e679... (41 %)
Writing at 0x000a3a37... (43 %)
Writing at 0x000a9ba1... (45 %)
Writing at 0x000af0cc... (46 %)
Writing at 0x000b405a... (48 %)
Writing at 0x000b9033... (50 %)
Writing at 0x000bdfe8... (51 %)
Writing at 0x000c33e3... (53 %)
Writing at 0x000c87cc... (55 %)
Writing at 0x000ce11f... (56 %)
Writing at 0x000d3ca0... (58 %)
Writing at 0x000d92b0... (60 %)
Writing at 0x000de084... (61 %)
Writing at 0x000e3a7d... (63 %)
Writing at 0x000e91b7... (65 %)
Writing at 0x000ee520... (66 %)
Writing at 0x000f3b83... (68 %)
Writing at 0x000f8e05... (70 %)
Writing at 0x000fe144... (71 %)
Writing at 0x00103497... (73 %)
Writing at 0x00108b87... (75 %)
Writing at 0x0010e1c0... (76 %)
Writing at 0x001136a8... (78 %)
Writing at 0x00119109... (80 %)
Writing at 0x0011f1d6... (81 %)
Writing at 0x00124bc7... (83 %)
Writing at 0x00129bcb... (85 %)
Writing at 0x0012fc99... (86 %)
Writing at 0x00137e18... (88 %)
Writing at 0x00140500... (90 %)
Writing at 0x00147929... (91 %)
Writing at 0x0014dcad... (93 %)
Writing at 0x0015323a... (95 %)
Writing at 0x00158bcc... (96 %)
Writing at 0x0015e1e9... (98 %)
Writing at 0x001641fc... (100 %)
Wrote 1457072 bytes (966988 compressed) at 0x00001000 in 23.5 seconds (effective 495.7 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...