From CCRMA Wiki
Revision as of 11:04, 24 May 2007 by Gurevich (Talk | contribs) (AVR Software at CCRMA)

Jump to: navigation, search

AVR Microcontrollers

AVR 8-bit RISC microcontrollers by Atmel are currently at the heart of the embedded technology platform we use in Physical Interaction Design research and courses at CCRMA. These chips have a modern Harvard Architecture, executing most instruction cycles in a single clock cycle. An extremely efficient open source C compiler is available, and these devices are used and supported by a large community of developers. The AVR architecture and instruction set is available in a large family of 8-bit devices that come in a variety of packages. AVR-series chips come with as little as 1 Kbyte of Flash program memory, up to 256 Kbytes; 32 bytes to 8 Kbytes of RAM; and up to 20 MHz clock speeds. Specialized AVR devices exist for automotive, LCD and lighting control, USB and radio applications. Code is easily portable between devices.

AVR Hardware at CCRMA


We currently use the ATMega 32 (Product Page). It features:

  • 32 Kbyte self-programming Flash Program Memory
  • 2 Kbyte SRAM
  • 1 Kbyte EEPROM
  • 8-Channel 10-bit A/D-converter
  • Up to 16 Mhz clock
  • I2C, SPI interfaces
  • 1 USART, 2-channels PWM, 2 ext. interrupts

ATMega 32 Documentation:

AVRmini Development Board

The AVRmini development board, made by Pascal Stang, is a general purpose prototyping board that provides easy access to the AVR's I/O and programming interfaces, along with a number of useful peripheral devices. We are currently using the AVRmini v4.0. The AVRmini v4.0 provides:

  • Support for most 40-pin AVR processors
  • Ethernet, USB, SD/MMC, RS-232 I/O
  • I2C header
  • Character LCD header
  • 10-pin I/O headers
  • 4 buttons and 4 LEDs
  • 5-V regulator

AVRmini Documentation:

CCRMA's AVR Prototyping Kit

For our PID courses at CCRMA, we assemble a prototyping kit based on the AVRmini. We mount the AVRmini on a sheet of 1/8" acrylic, and add solderless breadboard strips, an LCD display and a small loudspeaker. Pins 9 and 10 of the AVRmini v4.0's "AUX" header (JP13) are connected to the ground and +5V power rails of the breadboard strips.

  • kit Photo

Spyglass User Interface Board

The LCD display on CCRMA's prototyping board is part of the Spyglass User interface Board, which communicates with the AVR via I2C. We purchased these on eBay, and from a few other places, but they are hard to come by these days. Pascal Stang has written a brief Spyglass User Guide. He has also developed a replacement I2C button module/LCD driver (no documentation available) to which we can connect an LCD display and use just like the Spyglass.

  • Spyglass photo
  • buttonmodule photo

AVR Software at CCRMA

We use a set of open source tools for compiling C programs and downloading them to the AVR. Here is a brief description of what these tools are. See below for detailed instructions on setting up these tools on your own computer.

Programming the AVR


