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?
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!
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:
- -p <partno>: This is just to tell it what microcontroller its programming, in this case, its the ATtiny2313 so just use 'attiny2313' as the partnumber.
- -C <config-file>: The config file tells avrdude about all the different ways it can talk to the programmer. Theres a default configuration file, so lets just use that: don't use this command switch
- -c <programmer>: Here is where we specify the programmer type, which in this case is 'avr910'
- -D: This disables erasing the chip before programming. We don't want that so don't use this command switch.
- -P <port>: This is the communication port to use to talk to the programmer. For windows users, the port is going to be something like 'com3' or whatever COM port you're using. For MacOS/Linux it will be something like /dev/cuaa3 or /dev/tts/ttyUSB0
- -F: This overrides the signature check to make sure the chip you think you're programming is. Its not required but it's good to have, so don't use this switch.
- -e: This erases the chip, in general we don't use this because we auto-erase the flash before programming.
- -U <memtype>:r|w|v:<filename>[:format]: OK this one is the important command. Its the one that actually does the programming. The <memtype> is either flash or eeprom (or hfuse, lfuse or efuse for the chip configuration fuses, but we aren't going to mess with those). the r|w|v means you can use r (read) w (write) or v (verify) as the command. The <filename> is, well, the file that you want to write to or read from. and [:format] means theres an optional format flag. We will always be using "Intel Hex" format, so use 'i'
So, for example. If you wanted to write the file "test.hex" to the flash memory, you would use -U flash:w:test.hex:i. If you wanted to read the eeprom memory into the file "eedump.hex" you would use -U eeprom:r:eedump.hex:i - -n: This means you don't actually write anything, its good if you want to make sure you don't send any other commands that could damage the chip, sort of a 'safety lock'.
- -V: This turns off the auto-verify when writing. We want to verify when we write to flash so don't use this.
- -u: If you want to modify the fuse bits, use this switch to tell it you really mean it. Ironically, the bootloader doesn't actually let you read/modify the fuse bits, but we will use this switch. That's because otherwise the programmer will try to read the fuse bits and will fail at that.
- -t: This is a 'terminal' mode where you can type out commands in a row. We don't want this, so don't use it.
- -E: This lists some specifications, don't use it.
- -v: This gives you 'verbose' output...in case you want to debug something. If you want you can use it but in general we won't.
- -q: This is the opposite of the above, makes less output. In general we won't use it but maybe after a while you wold like to.
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.