How to Setup AVR Programming Environment on Linux
by Eteims in Circuits > Microcontrollers
5677 Views, 2 Favorites, 0 Comments
How to Setup AVR Programming Environment on Linux
If you want to program AVR microcontrollers on Windows you have a Studio but on Linux all we have is a Dude.
AVRDUDE is the command line interface to program AVR chips, it can be a little bit tricky to setup at first.In this Instructable, i will be setting up AVRDUDE and also be creating an AVR programming environment for the Linux terminal.
First i will be installing all AVRDUDE and all required dependencies then i will create a BASH script that will help in programming
Getting Your Compiler and Other Tools
In other to program AVR chips you need a special compiler known as gcc-avr and the other tools like binutils-avr, avr-libc, gdb-avr last but not the least avrdude.
sudo apt-get install gcc-avr binutils-avr avr-libc gdb-avr avrdude
Creating a Template.
If you open a new sketch in arduino you get a code template that contains two functions, this saves you a lot time.
AVRDUDE uses C and it can be a little bit annoying to always create a main method every time you want to code, so i will create an AVR template.
touch ~/Templates/AVR.c
Use the touch command to create an empty file in the Templates folder.
vi ~/Templates/AVR.c
open the file with your favorite text editor, i am using vi.
#define F_CPU 16000000L #include <avr/io.h> #include <util/delay.h> int main(){ while(){ } return 0; }
Type the code above and save the file. This code will serve as our template.
Note: i set my clock frequency as 16000000, you can set your's as any other frequency maybe 8000000.
Downloads
Create a New File.
Now we have a template for our AVR codes, all we need to do is to create a new file. I will be creating a bash command that will take in one argument(the file name) then create that file having the AVR template.
let's make an empty file called "create"
touch create
change the file permission because this will be a BASH script
chmod 755 create
Open "create" with your text editor. Now let's edit "create", add the following commands line by line.
#!/bin/bash
This is the path to the interpreter for "create" which is bash.
cp ~/Templates/AVR.c /home/$USER
This copies our template file to the users home directory.
mv ~/AVR.c $1
Remember i said that "create" takes in one argument, $1 means the first argument of our command this argument is the intended file name, the last thing we want is multiple files having the same filename. The command renames the filename to our argument.
vi $1
This is optional but it will be nice to open our file, immediately after we have created it.
We are done with editing create, save it and close it.
Here is an example of create in action.
./create blink.c
This creates a file known as blink.c, this file should have the template of AVR.c.
Let's Run
We have to create another bash script known as "run" , this script will be taking 3 arguments(the avr microcontroller we are using, the filename and the programmer)
Let's take it line by line.
#!/bin/bash
our shebang
avr-gcc -Wall -g -0s -mmcu=$1 -o $2.bin $2.c
The command above compliles our code, '$1' is our first argument which is the microcontroller we are programming. $2 is our second argument which is the filename.
avr-objcopy -j .text -j .data -O ihex $2.bin $2.hex
This converts our complied file to hex.
avrdude -p $1 -c $3 -U flash:w:$2.hex -P usb
Now avrdude burns the code into AVR chip. $3 is our 3rd argument which is the programmer we are using.
Save the file "run"
give it execute permission
chmod 755 run
Now let's test it. Let's say we want to upload blink.c and we are using an arduino board directly, we are also using a usbasp programmer. This is how we use the "run" script.
./run atmega328p blink USBasp
The arduino board has an atmega328p chip, you can use any AVR microcontroller of your choice.
The second argument is your filename, but this time do not add the file extension the script handles that.
Then we have the third argument which is the programmer you are using, i am using an USBasp programmer.
Conclusion
This is a great way of automating your avr programming experience, you can move the bash files "create" and "run" to "~/.local/bin" so you can use the scripts from any file directory of your choice.