summaryrefslogtreecommitdiff
path: root/main.c
blob: a3d1b287f7758776211ce495d1c4767815ced27c (plain)
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
#define F_CPU 12000000L

#include <avr/io.h>
#include <avr/delay.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#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
}