Sunday, 15 July 2012

AVR/Arduino ISP programmer using the Raspberry Pi GPIOs

Atmel ISP programming from Raspberry Pi GPIO
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.




AVR ISP Programmer for Raspberry Pi
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


SignalGPIOP1 header pin number
RESET8P1-24
SCK11P1-23
MOSI10P1-19
MISO9P1-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.

Sunday, 1 July 2012

Wireless Gateway for Raspberry Pi

Wireless gateway for Raspberry Pi
Wireless gateway for Raspberry Pi. Click on the image for an annotated version.

Introduction

A wireless gateway for the Raspberry Pi is presented. The gateway will connect remote clients to the Raspberry Pi and the internet. I plan to use these to connect a network of magnetometer sensors to improve the reliability and availability of the AuroraWatch UK network. The gateway can also be used to program Atmel AVR microcontrollers, as used in the magnetometer sensors.

Design Goals

  • Footprint for XBee, Ciseco XRF, or similar radio module.
  • Footprint for Hope RFM12B radio module.
  • AVR ISP programming header.
  • Maximum board size 50 mm × 50 mm (to use cheapest Iteadstudio PCB fabrication option).
  • All unused digital I/O brought out to pads for easier extension, prototyping and hacking.
It is intended that all hardware directly interfacing with the gateway will use 3.3 V signal levels.

Features

The board has footprints for both RFM12B and XBee (or similar, such as the Ciseco XRF) radio modules and since they use different interfaces and GPIOs both can be used simultaneously. There is an FTDI connector to the Raspberry Pi's UART; this is shared with the XBee so both cannot be fitted at the same time. Only the minimum required set of digtal I/Os for each module have been hard wired. The RFM12B /CS input and /IRQ, and XBee /RESET, can be connected via jumpers. The remaining digital I/Os from the RFM12B, XBee and even the FTDI connector can be used by adding link wires to the desired Raspberry Pi's GPIOs on the second header footprint. If not needed then a pin header can be fitted to allow another expansion board to be stacked on top of the wireless gateway. The maximum current draw from the P1 connector's 3V3 pin is 50 mA. To enable higher power radio modules to be used the wireless gateway includes a built-in 3.3 V voltage regulator (250 mA limit)

Connecting to the gateway

To connect to the Raspberry Pi wireless gateway you might also be interested in Calunium, my shield-compatible Arduino clone based on the ATmega1284P. The latest version will include space for an RFM12B radio module.

Build it

At the moment the design is preliminary and untested. Eagle PCB design files are available under the Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) licence. The files will be updated as the design progresses.

Warning!

The Wireless Gateway for Raspberry Pi is intended to be used with 3.3 V devices only. Using it with 5 V devices (e.g., Arduino) will very likely damage your Raspberry Pi.