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: Proprietary, free under certain conditions. See Documentation.
8 * This Revision: $Id: usbdrv.c 222 2006-07-17 15:07:34Z 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 /* usbRxBuf MUST be in 1 byte addressable range (because usbInputBuf is only 1 byte) */
37 __no_init uchar usbRxBuf
[2][USB_BUFSIZE
] __attribute__ ((section (USB_BUFFER_SECTION
))) IAR_SECTION(USB_BUFFER_SECTION
);/* raw RX buffer: PID, 8 bytes data, 2 bytes CRC */
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 uchar usbInputBuf
; /* ptr to raw buffer used for receiving */
42 uchar usbAppBuf
; /* ptr to raw buffer passed to app for processing */
43 volatile schar usbRxLen
; /* = 0; number of bytes in usbAppBuf; 0 means free */
44 uchar usbCurrentTok
; /* last token received, if more than 1 rx endpoint: MSb=endpoint */
45 uchar usbRxToken
; /* token for data we received; if more than 1 rx endpoint: MSb=endpoint */
46 uchar usbMsgLen
= 0xff; /* remaining number of bytes, no msg to send if -1 (see usbMsgPtr) */
47 volatile uchar usbTxLen
= USBPID_NAK
; /* number of bytes to transmit with next IN token or handshake token */
48 uchar usbTxBuf
[USB_BUFSIZE
];/* data to transmit with next IN, free if usbTxLen contains handshake token */
49 #if USB_CFG_HAVE_INTRIN_ENDPOINT
50 volatile uchar usbTxLen1
= USBPID_NAK
; /* TX count for endpoint 1 */
51 uchar usbTxBuf1
[USB_BUFSIZE
]; /* TX data for endpoint 1 */
52 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
53 volatile uchar usbTxLen3
= USBPID_NAK
; /* TX count for endpoint 1 */
54 uchar usbTxBuf3
[USB_BUFSIZE
]; /* TX data for endpoint 1 */
58 /* USB status registers / not shared with asm code */
59 uchar
*usbMsgPtr
; /* data to transmit next -- ROM or RAM address */
60 static uchar usbMsgFlags
; /* flag values see below */
61 static uchar usbIsReset
; /* = 0; USB bus is in reset phase */
63 #define USB_FLG_TX_PACKET (1<<0)
64 /* Leave free 6 bits after TX_PACKET. This way we can increment usbMsgFlags to toggle TX_PACKET */
65 #define USB_FLG_MSGPTR_IS_ROM (1<<6)
66 #define USB_FLG_USE_DEFAULT_RW (1<<7)
70 - do not post/pre inc/dec integer values in operations
71 - assign value of PRG_RDB() to register variables and don't use side effects in arg
72 - use narrow scope for variables which should be in X/Y/Z register
73 - assign char sized expressions to variables to force 8 bit arithmetics
76 /* ------------------------------------------------------------------------- */
78 #if USB_CFG_DESCR_PROPS_STRINGS == 0
80 #if USB_CFG_DESCR_PROPS_STRING_0 == 0
81 #undef USB_CFG_DESCR_PROPS_STRING_0
82 #define USB_CFG_DESCR_PROPS_STRING_0 sizeof(usbDescriptorString0)
83 PROGMEM
char usbDescriptorString0
[] = { /* language descriptor */
84 4, /* sizeof(usbDescriptorString0): length of descriptor in bytes */
85 3, /* descriptor type */
86 0x09, 0x04, /* language index (0x0409 = US-English) */
90 #if USB_CFG_DESCR_PROPS_STRING_VENDOR == 0 && USB_CFG_VENDOR_NAME_LEN
91 #undef USB_CFG_DESCR_PROPS_STRING_VENDOR
92 #define USB_CFG_DESCR_PROPS_STRING_VENDOR sizeof(usbDescriptorStringVendor)
93 PROGMEM
int usbDescriptorStringVendor
[] = {
94 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_VENDOR_NAME_LEN
),
99 #if USB_CFG_DESCR_PROPS_STRING_DEVICE == 0 && USB_CFG_DEVICE_NAME_LEN
100 #undef USB_CFG_DESCR_PROPS_STRING_DEVICE
101 #define USB_CFG_DESCR_PROPS_STRING_DEVICE sizeof(usbDescriptorStringDevice)
102 PROGMEM
int usbDescriptorStringDevice
[] = {
103 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_DEVICE_NAME_LEN
),
108 #if USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER == 0 && USB_CFG_SERIAL_NUMBER_LEN
109 #undef USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
110 #define USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER sizeof(usbDescriptorStringSerialNumber)
111 PROGMEM
int usbDescriptorStringSerialNumber
[] = {
112 USB_STRING_DESCRIPTOR_HEADER(USB_CFG_SERIAL_NUMBER_LEN
),
113 USB_CFG_SERIAL_NUMBER
117 #endif /* USB_CFG_DESCR_PROPS_STRINGS == 0 */
119 #if USB_CFG_DESCR_PROPS_DEVICE == 0
120 #undef USB_CFG_DESCR_PROPS_DEVICE
121 #define USB_CFG_DESCR_PROPS_DEVICE sizeof(usbDescriptorDevice)
122 PROGMEM
char usbDescriptorDevice
[] = { /* USB device descriptor */
123 18, /* sizeof(usbDescriptorDevice): length of descriptor in bytes */
124 USBDESCR_DEVICE
, /* descriptor type */
125 0x01, 0x01, /* USB version supported */
126 USB_CFG_DEVICE_CLASS
,
127 USB_CFG_DEVICE_SUBCLASS
,
129 8, /* max packet size */
130 USB_CFG_VENDOR_ID
, /* 2 bytes */
131 USB_CFG_DEVICE_ID
, /* 2 bytes */
132 USB_CFG_DEVICE_VERSION
, /* 2 bytes */
133 USB_CFG_DESCR_PROPS_STRING_VENDOR
!= 0 ?
1 : 0, /* manufacturer string index */
134 USB_CFG_DESCR_PROPS_STRING_DEVICE
!= 0 ?
2 : 0, /* product string index */
135 USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
!= 0 ?
3 : 0, /* serial number string index */
136 1, /* number of configurations */
140 #if USB_CFG_DESCR_PROPS_HID_REPORT != 0 && USB_CFG_DESCR_PROPS_HID == 0
141 #undef USB_CFG_DESCR_PROPS_HID
142 #define USB_CFG_DESCR_PROPS_HID 9 /* length of HID descriptor in config descriptor below */
145 #if USB_CFG_DESCR_PROPS_CONFIGURATION == 0
146 #undef USB_CFG_DESCR_PROPS_CONFIGURATION
147 #define USB_CFG_DESCR_PROPS_CONFIGURATION sizeof(usbDescriptorConfiguration)
148 PROGMEM
char usbDescriptorConfiguration
[] = { /* USB configuration descriptor */
149 9, /* sizeof(usbDescriptorConfiguration): length of descriptor in bytes */
150 USBDESCR_CONFIG
, /* descriptor type */
151 18 + 7 * USB_CFG_HAVE_INTRIN_ENDPOINT
+ (USB_CFG_DESCR_PROPS_HID
& 0xff), 0,
152 /* total length of data returned (including inlined descriptors) */
153 1, /* number of interfaces in this configuration */
154 1, /* index of this configuration */
155 0, /* configuration name string index */
156 #if USB_CFG_IS_SELF_POWERED
157 USBATTR_SELFPOWER
, /* attributes */
159 USBATTR_BUSPOWER
, /* attributes */
161 USB_CFG_MAX_BUS_POWER
/2, /* max USB current in 2mA units */
162 /* interface descriptor follows inline: */
163 9, /* sizeof(usbDescrInterface): length of descriptor in bytes */
164 USBDESCR_INTERFACE
, /* descriptor type */
165 0, /* index of this interface */
166 0, /* alternate setting for this interface */
167 USB_CFG_HAVE_INTRIN_ENDPOINT
, /* endpoints excl 0: number of endpoint descriptors to follow */
168 USB_CFG_INTERFACE_CLASS
,
169 USB_CFG_INTERFACE_SUBCLASS
,
170 USB_CFG_INTERFACE_PROTOCOL
,
171 0, /* string index for interface */
172 #if (USB_CFG_DESCR_PROPS_HID & 0xff) /* HID descriptor */
173 9, /* sizeof(usbDescrHID): length of descriptor in bytes */
174 USBDESCR_HID
, /* descriptor type: HID */
175 0x01, 0x01, /* BCD representation of HID version */
176 0x00, /* target country code */
177 0x01, /* number of HID Report (or other HID class) Descriptor infos to follow */
178 0x22, /* descriptor type: report */
179 USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH
, 0, /* total length of report descriptor */
181 #if USB_CFG_HAVE_INTRIN_ENDPOINT /* endpoint descriptor for endpoint 1 */
182 7, /* sizeof(usbDescrEndpoint) */
183 USBDESCR_ENDPOINT
, /* descriptor type = endpoint */
184 0x81, /* IN endpoint number 1 */
185 0x03, /* attrib: Interrupt endpoint */
186 8, 0, /* maximum packet size */
187 USB_CFG_INTR_POLL_INTERVAL
, /* in ms */
192 /* We don't use prog_int or prog_int16_t for compatibility with various libc
193 * versions. Here's an other compatibility hack:
196 #define PRG_RDB(addr) pgm_read_byte(addr)
204 /* We use this union to do type conversions. This is better optimized than
205 * type casts in gcc 3.4.3 and much better than using bit shifts to build
206 * ints from chars. Byte ordering is not a problem on an 8 bit platform.
209 /* ------------------------------------------------------------------------- */
211 #if USB_CFG_HAVE_INTRIN_ENDPOINT
212 uchar usbTxPacketCnt1
;
214 void usbSetInterrupt(uchar
*data
, uchar len
)
218 #if USB_CFG_IMPLEMENT_HALT
219 if(usbTxLen1
== USBPID_STALL
)
222 #if 0 /* No runtime checks! Caller is responsible for valid data! */
223 if(len
> 8) /* interrupt transfers are limited to 8 bytes */
227 if(usbTxPacketCnt1
& 1)
229 if(usbTxLen1
& 0x10){ /* packet buffer was empty */
232 usbTxLen1
= USBPID_NAK
; /* avoid sending incomplete interrupt data */
238 usbCrc16Append(&usbTxBuf1
[1], len
);
239 usbTxLen1
= len
+ 4; /* len must be given including sync byte */
240 DBG2(0x21, usbTxBuf1
, len
+ 3);
244 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
245 uchar usbTxPacketCnt3
;
247 void usbSetInterrupt3(uchar
*data
, uchar len
)
252 if(usbTxPacketCnt3
& 1)
254 if(usbTxLen3
& 0x10){ /* packet buffer was empty */
257 usbTxLen3
= USBPID_NAK
; /* avoid sending incomplete interrupt data */
263 usbCrc16Append(&usbTxBuf3
[1], len
);
264 usbTxLen3
= len
+ 4; /* len must be given including sync byte */
265 DBG2(0x23, usbTxBuf3
, len
+ 3);
270 static uchar
usbRead(uchar
*data
, uchar len
)
272 #if USB_CFG_IMPLEMENT_FN_READ
273 if(usbMsgFlags
& USB_FLG_USE_DEFAULT_RW
){
275 uchar i
= len
, *r
= usbMsgPtr
;
276 if(usbMsgFlags
& USB_FLG_MSGPTR_IS_ROM
){ /* ROM data */
278 uchar c
= PRG_RDB(r
); /* assign to char size variable to enforce byte ops */
282 }else{ /* RAM data */
288 #if USB_CFG_IMPLEMENT_FN_READ
290 if(len
!= 0) /* don't bother app with 0 sized reads */
291 return usbFunctionRead(data
, len
);
298 #define GET_DESCRIPTOR(cfgProp, staticName) \
300 if((cfgProp) & USB_PROP_IS_RAM) \
301 flags &= ~USB_FLG_MSGPTR_IS_ROM; \
302 if((cfgProp) & USB_PROP_IS_DYNAMIC){ \
303 replyLen = usbFunctionDescriptor(rq); \
305 replyData = (uchar *)(staticName); \
306 SET_REPLY_LEN((cfgProp) & 0xff); \
309 /* We use if() instead of #if in the macro above because #if can't be used
310 * in macros and the compiler optimizes constant conditions anyway.
314 /* Don't make this function static to avoid inlining.
315 * The entire function would become too large and exceed the range of
317 * 2006-02-25: Either gcc 3.4.3 is better than the gcc used when the comment
318 * above was written, or other parts of the code have changed. We now get
319 * better results with an inlined function. Test condition: PowerSwitch code.
321 static void usbProcessRx(uchar
*data
, uchar len
)
323 usbRequest_t
*rq
= (void *)data
;
324 uchar replyLen
= 0, flags
= USB_FLG_USE_DEFAULT_RW
;
325 /* We use if() cascades because the compare is done byte-wise while switch()
326 * is int-based. The if() cascades are therefore more efficient.
328 DBG2(0x10 + ((usbRxToken
>> 6) & 3), data
, len
);
329 #if USB_CFG_IMPLEMENT_FN_WRITEOUT
330 if(usbRxToken
& 0x80){
331 usbFunctionWriteOut(data
, len
);
332 return; /* no reply expected, hence no usbMsgPtr, usbMsgFlags, usbMsgLen set */
334 if(usbRxToken
== (uchar
)(USBPID_SETUP
& 0x7f)){ /* MSb contains endpoint (== 0) */
336 if(usbRxToken
== (uchar
)USBPID_SETUP
){
338 if(len
== 8){ /* Setup size must be always 8 bytes. Ignore otherwise. */
339 uchar type
= rq
->bmRequestType
& USBRQ_TYPE_MASK
;
340 if(type
== USBRQ_TYPE_STANDARD
){
341 #define SET_REPLY_LEN(len) replyLen = (len); usbMsgPtr = replyData
342 /* This macro ensures that replyLen and usbMsgPtr are always set in the same way.
343 * That allows optimization of common code in if() branches */
344 uchar
*replyData
= usbTxBuf
+ 9; /* there is 3 bytes free space at the end of the buffer */
345 replyData
[0] = 0; /* common to USBRQ_GET_STATUS and USBRQ_GET_INTERFACE */
346 if(rq
->bRequest
== USBRQ_GET_STATUS
){ /* 0 */
347 uchar
__attribute__((__unused__
)) recipient
= rq
->bmRequestType
& USBRQ_RCPT_MASK
; /* assign arith ops to variables to enforce byte size */
348 #if USB_CFG_IS_SELF_POWERED
349 if(recipient
== USBRQ_RCPT_DEVICE
)
350 replyData
[0] = USB_CFG_IS_SELF_POWERED
;
352 #if USB_CFG_HAVE_INTRIN_ENDPOINT && USB_CFG_IMPLEMENT_HALT
353 if(recipient
== USBRQ_RCPT_ENDPOINT
&& rq
->wIndex
.bytes
[0] == 0x81) /* request status for endpoint 1 */
354 replyData
[0] = usbTxLen1
== USBPID_STALL
;
358 }else if(rq
->bRequest
== USBRQ_SET_ADDRESS
){ /* 5 */
359 usbNewDeviceAddr
= rq
->wValue
.bytes
[0];
360 }else if(rq
->bRequest
== USBRQ_GET_DESCRIPTOR
){ /* 6 */
361 flags
= USB_FLG_MSGPTR_IS_ROM
| USB_FLG_USE_DEFAULT_RW
;
362 if(rq
->wValue
.bytes
[1] == USBDESCR_DEVICE
){ /* 1 */
363 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_DEVICE
, usbDescriptorDevice
)
364 }else if(rq
->wValue
.bytes
[1] == USBDESCR_CONFIG
){ /* 2 */
365 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_CONFIGURATION
, usbDescriptorConfiguration
)
366 }else if(rq
->wValue
.bytes
[1] == USBDESCR_STRING
){ /* 3 */
367 #if USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC
368 if(USB_CFG_DESCR_PROPS_STRINGS
& USB_PROP_IS_RAM
)
369 flags
&= ~USB_FLG_MSGPTR_IS_ROM
;
370 replyLen
= usbFunctionDescriptor(rq
);
371 #else /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
372 if(rq
->wValue
.bytes
[0] == 0){ /* descriptor index */
373 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_0
, usbDescriptorString0
)
374 }else if(rq
->wValue
.bytes
[0] == 1){
375 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_VENDOR
, usbDescriptorStringVendor
)
376 }else if(rq
->wValue
.bytes
[0] == 2){
377 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_DEVICE
, usbDescriptorStringDevice
)
378 }else if(rq
->wValue
.bytes
[0] == 3){
379 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_STRING_SERIAL_NUMBER
, usbDescriptorStringSerialNumber
)
380 }else if(USB_CFG_DESCR_PROPS_UNKNOWN
& USB_PROP_IS_DYNAMIC
){
381 replyLen
= usbFunctionDescriptor(rq
);
383 #endif /* USB_CFG_DESCR_PROPS_STRINGS & USB_PROP_IS_DYNAMIC */
384 }else if(rq
->wValue
.bytes
[1] == USBDESCR_HID
){ /* 0x21 */
385 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID
, usbDescriptorConfiguration
+ 18)
386 }else if(rq
->wValue
.bytes
[1] == USBDESCR_HID_REPORT
){ /* 0x22 */
387 GET_DESCRIPTOR(USB_CFG_DESCR_PROPS_HID_REPORT
, usbDescriptorHidReport
)
388 }else if(USB_CFG_DESCR_PROPS_UNKNOWN
& USB_PROP_IS_DYNAMIC
){
389 replyLen
= usbFunctionDescriptor(rq
);
391 }else if(rq
->bRequest
== USBRQ_GET_CONFIGURATION
){ /* 8 */
392 replyData
= &usbConfiguration
; /* send current configuration value */
394 }else if(rq
->bRequest
== USBRQ_SET_CONFIGURATION
){ /* 9 */
395 usbConfiguration
= rq
->wValue
.bytes
[0];
396 #if USB_CFG_IMPLEMENT_HALT
397 usbTxLen1
= USBPID_NAK
;
399 }else if(rq
->bRequest
== USBRQ_GET_INTERFACE
){ /* 10 */
401 #if USB_CFG_HAVE_INTRIN_ENDPOINT
402 }else if(rq
->bRequest
== USBRQ_SET_INTERFACE
){ /* 11 */
403 usbTxPacketCnt1
= 0; /* reset data toggling for interrupt endpoint */
404 # if USB_CFG_HAVE_INTRIN_ENDPOINT3
405 usbTxPacketCnt3
= 0; /* reset data toggling for interrupt endpoint */
407 # if USB_CFG_IMPLEMENT_HALT
408 usbTxLen1
= USBPID_NAK
;
409 }else if(rq
->bRequest
== USBRQ_CLEAR_FEATURE
|| rq
->bRequest
== USBRQ_SET_FEATURE
){ /* 1|3 */
410 if(rq
->wValue
.bytes
[0] == 0 && rq
->wIndex
.bytes
[0] == 0x81){ /* feature 0 == HALT for endpoint == 1 */
411 usbTxLen1
= rq
->bRequest
== USBRQ_CLEAR_FEATURE ? USBPID_NAK
: USBPID_STALL
;
412 usbTxPacketCnt1
= 0; /* reset data toggling for interrupt endpoint */
413 # if USB_CFG_HAVE_INTRIN_ENDPOINT3
414 usbTxPacketCnt3
= 0; /* reset data toggling for interrupt endpoint */
420 /* the following requests can be ignored, send default reply */
421 /* 1: CLEAR_FEATURE, 3: SET_FEATURE, 7: SET_DESCRIPTOR */
422 /* 12: SYNCH_FRAME */
425 }else{ /* not a standard request -- must be vendor or class request */
426 replyLen
= usbFunctionSetup(data
);
428 #if USB_CFG_IMPLEMENT_FN_READ || USB_CFG_IMPLEMENT_FN_WRITE
429 if(replyLen
== 0xff){ /* use user-supplied read/write function */
430 if((rq
->bmRequestType
& USBRQ_DIR_MASK
) == USBRQ_DIR_DEVICE_TO_HOST
){
431 replyLen
= rq
->wLength
.bytes
[0]; /* IN transfers only */
433 flags
&= ~USB_FLG_USE_DEFAULT_RW
; /* we have no valid msg, use user supplied read/write functions */
434 }else /* The 'else' prevents that we limit a replyLen of 0xff to the maximum transfer len. */
436 if(!rq
->wLength
.bytes
[1] && replyLen
> rq
->wLength
.bytes
[0]) /* limit length to max */
437 replyLen
= rq
->wLength
.bytes
[0];
439 /* make sure that data packets which are sent as ACK to an OUT transfer are always zero sized */
440 }else{ /* DATA packet from out request */
441 #if USB_CFG_IMPLEMENT_FN_WRITE
442 if(!(usbMsgFlags
& USB_FLG_USE_DEFAULT_RW
)){
443 uchar rval
= usbFunctionWrite(data
, len
);
445 if(rval
== 0xff){ /* an error occurred */
446 usbMsgLen
= 0xff; /* cancel potentially pending data packet for ACK */
447 usbTxLen
= USBPID_STALL
;
448 }else if(rval
!= 0){ /* This was the final package */
449 replyLen
= 0; /* answer with a zero-sized data packet */
451 flags
= 0; /* start with a DATA1 package, stay with user supplied write() function */
456 usbMsgLen
= replyLen
;
459 /* ------------------------------------------------------------------------- */
461 static void usbBuildTxBlock(void)
463 uchar wantLen
, len
, txLen
, token
;
468 usbMsgLen
-= wantLen
;
469 token
= USBPID_DATA1
;
470 if(usbMsgFlags
& USB_FLG_TX_PACKET
)
471 token
= USBPID_DATA0
;
473 len
= usbRead(usbTxBuf
+ 1, wantLen
);
474 if(len
<= 8){ /* valid data packet */
475 usbCrc16Append(&usbTxBuf
[1], len
);
476 txLen
= len
+ 4; /* length including sync byte */
477 if(len
< 8) /* a partial package identifies end of message */
480 txLen
= USBPID_STALL
; /* stall the endpoint */
485 DBG2(0x20, usbTxBuf
, txLen
-1);
488 static inline uchar
isNotSE0(void)
492 * return (USBIN & USBMASK);
493 * here, but the compiler does int-expansion acrobatics.
494 * We can avoid this by assigning to a char-sized variable.
496 rval
= USBIN
& USBMASK
;
500 /* ------------------------------------------------------------------------- */
506 if((len
= usbRxLen
) > 0){
507 /* We could check CRC16 here -- but ACK has already been sent anyway. If you
508 * need data integrity checks with this driver, check the CRC in your app
509 * code and report errors back to the host. Since the ACK was already sent,
510 * retries must be handled on application level.
511 * unsigned crc = usbCrc16((uchar *)(unsigned)(usbAppBuf + 1), usbRxLen - 3);
513 len
-= 3; /* remove PID and CRC */
514 if(len
< 128){ /* no overflow */
516 appBuf
.ptr
= (uchar
*)usbRxBuf
;
517 appBuf
.bytes
[0] = usbAppBuf
;
519 usbProcessRx(appBuf
.ptr
, len
);
521 #if USB_CFG_HAVE_FLOWCONTROL
522 if(usbRxLen
> 0) /* only mark as available if not inactivated */
525 usbRxLen
= 0; /* mark rx buffer as available */
528 if(usbMsgLen
!= 0xff){ /* transmit data pending? */
529 if(usbTxLen
& 0x10) /* transmit system idle */
532 if(isNotSE0()){ /* SE0 state */
535 /* check whether SE0 lasts for more than 2.5us (3.75 bit times) */
543 usbNewDeviceAddr
= 0;
545 #if USB_CFG_IMPLEMENT_HALT
546 usbTxLen1
= USBPID_NAK
;
547 #if USB_CFG_HAVE_INTRIN_ENDPOINT3
548 usbTxLen3
= USBPID_NAK
;
557 /* ------------------------------------------------------------------------- */
561 usbInputBuf
= (uchar
)usbRxBuf
[0];
562 usbAppBuf
= (uchar
)usbRxBuf
[1];
563 #if USB_INTR_CFG_SET != 0
564 USB_INTR_CFG
|= USB_INTR_CFG_SET
;
566 #if USB_INTR_CFG_CLR != 0
567 USB_INTR_CFG
&= ~(USB_INTR_CFG_CLR
);
569 USB_INTR_ENABLE
|= (1 << USB_INTR_ENABLE_BIT
);
572 /* ------------------------------------------------------------------------- */