Friday, January 21, 2011

Compiling and burning code for AVR ATmega328P on Linux

I know most of you here would prefer developing on Windows, since that's the environment you're most comfortable in, but for me, booting into Windows meant three things:
  1. Loss of my awesome uptime on Linux (You don't get to six days of uptime without making a few enemies)
  2. Venturing into an unfamiliar territory 
  3. Loss of productivity. Games.
Therefore, I decided that I will setup my development environment on Linux.

What you will need:
  • gcc-avr - compiler
  • avr-libc - standard AVR C library
  • avrdude - burner
  • simulavr - simulator
  • vim (or any other editor) - for, you know, stuff.
To install all of them together, do:
sudo apt-get install gcc-avr avr-libc avrdude simulavr
Note that you do not need the FTDI driver, since FTDI support is built into the Linux kernel


The environment:
We will use vim for writing the code. Let's start with something we did in Thursday's lab:
#define F_CPU 16000000UL
#include <avr/io.h>
#include "delay_x.h"

int main() {
  DDRD = 0x20;
  while (1) {
    PORTD |= 0x20;
    _delay_ms(100);
    PORTD &= ~0x20;
    _delay_ms(100);
  }
}
Write the code to a file (say, leds.c) and then compile it using avr-gcc. We need to tell avr-gcc what CPU to compile for. The entire command looks like this:
avr-gcc -mmcpu=atmega328p leds.c -o leds.out
Now we need to create a hex file containing the .text and .data part of the object to burn onto the board. We will use avr-objcopy for this.
avr-objcopy -j .text -j .data -O ihex leds.out leds.hex
Great! Now that we have the hex file, let's burn it onto the board. We're gonna use AVRDUDE for this. We need to specify the port (which you can see by running "demsg | tail" after you plug in the board), the baudrate (57600 is good), the programmer (m328p), and the programmer ID (let's use AVRISP). The complete command will be:
sudo avrdude -P /dev/ttyUSB0 -b 57600 -p m328p -c AVRISP -F -e -U flash:w:leds.hex
Great! Now you've burnt this program onto the board! Test it out by connecting the LED's red wire to pin 5, and yellow wire to ground.

I hope this will help the minority which wants to program the AVR board on Linux. Good luck!

PS: There's an easy way. You could use this Makefile to automate the tasks.

PPS: There's an even easier way. Just install the AVR plugin for Eclipse. It does everything for you.

Constructive criticism welcome.

4 comments:

  1. Hi,
    Can't we just have just a single makefile to do post-code-development things like compilation as well as burning onto the board ?
    If possible, please post an example...

    Thanks

    ReplyDelete
  2. Yes, we can. The Makefile that I linked to in the PS section has all of that.

    A modified Makefile specifically targeted at what we're using is available at http://paste.ubuntu.com/557208/

    This Makefile assumes that yuour code is in the file named main.c . To compile and burn in a single step, just do:
    $ make writeflash
    or
    $ make install

    ReplyDelete
  3. Just a comment:
    avr-gcc -mmcpu=atmega328p leds.c -o leds.out
    should be
    avr-gcc -mmcu=atmega328p leds.c -o leds.out

    ReplyDelete