#define F_CPU 12000000L #include #include #include #include #include "usbdrv/usbdrv.h" static uchar replyBuf[18]= "Hello, World!"; /* device is detected, however unreliably. dunno why. */ static uchar dataReceived, dataLength; void debug(int i) { if (i >= 63) i=63; PORTB = ~i; } USB_PUBLIC uchar usbFunctionSetup(uchar data[8]) { usbRequest_t* rq = (usbRequest_t*) data; switch(rq->bRequest) { case 0: PORTB |= 1; break; case 1: PORTB &= ~1; break; case 2: usbMsgPtr = replyBuf; return sizeof(replyBuf); // pc reads case 3: // alter byte PORTB = ~(rq->wIndex.word & 0xFF); if ((rq->wIndex.word >= 0) && (rq->wIndex.word < sizeof(replyBuf))) { replyBuf[rq->wIndex.word] = rq->wValue.word; } break; case 4: // pc writes dataLength = rq->wLength.word; debug(dataLength); dataReceived = 0; return USB_NO_MSG; } return 0; } USB_PUBLIC uchar usbFunctionWrite(uchar* data, uchar len) { PORTB&=~32; for (uchar i = 0; dataReceived < dataLength && i < len; i++, dataReceived++) { if (0 <= dataReceived && dataReceived < sizeof(replyBuf)) replyBuf[dataReceived] = data[i]; } return (dataReceived >= dataLength); } int main (void) { DDRC=0x00; PORTC=0x00; DDRB=0xFF; wdt_enable(WDTO_1S); /* for (int i=0;i<20;i++) { PORTB|=1; _delay_ms(50); PORTB&=~1; _delay_ms(50); wdt_reset(); } */ usbInit(); usbDeviceDisconnect(); for (int i=0;i<100;i++) { PORTB=~i; wdt_reset(); _delay_ms(5); } usbDeviceConnect(); PORTB=~42; sei(); while(1) { wdt_reset(); usbPoll(); } return 0; // never reached }