Over the air firmware updates on MKR GSM1400

I'm going to deploy a few MKR GSM1400s at remote sites and would like to be able to remotely update the sketch. I'd like to discuss my approach:

I would compile the new sketch and make it accessible over the internet by some protocol the Arduino supports, be it HTTP, FTP, MQTT (payload size dependant) or something else. The existing sketch would use the modem to download the new sketch and write it to an external memory chip. Once it had downloaded, the existing sketch would trigger a reset using the watchdog timer. On reset, the bootloader would then check the external memory chip for new firmware, and on detecting it flash the microcontroller with the new firmware. The new firmware must also have the ability to download new sketches.

I need a bootloader that can flash the SAM D21 processor from an external memory chip. After some searching I came across this:

This is a bootloader that can flash an Arduino Zero from a FAT16 or FAT32 formatted SD card. Should it be compatible with all of the boards that use the SAM D21 processor? I noticed that the official bootloaders are different for various MKR boards.

I need to install the bootloader on my Arduino. According to the SAM D21 datasheet (page 1), the SAM D21 can be flashed over SWD (Serial Wire Debug). The pin assignment is given in 7.2.2. According to the EAGLE files for the Arduino, the SWD interface is connected to the six large pads on the underside of the Arduino. So, I think I need to put some pogo pins in a breadboard to connect to the pads and buy an ATMEL ICE to install the new bootloader.

The nice thing about the bootloader is that you can put two firmware files on the SD card, selectable with pins on the Arduino. This could help mitigate the risk of remotely installing faulty firmware that can't update itself. I'd have a main firmware file that ran my application and could download new firmware, and a backup firmware file that only downloaded new main firmware. A button held down on reset would install the backup firmware.

In the event I installed bad main firmware, I could ask someone on site to use the button to install the backup firmware. The Arduino would then download and install new (hopefully good) main firmware.

  • Am I on the right track?
  • Has anyone done this already?

the Arduino WiFi101OTA library for upload from IDE over WiFi to SAMD21 Arduinos has two working options and one half-baked.

InternalStorage - the received binary is stored in upper half of the flash. after upload a function running in RAM copies the binary to normal run location in flash (after the bootloader) to run after reset

SDStorage - the uploaded binary is stored on SD card, then reset is invoked. the new sketch must use a SDU library, which is linked as 'second stage' bootloader and applies the binary from SD to flash above the SDU 'second stage'

external flash storage - same as SD Storage but I don't know about a library with the 'second stage' bootloader

you can use this objects InternalStorage and SDStorage as they are in the library without using the upload server of the library

I made a general version of this library enhancing it for almost all networking libraries and for ATmegas with at least 64kB flash. But it is still only a server, it doesn't have a binary download option. It is ArduinoOTA library.

I use it in my project, it works reliably. The Arduino is only downstairs built in in a box, but only time I had to get it was when I uploaded a not working sketch which didn't start at all. If the upload fails nothing wrong happen. I can start a new one. And the copying on flash is reliable.