atmel at90usb162 linux hello world howto

hi

here some nice howto for an hello world program with atmel at90usb162.

install some packages:

apt-get install avr-gcc avr-libc dfu-programmer

here is my hello world script which switches an led on an off

#include <avr/io.h>
#define F_CPU 2000000UL
#include <util/delay.h>
#include <avr/wdt.h>

int main(void) {
 MCUSR &= ~(1 << WDRF);
 wdt_disable();

 DDRD |= (1<<6);

 while(1) {
   if ( !(PIND & (1<<6) ) ){
    PORTD &= ~(1<<6);
    _delay_ms(1000);
    PORTD |= (1<<6);
    _delay_ms(1000);

    PORTD &= ~(1<<6);
    _delay_ms(1000);
    PORTD |= (1<<6);
    _delay_ms(1000);

    int i;
    for (i = 0; i < 20; i++) {
     PORTD &= ~(1<<6);
     _delay_ms(50);
     PORTD |= (1<<6);
     _delay_ms(50);
    }
 
   }
   else {
    PORTD &= ~(1<<6);
   }
 }

return 0;
}

due to my laziness i wrote a shell script whitch automates compiling, converting and the flashing process

#!/bin/bash

if [ -z $1 ] || [ -z $2 ] || [ -z $3 ]; then
echo "usage: doavr test.c test.elf test.hex"
exit
fi

if [ -e $2 ]; then
rm $2
fi
if [ -e $3 ]; then
rm $3
fi

avr-gcc -g -Os -mmcu=at90usb162 $1 -o $2
avr-objcopy -j .text -j .data -O ihex $2 $3

sudo dfu-programmer at90usb162 erase
sleep 1
sudo dfu-programmer at90usb162 flash $3
sleep 1
sudo dfu-programmer at90usb162 reset

have fun!