Pages Menu
TwitterRssFacebook
Categories Menu

Posted by on Jun 14, 2015 in Atmel AVR, Microcontrollers | 41 comments

Setting up AVR-GCC Toolchain on Linux and Mac OS X

Setting up AVR-GCC Toolchain on Linux and Mac OS X

Windows users have been enjoying various awesome tools to help with their AVR development process like the Atmel Studio, Codevision AVR, WinAVR, IAR Embedded Workbench, etc. This doesn’t mean that Mac and Linux users are at any unfair advantage. The avr-gcc toolchain supports Unix based OS like Linux and Mac OS X inherently.

In this post I will show how to install the avr-gcc toolchain and avrdude on Mac OS X and Linux. If you are using a Mac and want to save the trouble of going through these steps, simply install CrossPack for AVR Development and follow the instructions in the manual. I might go over it sometime in future though.

However if you really want to learn how to set up a cross-compiler, write Makefiles and use command line tools to get things done, I highly recommend doing the following steps. And trust me, they are pretty straightforward.

Step 1: Install Homebrew (Mac OS X only)
Step 2: Install avr-gcc toolchain
Step 3: Install avrdude
Step 4: Test the toolchain

Busy and just want to get s#!t done? Read the summary!

Step 1: Install Homebrew (Mac OS X only)


This step is not required for Linux users since almost all Linux distribution comes with a package manager.

Homebrew is the new super-awesome package manager for Mac OS X. Install it by typing (or pasting) the following in terminal:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Step 2: Install avr-gcc toolchain

Mac OS X

First tap the repository:

brew tap osx-cross/avr

Then install the latest version of avr-libc (version 4.9.2 at the time of writing):

brew install avr-libc

This will pull avr-binutils and avr-gcc along with it as well. The second steps takes a little while to install everything, so go get yourself some tea in the meantime.

The Homebrew repository doesn’t has the avr-gdb formulae yet. If you need the avr-gdb debugger, go for the AVR CrossPack.

Linux

The following steps are for Debian/Ubuntu Linux. For other Linux distributions, please install read this.

It is usually a good idea to update all the packages you already have installed.

sudo apt-get update
sudo apt-get upgrade all

Then install the required packages.

sudo apt-get install gcc-avr binutils-avr avr-libc

You can also install gdb-avr is you like. It is useful for in-system debugging/emulation.

sudo apt-get install gdb-avr

Once done, type avr- in the terminal and press tab twice (do not hit enter). You should be able to see all the tools installed for you.

avr-gcc tools installed on Mac

avr-gcc tools installed on Mac

Step 3: Install avrdude


AVR-GCC is a toolchain that will help you with the software development process, but doesn’t do anything about burning the final executable (the hex file) to the microcontroller. For that we need to install AVR Downloader UploaDEr (avrdude).

Mac OS X

We will again use Homebrew to install it.

brew install avrdude --with-usb

Linux

The following steps are for Debian/Ubuntu Linux. For other Linux distributions, please install read this.

sudo apt-get install avrdude

This should be pretty quick. Once installed, type avrdude -v in the terminal to check if it is installed properly.

Step 4: Test the toolchain


That’s all we need for now. Let’s test whether it works or not. I have used the LED blinking code called led.c for ATmega32 for demonstration. Refer to this post to understand what it does.

Once the tools are installed no matter how, everything from here remains the same for both Mac OS X and Linux.

#ifndef F_CPU
#define F_CPU 16000000UL // or whatever may be your frequency
#endif

#include <avr/io.h>
#include <util/delay.h>                // for _delay_ms()

int main(void)
{
    DDRC = 0x01;                       // initialize port C
    while(1)
    {
        // LED on
        PORTC = 0b00000001;            // PC0 = High = Vcc
        _delay_ms(500);                // wait 500 milliseconds

        //LED off
        PORTC = 0b00000000;            // PC0 = Low = 0v
        _delay_ms(500);                // wait 500 milliseconds
    }
}

Assuming the name of the file is led.c, type the following in the terminal and check if the led.hex file is generated or not. The code should compile without any errors. Don’t worry if you don’t understand what the commands do or mean, we will discuss about them in detail in the next post.

avr-gcc -g -Os -mmcu=atmega32 -c led.c
avr-gcc -g -mmcu=atmega32 -o led.elf led.o
avr-objcopy -j .text -j .data -O ihex led.elf led.hex

