# TinyUF2 for iMXRT

TinyUF2 port of iMXRT runs entirely on SRAM which is not only super fast but also easy to perform self-update. After powering on, if TinyUF2 already exists on external flash, it will be loaded to internal SRAM and start executing from there.

## Initial Flash

To initially flash TinyUF2 on your blank board or board that is shipped with other bootlaoder. You could either use external debugger or BootROM

### External Debugger

jlink or pyocd can be used to program .bin file to appropriate address on external flash which is typically **0x60000000** (RT1062) or **0x60000400** (RT1011). This  can be done with `flash-jlink-bin` or `flash-pyocd-bin` make target.

```
make BOARD=metro_m7_1011 flash-jlink-bin
```

### Serial Download Mode with BootROM

iMXRT has built-in BootROM that implements the Serial Download Protocol (SDP), which can be used to load & execute TinyUF2 to SRAM with `spdhost` tool via USB. You need to

1. Instal the NXP SPSDK as described in the [SPSDK Installation Guide](https://spsdk.readthedocs.io/en/latest/usage/installation.html).  

    a. Be sure to activate the venv before running sdphost.

    b. If you are running linux, make sure your user has permission for
    accessing `hidraw` (more details below)

2. Power up your board with the Boot Mode switch set to `BOOT_MODE[1:0]=01` to enter Serial Download mode. Note: Serial Download mode also automatically run with blank flash, therefore you don't have to manual change it in your production run.

3. Run `flash-sdp` make target which in turn uses the `sdphost` with correct address and arguments to load and execute TinyUF2. While running, TinyUF2 will program the external flash with its SRAM's image.

  ```
  make BOARD=imxrt1010_evk flash-sdp
  ```

Note: Since SDP with BootROM doesn't requires external debugger and always exists regardless of the external flash, this method can also be used to de-brick your board should it be needed.

## Update to newer version

Double tap to enter bootloader mode, then simply drag & drop `update-tinyuf2_BOARD.uf2` into BOOT drive to update. The update file can be generated by running make with `self-update` target or simply download it from [release page](https://github.com/adafruit/tinyuf2/releases).

## Supported Boards

- [Adafruit Metro M7 1011](https://www.adafruit.com/product/4950)
- [MIMX RT1010 Evaluation Kit](https://www.nxp.com/design/development-boards/i.mx-evaluation-and-development-boards/i.mx-rt1010-evaluation-kit:MIMXRT1010-EVK)
- [MIMX RT1020 Evaluation Kit](https://www.nxp.com/design/development-boards/i.mx-evaluation-and-development-boards/i.mx-rt1020-evaluation-kit:MIMXRT1020-EVK)
- [MIMX RT1060 Evaluation Kit](https://www.nxp.com/design/development-boards/i.mx-evaluation-and-development-boards/mimxrt1060-evk-i.mx-rt1060-evaluation-kit:MIMXRT1060-EVK)

## Linux hidraw access

Linux requires setting permissions for accessing hidraw devices.  This is done by adding udev rules.  Follow these instructions to add permission.

1. Create file named `50-nxp.rules` with these contents:
  ```
  KERNEL=="hidraw*", ATTRS{idVendor}=="1fc9", MODE="0666"
  ```

2. Copy `50-nxp.rules` to `/etc/udev/rules.d/50-nxp.rules`

3. Reload the rules:
  ```
  sudo udevadm control --reload-rules
  sudo udevadm trigger
  ```