The [avrlib http://hubbard.engr.scu.edu/avr/avrlib/] is a library of easy-to-use C functions for a variety of tasks on the AVR microcontroller. It gives high-level functions for accomplishing tasks that might otherwise be tedious to program. These include serial communication, analog-to-digital conversion, displaying text on LCD displays and interfacing to a whole range of other devices such as hard drives, GPS units, Ethernet devices and sensors. The [avrlib http://hubbard.engr.scu.edu/avr/avrlib/] is distributed under a [GNU Public License http://www.gnu.org/licenses/gpl.txt]. At CCRMA, we make a few slight modifications to the original [avrlib http://hubbard.engr.scu.edu/avr/avrlib/].


The avrlib-demos are a set of example AVR development projects developed at CCRMA, some based on code from Pascal Stang's examples in the [avrlib http://hubbard.engr.scu.edu/avr/avrlib/]. Each project in the avrlib-demos contains at least 3 files:


Downloading Compiled Programs to the AVR


At CCRMA, we install a bootloader on the ATMega32 microcontroller. The bootloader takes advantage of the AVR's self-programming feature. Residing in a special section of program memory, the bootloader is a small program that is executed as soon as the AVR is reset. For a certain amount of time, it looks for incoming programming instructions on the serial port, which it uses to load a new application program into memory, overwriting the rest of the Flash. If no programming instructions are received, the bootloader times out and branches to the beginning of the main program memory, running whatever application program was previously there.

The bootloader we use emulates the STK500, an AVR development board from Atmel. You can download the bootloader and get more information here: http://hubbard.engr.scu.edu/embedded/avr/bootloader/index.html.

If you have an AVR development kit that you got from CCRMA, you probably have a bootloader installed. If you didn't, you may want to install a bootloader or use an alternate programming method, such as #ftisp.

Installing the STKload Bootloader






Setting up your Computer to do AVRLib / CCRMA-Style AVR Development



No matter what platform you use, you need to get the avrlib. If you are using a Linux machine at CCRMA, see the next section for the best way to do this. Otherwise, you can download the avrlib here:

and decompress it into your home directory. On a Linux or Mac system you can do this with:

~> tar xzf avrlib_ccrma20070124.tar.gz


A set of demo programs to demonstrate the basic functionalities of the AVRlib and AVRmini are also available:

Again on Linux or Mac, you can decompress them with:

~/somewhere> tar xzf avrlib-demos20070124.tar.gz

The avrlib-demos can reside anywhere, but they assume that the avrlib is in your home directory.


Linux Machines at CCRMA

The Linux machines at CCRMA already have the AVR development tools installed, and in your path. All you need to do is get a copy of the AVRlib. The avrlib-demos are helpful in getting started.

To get the most up-to-date versions, use subversion.


In a terminal, change into your home directory, and then type:

~> svn checkout file:///usr/ccrma/group/pid/avr/svn/avrlib ./avrlib

This will create an avrlib directory in your home directory. You can update it anytime from a terminal, by changing into the avrlib directory and typing:

~/avrlib> svn update


To get the avrlib-demos with subversion:

~/somewhere> svn checkout file:///usr/ccrma/group/pid/avr/svn/avrlib-demos ./avrlib-demos

You can update them with svn update as well. The avrlib-demos can reside anywhere, but they assume that the avrlib is in your home directory.

PlanetCCRMA Linux on your own PC

AVR Development Tools

If you have PlanetCCRMA installed on your PC, then you just need to make sure that you have the AVR development packages installed, using yum or apt.

For Fedora Core 4, the AVR compiler tools are under Applications/Development. The AVR downloading/programming tools are under Development/Tools.

AVRlib and avrlib-demos

Download the avrlib and avrlib-demos as described above. Note that you need to get the .tar.gz files, as the svn repository is not available outside of CCRMA.

Mac OS X / Linux

These instructions are excerpted from avr-libc" Installing the GNU Tool Chain.

Mac OS X: The Easy Way

1.You need the gnumake program, and you also need a symbolic link to it called make.

There are two options: i) Install Apple's developer tools, which include gnumake and much, much, much more. If you want to develop OSX applications you probably want or already have these anyway. They have a fancy easy-to-use installer.

ii) Alternately, you could just download my copy of gnumake: For PPC: osx-gnumake.tar.gz For Intel:

Then, unstuff it, and install the resulting file like this:

~> tar xzf osc-gnumake.tar.gz
~> sudo mv gnumake /usr/bin
~> sudo ln -s /usr/bin/gnumake /usr/bin/make

2. Download, un-stuff this archive and put the resulting avr directory in /usr/local, which you may need to create if it doesn't exist:

You will probably need to do this as administrator, i.e.

~> tar xzf usr-local-avr_ppc.tar.gz
~> sudo mv avr /usr/local

3. Add /usr/local/avr/bin to your Unix path. For example, if you use the tcsh shell, add this line to the file .cshrc in your home directory:

set path=($path /usr/local/avr/bin)

If you use the bash shell, add these two lines to the .bash_profile file in your home directory:

export PATH

Linux / Mac OS X (the not-so easy way)

This requires compiling the tools from source. It is not too difficult, but may take some time, depending on your bandwidth and CPU speed. On a Mac, this assumes you have the Apple's developer tools installed. On Linux, it assumes you have gcc and a basic operational build setup.

First, you need to download the following sources (either .tar.gz or .tar.bz2):

