ISP programming of Calunium Arduino clone using Raspberry Pi GPIO. Click on the image for an annotated version. |
Introduction
As a fully-featured Linux computer there are many external programmers that can be used with your Raspberry Pi to program the Atmel AVR range of microprocessors. It's also possible to use the general purpose input/output lines (GPIOs) found on the Raspberry Pi to implement an ISP programmer with minimal extra hardware. I say "with minimal extra hardware" because although it can be done with no extra hardware I recommend adding a buffer and FET to protect the Raspberry Pi. You might reasonably wonder what is the point if extra hardware should be used since external USB programmers can be bought cheaply from Ebay. However, if you are going to add an extension PCB to your Raspberry Pi anyway, for instance to communicate with a remote Atmel processor, then including an ISP programmer makes sense and adds very little cost.AVR ISP programming interface
Most of the Atmel AVR range can be programmed using an ISP interface which resembles the SPI bus. For this description I'll assume that the SPI pins are reused for this purpose but you should check the datasheet to make sure this is true for your part. The RESET pin is used as the active-low chip select pin, SCK is the clock signal, MOSI is the input data pin and MISO is the output data pin. If the directions seem odd remember that the microcontroller is acting as a SPI slave in this scenario; with that in mind the names make perfect sense. The ISP programmer then communicates with the microcontroller, sending commands to read or write flash memory, EEPROM, fuses, and/or locks. Avrdude supports many different programmers which can be used for this task.Raspberry Pi ISP programmer hardware
The simplest interface on the Raspberry Pi is to use four GPIO pins and bit-bang the SPI commands. I don't recommend this however. After programming has finished the SPI interface on the microcontroller could revert to master mode where SCK and MOSI become outputs. Connecting two logic outputs together which could be at opposite logic levels is not wise. For a safer interface I used the circuit below.Schematic diagram of ISP programmer. Also available as full-size image, Eagle schematic or PDF. |
On the target microcontroller the RESET signal is typically connected to a 10 kΩ pull-up resistor, often with a reset switch pulling to ground. The 2N7000 FET provides an open-drain interface which safely connects the Pi to RESET, even if the reset switch is pressed whilst the programmer is connected. Compare this to the case of directly wiring a Raspberry Pi GPIO; pressing the reset switch could short a high-level output directly to ground. Ouch. The 100 kΩ pull-down resistor connected to the gate of the FET ensures it is normally off (and thus RESET is high) when the reset GPIO pin is an input, such as after programming. The 100 kΩ resistor connected to RESET acts as a weak pull-up in case no pull-up resistor is connected to the microcontroller (e.g., for easy programming on a solderless breadboard). In the circuit above a jumper can be fitted to power the target microcontroller from the 3.3 V supply on the Raspberry Pi (care, maximum current 50 mA).
To protect the remaining Raspberry Pi GPIOs I have used a 74LVC244 buffer. The active-low OE (output enable) allows the outputs to be disconnected when the microcontroller is not held in its reset state. I chose this part because it operates at 3.3 V but safely tolerates 5 V on its inputs, and also 5 V applied to the outputs when they are in the high-impedance state.
The open-drain FET inverts the state of its input. In theory it should be possible to configure avrdude to account for this but I could not make it work with the patched GPIO support so I used a spare gate in the 74LVC244 to form a logic inverter. The circuit described above should be safe to use even if the target system operates at 5V (but don't fit the shunt to the jumper). I've not tested it, so make your own judgement! You should also note what whilst it should be safe, it isn't guaranteed to be reliable; typically the high output level from the 74LVC244 buffer will be recognised as a high but it doesn't meet the worst-case specifications.
GPIO pin mapping
Signal | GPIO | P1 header pin number |
---|---|---|
RESET | 8 | P1-24 |
SCK | 11 | P1-23 |
MOSI | 10 | P1-19 |
MISO | 9 | P1-21 |
Although the SPI has been implemented by bit-banging the pins chosen are the SPI pins on the Raspberry Pi P1 header.
Credits
Gordon Henderson for providing the patched avrdude compiled for the Raspberry Pi, see http://project-downloads.drogon.net/files/.Radoslav Kolev who submitted the patch to add linux GPIO to avrdude.
Update
This design has been incorporated into my RFM12B shield for Raspberry Pi which contains a two-channel AVR ISP programmer. One channel is used to flash both the on-board ATmega328P, the other channel can be used to program external devices.I've added a description of how to install and use the patched version of avrdude, http://blog.stevemarple.co.uk/2013/03/how-to-use-gpio-version-of-avrdude-on.html.