Type cat led.hex in the terminal and see if the hex file is generated. If you see something like in the image below, you’re all set! :)

Final Hex FileWe will need the hardware to test avrdude, so let’s just skip it for now. In my next post, I’ll show you how you can use the avrdude to transfer the hex file generated above to run on an actual hardware. If you have any questions, please ask them below. Thank you.

Summary


Here’s what you gotta do on Mac OS X:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew tap osx-cross/avr
brew install avr-libc
brew install avrdude --with-usb

And here’s what you gotta do on Debian/Ubuntu Linux:

sudo apt-get install gcc-avr binutils-avr gdb-avr avr-libc avrdude

For all other Linux distributions, read this.

Thanks for reading! Subscribe and stay updated!

Written by Mayank Prasad
Last updated on June 14, 2015

41 Comments

  1. Hi . Can u pls tell how to flash the program into the micro controller. Im using Mac and r232 atmega88pa.

  2. Thanks for the post. It was exactly what I was looking for to get going on my Mac. You mentioned about your next post about using avrdude to transfer the hex file to the hardware. Did you do that post and I cant find it? I am looking forward to the next step and I like your instructions.

  3. what are the debugger that I can use for avr-gcc in mac ?

  4. Great instructions – many thanks. My first use of home-brew, too :) I’m on OSX and I initially installed AVR-Crosspack but soon discovered its avr-gcc is well out of date.

    One change I had to make: *brew install avr-libc* didn’t work for me but *brew install avr-gcc* worked fine.

    Using this with Jawha Moussa’s excellent X-AVR (https://github.com/jawher/xavr)) so that I can program for avr-gcc using Apple’s Xcode IDE.

    Life’s good :)

  5. Hello Mayank!
    Very nice tutorial!
    Do you still want to write part 2?
    Cheers

  6. Hi!

    Please check again, if for Mac OS it should be:

    $>brew install avr-gcc

    …instead of:

    $>brew install avr-libc.

    I couldn’t get it to work with the later, but former went smooth.

    Regards,
    Lukasz.

  7. Was able to install the toolchain on Debian Stretch and generate a .hex file without any problems. Thanks very much!

  8. Good One Thanks….

  9. Thanks it worked on my Pi. I installed avr tool chain . Yet to try on a real uno and test it out

  10. Hi, Is this tutorial still upto date for High Sierra 2018

  11. This is a clear, concise walk through, great job!
    After testing this for myself (Ubuntu 16.04 LTS) I noticed that the .hex file is considerably smaller than the .elf file. How is it that a ~7k program can be packed into ~500 bytes? Does the elf just have that much unnecessary stuff?

  12. “brew install avrdude –with-usb” is producing the response “Error: invalid option: –with-usb” . This thread: https://github.com/Homebrew/brew/issues/5732 indicates that brew is no longer accepting install options. I’m not a brew user, can someone tell me how to proceed?

  13. Hi. On MacOS Catalina Version 10.15.3,

    brew install avr-libc

    failed. I did a web search and found the following solution:

    brew tap osx-cross/avr && brew install avr-gcc

    Then I had a problem with AVRDUDE:

    brew install avrdude –with-usb

    It did not like the –with-usb flag. I then thought that this was not needed, because hardly any computer today would have RS-232 ports. I dropped that and it installed.

    Your led.c compiled and the .hex file was created. Now for the avrdude tutorial.

Trackbacks/Pingbacks

  1. Upgrade firmware FuriousFPV True-D v3.6 | ledrone.club - […] n’explique pas : Comment installer avrdude (via Homebrew par ex.), le driver USB Série […]
  2. 맥 OS에서 AVR 툴 체인 설치 - OpenMicro Labs OpenMicro Labs - […] 참고: http://maxembedded.com/2015/06/setting-up-avr-gcc-toolchain-on-linux-and-mac-os-x/ […]
  3. ATtiny13 TinyUPS: GitHub File Free Download - […] Make sure you have installed avr-gcc toolchain and avrdude. […]
  4. ATtiny13 TinySolder: GitHub File Free Download - […] Make sure you have installed avr-gcc toolchain and avrdude. […]
  5. ATtiny85 TinyFMRadio: GitHub File Free Download - […] Make sure you have installed avr-gcc toolchain and avrdude. […]

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: