1 |
/* Name: usbdrv.c |
1 |
/* Name: usbdrv.c |
2 |
* Project: AVR USB driver |
2 |
* Project: AVR USB driver |
3 |
* Author: Christian Starkjohann |
3 |
* Author: Christian Starkjohann |
4 |
* Creation Date: 2004-12-29 |
4 |
* Creation Date: 2004-12-29 |
5 |
* Tabsize: 4 |
5 |
* Tabsize: 4 |
6 |
* Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH |
6 |
* Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH |
7 |
* License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) |
7 |
* License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt) |
8 |
* This Revision: $Id: usbdrv.c,v 1.3 2007/06/07 13:53:47 harbaum Exp $ |
8 |
* This Revision: $Id: usbdrv.c,v 1.3 2007/06/07 13:53:47 harbaum Exp $ |
9 |
*/ |
9 |
*/ |
10 |
|
10 |
|
11 |
#include "iarcompat.h" |
11 |
#include "iarcompat.h" |
12 |
#ifndef __IAR_SYSTEMS_ICC__ |
12 |
#ifndef __IAR_SYSTEMS_ICC__ |
13 |
# include <avr/io.h> |
13 |
# include <avr/io.h> |
14 |
# include <avr/pgmspace.h> |
14 |
# include <avr/pgmspace.h> |
15 |
#endif |
15 |
#endif |
16 |
#include "usbdrv.h" |
16 |
#include "usbdrv.h" |
17 |
#include "oddebug.h" |
17 |
#include "oddebug.h" |
18 |
|
18 |
|
19 |
/* |
19 |
/* |
20 |
General Description: |
20 |
General Description: |
21 |
This module implements the C-part of the USB driver. See usbdrv.h for a |
21 |
This module implements the C-part of the USB driver. See usbdrv.h for a |
22 |
documentation of the entire driver. |
22 |
documentation of the entire driver. |
23 |
*/ |
23 |
*/ |
24 |
|
24 |
|
25 |
#ifndef IAR_SECTION |
25 |
#ifndef IAR_SECTION |
26 |
#define IAR_SECTION(arg) |
26 |
#define IAR_SECTION(arg) |
27 |
#define __no_init |
27 |
#define __no_init |
28 |
#endif |
28 |
#endif |
29 |
/* The macro IAR_SECTION is a hack to allow IAR-cc compatibility. On gcc, it |
29 |
/* The macro IAR_SECTION is a hack to allow IAR-cc compatibility. On gcc, it |
30 |
* is defined to nothing. __no_init is required on IAR. |
30 |
* is defined to nothing. __no_init is required on IAR. |
31 |
*/ |
31 |
*/ |
32 |
|
32 |
|
33 |
/* ------------------------------------------------------------------------- */ |
33 |
/* ------------------------------------------------------------------------- */ |
34 |
|
34 |
|
35 |
/* raw USB registers / interface to assembler code: */ |
35 |
/* raw USB registers / interface to assembler code: */ |
36 |
uchar usbRxBuf[2*USB_BUFSIZE]; /* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */ |
36 |
uchar usbRxBuf[2*USB_BUFSIZE]; /* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */ |
37 |
uchar usbInputBufOffset; /* offset in usbRxBuf used for low level receiving */ |
37 |
uchar usbInputBufOffset; /* offset in usbRxBuf used for low level receiving */ |
38 |
uchar usbDeviceAddr; /* assigned during enumeration, defaults to 0 */ |
38 |
uchar usbDeviceAddr; /* assigned during enumeration, defaults to 0 */ |
39 |
uchar usbNewDeviceAddr; /* device ID which should be set after status phase */ |
39 |
uchar usbNewDeviceAddr; /* device ID which should be set after status phase */ |
40 |
uchar usbConfiguration; /* currently selected configuration. Administered by driver, but not used */ |
40 |
uchar usbConfiguration; /* currently selected configuration. Administered by driver, but not used */ |
41 |
volatile schar usbRxLen; /* = 0; number of bytes in usbRxBuf; 0 means free, -1 for flow control */ |
41 |
volatile schar usbRxLen; /* = 0; number of bytes in usbRxBuf; 0 means free, -1 for flow control */ |
42 |
uchar usbCurrentTok; /* last token received, if more than 1 rx endpoint: MSb=endpoint */ |
42 |
uchar usbCurrentTok; /* last token received, if more than 1 rx endpoint: MSb=endpoint */ |
43 |
uchar usbRxToken; /* token for data we received; if more than 1 rx endpoint: MSb=endpoint */ |
43 |
uchar usbRxToken; /* token for data we received; if more than 1 rx endpoint: MSb=endpoint */ |
44 |
uchar usbMsgLen = 0xff; /* remaining number of bytes, no msg to send if -1 (see usbMsgPtr) */ |
44 |
uchar usbMsgLen = 0xff; /* remaining number of bytes, no msg to send if -1 (see usbMsgPtr) */ |
45 |
volatile uchar usbTxLen = USBPID_NAK; /* number of bytes to transmit with next IN token or handshake token */ |
45 |
volatile uchar usbTxLen = USBPID_NAK; /* number of bytes to transmit with next IN token or handshake token */ |
46 |
uchar usbTxBuf[USB_BUFSIZE];/* data to transmit with next IN, free if usbTxLen contains handshake token */ |
46 |
uchar usbTxBuf[USB_BUFSIZE];/* data to transmit with next IN, free if usbTxLen contains handshake token */ |
47 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
47 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
48 |
volatile uchar usbTxLen1 = USBPID_NAK; /* TX count for endpoint 1 */ |
48 |
volatile uchar usbTxLen1 = USBPID_NAK; /* TX count for endpoint 1 */ |
49 |
uchar usbTxBuf1[USB_BUFSIZE]; /* TX data for endpoint 1 */ |
49 |
uchar usbTxBuf1[USB_BUFSIZE]; /* TX data for endpoint 1 */ |
50 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
50 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
51 |
volatile uchar usbTxLen3 = USBPID_NAK; /* TX count for endpoint 1 */ |
51 |
volatile uchar usbTxLen3 = USBPID_NAK; /* TX count for endpoint 1 */ |
52 |
uchar usbTxBuf3[USB_BUFSIZE]; /* TX data for endpoint 1 */ |
52 |
uchar usbTxBuf3[USB_BUFSIZE]; /* TX data for endpoint 1 */ |
53 |
#endif |
53 |
#endif |
54 |
#endif |
54 |
#endif |
55 |
|
55 |
|
56 |
/* USB status registers / not shared with asm code */ |
56 |
/* USB status registers / not shared with asm code */ |
57 |
uchar *usbMsgPtr; /* data to transmit next -- ROM or RAM address */ |
57 |
uchar *usbMsgPtr; /* data to transmit next -- ROM or RAM address */ |
58 |
static uchar usbMsgFlags; /* flag values see below */ |
58 |
static uchar usbMsgFlags; /* flag values see below */ |
59 |
|
59 |
|
60 |
#define USB_FLG_TX_PACKET (1<<0) |
60 |
#define USB_FLG_TX_PACKET (1<<0) |
61 |
/* Leave free 6 bits after TX_PACKET. This way we can increment usbMsgFlags to toggle TX_PACKET */ |
61 |
/* Leave free 6 bits after TX_PACKET. This way we can increment usbMsgFlags to toggle TX_PACKET */ |
62 |
#define USB_FLG_MSGPTR_IS_ROM (1<<6) |
62 |
#define USB_FLG_MSGPTR_IS_ROM (1<<6) |
63 |
#define USB_FLG_USE_DEFAULT_RW (1<<7) |
63 |
#define USB_FLG_USE_DEFAULT_RW (1<<7) |
64 |
|
64 |
|
65 |
/* |
65 |
/* |
66 |
optimizing hints: |
66 |
optimizing hints: |
67 |
- do not post/pre inc/dec integer values in operations |
67 |
- do not post/pre inc/dec integer values in operations |
68 |
- assign value of PRG_RDB() to register variables and don't use side effects in arg |
68 |
- assign value of PRG_RDB() to register variables and don't use side effects in arg |
69 |
- use narrow scope for variables which should be in X/Y/Z register |
69 |
- use narrow scope for variables which should be in X/Y/Z register |
70 |
- assign char sized expressions to variables to force 8 bit arithmetics |
70 |
- assign char sized expressions to variables to force 8 bit arithmetics |
71 |
*/ |
71 |
*/ |
72 |
|
72 |
|
73 |
/* ------------------------------------------------------------------------- */ |
73 |
/* ------------------------------------------------------------------------- */ |
74 |
|
74 |
|
75 |
#if USB_CFG_DESCR_PROPS_STRINGS == 0 |
75 |
#if USB_CFG_DESCR_PROPS_STRINGS == 0 |
76 |
|
76 |
|
77 |
#if USB_CFG_DESCR_PROPS_STRING_0 == 0 |
77 |
#if USB_CFG_DESCR_PROPS_STRING_0 == 0 |
78 |
#undef USB_CFG_DESCR_PROPS_STRING_0 |
78 |
#undef USB_CFG_DESCR_PROPS_STRING_0 |
79 |
#define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0) |
79 |
#define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0) |
80 |
PROGMEM char usbDescriptorString0[] = { /* language descriptor */ |
80 |
PROGMEM char usbDescriptorString0[] = { /* language descriptor */ |
81 |
4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */ |
81 |
4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */ |
82 |
3, /* descriptor type */ |
82 |
3, /* descriptor type */ |
83 |
0x09, 0x04, /* language index (0x0409 = US-English) */ |
83 |
0x09, 0x04, /* language index (0x0409 = US-English) */ |
84 |
}; |
84 |
}; |
85 |
#endif |
85 |
#endif |
86 |
|
86 |
|
87 |
#if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN |
87 |
#if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN |
88 |
#undef USB_CFG_DESCR_PROPS_STRING_VENDOR |
88 |
#undef USB_CFG_DESCR_PROPS_STRING_VENDOR |
89 |
#define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor) |
89 |
#define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor) |
90 |
PROGMEM int usbDescriptorStringVendor[] = { |
90 |
PROGMEM int usbDescriptorStringVendor[] = { |
91 |
USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN), |
91 |
USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN), |
92 |
USB_CFG_VENDOR_NAME |
92 |
USB_CFG_VENDOR_NAME |
93 |
}; |
93 |
}; |
94 |
#endif |
94 |
#endif |
95 |
|
95 |
|
96 |
#if USB_CFG_DESCR_PROPS_STRING_DEVICE == 0 && USB_CFG_DEVICE_NAME_LEN |
96 |
#if USB_CFG_DESCR_PROPS_STRING_DEVICE == 0 && USB_CFG_DEVICE_NAME_LEN |
97 |
#undef USB_CFG_DESCR_PROPS_STRING_DEVICE |
97 |
#undef USB_CFG_DESCR_PROPS_STRING_DEVICE |
98 |
#define USB_CFG_DESCR_PROPS_STRING_DEVICE sizeof(usbDescriptorStringDevice) |
98 |
#define USB_CFG_DESCR_PROPS_STRING_DEVICE sizeof(usbDescriptorStringDevice) |
99 |
PROGMEM int usbDescriptorStringDevice[] = { |
99 |
PROGMEM int usbDescriptorStringDevice[] = { |
100 |
USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN), |
100 |
USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN), |
101 |
USB_CFG_DEVICE_NAME |
101 |
USB_CFG_DEVICE_NAME |
102 |
}; |
102 |
}; |
103 |
#endif |
103 |
#endif |
104 |
|
104 |
|
105 |
#if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN |
105 |
#if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN |
106 |
#undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER |
106 |
#undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER |
107 |
#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber) |
107 |
#define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber) |
108 |
PROGMEM int usbDescriptorStringSerialNumber[] = { |
108 |
PROGMEM int usbDescriptorStringSerialNumber[] = { |
109 |
USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN), |
109 |
USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN), |
110 |
USB_CFG_SERIAL_NUMBER |
110 |
USB_CFG_SERIAL_NUMBER |
111 |
}; |
111 |
}; |
112 |
#endif |
112 |
#endif |
113 |
|
113 |
|
114 |
#endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */ |
114 |
#endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */ |
115 |
|
115 |
|
116 |
#if USB_CFG_DESCR_PROPS_DEVICE == 0 |
116 |
#if USB_CFG_DESCR_PROPS_DEVICE == 0 |
117 |
#undef USB_CFG_DESCR_PROPS_DEVICE |
117 |
#undef USB_CFG_DESCR_PROPS_DEVICE |
118 |
#define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice) |
118 |
#define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice) |
119 |
PROGMEM char usbDescriptorDevice[] = { /* USB device descriptor */ |
119 |
PROGMEM char usbDescriptorDevice[] = { /* USB device descriptor */ |
120 |
18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */ |
120 |
18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */ |
121 |
USBDESCR_DEVICE, /* descriptor type */ |
121 |
USBDESCR_DEVICE, /* descriptor type */ |
122 |
0x10, 0x01, /* USB version supported */ |
122 |
0x10, 0x01, /* USB version supported */ |
123 |
USB_CFG_DEVICE_CLASS, |
123 |
USB_CFG_DEVICE_CLASS, |
124 |
USB_CFG_DEVICE_SUBCLASS, |
124 |
USB_CFG_DEVICE_SUBCLASS, |
125 |
0, /* protocol */ |
125 |
0, /* protocol */ |
126 |
8, /* max packet size */ |
126 |
8, /* max packet size */ |
127 |
USB_CFG_VENDOR_ID, /* 2 bytes */ |
127 |
USB_CFG_VENDOR_ID, /* 2 bytes */ |
128 |
USB_CFG_DEVICE_ID, /* 2 bytes */ |
128 |
USB_CFG_DEVICE_ID, /* 2 bytes */ |
129 |
USB_CFG_DEVICE_VERSION, /* 2 bytes */ |
129 |
USB_CFG_DEVICE_VERSION, /* 2 bytes */ |
130 |
USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0, /* manufacturer string index */ |
130 |
USB_CFG_DESCR_PROPS_STRING_VENDOR != 0 ? 1 : 0, /* manufacturer string index */ |
131 |
USB_CFG_DESCR_PROPS_STRING_DEVICE != 0 ? 2 : 0, /* product string index */ |
131 |
USB_CFG_DESCR_PROPS_STRING_DEVICE != 0 ? 2 : 0, /* product string index */ |
132 |
USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0, /* serial number string index */ |
132 |
USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER != 0 ? 3 : 0, /* serial number string index */ |
133 |
1, /* number of configurations */ |
133 |
1, /* number of configurations */ |
134 |
}; |
134 |
}; |
135 |
#endif |
135 |
#endif |
136 |
|
136 |
|
137 |
#if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0 |
137 |
#if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0 |
138 |
#undef USB_CFG_DESCR_PROPS_HID |
138 |
#undef USB_CFG_DESCR_PROPS_HID |
139 |
#define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */ |
139 |
#define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */ |
140 |
#endif |
140 |
#endif |
141 |
|
141 |
|
142 |
#if USB_CFG_DESCR_PROPS_CONFIGURATION == 0 |
142 |
#if USB_CFG_DESCR_PROPS_CONFIGURATION == 0 |
143 |
#undef USB_CFG_DESCR_PROPS_CONFIGURATION |
143 |
#undef USB_CFG_DESCR_PROPS_CONFIGURATION |
144 |
#define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration) |
144 |
#define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration) |
145 |
PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */ |
145 |
PROGMEM char usbDescriptorConfiguration[] = { /* USB configuration descriptor */ |
146 |
9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */ |
146 |
9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */ |
147 |
USBDESCR_CONFIG, /* descriptor type */ |
147 |
USBDESCR_CONFIG, /* descriptor type */ |
148 |
18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + (USB_CFG_DESCR_PROPS_HID & 0xff), 0, |
148 |
18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT + (USB_CFG_DESCR_PROPS_HID & 0xff), 0, |
149 |
/* total length of data returned (including inlined descriptors) */ |
149 |
/* total length of data returned (including inlined descriptors) */ |
150 |
1, /* number of interfaces in this configuration */ |
150 |
1, /* number of interfaces in this configuration */ |
151 |
1, /* index of this configuration */ |
151 |
1, /* index of this configuration */ |
152 |
0, /* configuration name string index */ |
152 |
0, /* configuration name string index */ |
153 |
#if USB_CFG_IS_SELF_POWERED |
153 |
#if USB_CFG_IS_SELF_POWERED |
154 |
USBATTR_SELFPOWER, /* attributes */ |
154 |
USBATTR_SELFPOWER, /* attributes */ |
155 |
#else |
155 |
#else |
156 |
USBATTR_BUSPOWER, /* attributes */ |
156 |
USBATTR_BUSPOWER, /* attributes */ |
157 |
#endif |
157 |
#endif |
158 |
USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */ |
158 |
USB_CFG_MAX_BUS_POWER/2, /* max USB current in 2mA units */ |
159 |
/* interface descriptor follows inline: */ |
159 |
/* interface descriptor follows inline: */ |
160 |
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ |
160 |
9, /* sizeof(usbDescrInterface): length of descriptor in bytes */ |
161 |
USBDESCR_INTERFACE, /* descriptor type */ |
161 |
USBDESCR_INTERFACE, /* descriptor type */ |
162 |
0, /* index of this interface */ |
162 |
0, /* index of this interface */ |
163 |
0, /* alternate setting for this interface */ |
163 |
0, /* alternate setting for this interface */ |
164 |
USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */ |
164 |
USB_CFG_HAVE_INTRIN_ENDPOINT, /* endpoints excl 0: number of endpoint descriptors to follow */ |
165 |
USB_CFG_INTERFACE_CLASS, |
165 |
USB_CFG_INTERFACE_CLASS, |
166 |
USB_CFG_INTERFACE_SUBCLASS, |
166 |
USB_CFG_INTERFACE_SUBCLASS, |
167 |
USB_CFG_INTERFACE_PROTOCOL, |
167 |
USB_CFG_INTERFACE_PROTOCOL, |
168 |
0, /* string index for interface */ |
168 |
0, /* string index for interface */ |
169 |
#if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */ |
169 |
#if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */ |
170 |
9, /* sizeof(usbDescrHID): length of descriptor in bytes */ |
170 |
9, /* sizeof(usbDescrHID): length of descriptor in bytes */ |
171 |
USBDESCR_HID, /* descriptor type: HID */ |
171 |
USBDESCR_HID, /* descriptor type: HID */ |
172 |
0x01, 0x01, /* BCD representation of HID version */ |
172 |
0x01, 0x01, /* BCD representation of HID version */ |
173 |
0x00, /* target country code */ |
173 |
0x00, /* target country code */ |
174 |
0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ |
174 |
0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */ |
175 |
0x22, /* descriptor type: report */ |
175 |
0x22, /* descriptor type: report */ |
176 |
USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */ |
176 |
USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH, 0, /* total length of report descriptor */ |
177 |
#endif |
177 |
#endif |
178 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */ |
178 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */ |
179 |
7, /* sizeof(usbDescrEndpoint) */ |
179 |
7, /* sizeof(usbDescrEndpoint) */ |
180 |
USBDESCR_ENDPOINT, /* descriptor type = endpoint */ |
180 |
USBDESCR_ENDPOINT, /* descriptor type = endpoint */ |
181 |
0x81, /* IN endpoint number 1 */ |
181 |
0x81, /* IN endpoint number 1 */ |
182 |
0x03, /* attrib: Interrupt endpoint */ |
182 |
0x03, /* attrib: Interrupt endpoint */ |
183 |
8, 0, /* maximum packet size */ |
183 |
8, 0, /* maximum packet size */ |
184 |
USB_CFG_INTR_POLL_INTERVAL, /* in ms */ |
184 |
USB_CFG_INTR_POLL_INTERVAL, /* in ms */ |
185 |
#endif |
185 |
#endif |
186 |
}; |
186 |
}; |
187 |
#endif |
187 |
#endif |
188 |
|
188 |
|
189 |
/* We don't use prog_int or prog_int16_t for compatibility with various libc |
189 |
/* We don't use prog_int or prog_int16_t for compatibility with various libc |
190 |
* versions. Here's an other compatibility hack: |
190 |
* versions. Here's an other compatibility hack: |
191 |
*/ |
191 |
*/ |
192 |
#ifndef PRG_RDB |
192 |
#ifndef PRG_RDB |
193 |
#define PRG_RDB(addr) pgm_read_byte(addr) |
193 |
#define PRG_RDB(addr) pgm_read_byte(addr) |
194 |
#endif |
194 |
#endif |
195 |
|
195 |
|
196 |
typedef union{ |
196 |
typedef union{ |
197 |
unsigned word; |
197 |
unsigned word; |
198 |
uchar *ptr; |
198 |
uchar *ptr; |
199 |
uchar bytes[2]; |
199 |
uchar bytes[2]; |
200 |
}converter_t; |
200 |
}converter_t; |
201 |
/* We use this union to do type conversions. This is better optimized than |
201 |
/* We use this union to do type conversions. This is better optimized than |
202 |
* type casts in gcc 3.4.3 and much better than using bit shifts to build |
202 |
* type casts in gcc 3.4.3 and much better than using bit shifts to build |
203 |
* ints from chars. Byte ordering is not a problem on an 8 bit platform. |
203 |
* ints from chars. Byte ordering is not a problem on an 8 bit platform. |
204 |
*/ |
204 |
*/ |
205 |
|
205 |
|
206 |
/* ------------------------------------------------------------------------- */ |
206 |
/* ------------------------------------------------------------------------- */ |
207 |
|
207 |
|
208 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
208 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
209 |
void usbSetInterrupt(uchar *data, uchar len) |
209 |
void usbSetInterrupt(uchar *data, uchar len) |
210 |
{ |
210 |
{ |
211 |
uchar *p, i; |
211 |
uchar *p, i; |
212 |
|
212 |
|
213 |
#if USB_CFG_IMPLEMENT_HALT |
213 |
#if USB_CFG_IMPLEMENT_HALT |
214 |
if(usbTxLen1 == USBPID_STALL) |
214 |
if(usbTxLen1 == USBPID_STALL) |
215 |
return; |
215 |
return; |
216 |
#endif |
216 |
#endif |
217 |
#if 0 /* No runtime checks! Caller is responsible for valid data! */ |
217 |
#if 0 /* No runtime checks! Caller is responsible for valid data! */ |
218 |
if(len > 8) /* interrupt transfers are limited to 8 bytes */ |
218 |
if(len > 8) /* interrupt transfers are limited to 8 bytes */ |
219 |
len = 8; |
219 |
len = 8; |
220 |
#endif |
220 |
#endif |
221 |
if(usbTxLen1 & 0x10){ /* packet buffer was empty */ |
221 |
if(usbTxLen1 & 0x10){ /* packet buffer was empty */ |
222 |
usbTxBuf1[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */ |
222 |
usbTxBuf1[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */ |
223 |
}else{ |
223 |
}else{ |
224 |
usbTxLen1 = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */ |
224 |
usbTxLen1 = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */ |
225 |
} |
225 |
} |
226 |
p = usbTxBuf1 + 1; |
226 |
p = usbTxBuf1 + 1; |
227 |
for(i=len;i--;) |
227 |
for(i=len;i--;) |
228 |
*p++ = *data++; |
228 |
*p++ = *data++; |
229 |
usbCrc16Append(&usbTxBuf1[1], len); |
229 |
usbCrc16Append(&usbTxBuf1[1], len); |
230 |
usbTxLen1 = len + 4; /* len must be given including sync byte */ |
230 |
usbTxLen1 = len + 4; /* len must be given including sync byte */ |
231 |
DBG2(0x21, usbTxBuf1, len + 3); |
231 |
DBG2(0x21, usbTxBuf1, len + 3); |
232 |
} |
232 |
} |
233 |
#endif |
233 |
#endif |
234 |
|
234 |
|
235 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
235 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
236 |
void usbSetInterrupt3(uchar *data, uchar len) |
236 |
void usbSetInterrupt3(uchar *data, uchar len) |
237 |
{ |
237 |
{ |
238 |
uchar *p, i; |
238 |
uchar *p, i; |
239 |
|
239 |
|
240 |
if(usbTxLen3 & 0x10){ /* packet buffer was empty */ |
240 |
if(usbTxLen3 & 0x10){ /* packet buffer was empty */ |
241 |
usbTxBuf3[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */ |
241 |
usbTxBuf3[0] ^= USBPID_DATA0 ^ USBPID_DATA1; /* toggle token */ |
242 |
}else{ |
242 |
}else{ |
243 |
usbTxLen3 = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */ |
243 |
usbTxLen3 = USBPID_NAK; /* avoid sending outdated (overwritten) interrupt data */ |
244 |
} |
244 |
} |
245 |
p = usbTxBuf3 + 1; |
245 |
p = usbTxBuf3 + 1; |
246 |
for(i=len;i--;) |
246 |
for(i=len;i--;) |
247 |
*p++ = *data++; |
247 |
*p++ = *data++; |
248 |
usbCrc16Append(&usbTxBuf3[1], len); |
248 |
usbCrc16Append(&usbTxBuf3[1], len); |
249 |
usbTxLen3 = len + 4; /* len must be given including sync byte */ |
249 |
usbTxLen3 = len + 4; /* len must be given including sync byte */ |
250 |
DBG2(0x23, usbTxBuf3, len + 3); |
250 |
DBG2(0x23, usbTxBuf3, len + 3); |
251 |
} |
251 |
} |
252 |
#endif |
252 |
#endif |
253 |
|
253 |
|
254 |
|
254 |
|
255 |
static uchar usbRead(uchar *data, uchar len) |
255 |
static uchar usbRead(uchar *data, uchar len) |
256 |
{ |
256 |
{ |
257 |
#if USB_CFG_IMPLEMENT_FN_READ |
257 |
#if USB_CFG_IMPLEMENT_FN_READ |
258 |
if(usbMsgFlags & USB_FLG_USE_DEFAULT_RW){ |
258 |
if(usbMsgFlags & USB_FLG_USE_DEFAULT_RW){ |
259 |
#endif |
259 |
#endif |
260 |
uchar i = len, *r = usbMsgPtr; |
260 |
uchar i = len, *r = usbMsgPtr; |
261 |
if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */ |
261 |
if(usbMsgFlags & USB_FLG_MSGPTR_IS_ROM){ /* ROM data */ |
262 |
while(i--){ |
262 |
while(i--){ |
263 |
uchar c = PRG_RDB(r); /* assign to char size variable to enforce byte ops */ |
263 |
uchar c = PRG_RDB(r); /* assign to char size variable to enforce byte ops */ |
264 |
*data++ = c; |
264 |
*data++ = c; |
265 |
r++; |
265 |
r++; |
266 |
} |
266 |
} |
267 |
}else{ /* RAM data */ |
267 |
}else{ /* RAM data */ |
268 |
while(i--) |
268 |
while(i--) |
269 |
*data++ = *r++; |
269 |
*data++ = *r++; |
270 |
} |
270 |
} |
271 |
usbMsgPtr = r; |
271 |
usbMsgPtr = r; |
272 |
return len; |
272 |
return len; |
273 |
#if USB_CFG_IMPLEMENT_FN_READ |
273 |
#if USB_CFG_IMPLEMENT_FN_READ |
274 |
}else{ |
274 |
}else{ |
275 |
if(len != 0) /* don't bother app with 0 sized reads */ |
275 |
if(len != 0) /* don't bother app with 0 sized reads */ |
276 |
return usbFunctionRead(data, len); |
276 |
return usbFunctionRead(data, len); |
277 |
return 0; |
277 |
return 0; |
278 |
} |
278 |
} |
279 |
#endif |
279 |
#endif |
280 |
} |
280 |
} |
281 |
|
281 |
|
282 |
|
282 |
|
283 |
#define GET_DESCRIPTOR(cfgProp, staticName) \ |
283 |
#define GET_DESCRIPTOR(cfgProp, staticName) \ |
284 |
if(cfgProp){ \ |
284 |
if(cfgProp){ \ |
285 |
if((cfgProp) & USB_PROP_IS_RAM) \ |
285 |
if((cfgProp) & USB_PROP_IS_RAM) \ |
286 |
flags &= ~USB_FLG_MSGPTR_IS_ROM; \ |
286 |
flags &= ~USB_FLG_MSGPTR_IS_ROM; \ |
287 |
if((cfgProp) & USB_PROP_IS_DYNAMIC){ \ |
287 |
if((cfgProp) & USB_PROP_IS_DYNAMIC){ \ |
288 |
replyLen = usbFunctionDescriptor(rq); \ |
288 |
replyLen = usbFunctionDescriptor(rq); \ |
289 |
}else{ \ |
289 |
}else{ \ |
290 |
replyData = (uchar *)(staticName); \ |
290 |
replyData = (uchar *)(staticName); \ |
291 |
SET_REPLY_LEN((cfgProp) & 0xff); \ |
291 |
SET_REPLY_LEN((cfgProp) & 0xff); \ |
292 |
} \ |
292 |
} \ |
293 |
} |
293 |
} |
294 |
/* We use if() instead of #if in the macro above because #if can't be used |
294 |
/* We use if() instead of #if in the macro above because #if can't be used |
295 |
* in macros and the compiler optimizes constant conditions anyway. |
295 |
* in macros and the compiler optimizes constant conditions anyway. |
296 |
*/ |
296 |
*/ |
297 |
|
297 |
|
298 |
|
298 |
|
299 |
/* Don't make this function static to avoid inlining. |
299 |
/* Don't make this function static to avoid inlining. |
300 |
* The entire function would become too large and exceed the range of |
300 |
* The entire function would become too large and exceed the range of |
301 |
* relative jumps. |
301 |
* relative jumps. |
302 |
* 2006-02-25: Either gcc 3.4.3 is better than the gcc used when the comment |
302 |
* 2006-02-25: Either gcc 3.4.3 is better than the gcc used when the comment |
303 |
* above was written, or other parts of the code have changed. We now get |
303 |
* above was written, or other parts of the code have changed. We now get |
304 |
* better results with an inlined function. Test condition: PowerSwitch code. |
304 |
* better results with an inlined function. Test condition: PowerSwitch code. |
305 |
*/ |
305 |
*/ |
306 |
static void usbProcessRx(uchar *data, uchar len) |
306 |
static void usbProcessRx(uchar *data, uchar len) |
307 |
{ |
307 |
{ |
308 |
usbRequest_t *rq = (void *)data; |
308 |
usbRequest_t *rq = (void *)data; |
309 |
uchar replyLen = 0, flags = USB_FLG_USE_DEFAULT_RW; |
309 |
uchar replyLen = 0, flags = USB_FLG_USE_DEFAULT_RW; |
310 |
/* We use if() cascades because the compare is done byte-wise while switch() |
310 |
/* We use if() cascades because the compare is done byte-wise while switch() |
311 |
* is int-based. The if() cascades are therefore more efficient. |
311 |
* is int-based. The if() cascades are therefore more efficient. |
312 |
*/ |
312 |
*/ |
313 |
/* usbRxToken can be: |
313 |
/* usbRxToken can be: |
314 |
* 0x2d 00101101 (USBPID_SETUP for endpoint 0) |
314 |
* 0x2d 00101101 (USBPID_SETUP for endpoint 0) |
315 |
* 0xe1 11100001 (USBPID_OUT for endpoint 0) |
315 |
* 0xe1 11100001 (USBPID_OUT for endpoint 0) |
316 |
* 0xff 11111111 (USBPID_OUT for endpoint 1) |
316 |
* 0xff 11111111 (USBPID_OUT for endpoint 1) |
317 |
*/ |
317 |
*/ |
318 |
DBG2(0x10 + ((usbRxToken >> 1) & 3), data, len); /* SETUP0=12; OUT0=10; OUT1=13 */ |
318 |
DBG2(0x10 + ((usbRxToken >> 1) & 3), data, len); /* SETUP0=12; OUT0=10; OUT1=13 */ |
319 |
#if USB_CFG_IMPLEMENT_FN_WRITEOUT |
319 |
#if USB_CFG_IMPLEMENT_FN_WRITEOUT |
320 |
if(usbRxToken == 0xff){ |
320 |
if(usbRxToken == 0xff){ |
321 |
usbFunctionWriteOut(data, len); |
321 |
usbFunctionWriteOut(data, len); |
322 |
return; /* no reply expected, hence no usbMsgPtr, usbMsgFlags, usbMsgLen set */ |
322 |
return; /* no reply expected, hence no usbMsgPtr, usbMsgFlags, usbMsgLen set */ |
323 |
} |
323 |
} |
324 |
#endif |
324 |
#endif |
325 |
if(usbRxToken == (uchar)USBPID_SETUP){ |
325 |
if(usbRxToken == (uchar)USBPID_SETUP){ |
326 |
usbTxLen = USBPID_NAK; /* abort pending transmit */ |
326 |
usbTxLen = USBPID_NAK; /* abort pending transmit */ |
327 |
if(len == 8){ /* Setup size must be always 8 bytes. Ignore otherwise. */ |
327 |
if(len == 8){ /* Setup size must be always 8 bytes. Ignore otherwise. */ |
328 |
uchar type = rq->bmRequestType & USBRQ_TYPE_MASK; |
328 |
uchar type = rq->bmRequestType & USBRQ_TYPE_MASK; |
329 |
if(type == USBRQ_TYPE_STANDARD){ |
329 |
if(type == USBRQ_TYPE_STANDARD){ |
330 |
#define SET_REPLY_LEN(len) replyLen = (len); usbMsgPtr = replyData |
330 |
#define SET_REPLY_LEN(len) replyLen = (len); usbMsgPtr = replyData |
331 |
/* This macro ensures that replyLen and usbMsgPtr are always set in the same way. |
331 |
/* This macro ensures that replyLen and usbMsgPtr are always set in the same way. |
332 |
* That allows optimization of common code in if() branches */ |
332 |
* That allows optimization of common code in if() branches */ |
333 |
uchar *replyData = usbTxBuf + 9; /* there is 3 bytes free space at the end of the buffer */ |
333 |
uchar *replyData = usbTxBuf + 9; /* there is 3 bytes free space at the end of the buffer */ |
334 |
replyData[0] = 0; /* common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */ |
334 |
replyData[0] = 0; /* common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */ |
335 |
if(rq->bRequest == USBRQ_GET_STATUS){ /* 0 */ |
335 |
if(rq->bRequest == USBRQ_GET_STATUS){ /* 0 */ |
336 |
uchar __attribute__((__unused__)) recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */ |
336 |
uchar __attribute__((__unused__)) recipient = rq->bmRequestType & USBRQ_RCPT_MASK; /* assign arith ops to variables to enforce byte size */ |
337 |
#if USB_CFG_IS_SELF_POWERED |
337 |
#if USB_CFG_IS_SELF_POWERED |
338 |
if(recipient == USBRQ_RCPT_DEVICE) |
338 |
if(recipient == USBRQ_RCPT_DEVICE) |
339 |
replyData[0] = USB_CFG_IS_SELF_POWERED; |
339 |
replyData[0] = USB_CFG_IS_SELF_POWERED; |
340 |
#endif |
340 |
#endif |
341 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT && USB_CFG_IMPLEMENT_HALT |
341 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT && USB_CFG_IMPLEMENT_HALT |
342 |
if(recipient == USBRQ_RCPT_ENDPOINT && rq->wIndex.bytes[0] == 0x81) /* request status for endpoint 1 */ |
342 |
if(recipient == USBRQ_RCPT_ENDPOINT && rq->wIndex.bytes[0] == 0x81) /* request status for endpoint 1 */ |
343 |
replyData[0] = usbTxLen1 == USBPID_STALL; |
343 |
replyData[0] = usbTxLen1 == USBPID_STALL; |
344 |
#endif |
344 |
#endif |
345 |
replyData[1] = 0; |
345 |
replyData[1] = 0; |
346 |
SET_REPLY_LEN(2); |
346 |
SET_REPLY_LEN(2); |
347 |
}else if(rq->bRequest == USBRQ_SET_ADDRESS){ /* 5 */ |
347 |
}else if(rq->bRequest == USBRQ_SET_ADDRESS){ /* 5 */ |
348 |
usbNewDeviceAddr = rq->wValue.bytes[0]; |
348 |
usbNewDeviceAddr = rq->wValue.bytes[0]; |
349 |
}else if(rq->bRequest == USBRQ_GET_DESCRIPTOR){ /* 6 */ |
349 |
}else if(rq->bRequest == USBRQ_GET_DESCRIPTOR){ /* 6 */ |
350 |
flags = USB_FLG_MSGPTR_IS_ROM | USB_FLG_USE_DEFAULT_RW; |
350 |
flags = USB_FLG_MSGPTR_IS_ROM | USB_FLG_USE_DEFAULT_RW; |
351 |
if(rq->wValue.bytes[1] == USBDESCR_DEVICE){ /* 1 */ |
351 |
if(rq->wValue.bytes[1] == USBDESCR_DEVICE){ /* 1 */ |
352 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice) |
352 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE, usbDescriptorDevice) |
353 |
}else if(rq->wValue.bytes[1] == USBDESCR_CONFIG){ /* 2 */ |
353 |
}else if(rq->wValue.bytes[1] == USBDESCR_CONFIG){ /* 2 */ |
354 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration) |
354 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION, usbDescriptorConfiguration) |
355 |
}else if(rq->wValue.bytes[1] == USBDESCR_STRING){ /* 3 */ |
355 |
}else if(rq->wValue.bytes[1] == USBDESCR_STRING){ /* 3 */ |
356 |
#if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC |
356 |
#if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC |
357 |
if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM) |
357 |
if(USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_RAM) |
358 |
flags &= ~USB_FLG_MSGPTR_IS_ROM; |
358 |
flags &= ~USB_FLG_MSGPTR_IS_ROM; |
359 |
replyLen = usbFunctionDescriptor(rq); |
359 |
replyLen = usbFunctionDescriptor(rq); |
360 |
#else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ |
360 |
#else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ |
361 |
if(rq->wValue.bytes[0] == 0){ /* descriptor index */ |
361 |
if(rq->wValue.bytes[0] == 0){ /* descriptor index */ |
362 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0) |
362 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0, usbDescriptorString0) |
363 |
}else if(rq->wValue.bytes[0] == 1){ |
363 |
}else if(rq->wValue.bytes[0] == 1){ |
364 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor) |
364 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR, usbDescriptorStringVendor) |
365 |
}else if(rq->wValue.bytes[0] == 2){ |
365 |
}else if(rq->wValue.bytes[0] == 2){ |
366 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_DEVICE, usbDescriptorStringDevice) |
366 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_DEVICE, usbDescriptorStringDevice) |
367 |
}else if(rq->wValue.bytes[0] == 3){ |
367 |
}else if(rq->wValue.bytes[0] == 3){ |
368 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber) |
368 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER, usbDescriptorStringSerialNumber) |
369 |
}else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ |
369 |
}else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ |
370 |
replyLen = usbFunctionDescriptor(rq); |
370 |
replyLen = usbFunctionDescriptor(rq); |
371 |
} |
371 |
} |
372 |
#endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ |
372 |
#endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */ |
373 |
}else if(rq->wValue.bytes[1] == USBDESCR_HID){ /* 0x21 */ |
373 |
}else if(rq->wValue.bytes[1] == USBDESCR_HID){ /* 0x21 */ |
374 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18) |
374 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID, usbDescriptorConfiguration + 18) |
375 |
}else if(rq->wValue.bytes[1] == USBDESCR_HID_REPORT){ /* 0x22 */ |
375 |
}else if(rq->wValue.bytes[1] == USBDESCR_HID_REPORT){ /* 0x22 */ |
376 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport) |
376 |
GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT, usbDescriptorHidReport) |
377 |
}else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ |
377 |
}else if(USB_CFG_DESCR_PROPS_UNKNOWN & USB_PROP_IS_DYNAMIC){ |
378 |
replyLen = usbFunctionDescriptor(rq); |
378 |
replyLen = usbFunctionDescriptor(rq); |
379 |
} |
379 |
} |
380 |
}else if(rq->bRequest == USBRQ_GET_CONFIGURATION){ /* 8 */ |
380 |
}else if(rq->bRequest == USBRQ_GET_CONFIGURATION){ /* 8 */ |
381 |
replyData = &usbConfiguration; /* send current configuration value */ |
381 |
replyData = &usbConfiguration; /* send current configuration value */ |
382 |
SET_REPLY_LEN(1); |
382 |
SET_REPLY_LEN(1); |
383 |
}else if(rq->bRequest == USBRQ_SET_CONFIGURATION){ /* 9 */ |
383 |
}else if(rq->bRequest == USBRQ_SET_CONFIGURATION){ /* 9 */ |
384 |
usbConfiguration = rq->wValue.bytes[0]; |
384 |
usbConfiguration = rq->wValue.bytes[0]; |
385 |
#if USB_CFG_IMPLEMENT_HALT |
385 |
#if USB_CFG_IMPLEMENT_HALT |
386 |
usbTxLen1 = USBPID_NAK; |
386 |
usbTxLen1 = USBPID_NAK; |
387 |
#endif |
387 |
#endif |
388 |
}else if(rq->bRequest == USBRQ_GET_INTERFACE){ /* 10 */ |
388 |
}else if(rq->bRequest == USBRQ_GET_INTERFACE){ /* 10 */ |
389 |
SET_REPLY_LEN(1); |
389 |
SET_REPLY_LEN(1); |
390 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
390 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
391 |
}else if(rq->bRequest == USBRQ_SET_INTERFACE){ /* 11 */ |
391 |
}else if(rq->bRequest == USBRQ_SET_INTERFACE){ /* 11 */ |
392 |
USB_SET_DATATOKEN1(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
392 |
USB_SET_DATATOKEN1(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
393 |
# if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
393 |
# if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
394 |
USB_SET_DATATOKEN3(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
394 |
USB_SET_DATATOKEN3(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
395 |
# endif |
395 |
# endif |
396 |
# if USB_CFG_IMPLEMENT_HALT |
396 |
# if USB_CFG_IMPLEMENT_HALT |
397 |
usbTxLen1 = USBPID_NAK; |
397 |
usbTxLen1 = USBPID_NAK; |
398 |
}else if(rq->bRequest == USBRQ_CLEAR_FEATURE || rq->bRequest == USBRQ_SET_FEATURE){ /* 1|3 */ |
398 |
}else if(rq->bRequest == USBRQ_CLEAR_FEATURE || rq->bRequest == USBRQ_SET_FEATURE){ /* 1|3 */ |
399 |
if(rq->wValue.bytes[0] == 0 && rq->wIndex.bytes[0] == 0x81){ /* feature 0 == HALT for endpoint == 1 */ |
399 |
if(rq->wValue.bytes[0] == 0 && rq->wIndex.bytes[0] == 0x81){ /* feature 0 == HALT for endpoint == 1 */ |
400 |
usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL; |
400 |
usbTxLen1 = rq->bRequest == USBRQ_CLEAR_FEATURE ? USBPID_NAK : USBPID_STALL; |
401 |
USB_SET_DATATOKEN1(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
401 |
USB_SET_DATATOKEN1(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
402 |
# if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
402 |
# if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
403 |
USB_SET_DATATOKEN3(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
403 |
USB_SET_DATATOKEN3(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
404 |
# endif |
404 |
# endif |
405 |
} |
405 |
} |
406 |
# endif |
406 |
# endif |
407 |
#endif |
407 |
#endif |
408 |
}else{ |
408 |
}else{ |
409 |
/* the following requests can be ignored, send default reply */ |
409 |
/* the following requests can be ignored, send default reply */ |
410 |
/* 1: CLEAR_FEATURE, 3: SET_FEATURE, 7: SET_DESCRIPTOR */ |
410 |
/* 1: CLEAR_FEATURE, 3: SET_FEATURE, 7: SET_DESCRIPTOR */ |
411 |
/* 12: SYNCH_FRAME */ |
411 |
/* 12: SYNCH_FRAME */ |
412 |
} |
412 |
} |
413 |
#undef SET_REPLY_LEN |
413 |
#undef SET_REPLY_LEN |
414 |
}else{ /* not a standard request -- must be vendor or class request */ |
414 |
}else{ /* not a standard request -- must be vendor or class request */ |
415 |
replyLen = usbFunctionSetup(data); |
415 |
replyLen = usbFunctionSetup(data); |
416 |
} |
416 |
} |
417 |
#if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE |
417 |
#if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE |
418 |
if(replyLen == 0xff){ /* use user-supplied read/write function */ |
418 |
if(replyLen == 0xff){ /* use user-supplied read/write function */ |
419 |
if((rq->bmRequestType & USBRQ_DIR_MASK) == USBRQ_DIR_DEVICE_TO_HOST){ |
419 |
if((rq->bmRequestType & USBRQ_DIR_MASK) == USBRQ_DIR_DEVICE_TO_HOST){ |
420 |
replyLen = rq->wLength.bytes[0]; /* IN transfers only */ |
420 |
replyLen = rq->wLength.bytes[0]; /* IN transfers only */ |
421 |
} |
421 |
} |
422 |
flags &= ~USB_FLG_USE_DEFAULT_RW; /* we have no valid msg, use user supplied read/write functions */ |
422 |
flags &= ~USB_FLG_USE_DEFAULT_RW; /* we have no valid msg, use user supplied read/write functions */ |
423 |
}else /* The 'else' prevents that we limit a replyLen of 0xff to the maximum transfer len. */ |
423 |
}else /* The 'else' prevents that we limit a replyLen of 0xff to the maximum transfer len. */ |
424 |
#endif |
424 |
#endif |
425 |
if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */ |
425 |
if(!rq->wLength.bytes[1] && replyLen > rq->wLength.bytes[0]) /* limit length to max */ |
426 |
replyLen = rq->wLength.bytes[0]; |
426 |
replyLen = rq->wLength.bytes[0]; |
427 |
} |
427 |
} |
428 |
/* make sure that data packets which are sent as ACK to an OUT transfer are always zero sized */ |
428 |
/* make sure that data packets which are sent as ACK to an OUT transfer are always zero sized */ |
429 |
}else{ /* DATA packet from out request */ |
429 |
}else{ /* DATA packet from out request */ |
430 |
#if USB_CFG_IMPLEMENT_FN_WRITE |
430 |
#if USB_CFG_IMPLEMENT_FN_WRITE |
431 |
if(!(usbMsgFlags & USB_FLG_USE_DEFAULT_RW)){ |
431 |
if(!(usbMsgFlags & USB_FLG_USE_DEFAULT_RW)){ |
432 |
uchar rval = usbFunctionWrite(data, len); |
432 |
uchar rval = usbFunctionWrite(data, len); |
433 |
replyLen = 0xff; |
433 |
replyLen = 0xff; |
434 |
if(rval == 0xff){ /* an error occurred */ |
434 |
if(rval == 0xff){ /* an error occurred */ |
435 |
usbMsgLen = 0xff; /* cancel potentially pending data packet for ACK */ |
435 |
usbMsgLen = 0xff; /* cancel potentially pending data packet for ACK */ |
436 |
usbTxLen = USBPID_STALL; |
436 |
usbTxLen = USBPID_STALL; |
437 |
}else if(rval != 0){ /* This was the final package */ |
437 |
}else if(rval != 0){ /* This was the final package */ |
438 |
replyLen = 0; /* answer with a zero-sized data packet */ |
438 |
replyLen = 0; /* answer with a zero-sized data packet */ |
439 |
} |
439 |
} |
440 |
flags = 0; /* start with a DATA1 package, stay with user supplied write() function */ |
440 |
flags = 0; /* start with a DATA1 package, stay with user supplied write() function */ |
441 |
} |
441 |
} |
442 |
#endif |
442 |
#endif |
443 |
} |
443 |
} |
444 |
usbMsgFlags = flags; |
444 |
usbMsgFlags = flags; |
445 |
usbMsgLen = replyLen; |
445 |
usbMsgLen = replyLen; |
446 |
} |
446 |
} |
447 |
|
447 |
|
448 |
/* ------------------------------------------------------------------------- */ |
448 |
/* ------------------------------------------------------------------------- */ |
449 |
|
449 |
|
450 |
static void usbBuildTxBlock(void) |
450 |
static void usbBuildTxBlock(void) |
451 |
{ |
451 |
{ |
452 |
uchar wantLen, len, txLen, token; |
452 |
uchar wantLen, len, txLen, token; |
453 |
|
453 |
|
454 |
wantLen = usbMsgLen; |
454 |
wantLen = usbMsgLen; |
455 |
if(wantLen > 8) |
455 |
if(wantLen > 8) |
456 |
wantLen = 8; |
456 |
wantLen = 8; |
457 |
usbMsgLen -= wantLen; |
457 |
usbMsgLen -= wantLen; |
458 |
token = USBPID_DATA1; |
458 |
token = USBPID_DATA1; |
459 |
if(usbMsgFlags & USB_FLG_TX_PACKET) |
459 |
if(usbMsgFlags & USB_FLG_TX_PACKET) |
460 |
token = USBPID_DATA0; |
460 |
token = USBPID_DATA0; |
461 |
usbMsgFlags++; |
461 |
usbMsgFlags++; |
462 |
len = usbRead(usbTxBuf + 1, wantLen); |
462 |
len = usbRead(usbTxBuf + 1, wantLen); |
463 |
if(len <= 8){ /* valid data packet */ |
463 |
if(len <= 8){ /* valid data packet */ |
464 |
usbCrc16Append(&usbTxBuf[1], len); |
464 |
usbCrc16Append(&usbTxBuf[1], len); |
465 |
txLen = len + 4; /* length including sync byte */ |
465 |
txLen = len + 4; /* length including sync byte */ |
466 |
if(len < 8) /* a partial package identifies end of message */ |
466 |
if(len < 8) /* a partial package identifies end of message */ |
467 |
usbMsgLen = 0xff; |
467 |
usbMsgLen = 0xff; |
468 |
}else{ |
468 |
}else{ |
469 |
txLen = USBPID_STALL; /* stall the endpoint */ |
469 |
txLen = USBPID_STALL; /* stall the endpoint */ |
470 |
usbMsgLen = 0xff; |
470 |
usbMsgLen = 0xff; |
471 |
} |
471 |
} |
472 |
usbTxBuf[0] = token; |
472 |
usbTxBuf[0] = token; |
473 |
usbTxLen = txLen; |
473 |
usbTxLen = txLen; |
474 |
DBG2(0x20, usbTxBuf, txLen-1); |
474 |
DBG2(0x20, usbTxBuf, txLen-1); |
475 |
} |
475 |
} |
476 |
|
476 |
|
477 |
static inline uchar isNotSE0(void) |
477 |
static inline uchar isNotSE0(void) |
478 |
{ |
478 |
{ |
479 |
uchar rval; |
479 |
uchar rval; |
480 |
/* We want to do |
480 |
/* We want to do |
481 |
* return (USBIN & USBMASK); |
481 |
* return (USBIN & USBMASK); |
482 |
* here, but the compiler does int-expansion acrobatics. |
482 |
* here, but the compiler does int-expansion acrobatics. |
483 |
* We can avoid this by assigning to a char-sized variable. |
483 |
* We can avoid this by assigning to a char-sized variable. |
484 |
*/ |
484 |
*/ |
485 |
rval = USBIN & USBMASK; |
485 |
rval = USBIN & USBMASK; |
486 |
return rval; |
486 |
return rval; |
487 |
} |
487 |
} |
488 |
|
488 |
|
489 |
/* ------------------------------------------------------------------------- */ |
489 |
/* ------------------------------------------------------------------------- */ |
490 |
|
490 |
|
491 |
void usbPoll(void) |
491 |
void usbPoll(void) |
492 |
{ |
492 |
{ |
493 |
uchar len, i; |
493 |
uchar len, i; |
494 |
|
494 |
|
495 |
if((len = usbRxLen) > 0){ |
495 |
if((len = usbRxLen) > 0){ |
496 |
/* We could check CRC16 here -- but ACK has already been sent anyway. If you |
496 |
/* We could check CRC16 here -- but ACK has already been sent anyway. If you |
497 |
* need data integrity checks with this driver, check the CRC in your app |
497 |
* need data integrity checks with this driver, check the CRC in your app |
498 |
* code and report errors back to the host. Since the ACK was already sent, |
498 |
* code and report errors back to the host. Since the ACK was already sent, |
499 |
* retries must be handled on application level. |
499 |
* retries must be handled on application level. |
500 |
* unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3); |
500 |
* unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3); |
501 |
*/ |
501 |
*/ |
502 |
usbProcessRx(usbRxBuf + USB_BUFSIZE + 1 - usbInputBufOffset, len - 3); |
502 |
usbProcessRx(usbRxBuf + USB_BUFSIZE + 1 - usbInputBufOffset, len - 3); |
503 |
#if USB_CFG_HAVE_FLOWCONTROL |
503 |
#if USB_CFG_HAVE_FLOWCONTROL |
504 |
if(usbRxLen > 0) /* only mark as available if not inactivated */ |
504 |
if(usbRxLen > 0) /* only mark as available if not inactivated */ |
505 |
usbRxLen = 0; |
505 |
usbRxLen = 0; |
506 |
#else |
506 |
#else |
507 |
usbRxLen = 0; /* mark rx buffer as available */ |
507 |
usbRxLen = 0; /* mark rx buffer as available */ |
508 |
#endif |
508 |
#endif |
509 |
} |
509 |
} |
510 |
if(usbTxLen & 0x10){ /* transmit system idle */ |
510 |
if(usbTxLen & 0x10){ /* transmit system idle */ |
511 |
if(usbMsgLen != 0xff){ /* transmit data pending? */ |
511 |
if(usbMsgLen != 0xff){ /* transmit data pending? */ |
512 |
usbBuildTxBlock(); |
512 |
usbBuildTxBlock(); |
513 |
} |
513 |
} |
514 |
} |
514 |
} |
515 |
for(i = 10; i > 0; i--){ |
515 |
for(i = 10; i > 0; i--){ |
516 |
if(isNotSE0()) |
516 |
if(isNotSE0()) |
517 |
break; |
517 |
break; |
518 |
} |
518 |
} |
519 |
if(i == 0){ /* RESET condition, called multiple times during reset */ |
519 |
if(i == 0){ /* RESET condition, called multiple times during reset */ |
520 |
usbNewDeviceAddr = 0; |
520 |
usbNewDeviceAddr = 0; |
521 |
usbDeviceAddr = 0; |
521 |
usbDeviceAddr = 0; |
522 |
#if USB_CFG_IMPLEMENT_HALT |
522 |
#if USB_CFG_IMPLEMENT_HALT |
523 |
usbTxLen1 = USBPID_NAK; |
523 |
usbTxLen1 = USBPID_NAK; |
524 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
524 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
525 |
usbTxLen3 = USBPID_NAK; |
525 |
usbTxLen3 = USBPID_NAK; |
526 |
#endif |
526 |
#endif |
527 |
#endif |
527 |
#endif |
528 |
DBG1(0xff, 0, 0); |
528 |
DBG1(0xff, 0, 0); |
529 |
} |
529 |
} |
530 |
} |
530 |
} |
531 |
|
531 |
|
532 |
/* ------------------------------------------------------------------------- */ |
532 |
/* ------------------------------------------------------------------------- */ |
533 |
|
533 |
|
534 |
void usbInit(void) |
534 |
void usbInit(void) |
535 |
{ |
535 |
{ |
536 |
#if USB_INTR_CFG_SET != 0 |
536 |
#if USB_INTR_CFG_SET != 0 |
537 |
USB_INTR_CFG |= USB_INTR_CFG_SET; |
537 |
USB_INTR_CFG |= USB_INTR_CFG_SET; |
538 |
#endif |
538 |
#endif |
539 |
#if USB_INTR_CFG_CLR != 0 |
539 |
#if USB_INTR_CFG_CLR != 0 |
540 |
USB_INTR_CFG &= ~(USB_INTR_CFG_CLR); |
540 |
USB_INTR_CFG &= ~(USB_INTR_CFG_CLR); |
541 |
#endif |
541 |
#endif |
542 |
USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT); |
542 |
USB_INTR_ENABLE |= (1 << USB_INTR_ENABLE_BIT); |
543 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
543 |
#if USB_CFG_HAVE_INTRIN_ENDPOINT |
544 |
USB_SET_DATATOKEN1(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
544 |
USB_SET_DATATOKEN1(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
545 |
# if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
545 |
# if USB_CFG_HAVE_INTRIN_ENDPOINT3 |
546 |
USB_SET_DATATOKEN3(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
546 |
USB_SET_DATATOKEN3(USBPID_DATA0); /* reset data toggling for interrupt endpoint */ |
547 |
# endif |
547 |
# endif |
548 |
#endif |
548 |
#endif |
549 |
} |
549 |
} |
550 |
|
550 |
|
551 |
/* ------------------------------------------------------------------------- */ |
551 |
/* ------------------------------------------------------------------------- */ |