Programming ATMEL Microcontrollers under Linux

Introduction

This page was written because I was having a lot of trouble getting a working environment for programming the ATMEL brand microcontrollers.

Some time ago I wanted to get into microcontroller programming. Eventually, my dad gave me an ATMEL AVR starter kit (AT89/90 series flash microcontroller kit). The kit came with an ATMEL AT90S1200 Microcontroller, serial and power cable, development board and CD with programs, compilers and a programmer, in the nice box shown below:

 

It was great and all, but it was Windows only. This proved to be quite a problem as I do not have any Windows PCs in my possession. I tried using VMware but found it too sluggish for my liking. So I went in search of Linux tools which would do the job.

#1 ATMEL Programmer

Before I could do any programming on Linux, I needed a way of writing and reading data from the development board. Only problem was that the board was quite old (circa 1997), meaning that I could find no information on the internet about it (as at 2005). The board has the number "A96021000C" printed on the front. A picture of the board is provided below:

 

And the microcontroller:

After a lot of searching I came across the program avrdude, only to find that the list of working programmers did not contain this one. After a lot of trial and error, I found out that the avr910 is compatible with my development board.
As such, to read from the microcontroller, I used:

avrdude -c avr910 -p 1200 -U flash:r:$filename:i -u

...where "filename" is the path to the hex file. Note that for writing we use the same commands as above except with "w" instead of "r"

Errors: I got "avrdude: error: programmer did not respond to command: flush final page". Even with this error, it seems that code is still written to the microcontroller properly. You can ignore the error (I do). I presume it's due to differences between my board and the avr910.

#2 Getting an assembler

Well, so far we have managed to get hold of a programmer for our development board. Now we need to find an assembler that works with the microcontroller.

After digging around the net, I decided on using tavrasm. It seemed complete and was still being maintained (as at 2005). Its usage is very easy:

tavrasm ./input.asm

will provide an output of "input.hex"
That's pretty much all there is to it.

#3 Testing your setup

So you have your dev board, programmer and assembler. Let's assemble and upload a program! I will provide a rather simple program here that will alternately blink two lights on the dev board.

So where to start? I started by first looking at the spec sheet for the AT90S1200 Microcontroller. Here we have a summary table of all the Assembly instructions, their operands and a short description of what they do. The spec sheet can be found here and the instruction summary can be found starting on page 63.

Once you know what instructions are available, you can start programming. You can use any text editor for writing the code. I used Kate because it provides nice syntax highlighting, but any will do. For those of you who just want to test your setup, here is my code which you can use:

twoflashled.asm

This program will alternately flip two bits on the dev boards "PORTD". Connect a ribbon cable from PORTD to your LED array and they will blink alternately.

So now you have your .asm file. You can use tavrasm to assemble it:

tavrasm ./twoflashled.asm

...and it should compile. If you get errors about undefined variables and/or a message saying "ERROR: Unable to open file '1200def.inc', you need the 1200DEF.INC file. This is available from ATMEL's Assembler download, somewhere on their site (unfortunately, I cannot work out where anymore, so I will provide a local mirror here).

NOTE: If you are still getting a lot of errors about undefined variables etc...
Remember that Windows is not case-sensitive, while Linux is. The include files provided by ATMEL are uppercase, while many programs written on Windows write the include using lower-case. This works fine in Windows, but case-sensitive operating systems will complain.

Once assembled you should have a file called $file.hex (in our case it will be twoflashled.hex). This is the assembled file which you can then upload to the board.

Upload the hex file using the commands shown earlier:

avrdude -c avr910 -p 1200 -U flash:r:twoflashled.hex:i -u

And you should be done! Hit the RESET button on the board and (provided you connected the LEDs to the port) see alternately flashing lights. That's it, hopefully so far there have been no problems!

For those of you who wish to learn more about Assembly programming of ATMEL Microcontrollers, look at this AVR-Tutorial

#4 Getting a C compiler

This section has been removed because the at90s1200 is not supported by AVR-GCC. This section will have to wait until I get an ATMEL chip which is supported by AVR-GCC

 

Responses to Email

To be honest I was surprised at the amount of mail this article got. I thought that the programmer was so old that nobody bothered using it (except me, penniless student that I am), but hey, I guess I'm not alone.

In the section I will answer some common questions people email me with:

 

Q:  What input voltage does the board take? What is the polarity?
A: The board takes 9-14V and has a built in rectifier, so it supports both AC and DC.

Q: You said you had the CD, Do you have the Windows programs for it? Can you send them to me?
A: Yes I have the CD. Yes I have the programs for it, but they are from 1997, so quite bad compared to what's on offer now. And I don't think I can send them to you, but thankfully ATMEL provide their own called AVR Studio. The latest version (AVR studio 4) apparently supports this board and micro, and is free to download from their site.