|
| 1 | +# Deploy firmware to the SAMD boards and a NINA/ESP32 WiFi module. |
| 2 | + |
| 3 | +## Deploy the MicroPython firmware |
| 4 | + |
1 | 5 | For deploying the MicroPython firmware to the SAMD module, follow
|
2 |
| -the procedure: |
| 6 | +the usual procedure: |
| 7 | + |
| 8 | +1. Push reset twice or call machine.bootloader(), if there was already |
| 9 | + MicroPyhton installed at the board. A drive icon should appear |
| 10 | + representing a virtual drive. |
| 11 | +2. Copy the .uf2 file with the required firmware to that drive. As |
| 12 | + soon as the drive disappears, the firmware is loaded and started. |
| 13 | + |
| 14 | +If a MicroPython firmware is already installed at the board, it is also |
| 15 | +possible to enable the bootloader by shortly switching the USB Serial |
| 16 | +to 1200 baud and back, like it is done in the short script below. That |
| 17 | +allows for scripted update. |
| 18 | + |
| 19 | +```Py |
| 20 | +import serial |
| 21 | +import time |
| 22 | +import sys |
| 23 | +com=serial.Serial(sys.argv[1], 1200, dsrdtr=True) |
| 24 | +com.dtr=False |
| 25 | +com.rts=False |
| 26 | +time.sleep(0.2) |
| 27 | +com.close() |
| 28 | +time.sleep(4) |
| 29 | +# then copy the firmware file to the drive |
| 30 | +``` |
| 31 | + |
| 32 | +## Deploy the Wifi firmware. |
| 33 | + |
| 34 | +The WiFi module uses the NINA firmware, which works on any ESP32 module, |
| 35 | +like the u.Blox NINA W102, the Adafruit Airlink modules or a generic |
| 36 | +ESP32 device with at least 2 MB flash. Before the first use the appropriate |
| 37 | +firmware has to be loaded to the device. A pre-existing firmware might |
| 38 | +not work. The procedure differs between device types. |
| 39 | + |
| 40 | +### Firmware source and types. |
| 41 | + |
| 42 | +There exist two firmware versions, which only differ in the connection |
| 43 | +for the MOSI connection between host MCU and WiFi module. The connections |
| 44 | +for Wifi use are: |
| 45 | +``` |
| 46 | + Driver NINA Airlift Airlift |
| 47 | + Name W102 pin Name pin |
| 48 | + ---------------------------------- |
| 49 | + MOSI 12 MOSI 14 |
| 50 | + MISO 23 MISO 23 |
| 51 | + SCK 18 SCK 18 |
| 52 | + GPIO1 5 CS 5 |
| 53 | + ACK 33 Busy 33 |
| 54 | + RESET EN Reset EN |
| 55 | +``` |
| 56 | +The firmware binaries are available at https://github.com/micropython/micropython-lib/tree/master/micropython/espflash or https://github.com/robert-hh/Shared-Stuff. |
| 57 | +For a generic ESP32 device you can use either version, given that the |
| 58 | +MOSI signal is wired properly. If possible, add a **pull-down** resistor to |
| 59 | +the RESET pin, value 1.5 to 10k, such that RESET is kept low as long as |
| 60 | +WiFi is not used. That keeps the ESP32 in inactive state and it's GPIO pins |
| 61 | +at high impedance. |
| 62 | + |
| 63 | +### Firmware upload to a Generic ESP32 device. |
| 64 | + |
| 65 | +That is the easiest task, if the USB port of the device is available. Just |
| 66 | +follow the standard ESP procedure for uploading firmware using esptool.py with |
| 67 | +the command: |
| 68 | + |
| 69 | +``` |
| 70 | +esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0 NINA_FW_v1.5.0_W102.bin |
| 71 | +``` |
| 72 | + |
| 73 | +Note that the load address is 0. The port name will be different depending on |
| 74 | +the PC set-up and it's operating system. If the USB port is not available, follow |
| 75 | +the procedure for built-in modules. |
| 76 | + |
| 77 | +### Firmware upload for built-in or breakout modules |
| 78 | + |
| 79 | +For firmware upload, the following connections to the WiFi module are required: |
| 80 | + |
| 81 | +- Pin Reset (as above) |
| 82 | +- Pin GPIO0 |
| 83 | +- UART RX |
| 84 | +- UART TX |
| 85 | + |
| 86 | +The GPIO pins and UART device id varies between boards. If the WiFi module is hardwired |
| 87 | +to a MCU or Add-on board, the Pin and UART properties can be found in the documentation, |
| 88 | +otherwise any suitable pins can be used. On some add-on boards like the Adafruit ItsyBitsy, |
| 89 | +Feather or Arduino Shield modules solder bridges may have to be closed to RX, TX and GPIO0. |
| 90 | +RX and TX are needed for bluetooth as well. |
| 91 | +Once the wiring is done, the firmware can be uploaded, using the espflash.py module, a short script |
| 92 | +using espflash.py and mpremote. espflash.py is usually embedded into the MicroPython firmware |
| 93 | +of the SAMD device as frozen bytecode. Otherwise, it is available at https://github.com/micropython/micropython-lib/tree/master/micropython/espflash. This place also holds the example script. |
| 94 | + |
| 95 | +``` |
| 96 | +import espflash |
| 97 | +from machine import Pin |
| 98 | +from machine import UART |
| 99 | +import sys |
| 100 | +sys.path.append("/") |
| 101 | +
|
| 102 | +if True: |
| 103 | + reset = Pin("ESP_RESET", Pin.OUT) |
| 104 | + gpio0 = Pin("ESP_GPIO0", Pin.OUT) |
| 105 | + uart = UART(0, 115200, tx=Pin("ESP_TX"), rx=Pin("ESP_RX"), timeout=350) |
3 | 106 |
|
4 |
| -- Push the reset button twice or call machine.bootloader(). A drive |
5 |
| - icon should appear representing a virtual drive. |
| 107 | + md5sum = b"b0b9ab23da820a469e597c41364acb3a" |
| 108 | + path = "/remote/NINA_FW_v1.5.0_Airlift.bin" |
6 | 109 |
|
7 |
| -- Copy the .uf2 file with the required firmware to that drive. |
| 110 | + esp = espflash.ESPFlash(reset, gpio0, uart) |
| 111 | + # Enter bootloader download mode, at 115200 |
| 112 | + esp.bootloader() |
| 113 | + # Can now change to higher/lower baud rate |
| 114 | + esp.set_baudrate(921600) |
| 115 | + # Must call this first before any flash functions. |
| 116 | + esp.flash_attach() |
| 117 | + # Read flash size |
| 118 | + size = esp.flash_read_size() |
| 119 | + # Configure flash parameters. |
| 120 | + esp.flash_config(size) |
| 121 | + # Write firmware image from internal storage. |
| 122 | + esp.flash_write_file(path) |
| 123 | + # Compares file and flash MD5 checksum. |
| 124 | + esp.flash_verify_file(path, md5sum) |
| 125 | + # Resets the ESP32 chip. |
| 126 | + esp.reboot() |
| 127 | +``` |
8 | 128 |
|
9 |
| -As soon as the drive disappears, the firmware is loaded and started. |
| 129 | +The script shows the set-up for the Metro M4 Express Airlift board. |
| 130 | +The md5sum is the one of the WiFi firmware. It may change and |
| 131 | +can be recalculated using e.g. the Linux `md5sum` command. It is used to |
| 132 | +verify the firmware upload. To upload the firmware, place the firmware |
| 133 | +and the above script (let's call it ninaflash.py) into the same directory |
| 134 | +on your PC, and run the command: |
| 135 | +``` |
| 136 | +mprememote connect <port> mount . run ninaflash.py |
| 137 | +``` |
| 138 | +After a while the upload will start. A typical start sequence looks like: |
| 139 | +``` |
| 140 | +Local directory . is mounted at /remote |
| 141 | +Failed to read response to command 8. |
| 142 | +Failed to read response to command 8. |
| 143 | +Changing baudrate => 921600 |
| 144 | +Flash attached |
| 145 | +Flash size 2.0 MBytes |
| 146 | +Flash write size: 1310720 total_blocks: 320 block size: 4096 |
| 147 | +Writing sequence number 0/320... |
| 148 | +Writing sequence number 1/320... |
| 149 | +Writing sequence number 2/320... |
| 150 | +Writing sequence number 3/320... |
| 151 | +Writing sequence number 4/320... |
| 152 | +.... |
| 153 | +.... |
| 154 | +Writing sequence number 317/320... |
| 155 | +Writing sequence number 318/320... |
| 156 | +Writing sequence number 319/320... |
| 157 | +Flash write finished |
| 158 | +Flash verify: File MD5 b'b0b9ab23da820a469e597c41364acb3a' |
| 159 | +Flash verify: Flash MD5 b'b0b9ab23da820a469e597c41364acb3a' |
| 160 | +Firmware verified. |
| 161 | +``` |
| 162 | +The initial messages `Failed to read response to command 8.` |
| 163 | +can be ignored. |
10 | 164 |
|
| 165 | +Once the firmware upload is finished, the connection to RX, TX and GPIO0 |
| 166 | +can be removed, unless the modules is used to Bluetooth communication, |
| 167 | +which needs RX and TX. |
0 commit comments