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
|
// ======================================================================
// JVdG USB Gadget Firmware
//
// Copyright 2011 Julien Viard de Galbert
//
// based on:
// USBtiny template application
//
// Copyright 2006-2010 Dick Streefland
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
// ======================================================================
#include "usb.h"
#include <avr/io.h>
enum
{
// Generic requests
USBTINY_ECHO, // echo test
USBTINY_READ, // read byte
USBTINY_WRITE, // write byte
USBTINY_CLR, // clear bit
USBTINY_SET, // set bit
//
USBTINY_KEY_READ // read key status
};
// ----------------------------------------------------------------------
// Handle a non-standard SETUP packet.
// ----------------------------------------------------------------------
extern byte_t usb_setup ( byte_t data[8] )
{
byte_t bit;
byte_t mask;
byte_t req;
// Generic requests
req = data[1];
if ( req == USBTINY_ECHO )
{
return 8;
}
// LED PORTC0-3
if ( req == USBTINY_READ )
{
data[0] = PINC & ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3));
return 1;
}
if ( req == USBTINY_WRITE )
{
PORTC = data[2] & ((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3));
return 0;
}
bit = data[2] & 3;
mask = 1 << bit;
if ( req == USBTINY_CLR )
{
PORTC &= ~ mask;
return 0;
}
if ( req == USBTINY_SET )
{
PORTC |= mask;
return 0;
}
if ( req == USBTINY_KEY_READ )
{
// PD6-7, keys are inverted
data[0] = (~PIND & ((1 << PD6) | (1 << PD7))) >> PD6;
return 1;
}
return 0;
}
// ----------------------------------------------------------------------
// Handle an IN packet. (USBTINY_CALLBACK_IN==1)
// ----------------------------------------------------------------------
extern byte_t usb_in ( byte_t* data, byte_t len )
{
return 0;
}
// ----------------------------------------------------------------------
// Handle an OUT packet. (USBTINY_CALLBACK_OUT==1)
// ----------------------------------------------------------------------
extern void usb_out ( byte_t* data, byte_t len )
{
}
// ----------------------------------------------------------------------
// Main
// ----------------------------------------------------------------------
extern int main ( void )
{
// my init
// PC0-3 led output
PORTC &= ~((1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3));
DDRC |= (1 << PC0) | (1 << PC1) | (1 << PC2) | (1 << PC3);
// PD6-7 key in with pull up
DDRD &= ~((1 << PD6) | (1 << PD7));
PORTD |= (1 << PD6) | (1 << PD7);
usb_init();
for ( ;; )
{
usb_poll();
}
}
|