8000 mimxrt/bootloader: Enable support for the UF2 bootloader. by robert-hh · Pull Request #15983 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

mimxrt/bootloader: Enable support for the UF2 bootloader. #15983

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions ports/mimxrt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ BUILD ?= build-$(BOARD)
PORT ?= /dev/ttyACM0
CROSS_COMPILE ?= arm-none-eabi-
GIT_SUBMODULES += lib/tinyusb lib/nxp_driver
UF2CONV ?= $(TOP)/tools/uf2conv.py

# MicroPython feature configurations
MICROPY_VFS_LFS2 ?= 1
Expand Down Expand Up @@ -162,6 +163,13 @@ SRC_HAL_IMX_C += \
$(MCU_DIR)/drivers/fsl_romapi.c
endif

# If not empty, then it is 10xx.
ifneq ($(findstring MIMXRT10, $(MCU_SERIES)),)
APPLICATION_ADDR := 0x6000C000
else
APPLICATION_ADDR := 0x3000C000
endif

ifeq ($(MCU_SERIES), MIMXRT1176)
INC += -I$(TOP)/$(MCU_DIR)/drivers/cm7

Expand Down Expand Up @@ -253,6 +261,11 @@ else
$(error Error: Unknown board flash type $(MICROPY_HW_FLASH_TYPE))
endif

# Set a flag if the UF2 bootloader is used
ifeq ($(USE_UF2_BOOTLOADER),1)
CFLAGS += -DMICROPY_MACHINE_UF2_BOOTLOADER=1
endif

# Add sources for respective board flash type
# Add hal/flexspi_nor_flash.c or hal/flashspi_hyper_flash.c respectively
SRC_HAL_C += hal/flexspi_$(subst qspi_,,$(FLEXSPI_FLASH_TYPE)).c
Expand Down Expand Up @@ -412,6 +425,12 @@ endif
ifdef MICROPY_HW_FLASH_CLK
CFLAGS += -DMICROPY_HW_FLASH_CLK=$(MICROPY_HW_FLASH_CLK)
endif
ifdef MICROPY_HW_FLASH_QE_CMD
CFLAGS += -DMICROPY_HW_FLASH_QE_CMD=$(MICROPY_HW_FLASH_QE_CMD)
endif
ifdef MICROPY_HW_FLASH_QE_ARG
CFLAGS += -DMICROPY_HW_FLASH_QE_ARG=$(MICROPY_HW_FLASH_QE_ARG)
endif

CFLAGS += $(CFLAGS_EXTRA)

Expand Down Expand Up @@ -470,7 +489,11 @@ $(BUILD)/lib/tinyusb/src/device/usbd.o: CFLAGS += -Wno-missing-braces
# Build targets
# =============================================================================

ifeq ($(USE_UF2_BOOTLOADER),1)
all: $(BUILD)/firmware.hex $(BUILD)/firmware.bin $(BUILD)/firmware.uf2
else
all: $(BUILD)/firmware.hex $(BUILD)/firmware.bin
endif

# Process linker scripts with C preprocessor to exchange LDDEFINES and
# aggregate output of preprocessor in a single linker script `link.ld`
Expand All @@ -487,6 +510,10 @@ $(BUILD)/firmware.bin: $(BUILD)/firmware.elf
$(BUILD)/firmware.hex: $(BUILD)/firmware.elf
$(Q)$(OBJCOPY) -O ihex -R .eeprom $< $@

$(BUILD)/firmware.uf2: $(BUILD)/firmware.elf
$(Q)$(OBJCOPY) -O binary -R .stack -R .ivt -R .flash_config $^ $@-binpart
$(Q)$(PYTHON) $(UF2CONV) -b $(APPLICATION_ADDR) -f MIMXRT10XX -c -o $@ $@-binpart

# Making OBJ use an order-only dependency on the generated pins.h file
# has the side effect of making the pins.h file before we actually compile
# any of the objects. The normal dependency generation will deal with the
Expand Down
148 changes: 9 additions & 139 deletions ports/mimxrt/boards/ADAFRUIT_METRO_M7/deploy_metro_m7.md
Original file line number Diff line number Diff line change
@@ -1,141 +1,11 @@
## 1. Deploy the MicroPython firmware to the Metro M7 board.

### 1.1 Deploy the firmware using the serial bootloader.

For initial deployment of the firmware a few preparation steps are required, which
have to be done once.

1. Get the files ufconv.py and uf2families.json from the micropython/tools directory,
e.g. at https://github.com/micropython/micropython/tree/master/tools.

