The Circuitous Route To Arduino SAMD Board Program Loading

Like most of the AVR-based Arduino boards, there is a bootloader program resident on the SAMD boards (Arduino Zero, Arduino Nano 33 IoT, and the Arduino MKR series) to handle the loading of programs (AKA “Scripts”) via the USB-serial port. This program can erase and write into the Flash ROM in the microcontroller, and start programs in the ROM by offsetting the interrupt vector table to the start of the program’s memory and then jumping to the location given by the reset vector in the program’s interrupt vector table.

And this is indeed what will happen if there is no application program in the ROM. However there usually is one, even if just the Blink program that is pre-installed on new boards. Here is the process that happens when the bootloader is started:

  1. If there is no application program, go directly to the bootloader application, check for a device connected to the USB, and then start the loading process.
  2. Spend half a second looking for the reset button to be pressed twice. If this occurs, then go to the bootloader application. This provides a fail-safe in case the application program is totally hosed. (“hosed” is a technical term?)
  3. Start the application program. Yes, this is the old one we don’t want.
  4. If the USB-serial connects at 1200BPS and lowers the DTR (data terminal ready) signal, code that is embedded in the application program detects this, erases the application, and does a system reset which causes going back to step 1. Note that it can erase itself because when the application starts it moves the function that does the erasing into RAM, so that function doesn’t itself get erased!

When the loading process of the bootloader completes, the bootloader starts executing the new application. When the application is running, pressing the reset button will just restart the application and not start the bootloader.

It’s actually fairly easy to accidentally hang an application in such a way that the USB signalling cannot be detected, requiring the double tap reset to get to the bootloader. Note that the double tap must be done immediately after applying the power, since that’s the only time the double tap is looked for. Unfortunately this operation of the bootloader is difficult to find in the Arduino website’s documentation, and isn’t in the documentation for the boards at all. In the source code, the operation is buried in files in three separate folders.