Computing stuff tied to the physical world

Re-flashing and ISP

In AVR, Hardware on Dec 11, 2009 at 00:01

This is the second of two posts about everything related to uploading, re-flashing, bootstraps, FTDI, and ISP.

Yesterday’s post described the process of uploading new sketches via USB or RS232 using a boot loader.

But how did that boot loader get into the ATmega328?

That’s a bit of a chicken-and-egg problem. Ya’ can’t use a boot loader to get the boot loader into flash memory!

This is where a lower-level hardware-based mechanism called In System Programming (ISP) comes in. ISP is a clever trick in the ATmega chip which in effect activates a special boot loader built into the hardware. This is done by holding the RESET line of the ATmega low, at which point the chip becomes essentially useless, since the reset prevents the chip from starting to run its firmware. The trick is that in this mode, some of the pins on the ATmega become an interface to that special hardware-based built-in boot loader.

So what we need to do, is to manipulate those pins to send commands which will store data into the flash memory. What we’re going to send in most cases, is the data for the boot loader in upper memory. With that boot loader in place we can then switch to “normal” uploading via the serial interface and USB.

ISP programming is also used to change “fuse bits” on an ATmega. There are a few configuration settings for the ATmega – what type of clock it uses, how it boots up, enabling a watchdog timer, etc. These are stored in “fuses” which can only be adjusted via ISP programming.

There are 3 I/O pins involved in ISP programming, plus the reset line and power. On the Arduino as well as on the JeeNode and JeeLink boards, these lines are available via a 2×3-pin ISP connector with the following layout:

ISP pins.png

The interesting thing is that you don’t even need an Arduino or JeeNode to set up flash memory and fuses. ISP is so low-level that it works directly on the pins of an ATmega chip. This is very useful to pre-load the boot loader (or test program – anything you like, really) onto a chip before soldering it permanently onto a board.


In fact, I built these two little contraptions for exactly that purpose:

DSC_0852.jpg DSC_0854.jpg

The one on the left has a 28-pin ZIF (zero-insertion force) socket which makes it easy to place a 28-pin DIP chip in there, while the one on the right does the same for 32-TQFP chips (see the sample ATmega3278 chip next to the adapter). Both of these have that same 2×3-pin ISP connector, to hook them up to an ISP programmer.

What we need next is this thing called anĀ ISP programmer – a little bit of hardware which knows how to supply the proper voltages and signals to perform in-system programming (ISP, also sometimes called ICSP, i.e. In-Circuit Serial Programming).

I’m using Adafruit’s USBtinyISP programmer for this, which connects to USB. There are many more systems out there which can perform the same task. Some can do a lot more: parallel and/or high-voltage programming, JTAG and/or DebugWire debugging, etc.

So how do we get our boot loader to the ISP programmer?

The confusing bit is that again, the “avrdude” software can be used to talk to the ISP programmer – often also over USB. So from a distance, we’re doing almost the same thing with an ISP programmer as with the FTDI + boot loader configuration described in the previous post. But as should be clear by now, the low level processes are completely different: uploading takes places through a normal serial port connection and puts the boot loader (software!) on the ATmega itself in charge of storing the code into lower memory, while ISP programming puts the ATmega chip in a special hardware mode, and puts the ISP programmer (hardware!) in charge of storing code anywhere in flash memory. Similar effect, different mechanisms.

For the boot loader, we have no choice but use the ISP approach. Which means you need an ISP programmer.

But for normal sketches, we can use either. The advantage of FTDI + uploading, is that you don’t need the ISP programmer. You can use the same USB interface (on-board or via an extra interface) which you’ll probably use anyway for debugging and for getting all sorts of information into and out of an Arduino or JeeNode.

All you need for FTDI + uploading, is a working boot loader. Which is precisely why Arduino and JeeNode boards come with the ATmega328 chip pre-loaded with such a boot loader. And because boot loaders are well protected against getting damaged (see previous post), once that boot loader is on the chip, it will stay on there – ready to kick into action whenever you want to upload a different sketch.

But as you’ve also seen, at some point someone will have to use the ISP programmer first to start the ball rolling with a blank ATmega chip from Atmel, the chip manufacturer.

Some people already have an ISP programmer anyway and just use it to re-flash their code onto the ATmega all the time. They don’t need a boot loader, which also means that they get 1..4 Kb more room for their own software. In some cases, that extra bit of flash memory may be crucial.

But the Arduino’s FTDI + upload approach seems to be more common these days, even with non-Arduino boards. It saves you the cost and the learning curve of an ISP programmer, and you won’t need ISP connectors or cables.

  1. Hi Jean-Claude, Do you have to set any particular fuses on a virgin (fresh from the factory) Atmega328 before flashing the bootloader? I ask because I am able to flash an Atmega8 with no problem, using two different programmers, but I get the dreaded “rc=-1” error with a 328 (and 168), using the same setup. I’m using avrdude on a Ubuntu 9.04 box.

    • Yep – I use “-U efuse:w:0x05:m -U hfuse:w:0xDA:m -U lfuse:w:0xFF:m” with avrdude, same as standard Arduino’s AFAIK.

  2. Thank you JCW! I guess I have another problem for which I won’t ask for help here (versus me studying and learning more myself). I can flash Attiny2313’s and Atmega8’s (on different target boards, with two programmers), but not 328’s and 168’s. I am thinking I have something a foul with my avrdude.conf, or other support files. I was just hoping that a “magic flash” might work. Separate from that, I plan to order two GeeNodes from ModernDevice soon. Thanks again.

  3. “JeeNodes”that is – a senior moment from an old ham radio guy….

Comments are closed.