One of the reasons I chose to use AVR microcontrollers with integrated USB is because of the Atmel DFU bootloader. For those that don’t know, DFU stands for Device Firmware Update and is a standardized USB device class that was created specifically for updating firmware. Since the Atmel bootloader resides in a protected area of memory, bad or stalled firmware changes will not render a board useless, ie: bricked. The board will always be recoverable by putting it into bootload mode. Also, the only hardware required to update the firmware on a board is a USB cable so basically, no extra hardware is required. Since full-speed USB is used at 12 Mbps, the firmware downloads are also quite fast.
Although these tutorials are written using the FreakLabs Chibi boards , they apply to any of the FreakLabs boards that uses an AVR with integrated USB or any AVR USB MCU that implements the hardware bootloader feature.
Windows Firmware DownloadingDownloading the firmware on Windows requires the use of Atmel’s free FLIP (FLexible In-system Programmer) software. The following steps outline how to install and use it to download your code to the microcontroller.
Install the Atmel FLIP application. The first thing you need to do is install the Atmel FLIP application. You can get it from here:
Atmel FLIP Software Link Note: You’ll also need Java to run Atmel FLIP which is kind of weird since the software is only used for Windows machines. If you don’t have it installed, you can pick up the special version that includes the Java runtime. They might as well saved the trouble and used .NET to write it. The Linux version hasn’t been updated since 2007 which is why I need to have separate tutorials for Windows and Linux.
- Connect the USB. You’ll need a USB cable with a mini-USB connector on one end to connect to the target. Plug one side into the PC and the mini-side into the target board. Power on the board. In case your board is off, you’ll need to flip the slide switch to the opposite position.
- Put board into bootloader mode. To put the board into bootloader mode, you need to follow a specific key sequence. You first push and hold down the BOOT button on the board. Then you need to press and de-press the RESET button. All FreakLabs USB-based AVR boards have these two buttons. You should hear the Windows USB Connect sound. You can let go of the BOOT button now.
- Load the driver. If this is your first time connecting the target board, you’ll probably get the “New Hardware” dialog box. On a Windows machine, you’ll need to load a driver that tells the OS who the device is and how to access it. The Atmel's USB DFU class driver should already be installed when you install FLIP and you just need to point it to Atmel’s .INF file. To do this, you’ll need to specify that Windows doesn’t need to automatically search for the driver and that you’ll tell it where to get it. Although my OS is in Japanese, the sequence of the options is the same as for the English version. The Atmel .INF file is located in the
%INSTALL DIRECTORY/Atmel/FlipXXX/usbdirectory where XXX just stands for the version of FLIP that you’re using.
- Start Atmel FLIP. Once the driver is loaded, you can start up the Atmel FLIP software. You’ll need to specify the target MCU. For Chibi boards , it will be the ATmega32U4. For the FreakLabs Pro AVRUSB128 , it will be the AT90USB1286. Once the proper MCU is selected, push OK.
- Open the USB port. Next, click on the big USB cable button on the top. This specifies which port to open. You’ll want to click on the USB option and push Open.
- Specify the firmware file to be loaded. You’ll next need to specify the firmware hex file that you want to load to the target. Go to “File/Load Hex File” and select it. Then browse to the directory that contains your hex file and click on the hex file. Then push OK.
- Program the board. Now you should be all set up. Push “Run”. You should see a progress bar as the firmware downloads. It will actually download quite fast.
- Reset the device. You’ll need to restart the device to take it out of bootload mode. You can either push the Reset button on the board, cycle the board power, or push “Start Application” on the Atmel FLIP software.
Linux Firmware Downloading
Atmel does have a version of the FLIP software for Linux but its basically unusable and hasn’t been updated since 2007. That means that the latest devices will not be supported by the program. Because of this, an open source project was spawned called “DFU Programmer” which is available at SourceForge:
This software supports multiple Atmel devices that have integrated USB interfaces and support the USB DFU (Device Firmware Update) class. The following are the instructions to get this program working on Linux using Ubuntu v9.10.
- Get the packages. The first thing you want to get is the dfu-programmer package. Ubuntu has a nice package manager that makes getting and installing packages easy. The reason I chose Ubuntu v9.10 is because it supports a recent build of the dfu-programmer, v0.5.1. Builds previous to this won’t support the ATMega32U4 MCU which is the MCU used on the Chibi board. If you’re using a version of Ubuntu lower than v9.10, then you’ll need to build the latest version of the dfu-programmer. I’ll go through that at the end of this tutorial. To get the dfu-programmer, you simply need to go into a terminal window and type the following:sudo apt-get install dfu-programmer
- Put board into bootload mode. The next step is to connect the board to the Linux machine and put the AVR board into bootloader mode. To do this, you follow the same sequence as the Windows setup. You’ll need to push and hold down the BOOT button. After that, you push and release the RESET button on the board.
You can do a quick check to see if the USB recognizes the board in bootloader mode. Just type:lsusband you should see the Atmel MCU in the listed USB devices.
- Erase the device. The next thing you’ll want to do is erase the device using the DFU programmer. The syntax is: dfu-programmer <mcu name> erase
sudo dfu-programmer atmega32u4 erase
- Flash the device. Finally, you’ll want to flash the device. To do this, you’ll need to specify the mcu model and the filename of the firmware image you want to write into it. The syntax for the flash command is: dfu-programmer <mcu name> flash <filename>
sudo dfu-programmer atmega32u4 flash chb_demo.hex
Appendix - Installing the Latest DFU Programmer for Linux
I recommend using Ubuntu 9.10 or higher if you’ll be using the Chibi boards since you can just download the dfu-programmer package and it will already have support for the ATMega32U4. However there will be some times when you need the latest version of the programmer like if you use a very recent version of a microcontroller. In those cases, you can either download the rpm and install it, or build the code from scratch yourself and install it.
Installing via rpm
- Download the rpm. Go to the Source Forge archives and download the .rpm file that you want to install. Here’s a link to the DFU Programmer project page at SourceForge.
- Install the rpm. If you’re using Ubuntu, then you’ll need the alien package to install rpm files. If you don’t already have this, then you can install it by typing the following:sudo apt-get install alienOnce you have alien, then you can install the rpm using the following command:sudo alien -i dfu-programmer.xxx.xxx.rpmwhere xxx.xxx is the version suffix of the dfu-programmer rpm file.
Installing via source code
- Download the source code. Go to the SourceForge archives and pick the source code file. Here is the DFU Programmer website where you can download the latest release code. Choose the source file tarball (.tar.gz format) that you want to build.DFU Programmer
- Unzip the files. Go to the directory where you downloaded the files and type:tar -xvf dfu-programmer-xxx.xxx.tar.gzwhere xxx.xxx is the version suffix for the source file tarball. The command unzips all the source files inside their own directory.
- Prepare to build. Go into the directory with the unzipped files. Building the programmer has some dependencies like the GCC toolchain and GNU make utility, but these are usually included in Ubuntu. Before you build, you should configure the build using the configure script inside the unzipped files directory. Type:
./configure --prefix=/usrThis configures the build and also checks if you have all the tools required to build the software installed. One of the things that you’ll probably need to get is the libusb development package. If this is not installed, then add it using the following command:sudo apt-get install libusb-devAdd anything else that the configure utility says that you don’t have.
- Build the code. To build the code, simply type:make
Updated 2010-06-07: Added AVR USB firmware downloading on a Mac:
- Install the image. To install the image, type:sudo make install
Mac Firmware Downloading
Downloading firmware on a Mac is fairly similar to downloading firmware on Linux, since both are based on a Unix type of environment. Once dfu-programmer is installed, its quite easy, however the most difficult part of downloading firmware on a Mac is just installing dfu-programmer. Here are the steps to get it on to the Mac:
Download and Install XCode. You’ll need to install the MacPorts installer which is a package manager for software thats ported to the Mac from other OSes. MacPorts will take care of the build and dependencies for you, but you’ll need to have XCode first for the build environment. You can find the MacPorts requirements list at the following site:
To download XCode, you’ll need to go to the Apple Developer’s website:
Once at the Apple Dev website, look for the XCode for Mac development. It will require registration as an Apple developer and then a rather large download on to your Mac. Once downloaded, install XCode.
Install MacPorts. MacPorts is an open source project to manage the building and installation of open source packages for the Mac. Its similar to the Ubuntu package manager where you just type the port you want at the command line and it will retrieve the source code, build it, and install it on to the Mac. There are “dmg” disk images for Snow Leopard, Leopard, and Tiger and you just need to choose which OSX version you’re running. The information about MacPorts is here:
Once you click on the OSX version, you’ll download the “dmg” image which will open to reveal a package. You just need to double click on the package to install MacPorts.
Get dfu-programmer and Install It. Now that the infrastructure is set up, you can just grab dfu-programmer from MacPorts and install it. To do that, you’ll need to go to the Mac Terminal window to get to the command line. Once in the command line, type:
sudo /opt/local/bin/port install dfu-programmer
Restart the Terminal. This might be a little facetious, however I found that the path wasn’t updated in the Terminal window after dfu-programmer was installed. To get it to work, you’ll need to close and re-open the terminal so that it will have the updated path. Then the OS should be able to see dfu-programmer and you can use it from whatever directory you’re working in.
Put the MCU target into bootloader mode. The next step is to connect the board to the Mac machine and put the AVR board into bootloader mode. To do this, you follow the same sequence as the Windows setup. You’ll need to push and hold down the BOOT button. After that, you push and release the RESET button on the board. You can do a quick check to see if the USB recognizes the board in bootloader mode. Just type:
and you should see the Atmel MCU in the listed USB devices.
Erase the Device. Now that your board is in bootload mode and it’s seen on the USB of the Mac machine, you can start erasing and flashing the device.
Here’s how to erase it: dfu-programmer <mcu name> erase
sudo dfu-programmer at90usb1286 erase
Flash the device. Finally, you’ll want to flash the device. To do this, you’ll need to specify the mcu model and the filename of the firmware image you want to write into it. The syntax for the flash command is: dfu-programmer <mcu name> flash <filename>
sudo dfu-programmer at90usb1286 flash rx.hex
I've also included this tutorial in a pdf format. You can download it here:
Firmware Downloading on AVR USB Microcontrollers