Usbed

Download the source:
usbed.tar.gz

Using OBDEV USB firmware (http://www.obdev.at/products/avrusb/index.html - no external chip is required, only zener / resistor). Can be clock at 12mhz, 16mhz, 20mhz (and internal clock too). If you want to use a clock different from 12mhz, you need to edit usbdrv/usbdrvasm.S (bottom of the file). This will be ported to mega164p an upgrade version of mega16 (will be clock at 20mhz).

The fuse for atmega164p:
-U lfuse:w:0xff:m -U hfuse:w:0x90:m -U efuse:w:0x05:m

From the Readme.txt of bootloader

You need an avr programmer to burn the bootloader (once).
http://www.atmel.com/dyn/Products/tools_card.asp?tool_id=2726 (and others)

::BOOTLOAD::
TODO: replace the bootloadhid with a bootloader in sysex: http://blogs.bl0rg.net/netzstaub/2008/08/18/sysex-bootloader-for-avr/
HID bootloader (doesn't require a driver)

cd bootload
Edit "bootloaderconfig.h" (USB_CFG* & bootLoaderInit & bootLoaderExit)
Edit "usbconfig.h" (USB_CFG_VENDOR_NAME & USB_CFG_DEVICE_NAME if you want)
Edit "Makefile" to suit your needs (AVRDUDE)
Edit "main.c" to configure you condition (how and when to use the bootloader):

char do_boot_load;
do_boot_load = ~PIND & (1 « JUMPER_BIT);
do_boot_load += ~MCUCSR & (1 « PORF);
if(do_boot_load){

}

make clean && make
make fuse
make flash

::COMMANDLINE::
An application to update the firmware

cd commandline
make clean && make
./bootloadHID -r somehex.hex

From the Readme.txt of firmware

Using part of:
http://cryptomys.de/horo/AVR-MIDI/

Using part of:
http://x37v.info/projects/microcontroller/avr-midi/

::FIRMWARE::
Midi/USB electronic drum module.
Doesn't require a driver.

Edit "usbconfig.h" (USB_CFG_VENDOR_NAME & USB_CFG_DEVICE_NAME if you want)
Edit "Makefile" to suit your needs (AVRDUDE)
Edit "main.c" - this is not really an example but more an implementation of my needs.

Here's some basic information…

usbFunctionWriteOut is the function called when sending from your application to your avr (via USB).
midiInit(MIDI_CLOCK_RATE, true, false); to have an external midi out connection (no need of opto-isolator)
midiSendNoteOn(MIDI_CHAN, 1, 67); to send to midi out connection

here's how to send via USB
while (!usbInterruptIsReady()) {
wdt_reset();
usbPoll();
}
midiMsg[0] = 0x09;
midiMsg[1] = MIDI_NOTEON;
midiMsg[2] = 1;
midiMsg[3] = 67;
midiMsg[4] = 0x08;
midiMsg[5] = MIDI_NOTEOFF;
midiMsg[6] = 1;
midiMsg[7] = 0;
sendEmptyFrame = 1;
usbSetInterrupt(midiMsg, 8);

::UPDATING THE FIRMWARE::

make clean && make
../bootloader/commandline/bootloadHID -r midimega.hex

THE FIRMWARE

/* 
 * Name: main.c
 * Project: Edubeat
 * 
 */

#include <string.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/wdt.h>
#include <avr/eeprom.h> 
#include <inttypes.h>
#include <stdlib.h>

#include "usbdrv.h"

#if DEBUG_LEVEL > 0
#include "oddebug.h"
#warning "Never compile production devices with debugging enabled"
#endif

#include "dm.h" //MIDI Device Descriptor
#include "midi.h" //MIDI protocol
#include "utils.h" //Convenience Macros

/* ------------------------------------------------------------------------- */
/* --------------------------------- DEFINE -------------------------------- */
/* ------------------------------------------------------------------------- */
#define MAJOR_VERSION 0
#define MINOR_VERSION 1
#define MIDI_CLOCK_RATE MIDI_CLOCK_12MHZ_OSC
#define MIDI_CHAN 9
#define SAMPLE 9 // 9 might be better
#define LOCK 47

#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
volatile unsigned long timer0_clock_cycles = 0;
volatile unsigned long timer0_millis = 0;
volatile unsigned char pad_msk;

volatile unsigned char dc_hand[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127 };
volatile unsigned char dc_bd[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127 };

/* ------------------------------------------------------------------------- */
/* ------------------------------- BOOTLOADER ------------------------------ */
/* ------------------------------------------------------------------------- */
// dummy function doing the jump to bootloader section (Adress 1C00 on Atmega16)
void (*jump_to_bootloader)(void) = 0x1C00; __attribute__ ((unused))

void startBootloader(void) {
        TIMSK &= ~(1<<TOIE0);            // disable timer overflow
        MCUCSR &= ~(1 << PORF);            // clear power on reset flag
                                        // this will hint the bootloader that it was forced

        cli();                            // turn off interrupts
        wdt_disable();                    // disable watchdog timer
        usbDeviceDisconnect();             // disconnect from USB bus
        /*
        cbi(ADCSRA, ADIE);                // disable ADC interrupts
        cbi(ADCSRA, ADEN);                // disable ADC (turn off ADC power)
        */

        PORTA = 0;                        // pull all pins low
        PORTB = 0;
        //PORTC = 0;

        jump_to_bootloader();
}

/* ------------------------------------------------------------------------- */
/* ----------------------------- MIDI interface ---------------------------- */
/* ------------------------------------------------------------------------- */
void midiInit(uint16_t clockScale, bool out, bool in){
    // Set baud rate
    UBRRH = (uint8_t)(clockScale >> 8);
    UBRRL = (uint8_t)(clockScale & 0xFF);
    // Enable transmitter
    if(out)
        UCSRB |= _BV(TXEN);
    if(in) {
        //Enable receiver
        //RX Complete Interrupt Enable  (user must provide routine)
        UCSRB |= _BV(RXEN) | _BV(RXCIE);
    }
    //Set frame format: Async, 8data, 1 stop bit, 1 start bit, no parity
    //needs to have URSEL set in order to write into this reg
    UCSRC = _BV(URSEL) | _BV(UCSZ1) | _BV(UCSZ0);
}

void midiSendByte(uint8_t inByte){
    // Wait for empty transmit buffer
    while ( !(UCSRA & _BV(UDRE)) );
    UDR = inByte;
}

void midiSendCC(uint8_t chan, uint8_t num, uint8_t val){
    //CC Status: 0xB0 to 0xBF where the low nibble is the MIDI channel.
    midiSendByte(MIDI_CC | (chan & MIDI_CHANMASK));
    //CC Data: Controller Num, Controller Val
    midiSendByte(num & 0x7F);
    midiSendByte(val & 0x7F);
}

void midiSendNoteOn(uint8_t chan, uint8_t num, uint8_t vel){
    midiSendByte(MIDI_NOTEON | (chan & MIDI_CHANMASK));
    //Note Data: Note Num, Note Velocity
    midiSendByte(num & 0x7F);
    midiSendByte(vel & 0x7F);
}

void midiSendNoteOff(uint8_t chan, uint8_t num, uint8_t vel){
    midiSendByte(MIDI_NOTEOFF | (chan & MIDI_CHANMASK));
    //Note Data: Note Num, Note Velocity
    midiSendByte(num & 0x7F);
    midiSendByte(vel & 0x7F);
}

/* ------------------------------------------------------------------------- */
/* ----------------------------- USB interface ----------------------------- */
/* ------------------------------------------------------------------------- */
uchar usbFunctionSetup(uchar data[8])
{
    usbRequest_t *rq = (void *) data;

    PORTC ^= 0x01;        // DEBUG LED

    if ((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS) {    /* class request type */

        /*  Prepare bulk-in endpoint to respond to early termination   */
        if ((rq->bmRequestType & USBRQ_DIR_MASK) ==
            USBRQ_DIR_HOST_TO_DEVICE)
            sendEmptyFrame = 1;
    }

    return 0xff;
}

/*---------------------------------------------------------------------------*/
/* usbFunctionDescriptor                                                     */
/*---------------------------------------------------------------------------*/
uchar usbFunctionDescriptor(usbRequest_t * rq)
{
    if (rq->wValue.bytes[1] == USBDESCR_DEVICE) {
        usbMsgPtr = (uchar *) deviceDescrMIDI;
        return sizeof(deviceDescrMIDI);
    } else {        /* must be config descriptor */
        usbMsgPtr = (uchar *) configDescrMIDI;
        return sizeof(configDescrMIDI);
    }
}

/*---------------------------------------------------------------------------*/
/*  usbSend                                                                  */
/*---------------------------------------------------------------------------*/
void usbsend(unsigned char note, unsigned char velo) {
    while (!usbInterruptIsReady()) {
              wdt_reset();
              usbPoll();
       }
    midiMsg[0] = 0x09;
    midiMsg[1] = MIDI_NOTEON|MIDI_CHAN;
    midiMsg[2] = note;
    //midiMsg[3] = velo;

    midiMsg[3] = dc_hand[velo];
    midiMsg[4] = 0x08;
    midiMsg[5] = MIDI_NOTEOFF|MIDI_CHAN;
    midiMsg[6] = note;
    midiMsg[7] = 0;

    sendEmptyFrame = 1;
    usbSetInterrupt(midiMsg, 8);
}

/*---------------------------------------------------------------------------*/
/* usbFunctionWriteOut                                                       */
/* this Function is called if a MIDI Out message (from PC) arrives.          */
/*---------------------------------------------------------------------------*/
uchar action[] = {0,0,0,0};
void usbFunctionWriteOut(uchar * data, uchar len)
{
    PORTC ^= 0x20;        // DEBUG LED    

    //data[0] == type of midi (noteon, noteoff, cc)
    //data[1] == maybe the midi port maybe the channel
    //data[2] == note, cc value
    //data[3] == velocity, cc number
    //len == 4

    //NRPN INCOMING - RESETTING action[]
    if(data[0] == 11 && data[3] == 99) {
            uchar i;
            for(i = 0; i < 4; i++) {
                action[i] = 0;
            }
    }

    //START BOOTLOADER
    if(data[0] == 11 && data[3] == 99 && data[2] == 1) {
        action[0] = 1;
    } else if(data[0] == 11 && data[3] == 98 && data[2] == 1) {
        action[1] = 1;
    } else if(data[0] == 11 && data[3] == 6 && data[2] == 0) {
        action[2] = 0;
    } else if(data[0] == 11 && data[3] == 38 && data[2] == 1) {
        action[3] = 1;

        //DOIT
        if(action[0] == 1 && action[1] == 1 && action[2] == 0 && action[3] == 1) {
            startBootloader();
        }
    }

    //GET VERSION
    if(data[0] == 11 && data[3] == 99 && data[2] == 1) {
        action[0] = 1;
    } else if(data[0] == 11 && data[3] == 98 && data[2] == 2) {
        action[1] = 2;
    } else if(data[0] == 11 && data[3] == 6 && data[2] == 0) {
        action[2] = 0;
    } else if(data[0] == 11 && data[3] == 38 && data[2] == 1) {
        action[3] = 1;

        //DOIT
        if(action[0] == 1 && action[1] == 2 && action[2] == 0 && action[3] == 1) {
            usbsend(1, MINOR_VERSION);
        }
    }

    //NPRN - PAD
    if(data[0] == 11 && data[3] == 99 && data[2] == 2) {
        action[0] = 2;
    } else if(data[0] == 11 && data[3] == 98 && data[2] == 1) {
        action[1] = 1;
    } else if(data[0] == 11 && data[3] == 6 && data[2] == 0) {
        action[2] = 0;
    } else if(data[0] == 11 && data[3] == 38 && data[2] != 0) {
        action[3] = data[2];

        //DOIT
        if(action[0] == 2 && action[1] == 1 && action[2] == 0 && action[3] != 0) {            
            eepromWrite(1, action[3]);
            if(action[3] == 1) {
                pad_msk = 0b00000001;
            } else if(action[3] == 2) {
                pad_msk = 0b00000011;
            } else if(action[3] == 3) {
                pad_msk = 0b00000111;
            } else if(action[3] == 4) {
                pad_msk = 0b00001111;
            } else if(action[3] == 5) {
                pad_msk = 0b00011111;
            } else if(action[3] == 6) {
                pad_msk = 0b00111111;
            } else if(action[3] == 7) {
                pad_msk = 0b01111111;
            }
        }
    }

    //NPRN - DYNAMIC CURVE
    if(data[0] == 11 && data[3] == 99 && data[2] == 3) {
        action[0] = 3;
    } else if(data[0] == 11 && data[3] == 98 && data[2] == 1) {
        action[1] = 1;
    } else if(data[0] == 11 && data[3] == 6) {
        action[2] = data[2];
    } else if(data[0] == 11 && data[3] == 38) {
        action[3] = data[2];

        //DOIT
        if(action[0] == 3 && action[1] == 1) {
            dc_hand[action[2]] = action[3];
        }
    }

    //NPRN - DYNAMIC CURVE - SAVE IN EEPROM
    if(data[0] == 11 && data[3] == 99 && data[2] == 3) {
        action[0] = 3;
    } else if(data[0] == 11 && data[3] == 98 && data[2] == 2) {
        action[1] = 2;
    } else if(data[0] == 11 && data[3] == 6 && data[2] == 0) {
        action[2] = 0;
    } else if(data[0] == 11 && data[3] == 38 && data[2] == 0) {
        action[3] = 0;

        //DOIT
        if(action[0] == 3 && action[1] == 2 &&  action[2] == 0 && action[3] == 0) {
                eeprom_write_block((const void*)&dc_hand, (void*)384, 128);
                //array //where //lenght
        }
    }

}

/*---------------------------------------------------------------------------*/
/* usbFunctionRead                                                           */
/*---------------------------------------------------------------------------*/
uchar usbFunctionRead(uchar * data, uchar len)
{
    //PORTC ^= 0x02;        // DEBUG LED
    return 1;
}

/*---------------------------------------------------------------------------*/
/* usbFunctionWrite                                                          */
/*---------------------------------------------------------------------------*/
uchar usbFunctionWrite(uchar * data, uchar len)
{
    //PORTC ^= 0x04;        // DEBUG LED
    return 1;
}

/*---------------------------------------------------------------------------*/
/* timerInit                                                                 */
/*---------------------------------------------------------------------------*/
static void timerInit(void)
{
   //Prescaler = FCPU/64
   TCCR0|=(1<<CS01)|(1<<CS00);
   //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);
   //Initialize Counter
   TCNT0=0;
}

/*---------------------------------------------------------------------------*/
/* timerISR                                                                  */
/*---------------------------------------------------------------------------*/
ISR(TIMER0_OVF_vect)
{
    timer0_clock_cycles += 64UL * 256UL;
    while (timer0_clock_cycles > clockCyclesPerMicrosecond() * 1000UL) {
        timer0_clock_cycles -= clockCyclesPerMicrosecond() * 1000UL;
        timer0_millis++;
    }
}

/*---------------------------------------------------------------------------*/
/* millis() - return ms since reset                                          */
/*---------------------------------------------------------------------------*/
unsigned long millis()
{
    cli();
    unsigned long m = timer0_millis;
    sei();
    return m;
}

// ------------------------------------------------------------------------------
// - Write to EEPROM
// ------------------------------------------------------------------------------
// from PowerSwitch by Objective Development
 void eepromWrite(unsigned char addr, unsigned char val)
{
    while(EECR & (1 << EEWE));
    EEARL = addr;
    EEDR = val;
    cli();
    EECR |= 1 << EEMWE;
    EECR |= 1 << EEWE;  /* must follow within a couple of cycles -- therefore cli() */
    sei();
}

// ------------------------------------------------------------------------------
// - Read EEPROM
// ------------------------------------------------------------------------------
// from PowerSwitch by Objective Development
uchar eepromRead(uchar addr)
{
    while(EECR & (1 << EEWE));
    EEARL = addr;
    EECR |= 1 << EERE;
    return EEDR;
}

/*---------------------------------------------------------------------------*/
/*  return 10 bit analog value                                               */
/*---------------------------------------------------------------------------*/
int adc(uchar channel)
{
    // single ended channel 0..7
    ADMUX = channel & 0x07;

    // AREF ext., adc right adjust result
    ADMUX |= (0 << REFS1) | (0 << REFS0) | (0 << ADLAR);

    // adc start conversion
    ADCSRA |= (1 << ADSC);

    while (ADCSRA & (1 << ADSC)) {
        ;        // idle
    }

    return ADC;
}

/*---------------------------------------------------------------------------*/
/* hardwareInit                                                              */
/*---------------------------------------------------------------------------*/
static void hardwareInit(void)
{
    uchar i, j;

    /* activate pull-ups except on USB lines */
    USB_CFG_IOPORT = (uchar) ~ ((1 << USB_CFG_DMINUS_BIT) | (1 << USB_CFG_DPLUS_BIT));

    /* all pins input except USB (-> USB reset) */
    #ifdef USB_CFG_PULLUP_IOPORT    /* use usbDeviceConnect()/usbDeviceDisconnect() if available */
        USBDDR = 0;        /* we do RESET by deactivating pullup */
        usbDeviceDisconnect();
    #else
        USBDDR = (1 << USB_CFG_DMINUS_BIT) | (1 << USB_CFG_DPLUS_BIT);
    #endif

    j = 0;
    while (--j) {        /* USB Reset by device only required on Watchdog Reset */
        i = 0;
        while (--i);    /* delay >10ms for USB reset */
    }

    #ifdef USB_CFG_PULLUP_IOPORT
        usbDeviceConnect();
    #else
        USBDDR = 0;        /*  remove USB reset condition */
    #endif

    // ADC Setup
    //ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0);
    ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);

    //PORTA = 0xff;   /* activate all pull-ups */
    DDRA = 0;       /* all pins input */

    // PORTB has eight keys (active low).
    PORTB = 0xff;        /* activate all pull-ups */
    DDRB = 0;        /* all pins input */

    // PORTC has eight (debug) LEDs (active low).
    PORTC = 0xff;        /* all LEDs off */
    DDRC = 0xff;        /* all pins output */

    // last nb of pad to use
    uchar e1 = eepromRead(1);
    if(e1 == 1) {
        pad_msk = 0b00000001;
    } else if(e1 == 2) {
        pad_msk = 0b00000011;
    } else if(e1 == 3) {
        pad_msk = 0b00000111;
    } else if(e1 == 4) {
        pad_msk = 0b00001111;
    } else if(e1 == 5) {
        pad_msk = 0b00011111;
    } else if(e1 == 6) {
        pad_msk = 0b00111111;
    } else if(e1 == 7) {
        pad_msk = 0b01111111;
    }

    // last dynamic curve
    eeprom_read_block((void*)&dc_hand, (const void*)384, 128); 
}

/*---------------------------------------------------------------------------*/
/* main                                                                      */
/*---------------------------------------------------------------------------*/
int main(void)
{
    //edubeat
    uchar i = 0;
    uchar j = 0;
    uint16_t pv[] = {0,0,0,0,0,0,0,0};
    uint16_t tpv[] = {0,0,0,0,0,0,0,0};
    unsigned long tpl[] = {0,0,0,0,0,0,0,0};

    wdt_enable(WDTO_1S);
    hardwareInit();

    #if DEBUG_LEVEL > 0
        odDebugInit();
    #endif
    usbInit();
    midiInit(MIDI_CLOCK_RATE, true, false);
    timerInit();
    sei();

    for (;;) {
        wdt_reset();

        //jump to bootloader if jumper is HIGH
        if(bit_is_clear(PIND, 5)) {
            startBootloader();
        }

        usbPoll();

        //USB
        //just reading adc7
        //usbsend(7, adc(7) >> 3);
        //usbsend(15, eepromRead(411));

        uchar pad_msk_read = PINB & pad_msk;
        //pad_msk_read = 0b00000001;
        for(j = 0; j < SAMPLE; j++) {
            for(i = 0; i < 8; i++) {
                    uint16_t maxk = 0;

                    if((pad_msk_read & 1<<i) && (millis() - tpl[i] > LOCK)) { 
                        if((tpv[i] = adc(i)) > maxk) {
                            pv[i] = maxk = tpv[i];
                        }

                        if(j == (SAMPLE - 1)) {

                            //hihat pedal
                            if(i == 2) {
                                usbsend(3, adc(7) >> 3);
                            }

                            tpl[i] = millis();

                            // DEBUG LED
                            PORTC ^= 0x40;

                            //MIDI
                            //midiSendNoteOn(MIDI_CHAN, i, pv[i] >> 3);
                            //midiSendNoteOff(MIDI_CHAN, i, 0);

                            //USB
                            usbsend(i, pv[i] >> 3);

                            pv[i] = 0;
                    }
                }
            }
        }

    }
    return 0;
}

THE MAKEFILE

## General Flags
PROJECT = usbed
MCU = atmega16
TARGET = $(PROJECT).elf
#DEBUG =  -DDEBUG_LEVEL=0
CC = avr-gcc
AVRDUDE = avrdude -c avrispmkII -P usb -p$(MCU)
FUSEH = 0xc1
FUSEL = 0x9f

## Options common to compile, link and assembly rules
COMMON = -g -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON)
#CFLAGS += -Wall -DF_CPU=12000000UL -Os -fsigned-char $(DEBUG)
CFLAGS += -Wall -DF_CPU=12000000UL -Os -fsigned-char

## Assembly specific flags
ASMFLAGS = $(COMMON)
ASMFLAGS += -x assembler-with-cpp -Wa,

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl,-Map=$(PROJECT).map

## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom

## Include Directories
INCLUDES = -I"." -I"usbdrv"

## Objects that must be built in order to link
#OBJECTS = usbdrv.o usbdrvasm.o oddebug.o main.o
OBJECTS = usbdrv.o usbdrvasm.o main.o

## Objects explicitly added by the user
LINKONLYOBJECTS = 

## Build
all: $(TARGET) $(PROJECT).hex $(PROJECT).lss

$(OBJECTS): usbconfig.h Makefile

## Compile
usbdrv.o: usbdrv/usbdrv.c
    $(CC) $(INCLUDES) $(CFLAGS) -c  $<

usbdrvasm.o: usbdrv/usbdrvasm.S
    $(CC) $(INCLUDES) $(ASMFLAGS) -c  $<

#oddebug.o: usbdrv/oddebug.c
    #$(CC) $(INCLUDES) $(CFLAGS) -c  $<

main.o: main.c
    $(CC) $(INCLUDES) $(CFLAGS) -c  $<

##Link
$(TARGET): $(OBJECTS)
     $(CC) $(LDFLAGS) $(OBJECTS) $(LINKONLYOBJECTS) $(LIBDIRS) $(LIBS) -o $(TARGET)

%.hex: $(TARGET)
    avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.lss: $(TARGET)
    avr-objdump -h -S $< > $@

## Clean target
.PHONY: clean
clean:
    -rm -rf $(OBJECTS) $(PROJECT).* *~

.PHONY: flash
flash:    all
    $(AVRDUDE) -U flash:w:$(PROJECT).hex

.PHONY: fuse
fuse:
    $(AVRDUDE) -U hfuse:w:$(FUSEH):m -U lfuse:w:$(FUSEL):m

usbconfig.h

/* Name: usbconfig.h
 * Project: AVR USB driver
 * Author: Christian Starkjohann
 * Creation Date: 2005-04-01
 * Tabsize: 4
 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
 * License: Proprietary, free under certain conditions. See Documentation.
 * This Revision: $Id: usbconfig-prototype.h 216 2006-07-14 21:51:00Z cs $
 */

#ifndef __usbconfig_h_included__
#define __usbconfig_h_included__

/*
General Description:
This file is an example configuration (with inline documentation) for the USB
driver. It configures AVR-USB for an ATMega8 with USB D+ connected to Port D
bit 2 (which is also hardware interrupt 0) and USB D- to Port D bit 0. You may
wire the lines to any other port, as long as D- is on bit 0 and D+ is also
wired to INT0.
To create your own usbconfig.h file, copy this file to the directory
containing "usbdrv" (that is your project firmware source directory) and
rename it to "usbconfig.h". Then edit it accordingly.
*/

// work around driver bug (?)
#define USB_INITIAL_DATATOKEN USBPID_DATA1

/* ---------------------------- Hardware Config ---------------------------- */

#define USB_CFG_IOPORTNAME      D
/* This is the port where the USB bus is connected. When you configure it to
 * "B", the registers PORTB, PINB and DDRB will be used.
 */
#define USB_CFG_DMINUS_BIT      3
/* This is the bit number in USB_CFG_IOPORT where the USB D- line is connected.
 * This may be any bit in the port.
 */
#define USB_CFG_DPLUS_BIT       2
/* This is the bit number in USB_CFG_IOPORT where the USB D+ line is connected.
 * This may be any bit in the port. Please note that D+ must also be connected
 * to interrupt pin INT0!
 */

/* ----------------------- Optional Hardware Config ------------------------ */

#define USB_CFG_PULLUP_IOPORTNAME   D
/* If you connect the 1.5k pullup resistor from D- to a port pin instead of
 * V+, you can connect and disconnect the device from firmware by calling
 * the macros usbDeviceConnect() and usbDeviceDisconnect() (see usbdrv.h).
 * This constant defines the port on which the pullup resistor is connected.
 */
#define USB_CFG_PULLUP_BIT          4
/* This constant defines the bit number in USB_CFG_PULLUP_IOPORT (defined
 * above) where the 1.5k pullup resistor is connected. See description
 * above for details.
 */
/* #define  USB_BUFFER_SECTION         ".bss" */
/* The USB receive buffer (variable "usbRxBuf") with a length of 22 bytes
 * MUST NOT cross a 256 byte boundary. We have introduced this configuration
 * option to allow you to change the data segment where this buffer is
 * allocated. If you have problems with the default segment (start of .bss),
 * you may change this setting. See the comment in usbdrv.h for details.
 * On IAR C, the default is the TINY_Z segment (first 256 bytes). You must
 * change this default for devices which don't have RAM below 0x100.
 */

/* --------------------------- Functional Range ---------------------------- */

#define USB_CFG_HAVE_INTRIN_ENDPOINT    1
/* Define this to 1 if you want to compile a version with two endpoints: The
 * default control endpoint 0 and an interrupt-in endpoint 1.
 */
#define USB_CFG_HAVE_INTRIN_ENDPOINT3   0
/* Define this to 1 if you want to compile a version with three endpoints: The
 * default control endpoint 0, an interrupt-in endpoint 1 and an interrupt-in
 * endpoint 3. You must also enable endpoint 1 above.
 */
#define USB_CFG_IMPLEMENT_HALT          1
/* Define this to 1 if you also want to implement the ENDPOINT_HALT feature
 * for endpoint 1 (interrupt endpoint). Although you may not need this feature,
 * it is required by the standard. We have made it a config option because it
 * bloats the code considerably.
 */
#define USB_CFG_INTR_POLL_INTERVAL      10
/* If you compile a version with endpoint 1 (interrupt-in), this is the poll
 * interval. The value is in milliseconds and must not be less than 10 ms for
 * low speed devices.
 */
#define USB_CFG_IS_SELF_POWERED         0
/* Define this to 1 if the device has its own power supply. Set it to 0 if the
 * device is powered from the USB bus.
 */
#define USB_CFG_MAX_BUS_POWER           500
/* Set this variable to the maximum USB bus power consumption of your device.
 * The value is in milliamperes. [It will be divided by two since USB
 * communicates power requirements in units of 2 mA.]
 */
#define USB_CFG_SAMPLE_EXACT            1
/* This variable affects Sampling Jitter for USB receiving. When it is 0, the
 * driver guarantees a sampling window of 1/2 bit. The USB spec requires
 * that the receiver has at most 1/4 bit sampling window. The 1/2 bit window
 * should still work reliably enough because we work at low speed. If you want
 * to meet the spec, set this value to 1. This will unroll a loop which
 * results in bigger code size.
 * If you have problems with long cables, try setting this value to 1.
 */
#define USB_CFG_IMPLEMENT_FN_WRITE      1
/* Set this to 1 if you want usbFunctionWrite() to be called for control-out
 * transfers. Set it to 0 if you don't need it and want to save a couple of
 * bytes.
 */
#define USB_CFG_IMPLEMENT_FN_READ       1
/* Set this to 1 if you need to send control replies which are generated
 * "on the fly" when usbFunctionRead() is called. If you only want to send
 * data from a static buffer, set it to 0 and return the data from
 * usbFunctionSetup(). This saves a couple of bytes.
 */
#define USB_CFG_IMPLEMENT_FN_WRITEOUT   1
/* Define this to 1 if you want to use interrupt-out (or bulk out) endpoint 1.
 * You must implement the function usbFunctionWriteOut() which receives all
 * interrupt/bulk data sent to endpoint 1.
 */
#define USB_CFG_HAVE_FLOWCONTROL        0
/* Define this to 1 if you want flowcontrol over USB data. See the definition
 * of the macros usbDisableAllRequests() and usbEnableAllRequests() in
 * usbdrv.h.
 */

/* -------------------------- Device Description --------------------------- */

#define  USB_CFG_VENDOR_ID       0xc0, 0x16    /* VOTI / obdev subrange */
/* USB vendor ID for the device, low byte first. If you have registered your
 * own Vendor ID, define it here. Otherwise you use obdev's free shared
 * VID/PID pair. Be sure to read USBID-License.txt for rules!
 * This template uses obdev's shared VID/PID pair: 0x16c0/0x5e4.
 * Use this VID/PID pair ONLY if you understand the implications!
 */
#define  USB_CFG_DEVICE_ID       0xe4, 0x05    /* 0x05e4 = 1508, obdev MIDI */
/* This is the ID of the product, low byte first. It is interpreted in the
 * scope of the vendor ID. If you have registered your own VID with usb.org
 * or if you have licensed a PID from somebody else, define it here. Otherwise
 * you use obdev's free shared VID/PID pair. Be sure to read the rules in
 * USBID-License.txt!
 */
#define USB_CFG_DEVICE_VERSION  0x01, 0x00
/* Version number of the device: Minor number first, then major number.
 */
#define USB_CFG_VENDOR_NAME     'E', 'd', 'u', 'b', 'e', 'a', 't'
#define USB_CFG_VENDOR_NAME_LEN 7
/* These two values define the vendor name returned by the USB device. The name
 * must be given as a list of characters under single quotes. The characters
 * are interpreted as Unicode (UTF-16) entities.
 * If you don't want a vendor name string, undefine these macros.
 * ALWAYS define a vendor name containing your Internet domain name if you use
 * obdev's free shared VID/PID pair. See the file USBID-License.txt for
 * details.
 */
#ifdef DEBUG_LEVEL
#    define USB_CFG_DEVICE_NAME     'E', 'd', 'u', 'b', 'e', 'a', 't', '-', 'D', 'B', 'G'
#    define USB_CFG_DEVICE_NAME_LEN 11
#else
#    define USB_CFG_DEVICE_NAME     'E', 'd', 'u', 'b', 'e', 'a', 't'
#    define USB_CFG_DEVICE_NAME_LEN 7
#endif
/* Same as above for the device name. If you don't want a device name, undefine
 * the macros. See the file USBID-License.txt before you assign a name if you
 * use a shared VID/PID.
 */
/*#define USB_CFG_SERIAL_NUMBER   'N', 'o', 'n', 'e' */
/*#define USB_CFG_SERIAL_NUMBER_LEN   0 */
/* Same as above for the serial number. If you don't want a serial number,
 * undefine the macros.
 * It may be useful to provide the serial number through other means than at
 * compile time. See the section about descriptor properties below for how
 * to fine tune control over USB descriptors such as the string descriptor
 * for the serial number.
 */
#define USB_CFG_DEVICE_CLASS        0    /* Defined at interface level */
#define USB_CFG_DEVICE_SUBCLASS     0    /* Defined at interface level */
/* See USB specification if you want to conform to an existing device class.
 */
#define USB_CFG_INTERFACE_CLASS     1    /* AUDIO class */
#define USB_CFG_INTERFACE_SUBCLASS  3    /* MIDI streaming */
#define USB_CFG_INTERFACE_PROTOCOL  0    /*  */
/* See USB specification if you want to conform to an existing device class or
 * protocol.
 * This template defines a HID class device. If you implement a vendor class
 * device, set USB_CFG_INTERFACE_CLASS to 0 and USB_CFG_DEVICE_CLASS to 0xff.
 */
#define USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH    0    /* total length of report descriptor */
/* Define this to the length of the HID report descriptor, if you implement
 * an HID device. Otherwise don't define it or define it to 0.
 * Since this template defines a HID device, it must also specify a HID
 * report descriptor length. You must add a PROGMEM character array named
 * "usbHidReportDescriptor" to your code which contains the report descriptor.
 * Don't forget to keep the array and this define in sync!
 */

/* ------------------- Fine Control over USB Descriptors ------------------- */
/* If you don't want to use the driver's default USB descriptors, you can
 * provide our own. These can be provided as (1) fixed length static data in
 * flash memory, (2) fixed length static data in RAM or (3) dynamically at
 * runtime in the function usbFunctionDescriptor(). See usbdrv.h for more
 * information about this function.
 * Descriptor handling is configured through the descriptor's properties. If
 * no properties are defined or if they are 0, the default descriptor is used.
 * Possible properties are:
 *   + USB_PROP_IS_DYNAMIC: The data for the descriptor should be fetched
 *     at runtime via usbFunctionDescriptor().
 *   + USB_PROP_IS_RAM: The data returned by usbFunctionDescriptor() or found
 *     in static memory is in RAM, not in flash memory.
 *   + USB_PROP_LENGTH(len): If the data is in static memory (RAM or flash),
 *     the driver must know the descriptor's length. The descriptor itself is
 *     found at the address of a well known identifier (see below).
 * List of static descriptor names (must be declared PROGMEM if in flash):
 *   char usbDescriptorDevice[];
 *   char usbDescriptorConfiguration[];
 *   char usbDescriptorHidReport[];
 *   char usbDescriptorString0[];
 *   int usbDescriptorStringVendor[];
 *   int usbDescriptorStringDevice[];
 *   int usbDescriptorStringSerialNumber[];
 * Other descriptors can't be provided statically, they must be provided
 * dynamically at runtime.
 *
 * Descriptor properties are or-ed or added together, e.g.:
 * #define USB_CFG_DESCR_PROPS_DEVICE   (USB_PROP_IS_RAM | USB_PROP_LENGTH(18))
 *
 * The following descriptors are defined:
 *   USB_CFG_DESCR_PROPS_DEVICE
 *   USB_CFG_DESCR_PROPS_CONFIGURATION
 *   USB_CFG_DESCR_PROPS_STRINGS
 *   USB_CFG_DESCR_PROPS_STRING_0
 *   USB_CFG_DESCR_PROPS_STRING_VENDOR
 *   USB_CFG_DESCR_PROPS_STRING_PRODUCT
 *   USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
 *   USB_CFG_DESCR_PROPS_HID
 *   USB_CFG_DESCR_PROPS_HID_REPORT
 *   USB_CFG_DESCR_PROPS_UNKNOWN (for all descriptors not handled by the driver)
 *
 */

#define USB_CFG_DESCR_PROPS_DEVICE                  USB_PROP_IS_DYNAMIC
#define USB_CFG_DESCR_PROPS_CONFIGURATION           USB_PROP_IS_DYNAMIC
#define USB_CFG_DESCR_PROPS_STRINGS                 0
#define USB_CFG_DESCR_PROPS_STRING_0                0
#define USB_CFG_DESCR_PROPS_STRING_VENDOR           0
#define USB_CFG_DESCR_PROPS_STRING_PRODUCT          0
#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER    0
#define USB_CFG_DESCR_PROPS_HID                     0    // USB_PROP_IS_DYNAMIC
#define USB_CFG_DESCR_PROPS_HID_REPORT              0
#define USB_CFG_DESCR_PROPS_UNKNOWN                 0

/* ----------------------- Optional MCU Description ------------------------ */

/* The following configurations have working defaults in usbdrv.h. You
 * usually don't need to set them explicitly. Only if you want to run
 * the driver on a device which is not yet supported or with a compiler
 * which is not fully supported (such as IAR C) or if you use a differnt
 * interrupt than INT0, you may have to define some of these.
 */
/* #define USB_INTR_CFG            MCUCR */
/* #define USB_INTR_CFG_SET        ((1 << ISC00) | (1 << ISC01)) */
/* #define USB_INTR_CFG_CLR        0 */
/* #define USB_INTR_ENABLE         GIMSK */
/* #define USB_INTR_ENABLE_BIT     INT0 */
/* #define USB_INTR_PENDING        GIFR */
/* #define USB_INTR_PENDING_BIT    INTF0 */

#endif                /* __usbconfig_h_included__ */
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License