Category Archives
Search the blogs:

Programming AVR Microcontrollers on a Macintosh (Getting Started)

Over the last couple of years, several friends and colleagues have told me to check out the Atmel AVR microcontrollers in comparison to the Microchip PIC. They are comparable to the PICs in price, and though there is not as much variety in the AVR family as there is in the PIC, there are a number of good features available.

What finally convinced me to try the AVRs was Hernando Barragán's thesis project at Ivrea, Wiring. Wiring is designed as a beginning physical computing platform that's equivalent to Processing for beginning programming. It's based on the Atmel ATMega128 chip. I like Wiring a lot, and I plan to help in the future development of it as much as possible, because it seems like an excellent entry point into microcontroller programming. At the current time, though, you have to make your own Wiring boards, or have a source of them. To date, Hernando and Massimo Banzi are the only source of boards, so it's not quite ready for universal use. So in order to get started and learn what's under the hood, I started looking into how to program the AVR chips directly.

There are a number of good compilers for the AVR chips, both commercial and open source. MCS Electronics' BASCOM is a decent BASIC compiler, and there are a number of C compilers available as well. As with most microcontroller environments, most of them are written for Windows only. Fortunately, there is also a version of the GNU C Compiler (gcc) that can compile code for the AVR chips as well. It's open source, and better yet, it's cross-platform (Windows, OSX, Linux). It's what's at the core of Wiring. There are a number of good sites detailing how to set it up, and after working my way through several of them, I had things up and running on my powerbook. The details follow. These details assume you know the basics of microcontroller programming. If you don't, I've got a few notes on the subject, and I can recommend a good book.

As with many microcontrollers, you need a handful of tools to program the AVR chips. First, a compiler, in this case, the avr-gcc. Second, a hardware programmer to load the code into the microcontroller. There are many programmers available for the AVRs. I bought an AVR ISP programmer from Digikey for $30. There are also plans online to build your own, but I figured that I would have spent as much on parts and labor as I did on buying the AVRISP. Finally, you need a program to communicate with the programmer. The uisp program, which I'll describe below, interfaces with most of the popular programmers, including the AVRISP.

The avr-gcc is not particularly user-friendly. In order to use it, you need to be familiar with command-line operation of your mac. It helps to know something about how software is built on Unix systems too. If you're not ready to spend a lot of time to learn some of this, don't go there yet. There will be more user-friendly tools soon.

There are a few basic UNIX project concepts you'll need to know. All of the projects you're downloading will be the actual source code for the programs, and all the necessary files to compile them into working programs. For example, you'll compile the compiler, the programmer, and so forth. In each case, there will be a configure file that comes with the package that checks your machine to see that the software can successfully compile, and a make file (called "makefile") that does the actual work. You don't need to know what these are doing -- until you hit errors. Which, hopefully, you won't.

All of this assumes you are the main user and administrator of your machine, and are logged into it with administrative rights. If you're not, this won't work.

Configuring the Shell