Next, you'll need to decide where to install the tools. It is usually a good idea to keep the avr build tools separate from your system build tools, so we recommend installing everything to /usr/local/avr. In order to do this, it is helpful to set an environment variable called PREFIX that points to this location. In a Bash shell (if your prompt is a $, this is probably what you're using), do:

$ PREFIX=/usr/local/avr
$ export PREFIX

The equivalent in csh or tcsh is:

~> setenv PREFIX /usr/local/avr

3. You'll also want to set your path to include the location where you are going to install the tools. In Bash:

$ export PATH

In csh or tcsh:

set PATH = ($PATH $PREFIX/bin)

In either case, you should also add this to your default path, as in Step 3 of the previous section.

GNU Binutils for the AVR target

The binutils package provides all the low-level utilities needed in building and manipulating object files. Once installed, your environment will have an AVR assembler (avr-as), linker (avr-ld), and librarian (avr-ar and avr-ranlib). In addition, you get tools which extract data from object files (avr-objcopy), dissassemble object file information (avr-objdump), and strip information from object files (avr-strip). Before we can build the C compiler, these tools need to be in place.

Download and unpack the source files:

~> bunzip2 -c binutils-<version>.tar.bz2 | tar xf -
~> cd binutils-<version>

Note: Replace <version> with the version of the package you downloaded. If you obtained a gzip compressed file (.gz), use gunzip instead of bunzip2.

There are 2 patches that need to be run before you can compile the binutils. From this URL:

Download the patch files called patch-newdevices.txt and patch-avr-coff.txt, and save them in the binutils-<version> directory. Apply the patches with:

~> patch -p0 < patch-newdevice.txt
~> patch -p0 < patch-avr-coff.txt

Now you are ready to compile binutils. It is usually a good idea to configure and build binutils in a subdirectory so as not to pollute the source with the compiled files.

~> mkdir obj-avr
~> cd obj-avr

The next step is to configure and build the tools. This is done by supplying arguments to the configure script that enable the AVR-specific options.

~> ../configure --prefix=$PREFIX --target=avr --disable-nls

When configure is run, it generates a lot of messages while it determines what is available on your operating system. When it finishes, it will have created several Makefiles that are custom tailored to your platform. At this point, you can build the project.

~> make

Note: BSD users should note that the project's Makefile uses GNU make syntax. This means FreeBSD users may need to build the tools by using gmake. If the tools compiled cleanly, you're ready to install them. If you specified a destination that isn't owned by your account, you'll need root access to install them. To install:

~> sudo make install

You should now have the programs from binutils installed into $PREFIX/bin.

GCC for the AVR target

Warning: You must install avr-binutils and make sure your path is set properly before installing avr-gcc.

The steps to build avr-gcc are essentially same as for binutils:

~> bunzip2 -c gcc-<version>.tar.bz2 | tar xf -
~> cd gcc-<version>

At this point there is another patch for avr-gcc. Download the file patch-newdevices.txt (different from the patch for binutils of the same name) from here:

Move the patch file into the gcc-<version> directory and apply it with:

~>patch -p0 < patch-newdevices.txt

Now, you can compile avr-gcc:

~> mkdir obj-avr
~> cd obj-avr
~> ../configure --prefix=$PREFIX --target=avr --enable-languages=c,c++ --disable-nls --disable-libssp --with-dwarf2
~> make
~> sudo make install


Warning: You must install avr-binutils, avr-gcc and make sure your path is set properly before installing avr-libc.

To compile and install the avr-libc

~> tar xzf avr-libc-<version>.tar.gz 
~> cd avr-libc-<version>
~> ./configure --prefix=$PREFIX --build=`./config.guess` --host=avr
~> make
~> sudo make install

avrdude is the application that downloads the compiled code from you computer into the AVR's program memory. To compile and install it:

~> tar xzf avrdude-<version>.tar.gz 
~> cd avrdude-<version>
~> mkdir obj-avr
~> cd obj-avr
~> ../configure --prefix=$PREFIX
~> make
~> sudo make install


Communicating with Pd via OSC over USB

Linux Machines at CCRMA

PlanetCCRMA Linux on your own PC

Mac OS X


Communicating with Max/MSP via OSC over USB

Mac OS X


==Communicating via OSC over udp ==</pre>