2015-10-08

Talking to the SAM-BA Bootloader

So far, we've been using BOSSA to upload our program to the microcontroller. This has been working okay, but recently, when picking up this little project again after a few months of absence, a problem appeared: The program upload stopped working completely. The bossac command would just hang forever. This was a huge problem of course, as it blocked all further development efforts.

After performing the typical "programmer in trouble" rain dance for a while (which is usually an alternation between trying everything I can think of and increasingly desperate Google searches that only turn up useless forum posts), I bit the bullet and downloaded Atmel's official upload tool, SAM-BA. I had wanted to avoid this, as this is the kind of software that requires you leave your name and address before you can even download it. But I was desperate, so I involuntarily signed up to Atmel's mailing list and gave the tool a try.

It took me a moment to figure out how to use it[1], but I succeeded in using it to upload my program. That gave me a working solution and told me that at least the microcontroller wasn't broken.

I wasn't very happy about that though. First, I'd rather use an open source tool that I can understand and modify, if need be. Second, I'd like to make my development process as smooth as possible, and fooling around with some GUI application every time I need to test a program on a microcontroller just doesn't cut it.

Both BOSSA and SAM-BA use the /dev/ttyACM* devices to communicate with the microcontroller. I don't fully understand this topic, but basically, the Linux kernel makes even modern hardware look like a teleprinter from days of yore. User space programs can access such a device via a file in the /dev directory. Microcontrollers seem to end up as a /dev/ttyACM* file, for example /dev/ttyACM0[2].

Now, if a device is presented to me as a file, I should be able to use standard tools like cat and echo to talk to the device, shouldn't I? Well, let's try it. Follow these steps:

  1. Connect the Arduino to your computer via USB, using the native USB port on the board. That's the one closer to the reset button.
  2. Press the erase button for a few seconds[3].
  3. There should be at least one ttyACM* device in /dev. If there are multiple, pick one that looks right and re-try the following steps until you figured out the one that connects to the microcontroller. I'll use /dev/ttyACM0 as an example in the following steps.
  4. Start a background process that will print anything the device sends: cat /dev/ttyACM0 &
  5. Tell the bootloader to print its version[4]: echo V# > /dev/ttyACM0
  6. You should see something like the following: v1.1 Dec 15 2010 19:25:04

Now we know how to talk to the microcontroller directly, without using some complicated program that we don't understand as a mediator. I don't know yet where this will lead, but it certainly is exciting.

That's it for today. As always, the full code is available on GitHub. See you next time!