Open the Terminal application. Before you install any of the programs, you need to configure your Terminal environment to know where the stuff is going to go. To do this, you need to change your shell configuration file (for more on the UNIX shell, here's a wikipedia article). the default for OSX 10.3 is the bash shell. In the terminal window, type the following:

cd

This changes directories to your home directory.

ls -la

This will list all the files in your directory, including the invisible ones (which all start with a ., as in .profile). If there is a file named .bash_profile, you'll be modifying it. If not, you'll be creating it. Type:

pico .bash_profile

This will open an editor called pico, and open a file called .bash_profile for editing. Type:

PATH=$PATH:/usr/local/bin ; export PATH

Then type control-X to exit, and Y to say that you do want to save changes. To put the change in effect, type exit, then open a new Terminal window.

Note to experienced shell users: if you're not using the bash shell, modify your shell profile as appropriate to put /usr/local/bin in your default path.

Downloading the Packages

There are four pieces of software you'll need. In each case, get the lastest version. My links go to the generic upload directories. For OSX, download the appropriate .tar.bz2 file.

* binutils, a set of utilities for the gcc. I used version 2.15.
* the gcc-core. This is the core of the compiler. I used version 3.3.2.
* avr-libc, a set of libraries specific to the AVR chips. I used version 1.2.3
* uisp, software that controls the hardware programmer. I used version 20050207

Unpack the packages by double-clicking. Each will unpack into its own directory. For these notes, I assume all those directories are on the desktop. In each case, you'll go into the directory and run the configure file and the make file. The resulting programs will get stored in a directory you don't normally see in the GUI, /usr/local.

Once you've unpacked them, it's time to make the programs. Type the following:

cd Desktop/binutils-2.15

this changes directories to the binutils folder. Your version may have a slightly different name.

./configure --target=avr --disable-nls 

this runs the configure file for binutils, specifically for the AVR. You'll see some status messages printing out as it runs.

make

this comples the programs. You'll see some status messages printing out as it runs.

make install

this installs the programs in the appropriate directories. You'll see some status messages printing out as it runs. You might have to run this as a super-user, using the sudo command, like this:

sudo make

and

sudo make install

sudo will ask you for your password before it runs the make. Any time you try to run one of the commands that follow with no success, try running it using sudo.

Once binutils is successfully installed, you can install the others in much the same way. To change directories to the gcc core:

cd ../gcc-3.3.2

Then run the following lines:

./configure --target=avr --enable-languages=c

make CC="cc -no-cpp-precomp"

make install

Note for expreienced programmers: I assumed that because I had installed XCode, the gcc core was already installed. It was, but the avr tools couldn't see that install, because it's in another directory. When I installed the gcc core as shown above, everything started to work. So don't make my mistake; install the gcc again. If you really know what you're doing, let me know how I could have avoided this install.

After the gcc is installed, you install the avr-libc, as follows. Change directories to the avr-libc:

cd ../avr-libc-1.0.4

Run the configuration file:

./doconf

And the make file:

./domake

And finally, the install:

./domake install 

Finally, install the programmer software, uisp. Change directories to uisp: cd uisp-20050207

Run the configuration file:

./configure

And the make file:

make

And finally, the install:

make install 

At this point, you've got everything installed to program the AVR microcontrollers. To test it, you'll need to build a circuit board for an AVR microcontroller, and write some code.

My First AVR Circuit

I started out with an AVR ATMega 8535 microcontroller, mainly because I knew the basic features of it from the BX-24 microcontroller module from NetMedia. I prototype on solderless breadboards a lot, so I bought the DIP package. Here's the basic circuit:

And here's a picture of it:

I made my own DIP-like header for the AVRISP with a some right-angle header pins and straight header pins. It looks like this:

I'm using a Keyspan USA19QW USB-to-serial adaptor to connect to serially to my powerbook. Other USB-to-serial adaptors may do the job, but whichever one you use needs to be capable of communicating at 115200 bps, since the AVRISP and uisp can only communicate at that rate, in my experience.

Programming the Chip

I got my first code from Mike Seeman's page, and adapted it to make an LED on PORTB pin 0 blink. Download the following files into a new directory of their own:

sample.c
sample.h
timer.c
makefile

There are a number of steps to compiling this code. You have to compile the timer.c file and the sample.c files, then you have to link their compiled files together to make a single binary file to be downloaded into the AVR's ROM. Then you have to download it. There are a number of optional parameters to the avr-gcc that you use to do this. However, a simpler way is to write a file that automates the process. The makefile, again adapted from Mike Seeman's makefile, does the trick nicely. In it, you'll see where it compiles the file for your chip (if you're using a chip other than the mega8535, you'd need to change the -mmcu option), then links the compiled files to make a binary file called sample.rom. To use the makefile, change directories in the Terminal window to get to the directory where you downloaded these files. Then type

make

You should get output like this:

avr-gcc -c -g -O3 -Wall -mmcu=atmega8535 -I. timer.c -o timer.o
avr-gcc -c -g -O3 -Wall -mmcu=atmega8535 -I. sample.c -o sample.o
avr-gcc timer.o sample.o -Wl,-Map=sample.map,--cref -mmcu=atmega8535 -o sample.elf
avr-objcopy -O ihex sample.elf sample.rom

if you list the files (type ls), you'll notice a bunch of new files. To download the code to the chip, type

make install

If you're using the same USB-to-serial very lucky, you'll get no errors. Otherwise, you probably got an error like this:

Error: No such file or directory
-> /dev/tty.USA19QW1b1P1.1

In other words, it can't fine your USB-to-serial adaptor. If you've used your adaptor for other purposes, you probably know its name, but you can get a list of all of the ports by typing

It'll probably be a long list, and at the top of it will be some oddly named ones like /dev/tty.USA19HW1b1P1.1, or something else. Third-party USB-to-serial adaptors usually have names like this. Find the one corresponding to your adaptor, and change the uisp line in the makefile to target your serial adaptor rather than mine. Then try make install again. You should get output like this:

uisp -v=1 -dprog=avrisp -dserial=/dev/tty.USA19QW3b1P1.1 -dspeed=115200  
-dpart=ATmega8535 --erase --upload --verify if=sample.rom
Firmware Version: 1.18
Atmel AVR ATmega8535 is found.
Firmware Version: 1.18
Uploading: flash
Verifying: flash

And your LED will blink. Congratulations! You've written your first AVR C program. Now you're on your own until I get further myself.

P.S. I think it would be possible to program a BX-24 using the avr-gcc as well. The holes at the top of the BX-24 are the SPI connections to the 8535 on the module. So presumably, one could attach the leads of the AVRISP programmer to those leads and it'd work. Of course, you'd be erasing the basicx os, and you'd never be able to program the chip in basic again. But who knows, if someone were to take the BX tokenizer from their site and wrap it up as an avr-gcc project, you could (someday) program the BX-24 native on a Mac, Linux box, what-have-you. It's a crazy dream, I know.

Links:

• Mike Seeman's page on AVR programming on a Mac was my main reference, even though some notes are out of date. He's got an installer package there that theoretically does all of the above install steps, but it didn't work for me. His instructions on doing it yourself were invaluable, though.
• The Stanford CCRMA page on the AVRmini and OSX was a great help also.
• Jim Schimpf wrote an article for O'Reilly Mac Dev Center called Embedded Development with XCode that details how to get the avr-gcc working with XCode. I didn't go that far, but if you like XCode, it's a way to go.
• Pascal Stang's Atmel AVR page is a useful resource of code and how-to's as well.
• Rich Neswold's manual for the avr-gcc was very helpful as well.
• I downloaded binutils and the gcc core from gnu.org
• The avr-libc and uisp projects are hosted on nongnu.org.
• I bought my avrisp programmer and microcontrollers from Digikey
• Thanks to Hernando Barragán, Massimo Banzi, and Bill Verplank for kicking me into doing this.

Posted by tigoe at April 16, 2005 07:30 AM