2. Get the NXP program sdphost for your operating system, e.g. from
https://github.com/adafruit/tinyuf2/tree/master/ports/mimxrt10xx/sdphost.
You can also get them from the NXP web sites.

3. Get the UF2 boot-loader package https://github.com/adafruit/tinyuf2/releases/download/0.9.0/tinyuf2-imxrt1010_evk-0.9.0.zip
and extract the file tinyuf2-imxrt1010_evk-0.9.0.bin.

Now you have all files at hand that you will need for updating.

1. Get the firmware you want to upload from the MicroPython download page.

2. Set the two BOOTSEL DIP switches to the 1/0 position, which is the opposite position of the normal use mode.

3. Push the reset button.

4. Run the commands:

```
sudo ./sdphost -u 0x1fc9,0x0145 -- write-file 0x20206400 tinyuf2-imxrt1010_evk-0.9.0.bin
sudo ./sdphost -u 0x1fc9,0x0145 -- jump-address 0x20207000
```
Wait until a drive icon appears on the computer (or mount it explicitly), and then run:
```
python3 uf2conv.py <firmware_xx.yy.zz.hex> --base 0x60000400 -f 0x4fb2d5bd
```
You can put all of that in a script. Just add a short wait before the 3rd command to let the drive connect.

5. Once the upload is finished, set the BOOTSEL DIP switches back to the 0/1 position and push reset.

Using sudo is Linux specific. You may not need it at all, if the access rights are set properly,
and you will not need it for Windows.

### 1.2 Deploy the firmware using a JTAG adapter.

With a JTAG adapter the firmware can be easily installed. Appropriate tools are Segger JFlash Lite and
the Segger Edu Mini adapter. Just use the firmware.hex file for loading, which will be loaded at the
proper address.


## 2. Deploy the WiFi firmware.

The NINA firmware in the NINA module has to be updated for use with MicroPython. That can be done
using MicroPython and two small Python scripts.

The firmware binaries are available at
https://github.com/micropython/micropython-lib/tree/master/micropython/espflash
or https://github.com/robert-hh/Shared-Stuff. For the Metro M7 board, the
NINA_FW_v1.5.0_Airlift.bin file is needed.

For firmware upload, the following connections to the WiFi module are required:

- Pin Reset (as above)
- Pin GPIO0
- UART RX
- UART TX

The GPIO pins and UART device id varies between boards. At the Adafruit Metro M7 board,
the UART is UART(1), and the Pin names for reset and GPIO0 are ESP_RESET and ESP_GPIO0.
The firmware can be uploaded, using the espflash.py module, a short script
using espflash.py and mpremote. espflash.py is available at
https://github.com/micropython/micropython-lib/tree/master/micropython/espflash.
This place also holds the example script.

```
import espflash
from machine import Pin
from machine import UART
import sys
sys.path.append("/flash")

reset = Pin("ESP_RESET", Pin.OUT)
gpio0 = Pin("ESP_GPIO0", Pin.OUT)
uart = UART(0, 115200, timeout=350)

md5sum = b"b0b9ab23da820a469e597c41364acb3a"
path = "/remote/NINA_FW_v1.5.0_Airlift.bin"

esp = espflash.ESPFlash(reset, gpio0, uart)
# Enter bootloader download mode, at 115200
esp.bootloader()
# Can now change to higher/lower baud rate
esp.set_baudrate(921600)
# Must call this first before any flash functions.
esp.flash_attach()
# Read flash size
size = esp.flash_read_size()
# Configure flash parameters.
esp.flash_config(size)
# Write firmware image from internal storage.
esp.flash_write_file(path)
# Compares file and flash MD5 checksum.
esp.flash_verify_file(path, md5sum)
# Resets the ESP32 chip.
esp.reboot()
```

The script shows the set-up for the Metro M7 board.
The md5sum is the one of the WiFi firmware. It may change and
can be recalculated using e.g. the Linux `md5sum` command. It is used to
verify the firmware upload. To upload the firmware, place the firmware
and the above script (let's call it ninaflash.py) into the same directory
on your PC, and run the command:
```
mpremote connect <port> mount . run ninaflash.py
```
After a while, the upload will start. A typical start sequence looks like:
```
Local directory . is mounted at /remote
Failed to read response to command 8.
Failed to read response to command 8.
Changing baudrate => 921600
Flash attached
Flash size 2.0 MBytes
Flash write size: 1310720 total_blocks: 320 block size: 4096
Writing sequence number 0/320...
Writing sequence number 1/320...
Writing sequence number 2/320...
Writing sequence number 3/320...
Writing sequence number 4/320...
....
....
Writing sequence number 317/320...
Writing sequence number 318/320...
Writing sequence number 319/320...
Flash write finished
Flash verify: File MD5 b'b0b9ab23da820a469e597c41364acb3a'
Flash verify: Flash MD5 b'b0b9ab23da820a469e597c41364acb3a'
Firmware verified.
```
The initial messages `Failed to read response to command 8.`
can be ignored.
The Metro M7 board comes pre-installed with a UF2 bootloader. It can
be started by pushing reset twice. Then the bootloader drive will
appear. If that does not happen or the bootloader was lost, you can
reinstall the bootloader using the instructions by Adafruit
here: https://learn.adafruit.com/adafruit-metro-m7-microsd/installing-the-bootloader

Once the bootloader is installed and started, you can install MicroPython
by copying the .uf2 version of the firmware file to the bootloader
drive. When the firmware is installed, the drive will disappear.
5 changes: 5 additions & 0 deletions ports/mimxrt/boards/ADAFRUIT_METRO_M7/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ MCU_VARIANT = MIMXRT1011DAE5A
MICROPY_FLOAT_IMPL = single
MICROPY_HW_FLASH_TYPE ?= qspi_nor_flash
MICROPY_HW_FLASH_SIZE ?= 0x800000 # 8MB
MICROPY_HW_FLASH_CLK = kFlexSpiSerialClk_133MHz
MICROPY_HW_FLASH_QE_CMD = 0x31
MICROPY_HW_FLASH_QE_ARG = 0x02

MICROPY_PY_NETWORK_NINAW10 ?= 1
MICROPY_PY_SSL ?= 1
MICROPY_SSL_MBEDTLS ?= 1

USE_UF2_BOOTLOADER = 1
5 changes: 5 additions & 0 deletions ports/mimxrt/boards/MIMXRT1010_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
10000 Expand Up @@ -4,6 +4,11 @@ MCU_VARIANT = MIMXRT1011DAE5A
MICROPY_FLOAT_IMPL = single
MICROPY_HW_FLASH_TYPE = qspi_nor_flash
MICROPY_HW_FLASH_SIZE = 0x1000000 # 16MB
MICROPY_HW_FLASH_CLK = kFlexSpiSerialClk_100MHz
MICROPY_HW_FLASH_QE_CMD = 0x31
MICROPY_HW_FLASH_QE_ARG = 0x02

USE_UF2_BOOTLOADER = 1

JLINK_PATH ?= /media/RT1010-EVK/
JLINK_COMMANDER_SCRIPT = $(BUILD)/script.jlink
Expand Down
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/MIMXRT1011.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ flash_config_start = flash_start + 0x00000400;
flash_config_size = 0x00000C00;
ivt_start = flash_start + 0x00001000;
ivt_size = 0x00001000;
interrupts_start = flash_start + 0x00002000;
interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x00002400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
Expand Down
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/MIMXRT1015.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ flash_config_start = flash_start;
flash_config_size = 0x00001000;
ivt_start = flash_start + 0x00001000;
ivt_size = 0x00001000;
interrupts_start = flash_start + 0x00002000;
interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x00002400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
Expand Down
5 changes: 5 additions & 0 deletions ports/mimxrt/boards/MIMXRT1015_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@ MCU_VARIANT = MIMXRT1015DAF5A
MICROPY_FLOAT_IMPL = single
MICROPY_HW_FLASH_TYPE = qspi_nor_flash
MICROPY_HW_FLASH_SIZE = 0x1000000 # 16MB
MICROPY_HW_FLASH_CLK = kFlexSpiSerialClk_100MHz
MICROPY_HW_FLASH_QE_CMD = 0x31
MICROPY_HW_FLASH_QE_ARG = 0x02

MICROPY_BOOT_BUFFER_SIZE = (32 * 1024)

USE_UF2_BOOTLOADER = 1
5 changes: 5 additions & 0 deletions ports/mimxrt/boards/MIMXRT1020_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ MCU_VARIANT = MIMXRT1021DAG5A
MICROPY_FLOAT_IMPL = double
MICROPY_HW_FLASH_TYPE = qspi_nor_flash
MICROPY_HW_FLASH_SIZE = 0x800000 # 8MB
MICROPY_HW_FLASH_CLK = kFlexSpiSerialClk_133MHz
MICROPY_HW_FLASH_QE_CMD = 0x01
MICROPY_HW_FLASH_QE_ARG = 0x40

MICROPY_HW_SDRAM_AVAIL = 1
MICROPY_HW_SDRAM_SIZE = 0x2000000 # 32MB
Expand All @@ -12,6 +15,8 @@ MICROPY_PY_LWIP = 1
MICROPY_PY_SSL = 1
MICROPY_SSL_MBEDTLS = 1

USE_UF2_BOOTLOADER = 1

FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py

JLINK_PATH ?= /media/RT1020-EVK/
Expand Down
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/MIMXRT1021.ld
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ flash_config_start = flash_start;
flash_config_size = 0x00001000;
ivt_start = flash_start + 0x00001000;
ivt_size = 0x00001000;
interrupts_start = flash_start + 0x00002000;
interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x00002400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
Expand Down
3 changes: 3 additions & 0 deletions ports/mimxrt/boards/MIMXRT1050_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ MICROPY_HW_FLASH_SIZE = 0x4000000 # 64MB

MICROPY_HW_SDRAM_AVAIL = 1
MICROPY_HW_SDRAM_SIZE = 0x2000000 # 32MB
MICROPY_HW_FLASH_CLK = kFlexSpiSerialClk_133MHz
MICROPY_HW_FLASH_QE_CMD = 0x01
MICROPY_HW_FLASH_QE_ARG = 0x40

MICROPY_PY_LWIP = 1
MICROPY_PY_SSL = 1
Expand Down
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/MIMXRT1052.ld
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ flash_config_start = flash_start;
flash_config_size = 0x00001000;
ivt_start = flash_start + 0x00001000;
ivt_size = 0x00001000;
interrupts_start = flash_start + 0x00002000;
interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x00002400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00200000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
Expand Down
5 changes: 5 additions & 0 deletions ports/mimxrt/boards/MIMXRT1060_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ MCU_VARIANT = MIMXRT1062DVJ6A
MICROPY_FLOAT_IMPL = double
MICROPY_HW_FLASH_TYPE = qspi_nor_flash
MICROPY_HW_FLASH_SIZE = 0x800000 # 8MB
MICROPY_HW_FLASH_CLK = kFlexSpiSerialClk_133MHz
MICROPY_HW_FLASH_QE_CMD = 0x01
MICROPY_HW_FLASH_QE_ARG = 0x40

MICROPY_HW_SDRAM_AVAIL = 1
MICROPY_HW_SDRAM_SIZE = 0x2000000 # 32MB
Expand All @@ -12,6 +15,8 @@ MICROPY_PY_LWIP = 1
MICROPY_PY_SSL = 1
MICROPY_SSL_MBEDTLS = 1

USE_UF2_BOOTLOADER = 1

FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py

JLINK_PATH ?= /media/RT1060-EVK/
Expand Down
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/MIMXRT1062.ld
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ flash_config_start = flash_start;
flash_config_size = 0x00001000;
ivt_start = flash_start + 0x00001000;
ivt_size = 0x00001000;
interrupts_start = flash_start + 0x00002000;
interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x00002400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
Expand Down
4 changes: 2 additions & 2 deletions ports/mimxrt/boards/MIMXRT1064.ld
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ flash_config_start = flash_start;
flash_config_size = 0x00001000;
ivt_start = flash_start + 0x00001000;
ivt_size = 0x00001000;
interrupts_start = flash_start + 0x00002000;
interrupts_start = flash_start + 0x0000C000;
interrupts_size = 0x00000400;
text_start = flash_start + 0x00002400;
text_start = flash_start + 0x0000C400;
vfs_start = flash_start + 0x00100000;
text_size = ((vfs_start) - (text_start));
vfs_size = ((flash_end) - (vfs_start));
Expand Down
5 changes: 5 additions & 0 deletions ports/mimxrt/boards/MIMXRT1064_EVK/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ MCU_VARIANT = MIMXRT1064DVL6A
MICROPY_FLOAT_IMPL = double
MICROPY_HW_FLASH_TYPE = internal
MICROPY_HW_FLASH_SIZE = 0x400000 # 4MB
MICROPY_HW_FLASH_CLK = kFlexSpiSerialClk_100MHz
MICROPY_HW_FLASH_QE_CMD = 0x31
MICROPY_HW_FLASH_QE_ARG = 0x02

MICROPY_HW_SDRAM_AVAIL = 1
MICROPY_HW_SDRAM_SIZE = 0x2000000 # 32MB
Expand All @@ -12,6 +15,8 @@ MICROPY_PY_LWIP = 1
MICROPY_PY_SSL = 1
MICROPY_SSL_MBEDTLS = 1

USE_UF2_BOOTLOADER = 1

FROZEN_MANIFEST ?= $(BOARD_DIR)/manifest.py

JLINK_PATH ?= /media/RT1064-EVK/
Expand Down
Loading
Loading
0