2 * Project: AVR USB driver
3 * Author: Christian Starkjohann
4 * Creation Date: 2004-12-29
6 * Copyright: (c) 2005 by OBJECTIVE DEVELOPMENT Software GmbH
7 * License: GNU GPL v2 (see License.txt) or proprietary (CommercialLicense.txt)
8 * This Revision: $Id: usbdrv.c 350 2007-06-13 11:43:16Z cs $
11 #include "iarcompat.h"
12 #ifndef __IAR_SYSTEMS_ICC__
14 # include <avr/pgmspace.h>
21 This module implements the C-part of the USB driver. See usbdrv.h for a
22 documentation of the entire driver.
26 #define IAR_SECTION(arg)
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.
33 /* ------------------------------------------------------------------------- */
35 /* raw USB registers / interface to assembler code: */
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 */
38 uchar usbDeviceAddr
; /* assigned during enumeration, defaults to 0 */
39 uchar usbNewDeviceAddr
; /* device ID which should be set after status phase */
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 */
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 */
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 */
46 uchar usbTxBuf
[USB_BUFSIZE
];/* data to transmit with next IN, free if usbTxLen contains handshake token */
47 #if USB_CFG_HAVE_INTRIN_ENDPOINT
48 volatile uchar usbTxLen1
= USBPID_NAK
; /* TX count for endpoint 1 */
49 uchar usbTxBuf1
[USB_BUFSIZE
]; /* TX data for endpoint 1 */
50 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
51 volatile uchar usbTxLen3
= USBPID_NAK
; /* TX count for endpoint 1 */
52 uchar usbTxBuf3
[USB_BUFSIZE
]; /* TX data for endpoint 1 */
56 /* USB status registers / not shared with asm code */
57 uchar
*usbMsgPtr
; /* data to transmit next -- ROM or RAM address */
58 static uchar usbMsgFlags
; /* flag values see below */
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 */
62 #define USB_FLG_MSGPTR_IS_ROM (1<<6)
63 #define USB_FLG_USE_DEFAULT_RW (1<<7)
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
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
73 /* ------------------------------------------------------------------------- */
75 #if USB_CFG_DESCR_PROPS_STRINGS == 0
77 #if USB_CFG_DESCR_PROPS_STRING_0 == 0
78 #undef USB_CFG_DESCR_PROPS_STRING_0
79 #define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0)
80 PROGMEM
char usbDescriptorString0
[] = { /* language descriptor */
81 4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */
82 3, /* descriptor type */
83 0x09, 0x04, /* language index (0x0409 = US-English) */
87 #if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN
88 #undef USB_CFG_DESCR_PROPS_STRING_VENDOR
89 #define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor)
90 PROGMEM
int usbDescriptorStringVendor
[] = {
91 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN
),
96 #if USB_CFG_DESCR_PROPS_STRING_PRODUCT == 0 && USB_CFG_DEVICE_NAME_LEN
97 #undef USB_CFG_DESCR_PROPS_STRING_PRODUCT
98 #define USB_CFG_DESCR_PROPS_STRING_PRODUCT sizeof(usbDescriptorStringDevice)
99 PROGMEM
int usbDescriptorStringDevice
[] = {
100 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_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
107 #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber)
108 PROGMEM
int usbDescriptorStringSerialNumber
[] = {
109 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN
),
110 USB_CFG_SERIAL_NUMBER
114 #endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */
116 #if USB_CFG_DESCR_PROPS_DEVICE == 0
117 #undef USB_CFG_DESCR_PROPS_DEVICE
118 #define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice)
119 PROGMEM
char usbDescriptorDevice
[] = { /* USB device descriptor */
120 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */
121 USBDESCR_DEVICE
, /* descriptor type */
122 0x10, 0x01, /* USB version supported */
123 USB_CFG_DEVICE_CLASS
,
124 USB_CFG_DEVICE_SUBCLASS
,
126 8, /* max packet size */
127 USB_CFG_VENDOR_ID
, /* 2 bytes */
128 USB_CFG_DEVICE_ID
, /* 2 bytes */
129 USB_CFG_DEVICE_VERSION
, /* 2 bytes */
130 USB_CFG_DESCR_PROPS_STRING_VENDOR
!= 0 ?
1 : 0, /* manufacturer string index */
131 USB_CFG_DESCR_PROPS_STRING_PRODUCT
!= 0 ?
2 : 0, /* product string index */
132 USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
!= 0 ?
3 : 0, /* serial number string index */
133 1, /* number of configurations */
137 #if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0
138 #undef USB_CFG_DESCR_PROPS_HID
139 #define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */
142 #if USB_CFG_DESCR_PROPS_CONFIGURATION == 0
143 #undef USB_CFG_DESCR_PROPS_CONFIGURATION
144 #define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration)
145 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
146 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
147 USBDESCR_CONFIG
, /* descriptor type */
148 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT
+ (USB_CFG_DESCR_PROPS_HID
& 0xff), 0,
149 /* total length of data returned (including inlined descriptors) */
150 1, /* number of interfaces in this configuration */
151 1, /* index of this configuration */
152 0, /* configuration name string index */
153 #if USB_CFG_IS_SELF_POWERED
154 USBATTR_SELFPOWER
, /* attributes */
156 USBATTR_BUSPOWER
, /* attributes */
158 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
159 /* interface descriptor follows inline: */
160 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
161 USBDESCR_INTERFACE
, /* descriptor type */
162 0, /* index of this interface */
163 0, /* alternate setting for this interface */
164 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
165 USB_CFG_INTERFACE_CLASS
,
166 USB_CFG_INTERFACE_SUBCLASS
,
167 USB_CFG_INTERFACE_PROTOCOL
,
168 0, /* string index for interface */
169 #if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */
170 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
171 USBDESCR_HID
, /* descriptor type: HID */
172 0x01, 0x01, /* BCD representation of HID version */
173 0x00, /* target country code */
174 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
175 0x22, /* descriptor type: report */
176 USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
, 0, /* total length of report descriptor */
178 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
179 7, /* sizeof(usbDescrEndpoint) */
180 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
181 0x81, /* IN endpoint number 1 */
182 0x03, /* attrib: Interrupt endpoint */
183 8, 0, /* maximum packet size */
184 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
189 /* We don't use prog_int or prog_int16_t for compatibility with various libc
190 * versions. Here's an other compatibility hack:
193 #define PRG_RDB(addr) pgm_read_byte(addr)
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
203 * ints from chars. Byte ordering is not a problem on an 8 bit platform.
206 /* ------------------------------------------------------------------------- */
208 #if USB_CFG_HAVE_INTRIN_ENDPOINT
209 USB_PUBLIC
void usbSetInterrupt(uchar
*data
, uchar len
)
213 #if USB_CFG_IMPLEMENT_HALT
214 if(usbTxLen1
== USBPID_STALL
)
217 #if 0 /* No runtime checks! Caller is responsible for valid data! */
218 if(len
> 8) /* interrupt transfers are limited to 8 bytes */
221 if(usbTxLen1
& 0x10){ /* packet buffer was empty */
222 usbTxBuf1
[0] ^= USBPID_DATA0
^ USBPID_DATA1
; /* toggle token */
224 usbTxLen1
= USBPID_NAK
; /* avoid sending outdated (overwritten) interrupt data */
229 usbCrc16Append(&usbTxBuf1
[1], len
);
230 usbTxLen1
= len
+ 4; /* len must be given including sync byte */
231 DBG2(0x21, usbTxBuf1
, len
+ 3);
235 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
236 USB_PUBLIC
void usbSetInterrupt3(uchar
*data
, uchar len
)
240 if(usbTxLen3
& 0x10){ /* packet buffer was empty */
241 usbTxBuf3
[0] ^= USBPID_DATA0
^ USBPID_DATA1
; /* toggle token */
243 usbTxLen3
= USBPID_NAK
; /* avoid sending outdated (overwritten) interrupt data */
248 usbCrc16Append(&usbTxBuf3
[1], len
);
249 usbTxLen3
= len
+ 4; /* len must be given including sync byte */
250 DBG2(0x23, usbTxBuf3
, len
+ 3);
255 static uchar
usbRead(uchar
*data
, uchar len
)
257 #if USB_CFG_IMPLEMENT_FN_READ
258 if(usbMsgFlags
& USB_FLG_USE_DEFAULT_RW
){
260 uchar i
= len
, *r
= usbMsgPtr
;
261 if(usbMsgFlags
& USB_FLG_MSGPTR_IS_ROM
){ /* ROM data */
263 uchar c
= PRG_RDB(r
); /* assign to char size variable to enforce byte ops */
267 }else{ /* RAM data */
273 #if USB_CFG_IMPLEMENT_FN_READ
275 if(len
!= 0) /* don't bother app with 0 sized reads */
276 return usbFunctionRead(data
, len
);
283 #define GET_DESCRIPTOR(cfgProp, staticName) \
285 if((cfgProp) & USB_PROP_IS_RAM) \
286 flags &= ~USB_FLG_MSGPTR_IS_ROM; \
287 if((cfgProp) & USB_PROP_IS_DYNAMIC){ \
288 replyLen = usbFunctionDescriptor(rq); \
290 replyData = (uchar *)(staticName); \
291 SET_REPLY_LEN((cfgProp) & 0xff); \
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.
299 /* Don't make this function static to avoid inlining.
300 * The entire function would become too large and exceed the range of
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
304 * better results with an inlined function. Test condition: PowerSwitch code.
306 static void usbProcessRx(uchar
*data
, uchar len
)
308 usbRequest_t
*rq
= (void *)data
;
309 uchar replyLen
= 0, flags
= USB_FLG_USE_DEFAULT_RW
;
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.
313 /* usbRxToken can be:
314 * 0x2d 00101101 (USBPID_SETUP for endpoint 0)
315 * 0xe1 11100001 (USBPID_OUT for endpoint 0)
316 * 0xff 11111111 (USBPID_OUT for endpoint 1)
318 DBG2(0x10 + ((usbRxToken
>> 1) & 3), data
, len
); /* SETUP0=12; OUT0=10; OUT1=13 */
319 #if USB_CFG_IMPLEMENT_FN_WRITEOUT
320 if(usbRxToken
== 0xff){
321 usbFunctionWriteOut(data
, len
);
322 return; /* no reply expected, hence no usbMsgPtr, usbMsgFlags, usbMsgLen set */
325 if(usbRxToken
== (uchar
)USBPID_SETUP
){
326 usbTxLen
= USBPID_NAK
; /* abort pending transmit */
327 if(len
== 8){ /* Setup size must be always 8 bytes. Ignore otherwise. */
328 uchar type
= rq
->bmRequestType
& USBRQ_TYPE_MASK
;
329 if(type
== USBRQ_TYPE_STANDARD
){
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.
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 */
334 replyData
[0] = 0; /* common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */
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 */
337 #if USB_CFG_IS_SELF_POWERED
338 if(recipient
== USBRQ_RCPT_DEVICE
)
339 replyData
[0] = USB_CFG_IS_SELF_POWERED
;
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 */
343 replyData
[0] = usbTxLen1
== USBPID_STALL
;
347 }else if(rq
->bRequest
== USBRQ_SET_ADDRESS
){ /* 5 */
348 usbNewDeviceAddr
= rq
->wValue
.bytes
[0];
349 }else if(rq
->bRequest
== USBRQ_GET_DESCRIPTOR
){ /* 6 */
350 flags
= USB_FLG_MSGPTR_IS_ROM
| USB_FLG_USE_DEFAULT_RW
;
351 if(rq
->wValue
.bytes
[1] == USBDESCR_DEVICE
){ /* 1 */
352 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE
, usbDescriptorDevice
)
353 }else if(rq
->wValue
.bytes
[1] == USBDESCR_CONFIG
){ /* 2 */
354 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION
, usbDescriptorConfiguration
)
355 }else if(rq
->wValue
.bytes
[1] == USBDESCR_STRING
){ /* 3 */
356 #if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC
357 if(USB_CFG_DESCR_PROPS_STRINGS
& USB_PROP_IS_RAM
)
358 flags
&= ~USB_FLG_MSGPTR_IS_ROM
;
359 replyLen
= usbFunctionDescriptor(rq
);
360 #else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
361 if(rq
->wValue
.bytes
[0] == 0){ /* descriptor index */
362 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0
, usbDescriptorString0
)
363 }else if(rq
->wValue
.bytes
[0] == 1){
364 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR
, usbDescriptorStringVendor
)
365 }else if(rq
->wValue
.bytes
[0] == 2){
366 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_PRODUCT
, usbDescriptorStringDevice
)
367 }else if(rq
->wValue
.bytes
[0] == 3){
368 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
, usbDescriptorStringSerialNumber
)
369 }else if(USB_CFG_DESCR_PROPS_UNKNOWN
& USB_PROP_IS_DYNAMIC
){
370 replyLen
= usbFunctionDescriptor(rq
);
372 #endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
373 }else if(rq
->wValue
.bytes
[1] == USBDESCR_HID
){ /* 0x21 */
374 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID
, usbDescriptorConfiguration
+ 18)
375 }else if(rq
->wValue
.bytes
[1] == USBDESCR_HID_REPORT
){ /* 0x22 */
376 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT
, usbDescriptorHidReport
)
377 }else if(USB_CFG_DESCR_PROPS_UNKNOWN
& USB_PROP_IS_DYNAMIC
){
378 replyLen
= usbFunctionDescriptor(rq
);
380 }else if(rq
->bRequest
== USBRQ_GET_CONFIGURATION
){ /* 8 */
381 replyData
= &usbConfiguration
; /* send current configuration value */
383 }else if(rq
->bRequest
== USBRQ_SET_CONFIGURATION
){ /* 9 */
384 usbConfiguration
= rq
->wValue
.bytes
[0];
385 #if USB_CFG_IMPLEMENT_HALT
386 usbTxLen1
= USBPID_NAK
;
388 }else if(rq
->bRequest
== USBRQ_GET_INTERFACE
){ /* 10 */
390 #if USB_CFG_HAVE_INTRIN_ENDPOINT
391 }else if(rq
->bRequest
== USBRQ_SET_INTERFACE
){ /* 11 */
392 USB_SET_DATATOKEN1(USBPID_DATA0
); /* reset data toggling for interrupt endpoint */
393 # if USB_CFG_HAVE_INTRIN_ENDPOINT3
394 USB_SET_DATATOKEN3(USBPID_DATA0
); /* reset data toggling for interrupt endpoint */
396 # if USB_CFG_IMPLEMENT_HALT
397 usbTxLen1
= USBPID_NAK
;
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 */
400 usbTxLen1
= rq
->bRequest
== USBRQ_CLEAR_FEATURE ? USBPID_NAK
: USBPID_STALL
;
401 USB_SET_DATATOKEN1(USBPID_DATA0
); /* reset data toggling for interrupt endpoint */
402 # if USB_CFG_HAVE_INTRIN_ENDPOINT3
403 USB_SET_DATATOKEN3(USBPID_DATA0
); /* reset data toggling for interrupt endpoint */
409 /* the following requests can be ignored, send default reply */
410 /* 1: CLEAR_FEATURE, 3: SET_FEATURE, 7: SET_DESCRIPTOR */
411 /* 12: SYNCH_FRAME */
414 }else{ /* not a standard request -- must be vendor or class request */
415 replyLen
= usbFunctionSetup(data
);
417 #if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE
418 if(replyLen
== 0xff){ /* use user-supplied read/write function */
419 if((rq
->bmRequestType
& USBRQ_DIR_MASK
) == USBRQ_DIR_DEVICE_TO_HOST
){
420 replyLen
= rq
->wLength
.bytes
[0]; /* IN transfers only */
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. */
425 if(!rq
->wLength
.bytes
[1] && replyLen
> rq
->wLength
.bytes
[0]) /* limit length to max */
426 replyLen
= rq
->wLength
.bytes
[0];
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 */
430 #if USB_CFG_IMPLEMENT_FN_WRITE
431 if(!(usbMsgFlags
& USB_FLG_USE_DEFAULT_RW
)){
432 uchar rval
= usbFunctionWrite(data
, len
);
434 if(rval
== 0xff){ /* an error occurred */
435 usbMsgLen
= 0xff; /* cancel potentially pending data packet for ACK */
436 usbTxLen
= USBPID_STALL
;
437 }else if(rval
!= 0){ /* This was the final package */
438 replyLen
= 0; /* answer with a zero-sized data packet */
440 flags
= 0; /* start with a DATA1 package, stay with user supplied write() function */
445 usbMsgLen
= replyLen
;
448 /* ------------------------------------------------------------------------- */
450 static void usbBuildTxBlock(void)
452 uchar wantLen
, len
, txLen
, token
;
457 usbMsgLen
-= wantLen
;
458 token
= USBPID_DATA1
;
459 if(usbMsgFlags
& USB_FLG_TX_PACKET
)
460 token
= USBPID_DATA0
;
462 len
= usbRead(usbTxBuf
+ 1, wantLen
);
463 if(len
<= 8){ /* valid data packet */
464 usbCrc16Append(&usbTxBuf
[1], len
);
465 txLen
= len
+ 4; /* length including sync byte */
466 if(len
< 8) /* a partial package identifies end of message */
469 txLen
= USBPID_STALL
; /* stall the endpoint */
474 DBG2(0x20, usbTxBuf
, txLen
-1);
477 static inline uchar
isNotSE0(void)
481 * return (USBIN & USBMASK);
482 * here, but the compiler does int-expansion acrobatics.
483 * We can avoid this by assigning to a char-sized variable.
485 rval
= USBIN
& USBMASK
;
489 /* ------------------------------------------------------------------------- */
491 USB_PUBLIC
void usbPoll(void)
496 if((len
= usbRxLen
) > 0){
497 /* We could check CRC16 here -- but ACK has already been sent anyway. If you
498 * need data integrity checks with this driver, check the CRC in your app
499 * code and report errors back to the host. Since the ACK was already sent,
500 * retries must be handled on application level.
501 * unsigned crc = usbCrc16(buffer + 1, usbRxLen - 3);
503 usbProcessRx(usbRxBuf
+ USB_BUFSIZE
+ 1 - usbInputBufOffset
, len
- 3);
504 #if USB_CFG_HAVE_FLOWCONTROL
505 if(usbRxLen
> 0) /* only mark as available if not inactivated */
508 usbRxLen
= 0; /* mark rx buffer as available */
511 if(usbTxLen
& 0x10){ /* transmit system idle */
512 if(usbMsgLen
!= 0xff){ /* transmit data pending? */
516 for(i
= 10; i
> 0; i
--){
520 if(i
== 0){ /* RESET condition, called multiple times during reset */
521 usbNewDeviceAddr
= 0;
523 #if USB_CFG_IMPLEMENT_HALT
524 usbTxLen1
= USBPID_NAK
;
525 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
526 usbTxLen3
= USBPID_NAK
;
533 /* ------------------------------------------------------------------------- */
535 USB_PUBLIC
void usbInit(void)
537 #if USB_INTR_CFG_SET != 0
538 USB_INTR_CFG
|= USB_INTR_CFG_SET
;
540 #if USB_INTR_CFG_CLR != 0
541 USB_INTR_CFG
&= ~(USB_INTR_CFG_CLR
);
543 USB_INTR_ENABLE
|= (1 << USB_INTR_ENABLE_BIT
);
544 #if USB_CFG_HAVE_INTRIN_ENDPOINT
545 USB_SET_DATATOKEN1(USBPID_DATA0
); /* reset data toggling for interrupt endpoint */
546 # if USB_CFG_HAVE_INTRIN_ENDPOINT3
547 USB_SET_DATATOKEN3(USBPID_DATA0
); /* reset data toggling for interrupt endpoint */
552 /* ------------------------------------------------------------------------- */