Project

General

Profile

Statistics
| Branch: | Revision:

root / arduino-1.0 / hardware / arduino / bootloaders / diskloader / src / USBCore.h @ 58d82c77

History | View | Annotate | Download (6.36 KB)

1 58d82c77 Tom Mullins
2
// Copyright (c) 2010, Peter Barrett 
3
/*
4
** Permission to use, copy, modify, and/or distribute this software for  
5
** any purpose with or without fee is hereby granted, provided that the  
6
** above copyright notice and this permission notice appear in all copies.  
7
**  
8
** THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL  
9
** WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED  
10
** WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR  
11
** BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES  
12
** OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,  
13
** WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,  
14
** ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS  
15
** SOFTWARE.  
16
*/
17
18
#ifndef __USBCORE_H__
19
#define __USBCORE_H__
20
21
#define GET_STATUS                        0
22
#define CLEAR_FEATURE                1
23
#define SET_FEATURE                        3
24
#define SET_ADDRESS                        5
25
#define GET_DESCRIPTOR                6
26
#define GET_CONFIGURATION        8
27
#define SET_CONFIGURATION        9
28
#define GET_INTERFACE                10
29
#define SET_INTERFACE                11
30
31
// bmRequestType
32
#define HOSTTODEVICE        0x00
33
#define DEVICETOHOST        0x80
34
#define STANDARD                0x00
35
#define CLASS                        0x20
36
#define VENDOR                        0x40
37
#define DEVICE                        0x00
38
#define INTERFACE                0x01
39
#define ENDPOINT                0x02
40
#define OTHER                        0x03
41
42
#define CDC_SET_LINE_CODING                0x20
43
#define CDC_GET_LINE_CODING                0x21
44
#define CDC_SET_CONTROL_LINE_STATE        0x22
45
46
//        Descriptors
47
48
#define USB_DEVICE_DESC_SIZE 18
49
#define USB_CONFIGUARTION_DESC_SIZE 9
50
#define USB_INTERFACE_DESC_SIZE 9
51
#define USB_ENDPOINT_DESC_SIZE 7
52
53
#define USB_DEVICE_DESCRIPTOR_TYPE             1
54
#define USB_CONFIGURATION_DESCRIPTOR_TYPE      2
55
#define USB_STRING_DESCRIPTOR_TYPE             3
56
#define USB_INTERFACE_DESCRIPTOR_TYPE          4
57
#define USB_ENDPOINT_DESCRIPTOR_TYPE           5
58
59
#define USB_DEVICE_CLASS_COMMUNICATIONS        0x02
60
#define USB_DEVICE_CLASS_HUMAN_INTERFACE       0x03
61
#define USB_DEVICE_CLASS_STORAGE               0x08
62
#define USB_DEVICE_CLASS_VENDOR_SPECIFIC       0xFF
63
64
#define USB_CONFIG_POWERED_MASK                0x40
65
#define USB_CONFIG_BUS_POWERED                 0x80
66
#define USB_CONFIG_SELF_POWERED                0xC0
67
#define USB_CONFIG_REMOTE_WAKEUP               0x20
68
69
// bMaxPower in Configuration Descriptor
70
#define USB_CONFIG_POWER_MA(mA)                ((mA)/2)
71
72
// bEndpointAddress in Endpoint Descriptor
73
#define USB_ENDPOINT_DIRECTION_MASK            0x80
74
#define USB_ENDPOINT_OUT(addr)                 ((addr) | 0x00)
75
#define USB_ENDPOINT_IN(addr)                  ((addr) | 0x80)
76
77
#define USB_ENDPOINT_TYPE_MASK                 0x03
78
#define USB_ENDPOINT_TYPE_CONTROL              0x00
79
#define USB_ENDPOINT_TYPE_ISOCHRONOUS          0x01
80
#define USB_ENDPOINT_TYPE_BULK                 0x02
81
#define USB_ENDPOINT_TYPE_INTERRUPT            0x03
82
83
#define TOBYTES(x) ((x) & 0xFF),(((x) >> 8) & 0xFF)
84
85
#define CDC_V1_10                               0x0110
86
#define CDC_COMMUNICATION_INTERFACE_CLASS       0x02
87
88
#define CDC_CALL_MANAGEMENT                     0x01
89
#define CDC_ABSTRACT_CONTROL_MODEL              0x02
90
#define CDC_HEADER                              0x00
91
#define CDC_ABSTRACT_CONTROL_MANAGEMENT         0x02
92
#define CDC_UNION                               0x06
93
#define CDC_CS_INTERFACE                        0x24
94
#define CDC_CS_ENDPOINT                         0x25
95
#define CDC_DATA_INTERFACE_CLASS                0x0A
96
97
98
//        Device
99
typedef struct {
100
        u8 len;                                // 18
101
        u8 dtype;                        // 1 USB_DEVICE_DESCRIPTOR_TYPE
102
        u16 usbVersion;                // 0x200
103
        u8        deviceClass;
104
        u8        deviceSubClass;
105
        u8        deviceProtocol;
106
        u8        packetSize0;        // Packet 0
107
        u16        idVendor;
108
        u16        idProduct;
109
        u16        deviceVersion;        // 0x100
110
        u8        iManufacturer;
111
        u8        iProduct;
112
        u8        iSerialNumber;
113
        u8        bNumConfigurations;
114
} DeviceDescriptor;
115
116
//        Config
117
typedef struct {
118
        u8        len;                        // 9
119
        u8        dtype;                        // 2
120
        u16 clen;                        // total length
121
        u8        numInterfaces;
122
        u8        config;
123
        u8        iconfig;
124
        u8        attributes;
125
        u8        maxPower;
126
} ConfigDescriptor;
127
128
//        String
129
130
//        Interface
131
typedef struct
132
{
133
        u8 len;                // 9
134
        u8 dtype;        // 4
135
        u8 number;
136
        u8 alternate;
137
        u8 numEndpoints;
138
        u8 interfaceClass;
139
        u8 interfaceSubClass;
140
        u8 protocol;
141
        u8 iInterface;
142
} InterfaceDescriptor;
143
144
//        Endpoint
145
typedef struct
146
{
147
        u8 len;                // 7
148
        u8 dtype;        // 5
149
        u8 addr;
150
        u8 attr;
151
        u16 packetSize;
152
        u8 interval;
153
} EndpointDescriptor;
154
155
// Interface Association Descriptor
156
// Used to bind 2 interfaces together in CDC compostite device
157
typedef struct
158
{
159
        u8 len;                                // 8
160
        u8 dtype;                        // 11
161
        u8 firstInterface;
162
        u8 interfaceCount;
163
        u8 functionClass;
164
        u8 funtionSubClass;
165
        u8 functionProtocol;
166
        u8 iInterface;
167
} IADDescriptor;
168
169
//        CDC CS interface descriptor
170
typedef struct
171
{
172
        u8 len;                // 5
173
        u8 dtype;        // 0x24
174
        u8 subtype;
175
        u8 d0;
176
        u8 d1;
177
} CDCCSInterfaceDescriptor;
178
179
typedef struct
180
{
181
        u8 len;                // 4
182
        u8 dtype;        // 0x24
183
        u8 subtype;
184
        u8 d0;
185
} CDCCSInterfaceDescriptor4;
186
187
typedef struct 
188
{
189
        IADDescriptor                                iad;        // Only needed on compound device
190
191
        //        Control
192
        InterfaceDescriptor                        cif;        // 
193
        CDCCSInterfaceDescriptor        header;
194
        CDCCSInterfaceDescriptor        callManagement;
195
        CDCCSInterfaceDescriptor4        controlManagement;
196
        CDCCSInterfaceDescriptor        functionalDescriptor;
197
        EndpointDescriptor                        cifin;
198
199
        //        Data
200
        InterfaceDescriptor                        dif;
201
        EndpointDescriptor                        in;
202
        EndpointDescriptor                        out;
203
} CDCDescriptor;
204
205
typedef struct
206
{
207
        u8 len;                        // 9
208
        u8 dtype;                // 0x21
209
        u8 addr;
210
        u8        versionL;        // 0x101
211
        u8        versionH;        // 0x101
212
        u8        country;
213
        u8        desctype;        // 0x22 report
214
        u8        descLenL;
215
        u8        descLenH;
216
} HIDDescDescriptor;
217
218
typedef struct 
219
{
220
        InterfaceDescriptor                        hid;
221
        HIDDescDescriptor                        desc;
222
        EndpointDescriptor                        in;
223
} HIDDescriptor;
224
225
#define D_DEVICE(_class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs) \
226
        { 18, 1, 0x200, _class,_subClass,_proto,_packetSize0,_vid,_pid,_version,_im,_ip,_is,_configs }
227
228
#define D_CONFIG(_totalLength,_interfaces) \
229
        { 9, 2, _totalLength,_interfaces, 1, 0, USB_CONFIG_BUS_POWERED, USB_CONFIG_POWER_MA(100) }
230
231
#define D_INTERFACE(_n,_numEndpoints,_class,_subClass,_protocol) \
232
        { 9, 4, _n, 0, _numEndpoints, _class,_subClass, _protocol, 0 }
233
234
#define D_ENDPOINT(_addr,_attr,_packetSize, _interval) \
235
        { 7, 5, _addr,_attr,_packetSize, _interval }
236
237
#define D_IAD(_firstInterface, _count, _class, _subClass, _protocol) \
238
        { 8, 11, _firstInterface, _count, _class, _subClass, _protocol, 0 }
239
240
#define D_HIDREPORT(_descriptorLength) \
241
        { 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
242
243
#define D_CDCCS(_subtype,_d0,_d1)        { 5, 0x24, _subtype, _d0, _d1 }
244
#define D_CDCCS4(_subtype,_d0)                { 4, 0x24, _subtype, _d0 }
245
246
#endif