Flashing the Atmel AVR ATtiny85
As I the days start to grow darker again, I was looking around for some interesting things to do with all the electronic ‘crap’ I have lying around and about in the house, and I came up with writing a series of post covering the setup of a Raspberry Pi2 to use it as a developing environment for AVR microcontrollers.
This is the second post in the ‘hello-avr’ series, dealing with setting up a basic AVR toolset on a Raspberry Pi. When finished the Raspberry Pi can act as an ISP and you will have the ability to flash the controller with a build artefact from the command line interface.
- You followed the the first instalment - link - and can ssh into the Pi.
- Breadboard to put the controller on
- Wires/leads to make connections between RPI GPIO and controller pins
In order to flash the controller we need a tool called avrdude. When avrdude is configured correctly we can easily flash the micro controller from the command line through the ISP circuit.
There are a couple of way to install avrdude. One is via Adafruit
Add adafruit package repository to apt
$ curl -sLS https://apt.adafruit.com/add | sudo bash
Then install the package
$ sudo apt-get install avrdude
Or use the second option. This will take little longer and requires you to build manually from source.
Install dependencies for building avrdude
$ sudo apt-get update
$ sudo apt-get install -y \
Get and build avrdude
$ wget http://download.savannah.gnu.org/releases/avrdude/avrdude-6.3.tar.gz
This might take some time, be patient, enjoy some beverage, read a book. Either way, once everything is done avrdude should be available from the command line.
$ avrdude -v
Lets continue with making some connections.
The ISP circuit
With avrdude alone we cannot really do anything special. We need to create an ISP circuit to connect the Raspberry Pi’s GPIOs with the microcontroller. The circuit here is tested with an attiny85 microcontroller but it should support a wider range of this type of controllers.
Place the microcontroller on a breadboard and route wires from the RPI GPIO header to the Attiny85 pins according to this table
|VCC||5V (pin 2, 4)|
|GND||GND pin 6,9,14,20,25,30,34,39|
|RESET||GPIO 12 (pin 32)|
|SCK||GPIO 24 (pin 18)|
|MOSI||GPIO 23 (pin 16)|
|MISO||GPIO 18 (pin 12)|
Note that the GPIO numbering is NOT the physical pin numbering. Have a look at this excellent Raspberry GPIO reference. For the controller you can refer to the following Attiny85 pin schematics.
# Attiny85 Pins
Please check carefully if everything is connected as described. Then lets get busy with configuring avrdude.
To make avrdude aware about us wanting to use the Raspberry as the ISP, we need to add some configuration.
Copy an example configuration and open it
$ cp /usr/local/etc/avrdude.conf ~/avrdude_gpio.conf
$ vim ~/avrdude_gpio.conf
to end of file add
# RPI GPIO configuration for avrdude.
id = "pi_1";
desc = "Use the Linux sysfs interface to bitbang GPIO lines";
type = "linuxgpio";
reset = 12;
sck = 24;
mosi = 23;
miso = 18;
This make avrdude aware of a new programmer that uses linuxgpio to talk to the configured pins. If you have deviated from the pin numbers in previous sections, you need to also update the pin assignments accordingly. When done, save! Now we can check if our config works
$ sudo avrdude -p attiny85 -C ~/avrdude_gpio.conf -c pi_1
You should get an output similar to the above. If not, you can try again with
-v flag added to the end to the command, to get a more detailed output for troubleshooting. Only continue with the
next section when you have finished the current one successfully.
Flashing the chip
Finally, you are ready to flash the controller with some code. We have not covered how to build artifacts which we can flash to the controller, I’ll cover that in the next post. For now, you can download a hex file I build before onto the Pi with eg curl.
$ curl <url> -o main.hex
This is a build of a simple c program that will blink a LED connected to PBO (Pin 5) of the Attiny85.
/* Name: main.c
Please excuse me if some of the comments are incorrect, I’m not a professional C/AVR programmer, I’m open to feedback for better explanations.
Now, from the directory where you downloaded the
main.hex to, execute the following to finally flash the controller.
$ sudo avrdude -p attiny85 -C ~/avrdude_gpio.conf -c pi_1 -U flash:w:main.hex:i
This is the output when the flashing went smooth. If you get errors, again just add
-v to the command to have a more verbose output.
Now that our Pi is setup to flash we are ready to start developing. In the next post I’ll cover one way of setting up your development environment in order to have a relatively smooth workflow.
Debugging RPI GPIO:
With a basic LED circuit connected to PIN 16 (GPIO 23) you should be able to manually blink it with
$ echo "23" > /sys/class/gpio/export
if not, you might have to unexport the GPIO first.
$ echo "24" > /sys/class/gpio/unexport