Tuesday, March 12, 2013

How to use the GPIO version of avrdude on the Raspberry Pi

Introduction

I previously showed an implementation of a AVR ISP programmer using the Raspberry Pi GPIO port which can be used to program Atmel's AVR range of microcontrollers with avrdude. An ISP programmer based on this design was incorporated into a shield to interface to the RFM12B radio module. This post explains how to use avrdude to actually program devices.

Software installation

Download the patched version of avrdude from http://project-downloads.drogon.net/files/. I also keep a copy in the RPi_RFM12B_ISP respository, https://github.com/stevemarple/RPi_RFM12B_ISP/tree/master/software/avrdude. You will probably want the armhf (hardware floating-point) version. Download the documentation package for avrdude too. Install the packages using "dpkg -i". For example

sudo dpkg -i avrdude_5.10-4_armhf.deb
sudo dpkg -i avrdude-doc_5.10-4_all.deb

Using avrdude over the GPIO interface is problematic for users other than root. The easiest solution is to give the avrdude binary setgroup permission:
sudo chmod g+s /usr/bin/avrdude

Usage

Selecting the GPIO programmer is simply a matter of including "-P gpio -c gpio" options; the -P option specifies that the GPIO port is used (as opposed to USB, serial or parallel interfaces) whilst the -c option selects the correct programmer type on that port.

For example, to check the signature on an ATmega328P execute the command
avrdude -P gpio -c gpio -p atmega328p

To read the fuses execute the command
avrdude -P gpio -c gpio -p atmega328 -U lfuse:r:-:h -U hfuse:r:-:h -U efuse:r:-:h

Customisation

The packages above define a single programmer called gpio which uses the gpio interface on GPIO pins 8 to 11. Since the RFM12B shield for Raspberry Pi implements two independent programmers I prefer to use gpio0 and gpio1. You can add these by creating a .avrduderc file in your home directory. The file should contain:

programmer
  id    = "gpio0";
  desc  = "Use sysfs interface to bitbang GPIO lines";
  type  = gpio;
  reset = 8;
  sck   = 11;
  mosi  = 10;
  miso  = 9;
;

programmer
  id    = "gpio1";
  desc  = "Use sysfs interface to bitbang GPIO lines";
  type  = gpio;
  reset = 7;
  sck   = 11;
  mosi  = 10;
  miso  = 9;
;

14 comments:

  1. I have been looking for some information on how to program micro controllers from the rp and this looks very good :)

    Just a little question can you compile the code and produce a hex file on the raspberry pi? Or do you compile the code on a computer and then transfer the hex file to the Raspberry pi and then upload it.

    Thanks a lot for your help :)

    ReplyDelete
    Replies
    1. I use both approaches. It is even possible to compile with the Arduino IDE on the Raspberry Pi and upload directly without manually invoking avrdude. I don't have any specific instructions to set this up but I do this with my RFM12B and Atmel ISP programmer shield. You can probably figure it out from the boards.txt and README in project's Github repository.

      Delete
    2. Oh ok just got this working today thanks a lot for the help :)

      Delete
  2. I'm asking what kind of programmer it is because avrdude and the Arduino IDE want to know it.

    ReplyDelete
    Replies
    1. IT's a gpio programmer. You can find an example of how the programmers.txt file should look in my RPi_RFM12B_ISP repository:
      https://github.com/stevemarple/RPi_RFM12B_ISP/blob/master/software/raspi/programmers.txt

      You can find an example boards.txt file at https://github.com/stevemarple/RPi_RFM12B_ISP/blob/master/software/raspi/boards.txt but you will need to modify the MCU entries to suit your needs.

      Delete
    2. Where must I do this? And how do I really write something on it with the Arduino IDE e.g. flashing the bootloader. Or how can I flash the Arduino BL with 3,3V on it with the avrdude.

      Delete
    3. You'll need the Arduino IDE installed on the Raspberry Pi. In your sketches folder create a folder called hardware. Put the programmers.txt file I referenced above into the hardware folder. Restart the Arduino IDE. You can then select the Raspberry Pi programmer from the tools menu in the Arduino IDE. Make sure you use "upload using programmer".

      Alternatively you can call avrdude directly. You'll need to use the -U flash option, and pass the path to the bootloader file. The avrdude man page has more information.

      Remember Arduinos are powered at 5V. If you power a standard 16MHz one at 3.3V it will be overclocked and may not work. If you power it at 5V use the correct logic buffer or make sure you include series resistors (4k7 should do) to limit the current through the input protection diodes.

      Delete
  3. How would I set some fuses like

    HIGH 0xDA
    LOW 0xE2
    Extended 0xFF

    ReplyDelete
    Replies
    1. The avr fuse calculator is a very useful resource for setting fuses. It also displays the relevant options for setting the fuses using avrdude. I haven't tested it but I think the command you need to program an ATmega328P using channel 0 of my Raspberry Pi programmer is

      avrdude -P gpio -c gpio -p atmega328 -U lfuse:w:0xE2:m -U hfuse:w:0xDA:m -U efuse:w:0xFF:m

      Delete
  4. Hallo.
    Your tutorial was very useful bit i don't know witch programmer i must use.
    I have Atmega8A and i made board as seen your tutorial.
    I wanna know how can I connect pieces together cause it's a little bit confusing and i never used avr before.
    I'd realy aprisiate your helt.

    KLemen

    ReplyDelete
    Replies
    1. You need to use the GPIO programmer option on avrdude. Wiring follows the standard AVR ISP pin out.

      Delete
  5. Can i use it to connect an atmel direct to the SPI Port of the Raspberry PI?

    ReplyDelete
    Replies
    1. Yes, but when connecting directly you need to be very careful otherwise it is easy to damage the Pi. The Atmel part must absolutely be powered from 3.3V, and so must any other devices on the SPI bus. There are good reasons why I included the buffering on my Raspberry Pi AVR ISP programmer. This version of avrdude bit-bangs the SPI pins but there is another version now exists which uses hardware SPI.

      Delete
  6. Thank you so Much Bro........luved this....

    ReplyDelete