|
| 1 | +# SPDX-FileCopyrightText: 2025 Brent Rubell for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +import time |
| 5 | +import board |
| 6 | +import busio |
| 7 | +import neopixel |
| 8 | +import digitalio |
| 9 | +import adafruit_miniesptool |
| 10 | + |
| 11 | +print("nina-fw Updater for Fruit Jam") |
| 12 | +print("Press FruitJam Button 3 to start flashing...") |
| 13 | + |
| 14 | +# Configure Fruit Jam's ESP32-C6 pinout |
| 15 | +tx = board.ESP_TX |
| 16 | +rx = board.ESP_RX |
| 17 | +reset = digitalio.DigitalInOut(board.ESP_RESET) |
| 18 | +reset.direction = digitalio.Direction.OUTPUT |
| 19 | +gpio0 = digitalio.DigitalInOut(board.I2S_IRQ) |
| 20 | +gpio0.direction = digitalio.Direction.OUTPUT |
| 21 | + |
| 22 | +# Setup Fruit Jam button #3 |
| 23 | +button = digitalio.DigitalInOut(board.BUTTON3) |
| 24 | +button.direction = digitalio.Direction.INPUT |
| 25 | +button.pull = digitalio.Pull.UP |
| 26 | + |
| 27 | +# Setup NeoPixels |
| 28 | +pixels = neopixel.NeoPixel(board.NEOPIXEL, 5) |
| 29 | +pixels.brightness = 0.5 |
| 30 | + |
| 31 | +# Wait for button press to begin flashing |
| 32 | +pixels.fill((0, 0, 255)) |
| 33 | +while button.value: |
| 34 | + time.sleep(0.1) |
| 35 | + |
| 36 | +print("Button pressed! Starting flashing process...") |
| 37 | +pixels.fill((255, 0, 0)) |
| 38 | + |
| 39 | +# Initialize UART and esptool |
| 40 | +uart = busio.UART(tx, rx, baudrate=115200, timeout=1) |
| 41 | +esptool = adafruit_miniesptool.miniesptool(uart, gpio0, reset, flashsize=4 * 1024 * 1024) |
| 42 | + |
| 43 | +# Attempt to sync with the ESP32-C6 |
| 44 | +esptool.sync() |
| 45 | +chip_name = esptool.chip_name |
| 46 | +if chip_name is None: |
| 47 | + pixels.fill((255, 0, 0)) |
| 48 | + time.sleep(3) |
| 49 | + raise RuntimeError("Unable to detect chip type!") |
| 50 | +print("Chip Name:", chip_name) |
| 51 | +print("Chip MAC Addr: ", [hex(i) for i in esptool.mac_addr]) |
| 52 | +# Increase baudrate to speed up flashing |
| 53 | +esptool.baudrate = 912600 |
| 54 | + |
| 55 | +# Attempt to flash nina.bin to the ESP32-C6 |
| 56 | +print("Flashing...") |
| 57 | +pixels.fill((255, 255, 0)) |
| 58 | +try: |
| 59 | + esptool.flash_file("nina.bin", 0x0) |
| 60 | +except (OSError, RuntimeError) as e: |
| 61 | + pixels.fill((255, 0, 0)) |
| 62 | + time.sleep(2) |
| 63 | + raise RuntimeError("Flashing failed: " + str(e)) from e |
| 64 | + |
| 65 | +print("Done flashing, resetting..") |
| 66 | +esptool.reset() |
| 67 | + |
| 68 | +# Blink green to show we're done flashing |
| 69 | +while True: |
| 70 | + pixels.fill((0, 255, 0)) |
| 71 | + time.sleep(0.5) |
| 72 | + pixels.fill((0, 0, 0)) |
| 73 | + time.sleep(0.5) |
0 commit comments