Subversion Repositories group.electronics

Rev

Blame | Last modification | View Log | RSS feed

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>

#include "usbdrv.h"

#define F_CPU 12000000
#include <util/delay.h>

#define USB_LED_OFF 0
#define USB_LED_ON  1


int main() {
    uchar i;
        DDRD |= (1 << PD6); // PB0 as output

    wdt_enable(WDTO_1S); // enable 1s watchdog timer

    usbInit();
        
    usbDeviceDisconnect(); // enforce re-enumeration
    for(i = 0; i<250; i++) { // wait 500 ms
        wdt_reset(); // keep the watchdog happy
        _delay_ms(2);
    }
    usbDeviceConnect();
        
    sei(); // Enable interrupts after re-enumeration
        
    while(1) {
        wdt_reset(); // keep the watchdog happy
        usbPoll();
    }
        
    return 0;
}

// this gets called when custom control message is received
USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) {
    usbRequest_t *rq = (void *)data; // cast data to correct type
        
    switch(rq->bRequest) { // custom command is in the bRequest field
    case USB_LED_ON:
        PORTD |= (1 << PD6); // turn LED on
        return 0;
    case USB_LED_OFF: 
        PORTD &= ~(1 << PD6); // turn LED off
        return 0;
    }

    return 0; // should not get here
}