Using the Raspberry Pi SPI as an AVR ISP (In-system programmer)

A few days ago I blogged about using the Arduino IDE on the Raspberry Pi. It works fine, but of course the Arduino or clone needs to have a boot-loader installed in order for the IDE to work its magic.

I wondered if you could also use a Pi to install the boot-loader in the first place. There are lots of ways to install a boot-loader, but most of them require that you start out with at least one working Arduino or clone. What if you don't have one? Could you use a Pi as a programmer?

(c) Jason Mann
I'd recently seen a rather cool project which uses a custom board with an ATTiny AVR chip and an LED display to show the Pi's IP address.

Jason Mann, the author, explained how he used the Pi to program the ATTiny.

It looked to me as if his technique should work just as well on an ATMega 328p. (That's the chip that forms the heart of the Arduino Uno and the Shrimp).

It does.

I hit some minor complications while getting things to work and thought I should share the solutions here.

The Software

In order to install the bootloader I used avrdude. This does not support programming using the Pi's SPI port out of the box, but there's a forked version written by Kevin Cuzner which does.

Enabling SPI

Jason's article refers to an old way of configuring the Pi to use SPI ports. The raspi-blacklist.conf file appears to have disappeared. Instead, this article describes three ways of enabling SPI.

I went for the simplest, ran sudo raspi-config, selected the option to enable SPI, and rebooted.

I followed  Kevin Cuzner's instructions to build his forked version, and then realised that I had another potential problem. I now had a modified version of avrdude in the /usr/local/bin directory, but I also had an un-patched copy which the Arduino IDE had installed.

Luckily, the Arduino IDE finds its own version because it comes earlier in the search path, so the IDE still works perfectly. To use the patched version I referred to its location explicitly. You can see that in the example at the end of this post.

The Hardware

Jason's project runs the ATTiny at 3.3 volts but the Arduino normally runs at 5v. While you can just about get away with directly connecting a 5v Arduino to a Pi if you're using I2C, you cannot do so using SPI.

I had an Adafruit Pi Cobbler+ and couple of SparkFun level shifters lying around. I connected the Pi via the cobbler to the shifters and the shifters to the Arduino. You can see a picture at the top of the page.

Trying it out

I ran

sudo /usr/local/bin/avrdude -c linuxspi -p m328p -v -P /dev/spidev0.0

Here was the tail of the response:

         Programmer Type : linuxspi
         Description     : Use Linux SPI device in /dev/spidev*

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f
avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DA
avrdude: safemode: efuse reads as 5

avrdude: safemode: lfuse reads as FF
avrdude: safemode: hfuse reads as DA
avrdude: safemode: efuse reads as 5
avrdude: safemode: Fuses OK (E:05, H:DA, L:FF)

avrdude done.  Thank you.

More detail

This post is already long enough. If you want the full details of the installation and build process, along with a fritzed diagram of the breadboard connections, I'll be posting them in next Friday's free newsletter.

You can sign up for the newsletter here.

Comments

Popular posts from this blog

Controlling a Raspberry Pi Pico remotely using PySerial

Five steps to connect Jetson Nano and Arduino

Raspberry Pi Pico project 2 - MCP3008