Martin's corner on the web

Flashing Arduino from SD card

Remote sketch uploading has been something I have long wanted for my projects. I can outline two major cases:

1) Remote flashing over TFTP – useful for remote locations where it won’t be convenient to go physically

2) Flashing from SD card – useful when you can provide firmware (compiled to .bin sketch) for downloading by end clients

Typically case 1 would require Internet connectivity and setting up port forwarding so that the unit is visible behind firewalls. Clients do not not like the idea of someone poking their firmware remotely as well, but for unattended projects this is adequate solution. The problem here comes from the greater hardware dependency on the Ethernet chip, most common are the WizNet 5100 and  ENC28J60.

The ENC28J60 options are Andrew Lindsay’s work on TFTP client for Nanode that downloads a .hex image in SRAM and a custom bootloader to flash it afterwards. Not an elegant solution in my view.

Another option, unfortunately not open-source is the one from Sketch Garden:

Sketch Garden lets you upload sketches to your Nanode over the web instead of with an FTDI cable. You develop your sketches in the Arduino environment as normal, compile and upload them to Sketch Garden, currently in private beta.

This means you can reprogram your Nanode in the field, from any computer anywhere.

Using Sketch Garden requires a new web enabled bootloader, available from Wicked Device soon.

Well, no good as well, I don’t want someone else to re-program my NaNodes quietly at night so they attack me in my sleep 🙂

The options for WizNet5100 chipset seem more viable, I found at least two bootloaders for that chipset:

https://github.com/mharizanov/TFTPBootloader_0_2

https://github.com/mharizanov/arduino-netboot

I tried the first one but failed to get it working, It does connect but then hangs for some reason. I haven’t had the time to poke further.

The second one seems really advanced one where the DHCP provides the binary image for flashing, looks way over my head at this time so I didn’t even try it.

So I re-focused on flashing a sketch from SD card, it looks like more platform compatible option. The option I was reviewing was this one:

I just released my proof-of-concept dual bootloader(serial/mmc) for the arduino platform. Code is on github

 

Features:

  • Just 2kb of flash, fits into normal 2kb bootblock of most atmega chips
  • Serial (stk500v1) bootloader, compatible with avrdude and Arduino IDE
  • Additional MMC bootloader, can read directly a intel .hex format file from an MMC/SD
  • Includes all of ladyada’s fixes to the original bootloader

You might ask why?. I have quite a few arduino “in the field”. With this bootloader, I can simply drag a .hex file to an SD card, insert it in an SD shield, and field-upgrade my boards without additional hardware. But I can still use the serial/usb port to develop on the very same hardware.

Well, lots of us tried to get this to work as it is the best option so far, but failed. The author seems to have abandoned the project and is not responding, therefore few of us opened a new thread in the Arduino Forums to try to get it working.

It all stalled, until one day I found an example in the PetitFatFs that was doing exactly what I needed! The AVR_BOOT provided the ability to flash a sketch from SD card. With minor tweaks, it was working on Arduino Ethernet.

I have created a fork on github here. The downsides are that this is a 4KB bootloader and it doesn’t support flashing over FTDI, but I can live with that. I have modified the original code to check if the binary image is changed and only upon change to re-flash it as the original version was flashin upon every reset. I also added some LED blinking on pin 9 so that I can visualise the re-flashing process.

I plan to modify the bootloader to look for sketch name in the EEPROM as well and if found, to flash from that name rather than the fixed “app.bin” name. That will allow the sketch to modify the firmware as needed.

This bootloader can work for both Arduino’s and Nanode’s with SD card, so I find it more platform independent.

I will be selling pre-flashed with that bootloader ATMega328s in the store soon.

Tagged on: , , ,

9 thoughts on “Flashing Arduino from SD card

  1. xxx

    If I got the idea right you are doing this:
    1. Have Arduino Ehernet (no programming using USB to RS232 possible, because there is RJ45 socket instead USB type B connector)
    2. Put the HEX in to the card, insert it and during boot time the Arduino is updated with new firmware from the card.

    Have you considered this idea: Upload the .HEX file to the SD card using the Arduino Ethernet capabilites, then restart and then the bootloader will take the new image form the SD.

    1. admin Post author

      1) The Arduino Ethernet does have a FTDI for serial programming, but this particular bootloader doesn’t support it because of size constraints.
      2) You are correct, but instead of .HEX it requires a .BIN file ( generated with avr-objcopy from the .HEX ); There is no room left for a .HEX parser right now, but it is a to-do

      Yes, I have considered that Idea and spent some time working on it, basically I am trying to get the a Webduino based file manager to work, unsuccessfully so far.
      http://code.google.com/p/webduino/issues/detail?id=10

      In another aspect, I have a fork of the code that reads the firmware file name from EEPROM (if exists) and tries to load that firmware. So that would mean that the Arduino can easily be programmed to run and switch firmwares based on certain needs. For example for firmware upgrade mode, it can flash itself with the File Manager firmware and you can upload the new firmware via the web, then require the Arduino to flash it.

      prospects are nice, aren’t they?

  2. xxx

    Huh, I did not read the specification (http://arduino.cc/en/Main/ArduinoBoardEthernet) carefully:

    The Ethernet differs from other boards in that it does not have an onboard USB-to-serial driver chip, but has a Wiznet Ethernet interface. This is the same interface found on the Ethernet shield.

    So it does not have a *on board* USB-to-Serial, but one can buy http://arduino.cc/en/Main/USBSerial plug it in to the six horizontal pins and can flash the chip from the PC.

    Anyway the whole idea of having a Ethernet enabled device and being forced to program it using USB-to-Serial is just lame. This bard should have been tftp bootloader enabled from day one 🙂

    The idea with the firmware switching will also work, but it would be a bit slow for rapid application development (but I guess in that case USB-to-Serial will also do the trick)

    There is another option also, just switch to Microchip, their ethernet enabled family (PIC18F97J60 for example) directly ships with a free, “Internet Bootloader App” demo project that you can burn only once (via PICKIT3 for example) and then have tftp bootloading from day 1 🙂

    I am currently thinking in that direction – I need to have a device that reads data from a server and displays some info – originally I was very excited about the whole Arduino story (I like the way they hide the boilerplate code from the user) but then more and more I think I come to realize that I may have to do it with Microchip … with microchip it will be a bit slow in the beginning becasue I have to produce a working ethernet board myself but then it will be easy with tftp bootloader already available.

  3. Pingback: 2boots bootloader on the uIoT: Serial + MMC bootloader in 2Kb | Martin's corner on the web

  4. Matt

    Hi Martin,
    Your above project is exactly what I’m attempting.
    I have an Arduino Uno and the Ethernet Shield.
    I have an AVRISPMKII connected on the Arduino
    I have created an Atmel Studio 6 project and opened your ‘main.c’
    I have requested AtmelStudio6 uses your ‘Makefile’ but it wont build.
    Is the above process the same as you followed? Would you happen to have an AtmelStudio6 project setup for this because I would love to test your system.
    Kind Regards,
    Matt

  5. Chad

    If I am understanding this correctly, if I use this bootloader, I would no longer be able to flash over USB, is that correct?

    Chad