Project 0 Bootloading
Overview
This section reviews the Atmex bootloader and how to use it to upload new firmware to the onboard micrcontroller.
What is this "bootloader" thing, anyways?

A bootloader is a piece of software that is the first thing to run (i.e. when the system "boots up") and can either perform some sort of administative function or run another program (i.e. "loads software"). Anybody who has run or installed Linux, BSD or another unix-like operating system is very familiar with the idea behind a bootloader: for example, both LILO (LInux LOader) and GRUB (?) are bootloaders. They are small pieces of software that run at boot-time and can load linux, or perhaps another OS, and can be used to send custom kernel commands.

So what does the bootloader actually do? Well, the bootloader is actually a program that lets you program the microcontroller using the serial port. Basically: using the bootloader you can send new firmware to the chip and it will program the permanent memory of the microcontroller (which we will refer to as flash memory to differentiate it from the non-program EEPROM memory ) with that firmware. This is really neat, actually. Only somewhat recently did microcontrollers gain the ability to progam themselves!

Some history: If you want to make an electronic device with a 'program' in it, you write ('burn') the code onto ROM (read-only-memory). ROM is convenient because it is dirt cheap and it cant overwrite itself. If you can only program it once, ROM is called PROM (programmable ROM). Of course, writing it once is pretty risky because you can always mess up. And if you're not 100% sure that the program is ready to go, you'll burn through lots and lots of PROMs. A long time ago, if you were a developer and you wanted to write a program for a chip, you would get a programmer (cheap ones are about $100-$50), a few EPROMs (Erasable Programmable ROM) and a UV eraser. The UV eraser is a box with strong UV bulb in it. The EPROMs look like chips, but they have a quartz window in the middle and through the window you can see the actual chip! You can program the chip with the programmer and erase it by sticking it in the box with the light on for about 10 minutes. (Read a more technical discussion about EPROMs at Wikipedia

Obviously, this system sucked. You'd have to take the chip out of the circuit, put it in the box, maybe you had a queue of empty ones and you'd have to keep swapping them in and out. They were also pretty expensive. Luckily, some people invented flash memory/EEPROMs (Electrically-erasable PROMs) which are exactly what you think: instead of using UV, the chip can be erased electronically. Hooray! The next step is that some people developed the technology so that the microcontroller can actually erase and program its own program-memory/flash. Basically this was done with the sole intention of letting people have bootloaders. Nowadays, almost all electronics with a microcontroller in it (your digital camera, your mp3 player, your DVD player, etc) have bootloaders so that you can "upgrade the firmware/flash" over USB. When I was a wee girl and had a modem that could be upgraded to be faster & more reliable, I had to ship the modem off to the company, they upgraded it & shipped it back after a week. Now its instant!

Of course, there is some downsides to this capability. The most obvious one is that you can, theoretically, overwrite your own code by accident. Worse is that you can somehow mess up the bootloader, making the system completely inoperable! For this reason, many microcontrollers have a special section of program memory dedicated to the bootloader and don't allow that section to be written to. Unfortunatly, while the chip we're using, the ATtiny2313 has bootloading capability, it doesn't have protection fuses. There's nothing stopping you from writing a program that erases the bootloader, eek! So try not to do that, eh?

How the bootloader works

The Atmex bootloader is a small piece of firmware that is programmed into the microcontroller somehow and is the first piece of code to run. The bootloader first blinks the LED (just to say 'hi', really). Then it checks to see if there's any other firmware (we'll call this the "user firmware" since you, the user, wrote it) already loaded onto the microcontroller. If there is, it listens to the serial port for a few seconds and waits for any sort of message. If it doesn't get any message within 3 seconds, it starts the user firmware program. If either there is no user firmware programmed in, or it gets a message within 3 seconds, it starts the actual bootloading part of the program. (This is kind of like hitting F8 during a Windows boot screen or stopping the auto-timeout on GRUB so-as to run the bootloader by hand.)

The real part of the bootloader is a fairly long chunk of code that listens to instructions coming from the serial port and then erases/reads/writes the program flash as instructed. Each instruction starts with a single letter and then maybe has some parameters that follow it. For example, the command 'S' instructs the bootloader to identify itself. (You can try this by opening up a terminal window connected to the COM port the Atmex is plugged into, at 19.2K/8N1 (19.2Kbps speed and 8 bit, No parity, 1 stop bit, which is quite standard these days and will be assumed unless stated otherwise). Then press the reset button on the Atmex and quickly afterwards typing S into the terminal window. You should see the identifying string AVR2313. Most all of the other commands have input or output that is tough to type out because they're in raw binary. For that reason, one doesn't use the bootloader by hand, laboriously typing commands into a serial terminal window. Instead, a piece of software is used.

The names and format of the commands for the Atmex bootloader conform to an existing standard called AVR910 (named so because of the application note in which the protocol is described) As such, existing microcontroller-programming software out there supports programming the Atmex bootloader out-of-the-box!

What to do if you lose the bootloader
text
How to use the bootloader

To talk to the bootloader & program the microcontroller, we'll need a piece of software to do the hard work of transferring the binary data over the serial port according to the AVR910 protocol. Lucky for us, theres a cross-platform tool all ready to go! It's called AVRDUDE and if you've gone through the process of installing the development environment, it should be installed too. One of the downsides to the software is that it is commandline only and the commands are a little long & clumsy. However, later on we will automate the programming in a Makefile so that we don't have to type out the full command every time.

OK, start by opening up a command line window (in Unix and MacOSX this is just a terminal, in Windows, go to Start->Run...->"cmd" to pop one up) Now type in avrdude to get a list of all the command options available.

There's a lot of them. But here is all you need to know:

OK so now we're ready to test it out. Plug in the Atmex board and verify it's powered up (the LED blinks). Type in avrdude -P <port> -c avr910 -p attiny2313 -u into the command window. Now reset the Atmex by pressing the reset button briefly and press return on the command above at the same time (or at least, within 3 seconds). You should get a response like this:

See? It even says Thank you, just like your mom always told you to.

OK now you are ready to actually upload some of that there firmware.

January 4, 2006 18:28