Larry Wall once said that laziness is one of the 3 main virtues of a programmer. I guess that makes me a great programmer... Anyway, this post describes my ongoing quest to make programming an ATTiny as painless as possible, something that I seem strangely driven to achieve.
First, a note of explanation of the technique behind all of this. AVR chips include functionality called In Circuit System Programming (or ICSP, sometimes shortened to ISP). In essence, this allows the non-volatile memory on the chip to be reprogrammed at any time, even when it's already embedded in a circuit. ICSP works by holding the /RESET line low, which disables the normal operation of the chip, and then manipulating some special data lines to effectively communicate with an in-built primitive bootloader. AVR sell a number of programmer boards that implement ICSP, but they can be quite pricey (~ £50). All modern Arduino boards include a 6-pin ICSP header, clearly labelled, which allows your Arduino to be reflashed in situ, if you have such a programmer. To avoid confusion, I'm going to refer to the device that is generating ICSP control signals as the ICSP master, and the device being programmed as the ICSP slave.
The cheapest way to start programming any AVR chip, assuming that you have an Arduino already, is to use the Arduino as an ICSP master, using a sketch that simulates one of AVR's branded boards. This is all documented very well here.
Important note: The ArduinoISP sketch delivered with Ardunio 1.0 is broken. The easiest solution is to install Arduino 022 alongside your 1.0 installation, and follow those instructions (rather than the 1.0 instructions on the same site).
That works pretty well, but there are drawbacks:
- It uses up breadboard space, and takes sufficient time to build the circuit that you won't want to dismantle and remantle every time you program a chip.
- You have to upload the ArduinoISP sketch to your Arduino each time. If you're working on a project that uses an Arduino talking to another AVR chip, then you have to keep switching sketches. That's dull.
- You can't reflash your Arduino's chip, as it can't be both an ICSP master and ICSP slave at the same time.
- Having to install two versions of the Arduino IDE is annoying.
Surely we can do better than this...
My first enhancement was to build a little stripboard project that freed up the need for breadboard to hold my ICSP harness. By sticking an 8-pin DIL socket on stripboard, a row of header pins (note that you can use the GND connection next to A3, so 5 pins in a row will take care of most of the connections) allowing it to be plugged directly into the Arduino's header sockets. A couple of flying leads take care of the remaining connections. Here it is, both on its own and plugged into an Arduino.
That's a good start. Now it was a lot easier to switch the hardware in and out, though I still had to switch sketches on the Arduino every time. And pull the ATTiny out of the socket, and stick it into the breadboard, then vice versa, each cycle.
At this point, I decided to bite the bullet and plump for a USB ICSP programmer, to free up the Arduino too. Luckily, there's an open-source clone of the AVR boards, called USBtinyISP. I could have built one, but then I found one for about £10 here. That site looks marginally dodgy, but I didn't have any problems. A couple of things to be aware of with these:
- Some designs don't properly limit the USB data line voltage levels, which should range up to 3.3V, rather than the 5V as seen here. This is unlikely to damage anything, but you never know. More likely is difficulty making your PC (or especially Intel Mac) recognise it as a device, unless you plug in via an external USB hub. If you can, find one that mentions Zener diodes - those do it properly. I can't actually tell whether this one has them, sorry.
- At least on Intel Macs, the power light doesn't seem to light when first plugged in. That's odd, because it does on my Windows Vista (cough) laptop. I think it depends on the chipset. On the Mac, you need to actually start flashing something before the light comes on - after which, it stays on.
OK, that's pretty neat now:
Now I can select USBtinyISP as the programmer in Arduino 1.0, and everything is good. I can ditch Arduino 022, and don't need to use an Arduino to program anything. I can also now reflash my Arduino if I want to, via its ICSP slave header.
Can I take it still further? Of course! See the next post.