main.c


1
#include <avr/io.h>
2
#include <avr/interrupt.h>
3
#include <avr/wdt.h>
4
5
#include "usbdrv.h"
6
7
#define F_CPU 12000000L
8
#include <util/delay.h>
9
10
#define USB_LED_OFF 0
11
#define USB_LED_ON  1
12
13
// this gets called when custom control message is received
14
USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) {
15
    usbRequest_t *rq = (void *)data; // cast data to correct type
16
17
    switch(rq->bRequest) { // custom command is in the bRequest field
18
    case USB_LED_ON:
19
        PORTB = ( 1 << PD5); // turn LED on
20
        return 0;
21
    case USB_LED_OFF:
22
        PORTB = ( 0 << PD5); // turn LED off
23
        return 0;
24
    }
25
26
    return 0; // should not get here
27
}
28
29
int main() {
30
    uchar i;
31
32
    DDRB = (1 << PD5);
33
34
    wdt_enable(WDTO_1S); // enable 1s watchdog timer
35
36
    usbInit();
37
38
    usbDeviceDisconnect(); // enforce re-enumeration
39
    for(i = 0; i<250; i++) { // wait 500 ms
40
        wdt_reset(); // keep the watchdog happy
41
        _delay_ms(2);
42
    }
43
    usbDeviceConnect();
44
45
    sei(); // Enable interrupts after re-enumeration
46
47
    while(1) {
48
        wdt_reset(); // keep the watchdog happy
49
        usbPoll();
50
    }
51
52
    return 0;
53
}