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
|
// ======================================================================
// 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
};
static volatile byte_t mux_sel;
// ----------------------------------------------------------------------
// Handle a non-standard SETUP packet.
// ----------------------------------------------------------------------
extern byte_t usb_setup ( byte_t data[8] )
{
byte_t req;
// Generic requests
req = data[1];
if ( req == USBTINY_ECHO )
{
return 8;
}
// LED PORTC0-3
if ( req == USBTINY_READ )
{
data[0] = mux_sel;
return 1;
}
if ( req == USBTINY_WRITE )
{
mux_sel = data[2] & 1;
return 0;
}
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
// PB2-3 led output
PORTB &= ~(1 << PB3);
PORTB |= (1 << PB2);
DDRB |= (1 << PB2) | (1 << PB3);
// PB0-1 key in with pull up
DDRB &= ~((1 << PB0) | (1 << PB1));
PORTB |= (1 << PB0) | (1 << PB1);
mux_sel = 0;
usb_init();
for ( ;; )
{
usb_poll();
if( ~PINB & (1 << PB0)) {
mux_sel = 0;
} else if ( ~PINB & (1 << PB1)) {
mux_sel = 1;
}
if (mux_sel) {
PORTB &= ~(1 << PB2);
PORTB |= (1 << PB3);
} else {
PORTB &= ~(1 << PB3);
PORTB |= (1 << PB2);
}
}
}
|