Easy Atmel AVRDUDE upload script

For my ASM and systems programming class, we occasionally write programs and need to upload them to the programmer board. Since my laptop only has USB ports, I was given a USB-to-Serial converter. In windows, there is device probing using AVR Studio that lets it find the device by itself. In linux, you only know which tty is the USB-to-Serial converter by looking at dmesg. Usually.

But this is the year 2008! Linux is a modern operating system! You should be able to do that probing through the command line, right?

Using HAL, you can easily find which device is the converter. It isn’t what AVR Studio does (sending magic packets) but it still works, and it doesn’t send any unwanted magic to my bluetooth radio or CDMA modem. I’ve written a small function for my zsh shell that automatically detects which USB tty is the serial converter and sets up the avrdude command as needed:

  1. alias avrdude=‘_avrdude’
  2. _avrdude() {
  3.     #Find the USB-to-serial adaptor entries, then find the actual USB device entry
  4.     UDI=$(hal-find-by-property –key info.product –string ‘FT232 USB-Serial (UART) IC’);
  5.     if [ -z "$UDI" ];then
  6.         echo "No USB device found."
  7.         return -1
  8.     fi;
  9.     UDI=$(hal-find-by-capability –capability ’serial’ | grep "$UDI")
  10.     #Get the USB character device
  11.     DEVICE=$(hal-get-property –udi "$UDI" –key linux.device_file)
  12.     NAME=$(hal-get-property –udi "$UDI" –key info.product)
  13.     echo "Using discovered device ${DEVICE} – ${NAME}"
  14.     =avrdude -c stk500v2 -p m16 -y -P "$DEVICE" $@
  15. }

Of course, this script is written for that specific USB device, and tells avrdude to prepare for my ATMega16 (-p m16) on the STK500v2 programming board. YMMV.

Comments »

My Atmel AVR

Last wednesday in my ASM and Systems Programming class we received our STK500 programming boards for use with our projects. Yesterday we were given some taste of how to program a board in Atmel ASM.

The ‘official’ way to download your program to an atmel board is to use their Windows-only AVR Studio. Boo!

Luckily, there is Linux software already available for this. AVRDUDE lets you communicate with the STK500 programmer to download code compiled into the Intel Hex format. Finding a compiler is a bit more difficult, but still pretty easy. There isn’t a compiler in the fedora repos, so I went searching and found avra. It is 100% compatible with the official AVR compiler.

As my first free time project, I made some leds count in binary:


;comment
.cseg
.org 0
.def count = r16
.def temp = r17
.equ PORTB = 0x18
.equ DDRB = 0x17
ldi temp,0xFF
out DDRB,temp
ldi count,0x00

lp:
out PORTB,count
inc count
rjmp lp

That was pretty thrilling for me, but I wanted more, so I made a pattern expand outwards:


;comment
.device ATmega16
.cseg
.org 0
.def output = r16
.def temp = r18
.equ PORTB = 0x18
.equ DDRB = 0x17
.equ LEFT = 0b11110000
.equ RIGHT = 0b00001111
.equ PATTERN = 0b00011000

ldi temp,0xFF
out DDRB,temp
ldi output,PATTERN

lp:
out PORTB,output
mov temp,output
andi output,LEFT
lsl output
andi temp,RIGHT
lsr temp
or output,temp
ldi temp,0
cpse temp,output
rjmp lp
ldi output,PATTERN
rjmp lp

And here’s the device in its blinking glory:

Comments »