dist/tools/uf2: integrate flashing of adafruit bootloader
Contribution description
Since #21281, boards using the adafruit nrf52 uf2 bootloader will default to keeping the SoftDevice (although not using it) and are checked to have the expected SoftDevice version present. If it is not, a warning is presented to the user requiring them to (re)install the bootloader manually.
This PR offers an automated way of installing the bootloader. Currently it doesn't work as intended though since the bootloader is for some reason required to be flashed twice (for the softdevice to be properly installed), and a second flashing fails unless the board was power-cycled in-between. We could certainly add interactive instructions for the user, but maybe there is a better way?
Also, the flash-dfu command requires pip3 install --user adafruit-nrfutil which is currently not checked for nor installed automatically. Maybe pipx would be an option.
Would be nice to have some kind of solution before the next RIOT release though, as previous users will otherwise need to perform the inconvenient manual bootloader reflash.
Testing procedure
-
make -C examples/basic/hello-world BOARD=feather-nrf52840-sense UF2_SOFTDEV=DROP flash, press enter to continue. -
make -C examples/basic/hello-world BOARD=feather-nrf52840-sense flash, press enter to continue and install the bootloader.
Issues/PRs references
Follow-up of #21281.
It's not entirely unlikely that that's a bug in adafruit-nrfutil. Apparently, the Activate Firmware and Validate Firmware don't do anything :sweat_smile:
https://github.com/adafruit/Adafruit_nRF52_nrfutil/blob/1361059009ff6a24d63b37eb3a4b28127837ead2/nordicsemi/dfu/dfu_transport_serial.py#L143-L145
https://github.com/adafruit/Adafruit_nRF52_nrfutil/blob/1361059009ff6a24d63b37eb3a4b28127837ead2/nordicsemi/dfu/dfu_transport_serial.py#L186-L192
It seems like not all banks are flashed, I downloaded the CURRENT.UF2 files during different steps and they are all different, but I can't really tell what's going on.
Flash Steps.zip
I played around with this more and I don't think that the adafruit-nrfutil is to blame here because the Mass Storage Device will pop up even before the timer for the activation period has elapsed. Therefore the bootloader must be already done at this point.
However I don't reeeeeeeeally feel like debugging the Arduino nRF52 Bootloader, but apparently it can be done with the nRF52840DK using Segger RTT to receive the debug output.
Also, the
flash-dfucommand requirespip3 install --user adafruit-nrfutilwhich is currently not checked for nor installed automatically. Maybepipxwould be an option.
The same is true for the Adafruit Clue and the Itsybitsy-nRF52; https://doc.riot-os.org/group__boards__adafruit-clue.html https://doc.riot-os.org/group__boards__adafruit-itsybitsy-nrf52.html
The adafruit-nrfutil does not have to be installed in /usr/local/bin, it can be executed locally (https://github.com/adafruit/Adafruit_nRF52_nrfutil?tab=readme-ov-file#create-self-contained-binary) and pip can also install into local directories with pip install --target <dir>
That might be an option to avoid installing pip package (semi-)globally?
Thanks for picking this up. We have a similar situation with the nrf52840dongle (which use the original nordic bootloader). Let's not slow down this effort over a generic solution, but on the long run, a make BOARD=myboard123 PROGRAMMER=obviously-not-the-default-because-that-one-cant-restore-itself restore-factory-bootloader would be nice to have on many boards. Might include ATMega boards just as well, particle, you name it.