Building AVR firmware with make
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 Pi 2 to use it as a developing environment for AVR microcontrollers.
This is the third post in the ‘hello-avr’ series. This time I’ll describe one way of setting up your Raspberry in order to build the artifacts needed for flashing the device. If you follow along you’ll have a complete AVR development environment at your disposal.
- You followed the the first two installments
- You are able to flash the controller from the RPI
- Some nano/vim editor experience
I’ll just start with a new git project in ~/Workspace on the RPI
If you are in need of git, you can simply install it with
sudo apt-get install git. Then in this directory create a subdirectory
./firmware. In this directory we place two files, main.c containing our source and a Makefile with build instructions.
The content of the Makefile its content is based on a template from CrossPack for AVR with which I dabbled around for a bit before changing to a Raspberry based setup.
ENTRYPOINT ?= main
Our source, main.c is code that should blink a LED on PB0 (pin 5) of the Attiny85. I wont cover the details in this post.
In the project root we place a README.md file, where we can put instructions for our future selves and a .gitignore file where we can make git ignore our build files, we dont need to version control those files.
When finished, the project structure should look like
and then we’ll can try to run
make from the firmware directory… which won’t work… and complains with errors:
avr-gcc -g -Wall -Os -DF_CPU=8000000 -mmcu=attiny85 -c main.c -o main.o
We obviously need to install avr-gcc, unfortunately a
$ sudo apt-cache search avr-gcc
doesn’t find any packages, so I’ll just widen the search with
$ sudo apt-cache search avr
Seems we can install avr-gcc with
sudo apt-get install gcc-avr instead. Lets try the build again… still failing, because our code depends on avr-libc which was also listed in the previous search results, so we can install it with
$ sudo apt-get install avr-libc
Now, when we run
make, the build should pass and can be uploaded it with
$ sudo avrdude -p attiny85 \
The command is a bit long to remember, but we i have put most config flags in the Makefile template instead. The programmer configuration is the one we covered in the previous post.
PROGRAMMER = -C ~/avrdude_gpio.conf -c pi_1
in addition to the Attiny85 and Rapberry GPIO Programmer settings, a flash target was added the makefile.
flash: all ## Flash the chip with the current build
Update the code to have a different blinking rate and build it again, this time with
and then flash the chip with
$ make flash
You should see the LED flicker a little during the flashing and then it should start blinking after flashing has completed. It is probably not recommended to have your own circuit connected while you’re flashing the chip though, in order to do ISP, you need to have a proper ISP circuit setup.
When done, I recommended you to create a new git repository in e.g Bitbucket or Github. To do this you need to setup an ssh-key on the Raspberry Pi with
ssh-keygen, then copy the key
cat ~/.ssh/id_rsa.pub and add it to your scm provider and follow their directions for setting up a new repo and pushing your code to it.