Flashing an Arduino Mega 2560 with firmware stored on a micro SD card

I was trying to determine if it is possible to remotely flash firmware on an ATmega2560 microcontroller using a micro SD card. I came across this repository of an ATmega2560 bootloader that flashes the microcontroller’s flash memory when a particular address on the EEPROM (Address: 0x1FF)  is set to the value ‘0xF0’.

When the microcontroller is reset, if the EEPROM value(at address ‘0x1FF’) is set to ‘0xF0’, the bootloader looks for a file ‘firmware.bin’ on the SD card and flashes the microcontroller with the new firmware.


Adafruit Music Maker shield mounted on the Arduino Mega2560

I had to make minor modifications to make use of this bootloader. I changed the Chip Select pin number of the micro SD Card (there are tutorials that explain how SD cards are interfaced to microcontrollers) in the macro definition to match that of the Adafruit Music Maker shield (the micro SD holder’s chip select pin is connected to Digital Pin 4 of the Arduino. This corresponds to pin 5, PORTG on the ATmega2560. Source: Arduino Mega 2560 schematic).

In the file asmfunc.S, I changed the CS pin to pin 5 on PORTG:

I also had to change the pin number of the LED used to indicate the progress of flashing the microcontroller by the bootloader.

After recompiling the bootloader’s source files, I flashed the new bootloader using AVR ISP MK II. I also had to make sure that the BOOTRST fuse was set in the ATmega2560’s fuse settings.


ATmega2560 fuse settings

I compiled an LED blinking sketch and converted it into a binary file by executing the following command:

Note: The Arduino IDE compiles and stores its hex files in a temporary directory. In order to save the hex file upon compilation, edit the IDE preferences after closing the IDE. The preferences text file can be located at File–>Preferences


Arduino IDE preferences

A line build.path has to be added to the preferences file. I included the path where the output of the compiled Arduino sketch would be located. I used Atmel Studio to convert the Arduino sketch’s hex file to a binary file (the avr-objcopy command shown in the above code snippet). I copied the binary file onto a micro SD card and inserted into the music maker shield’s micro SD card holder.

I made use of this sketch to set the value ‘0xF0’ at the address ‘0x1FF’. Upon reset, the bootloader flashed the microcontroller with the binary file located on the SD card.

Things to investigate:

1) When I was looking for a bootloader that would flash a microcontroller from an SD card, I read about problems encountered by others. I found out that people had problems using certain types of micro SD cards. I did have a hard time getting the bootloader working but it was due to the fact that I did not set the BOOTRST fuse.

Do let me know if you have used this bootloader or something similar in the past.


6 Responses to Flashing an Arduino Mega 2560 with firmware stored on a micro SD card

  1. Tech Pacs says:

    The ideas of mocrosd card is awesome and the blog is very intresting .

  2. Michel says:

    Thank you for your work.
    I was wondering how you compiled the modified bootloader you took from the repository…or did-I misunderstood something?

  3. DUTT says:

    Hi, i changed the boot loader source code as per my requirement, then how to recompile the boot loader code and generate .hex. Could you please help in this

  4. Pascal says:

    Thanks for sharing that with us. Awesome! It’s exactly what i’m looking for my small project. Download I file from a Web server to SD card and flash the AVR.

    I copied the hardware folder in my Arduino Folder and restart the IDE, but I can not find the Entry “Arduino mega 2560 (Dual sd bootloader)” as described.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: