2 FreeRTOS.org V4.0.3 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
30 ***************************************************************************
\r
34 Sample interrupt driven USB device driver. This is a minimal implementation
\r
35 for demonstration only. Although functional, it is not a full and compliant
\r
38 The USB device enumerates as a simple 3 axis joystick, and once configured
\r
39 transmits 3 axis of data which can be viewed from the USB host machine.
\r
41 This file implements the USB interrupt service routine, and a demo FreeRTOS
\r
42 task. The interrupt service routine handles the USB hardware - taking a
\r
43 snapshot of the USB status at the point of the interrupt. The task receives
\r
44 the status information from the interrupt for processing at the task level.
\r
46 See the FreeRTOS.org WEB documentation for more information.
\r
52 + Descriptors that have a length that is an exact multiple of usbFIFO_LENGTH
\r
53 can now be transmitted. To this end an extra parameter has been
\r
54 added to the prvSendControlData() function, and the state
\r
55 eSENDING_EVEN_DESCRIPTOR has been introduced. Thanks to Scott Miller for
\r
56 assisting with this contribution.
\r
60 + Replaced the duplicated RX_DATA_BK0 in the interrupt mask with the
\r
64 /* Standard includes. */
\r
67 /* Demo board includes. */
\r
70 /* Scheduler includes. */
\r
71 #include "FreeRTOS.h"
\r
76 /* Descriptor type definitions. */
\r
77 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
78 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
79 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
81 /* USB request type definitions. */
\r
82 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
83 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
84 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
85 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
86 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
87 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
88 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
89 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
90 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
91 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
92 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
93 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
94 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
95 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
96 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
99 /* Misc USB definitions. */
\r
100 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
101 #define usbBUS_POWERED ( 0x80 )
\r
102 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
103 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
105 /* Index to the various string. */
\r
106 #define usbLANGUAGE_STRING ( 0 )
\r
107 #define usbMANUFACTURER_STRING ( 1 )
\r
108 #define usbPRODUCT_STRING ( 2 )
\r
109 #define usbCONFIGURATION_STRING ( 3 )
\r
110 #define usbINTERFACE_STRING ( 4 )
\r
112 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
113 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
115 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
116 #define usbREQUEST_INDEX ( 6 )
\r
117 #define usbVALUE_HIGH_BYTE ( 4 )
\r
118 #define usbVALUE_LOW_BYTE ( 5 )
\r
119 #define usbINDEX_HIGH_BYTE ( 2 )
\r
120 #define usbINDEX_LOW_BYTE ( 3 )
\r
121 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
122 #define usbLENGTH_LOW_BYTE ( 1 )
\r
124 /* Misc application definitions. */
\r
125 #define usbINTERRUPT_PRIORITY ( 3 )
\r
126 #define usbQUEUE_LENGTH ( 0x3 ) /* Must have all bits set! */
\r
127 #define usbFIFO_LENGTH ( ( unsigned portLONG ) 8 )
\r
128 #define usbEND_POINT_0 ( 0 )
\r
129 #define usbEND_POINT_1 ( 1 )
\r
130 #define usbXUP ( 1 )
\r
131 #define usbXDOWN ( 2 )
\r
132 #define usbYUP ( 3 )
\r
133 #define usbYDOWN ( 4 )
\r
134 #define usbMAX_COORD ( 120 )
\r
135 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
136 #define usbRX_COUNT_MASK ( ( unsigned portLONG ) 0x7ff )
\r
137 #define AT91C_UDP_STALLSENT AT91C_UDP_ISOERROR
\r
138 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
139 #define usbINIT_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
140 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
141 #define usbEND_POINT_RESET_MASK ( ( unsigned portLONG ) 0x0f )
\r
142 #define usbDATA_INC ( ( portCHAR ) 5 )
\r
143 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned portLONG ) 8 )
\r
145 /* Control request types. */
\r
146 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
147 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
148 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
149 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
151 /*-----------------------------------------------------------*/
\r
153 /* Structure used to take a snapshot of the USB status from within the ISR. */
\r
154 typedef struct X_ISR_STATUS
\r
156 unsigned portLONG ulISR;
\r
157 unsigned portLONG ulCSR0;
\r
158 unsigned portCHAR ucFifoData[ 8 ];
\r
161 /* Structure used to hold the received requests. */
\r
164 unsigned portCHAR ucReqType;
\r
165 unsigned portCHAR ucRequest;
\r
166 unsigned portSHORT usValue;
\r
167 unsigned portSHORT usIndex;
\r
168 unsigned portSHORT usLength;
\r
177 eSENDING_EVEN_DESCRIPTOR,
\r
181 /* Structure used to control the data being sent to the host. */
\r
184 unsigned portCHAR ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
185 unsigned portLONG ulNextCharIndex;
\r
186 unsigned portLONG ulTotalDataLength;
\r
189 /*-----------------------------------------------------------*/
\r
192 * The USB interrupt service routine. This takes a snapshot of the USB
\r
193 * device at the time of the interrupt, clears the interrupts, and posts
\r
194 * the data to the USB processing task.
\r
196 __arm void vUSB_ISR( void );
\r
199 * Called after the bus reset interrupt - this function readies all the
\r
200 * end points for communication.
\r
202 static void prvResetEndPoints( void );
\r
205 * Setup the USB hardware, install the interrupt service routine and
\r
206 * initialise all the state variables.
\r
208 static void vInitUSBInterface( void );
\r
211 * Decode and act upon an interrupt generated by the control end point.
\r
213 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
216 * For simplicity requests are separated into device, interface, class
\r
217 * interface and end point requests.
\r
219 * Decode and handle standard device requests originating on the control
\r
222 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
225 * For simplicity requests are separated into device, interface, class
\r
226 * interface and end point requests.
\r
228 * Decode and handle standard interface requests originating on the control
\r
231 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
234 * For simplicity requests are separated into device, interface, class
\r
235 * interface and end point requests.
\r
237 * Decode and handle standard end point requests originating on the control
\r
240 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
243 * For simplicity requests are separated into device, interface, class
\r
244 * interface and end point requests.
\r
246 * Decode and handle the class interface requests.
\r
248 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
251 * Setup the Tx buffer to send data in response to a control request.
\r
253 * The data to be transmitted is buffered, the state variables are updated,
\r
254 * then prvSendNextSegment() is called to start the transmission off. Once
\r
255 * the first segment has been sent the remaining segments are transmitted
\r
256 * in response to TXCOMP interrupts until the entire buffer has been
\r
259 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthLeftToSend, portLONG lSendingDescriptor );
\r
262 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
264 * If there is data to be transmitted then send the next segment. A segment
\r
265 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
266 * point by the descriptor). The final segment may be less than 8 bytes if
\r
267 * the total data length was not an exact multiple of 8.
\r
269 static void prvSendNextSegment( void );
\r
272 * A stall condition is forced each time the host makes a request that is not
\r
273 * supported by this minimal implementation.
\r
275 * A stall is forced by setting the appropriate bit in the end points control
\r
276 * and status register.
\r
278 static void prvSendStall( void );
\r
281 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
282 * of certain events from the host.
\r
284 static void prvUSBTransmitNull( void );
\r
287 * When the host requests a descriptor this function is called to determine
\r
288 * which descriptor is being requested and start its transmission.
\r
290 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
293 * This demo USB device enumerates as a simple 3 axis joystick. Once
\r
294 * configured this function is periodically called to generate some sample
\r
297 * The x and y axis are made to move in a square. The z axis is made to
\r
298 * repeatedly increment up to its maximum.
\r
300 static void prvTransmitSampleValues( void );
\r
303 * The created task to handle the USB demo functionality.
\r
305 void vUSBDemoTask( void *pvParameters );
\r
307 /*-----------------------------------------------------------*/
\r
310 - DESCRIPTOR DEFINITIONS -
\r
313 /* String descriptors used during the enumeration process.
\r
314 These take the form:
\r
317 Length of descriptor,
\r
322 const portCHAR pxLanguageStringDescriptor[] =
\r
325 usbDESCRIPTOR_TYPE_STRING,
\r
329 const portCHAR pxManufacturerStringDescriptor[] =
\r
332 usbDESCRIPTOR_TYPE_STRING,
\r
344 const portCHAR pxProductStringDescriptor[] =
\r
347 usbDESCRIPTOR_TYPE_STRING,
\r
372 const portCHAR pxConfigurationStringDescriptor[] =
\r
375 usbDESCRIPTOR_TYPE_STRING,
\r
397 const portCHAR pxInterfaceStringDescriptor[] =
\r
400 usbDESCRIPTOR_TYPE_STRING,
\r
418 /* Enumeration descriptors. */
\r
419 const portCHAR pxReportDescriptor[] =
\r
421 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
422 0x09, 0x04, /* USAGE (Joystick) */
\r
423 0xa1, 0x01, /* COLLECTION (Application) */
\r
424 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
425 0x09, 0x01, /* USAGE (Pointer) */
\r
426 0xa1, 0x00, /* COLLECTION (Physical) */
\r
427 0x09, 0x30, /* USAGE (X) */
\r
428 0x09, 0x31, /* USAGE (Y) */
\r
429 0x09, 0x32, /* USAGE (Z) */
\r
430 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
431 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
432 0x75, 0x08, /* REPORT_SIZE (8) */
\r
433 0x95, 0x03, /* REPORT_COUNT (3) */
\r
434 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
435 0xc0, /* END_COLLECTION */
\r
436 0xc0 /* END_COLLECTION */
\r
439 const char pxDeviceDescriptor[] =
\r
441 /* Device descriptor */
\r
442 0x12, /* bLength */
\r
443 0x01, /* bDescriptorType */
\r
444 0x10, 0x01, /* bcdUSBL */
\r
445 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
446 0x00, /* bDeviceSubclass: */
\r
447 0x00, /* bDeviceProtocol: */
\r
448 0x08, /* bMaxPacketSize0 */
\r
449 0xFF, 0xFF, /* idVendorL */
\r
450 0x01, 0x00, /* idProductL */
\r
451 0x00, 0x01, /* bcdDeviceL */
\r
452 usbMANUFACTURER_STRING, /* iManufacturer */
\r
453 usbPRODUCT_STRING, /* iProduct */
\r
454 0x00, /* SerialNumber */
\r
455 0x01 /* bNumConfigs */
\r
458 const char pxConfigDescriptor[] = {
\r
459 /* Configuration 1 descriptor */
\r
460 0x09, /* CbLength */
\r
461 0x02, /* CbDescriptorType */
\r
462 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
463 0x01, /* CbNumInterfaces */
\r
464 0x01, /* CbConfigurationValue */
\r
465 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
466 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
467 0x32, /* CMaxPower: 100mA */
\r
469 /* Joystick Interface Descriptor Requirement */
\r
470 0x09, /* bLength */
\r
471 0x04, /* bDescriptorType */
\r
472 0x00, /* bInterfaceNumber */
\r
473 0x00, /* bAlternateSetting */
\r
474 0x01, /* bNumEndpoints */
\r
475 0x03, /* bInterfaceClass: HID code */
\r
476 0x00, /* bInterfaceSubclass */
\r
477 0x00, /* bInterfaceProtocol */
\r
478 usbINTERFACE_STRING,/* iInterface */
\r
480 /* HID Descriptor */
\r
481 0x09, /* bLength */
\r
482 0x21, /* bDescriptor type: HID Descriptor Type */
\r
483 0x00, 0x01, /* bcdHID */
\r
484 0x00, /* bCountryCode */
\r
485 0x01, /* bNumDescriptors */
\r
486 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
487 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
489 /* Endpoint 1 descriptor */
\r
490 0x07, /* bLength */
\r
491 0x05, /* bDescriptorType */
\r
492 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
493 0x03, /* bmAttributes INT */
\r
494 0x03, 0x00, /* wMaxPacketSize: 3 bytes (x, y, z) */
\r
495 0x0A /* bInterval */
\r
498 /*-----------------------------------------------------------*/
\r
500 /* File scope state variables. */
\r
501 static unsigned portCHAR ucUSBConfig = ( unsigned portCHAR ) 0;
\r
502 static unsigned portLONG ulReceivedAddress = ( unsigned portLONG ) 0;
\r
503 static eDRIVER_STATE eDriverState = eNOTHING;
\r
505 /* Array in which the USB interrupt status is passed between the ISR and task. */
\r
506 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
508 /* Structure used to control the characters being sent to the host. */
\r
509 static xTX_MESSAGE pxCharsForTx;
\r
511 /* Queue used to pass messages between the ISR and the task. */
\r
512 static xQueueHandle xUSBInterruptQueue;
\r
514 /* ISR entry has to be written in the asm file as we want a context switch
\r
515 to occur from within the ISR. See the port documentation on the FreeRTOS.org
\r
516 WEB site for more information. */
\r
517 extern void vUSBISREntry( void );
\r
519 /*-----------------------------------------------------------*/
\r
521 /* Macros to manipulate the control and status registers. These registers
\r
522 cannot be accessed using a direct read modify write operation outside of the
\r
523 ISR as some bits are left unchanged by writing with a 0, and some are left
\r
524 unchanged by writing with a 1. */
\r
526 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
528 #define usbCSR_SET_BIT( pulValueNow, ulBit ) \
\r
530 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
531 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
532 /* write has no effect. */ \
\r
533 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f; \
\r
535 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
536 /* so the write has no effect. */ \
\r
537 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf; \
\r
539 /* Set whichever bit we want set. */ \
\r
540 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( ulBit ); \
\r
543 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit ) \
\r
545 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
546 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
547 /* write has no effect. */ \
\r
548 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f; \
\r
550 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
551 /* so the write has no effect. */ \
\r
552 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf; \
\r
554 /* Clear whichever bit we want clear. */ \
\r
555 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( ~ulBit ); \
\r
558 /*-----------------------------------------------------------*/
\r
560 __arm void vUSB_ISR( void )
\r
562 portBASE_TYPE xTaskWokenByPost = pdFALSE;
\r
563 static volatile unsigned portLONG ulNextMessage = 0;
\r
564 xISRStatus *pxMessage;
\r
565 unsigned portLONG ulTemp, ulRxBytes;
\r
567 /* Take the next message from the queue. Note that usbQUEUE_LENGTH *must*
\r
568 be all 1's, as in 0x01, 0x03, 0x07, etc. */
\r
569 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
572 /* Take a snapshot of the current USB state for processing at the task
\r
574 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
575 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
577 /* Clear the interrupts from the ICR register. The bus end interrupt is
\r
578 cleared separately as it does not appear in the mask register. */
\r
579 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
581 /* If there are bytes in the FIFO then we have to retrieve them here.
\r
582 Ideally this would be done at the task level. However we need to clear the
\r
583 RXSETUP interrupt before leaving the ISR, and this may cause the data in
\r
584 the FIFO to be overwritten. Also the DIR bit has to be changed before the
\r
585 RXSETUP bit is cleared (as per the SAM7 manual). */
\r
586 ulTemp = pxMessage->ulCSR0;
\r
588 /* Are there any bytes in the FIFO? */
\r
589 ulRxBytes = ulTemp >> 16;
\r
590 ulRxBytes &= usbRX_COUNT_MASK;
\r
592 /* With this minimal implementation we are only interested in receiving
\r
593 setup bytes on the control end point. */
\r
594 if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )
\r
596 /* Take off 1 for a zero based index. */
\r
597 while( ulRxBytes > 0 )
\r
600 pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
603 /* The direction must be changed first. */
\r
604 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );
\r
605 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
608 /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA
\r
609 registers to clear the interrupts in the CSR register. */
\r
610 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
611 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
613 /* Also clear the interrupts in the CSR1 register. */
\r
614 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
615 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
616 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
618 /* The message now contains the entire state and optional data from
\r
619 the USB interrupt. This can now be posted on the Rx queue ready for
\r
620 processing at the task level. */
\r
621 xTaskWokenByPost = xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, xTaskWokenByPost );
\r
623 /* We may want to switch to the USB task, if this message has made
\r
624 it the highest priority task that is ready to execute. */
\r
625 portEND_SWITCHING_ISR( xTaskWokenByPost );
\r
627 /* Clear the AIC ready for the next interrupt. */
\r
628 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
630 /*-----------------------------------------------------------*/
\r
632 void vUSBDemoTask( void *pvParameters )
\r
634 xISRStatus *pxMessage;
\r
636 /* The parameters are not used in this task. */
\r
637 ( void ) pvParameters;
\r
639 /* Init USB device */
\r
640 portENTER_CRITICAL();
\r
641 vInitUSBInterface();
\r
642 portEXIT_CRITICAL();
\r
644 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
645 interrupt status then posts the information on this queue for processing
\r
646 at the task level. This simple demo implementation only processes
\r
647 a few interrupt sources. */
\r
650 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
652 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
654 /* Process end point 0 interrupt. */
\r
655 prvProcessEndPoint0Interrupt( pxMessage );
\r
658 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
660 /* Process an end of bus reset interrupt. */
\r
661 prvResetEndPoints();
\r
666 /* The ISR did not post any data for us to process on the queue, so
\r
667 just generate and send some sample data. */
\r
668 if( eDriverState == eREADY_TO_SEND )
\r
670 prvTransmitSampleValues();
\r
675 /*-----------------------------------------------------------*/
\r
677 static void prvTransmitSampleValues( void )
\r
679 unsigned portLONG ulStatus;
\r
680 static portLONG lState = usbXUP;
\r
682 /* Variables to hold dummy x, y and z joystick axis data. */
\r
683 static signed portCHAR x = 0, y = 0, z = 0;
\r
685 /* Generate some sample data in the x and y axis - draw a square. */
\r
688 case usbXUP : x += usbDATA_INC;
\r
689 if( x >= usbMAX_COORD )
\r
695 case usbXDOWN : x -= usbDATA_INC;
\r
696 if( x <= -usbMAX_COORD )
\r
702 case usbYUP : y += usbDATA_INC;
\r
703 if( y >= usbMAX_COORD )
\r
709 case usbYDOWN : y -= usbDATA_INC;
\r
710 if( y <= -usbMAX_COORD )
\r
717 /* Just make the z axis go up and down. */
\r
720 /* Can we place data in the fifo? */
\r
721 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
723 /* Write our sample data to the fifo. */
\r
724 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
725 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
726 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
728 /* Send the data. */
\r
729 portENTER_CRITICAL();
\r
731 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
732 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
733 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
735 portEXIT_CRITICAL();
\r
738 /*-----------------------------------------------------------*/
\r
740 static void prvUSBTransmitNull( void )
\r
742 unsigned portLONG ulStatus;
\r
744 /* Wait until the FIFO is free - even though we are not going to use it.
\r
745 THERE IS NO TIMEOUT HERE! */
\r
746 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
748 vTaskDelay( usbSHORTEST_DELAY );
\r
751 portENTER_CRITICAL();
\r
753 /* Set the length of data to send to equal the index of the next byte
\r
754 to send. This will prevent the ACK to this NULL packet causing any
\r
755 further data transmissions. */
\r
756 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
758 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
759 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
760 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
761 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
763 portEXIT_CRITICAL();
\r
765 /*-----------------------------------------------------------*/
\r
767 static void prvSendStall( void )
\r
769 unsigned portLONG ulStatus;
\r
771 portENTER_CRITICAL();
\r
773 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
774 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
775 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
776 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
778 portEXIT_CRITICAL();
\r
780 /*-----------------------------------------------------------*/
\r
782 static void prvResetEndPoints( void )
\r
784 unsigned portLONG ulTemp;
\r
786 eDriverState = eJUST_RESET;
\r
788 /* Reset all the end points. */
\r
789 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
790 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned portLONG ) 0x00;
\r
792 /* Enable data to be sent and received. */
\r
793 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
795 /* Repair the configuration end point. */
\r
796 portENTER_CRITICAL();
\r
798 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
799 usbCSR_SET_BIT( &ulTemp, ( ( unsigned portLONG ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
800 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
801 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT0 );
\r
803 portEXIT_CRITICAL();
\r
805 /*-----------------------------------------------------------*/
\r
807 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
809 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
811 /* We only expect to receive zero length data here as ACK's.
\r
812 Set the data pointer to the end of the current Tx packet to
\r
813 ensure we don't send out any more data. */
\r
814 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
817 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
819 /* We received a TX complete interrupt. What we do depends on
\r
820 what we sent to get this interrupt. */
\r
822 if( eDriverState == eJUST_GOT_CONFIG )
\r
824 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
825 are now at the end of the enumeration. */
\r
826 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
828 /* Read the end point for data transfer. */
\r
829 portENTER_CRITICAL();
\r
831 unsigned portLONG ulTemp;
\r
833 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
834 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
835 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
836 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT1 );
\r
838 portEXIT_CRITICAL();
\r
840 eDriverState = eREADY_TO_SEND;
\r
842 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
844 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
845 to the addressed state. */
\r
846 if( ulReceivedAddress != ( unsigned portLONG ) 0 )
\r
848 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
852 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
855 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
856 eDriverState = eNOTHING;
\r
860 /* The TXCOMP was not for any special type of transmission. See
\r
861 if there is any more data to send. */
\r
862 prvSendNextSegment();
\r
866 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
868 xUSB_REQUEST xRequest;
\r
869 unsigned portCHAR ucRequest;
\r
870 unsigned portLONG ulRxBytes;
\r
872 /* A data packet is available. */
\r
873 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
874 ulRxBytes &= usbRX_COUNT_MASK;
\r
876 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
878 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
880 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
881 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
883 /* NOT PORTABLE CODE! */
\r
884 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
885 xRequest.usValue <<= 8;
\r
886 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
888 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
889 xRequest.usIndex <<= 8;
\r
890 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
892 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
893 xRequest.usLength <<= 8;
\r
894 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
896 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
897 generate a zero based request selection. This is just done to
\r
898 break up the requests into subsections for clarity. The
\r
899 alternative would be to have more huge switch statement that would
\r
900 be difficult to optimise. */
\r
901 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
902 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
904 switch( ucRequest )
\r
906 case usbSTANDARD_DEVICE_REQUEST:
\r
907 /* Standard Device request */
\r
908 prvHandleStandardDeviceRequest( &xRequest );
\r
911 case usbSTANDARD_INTERFACE_REQUEST:
\r
912 /* Standard Interface request */
\r
913 prvHandleStandardInterfaceRequest( &xRequest );
\r
916 case usbSTANDARD_END_POINT_REQUEST:
\r
917 /* Standard Endpoint request */
\r
918 prvHandleStandardEndPointRequest( &xRequest );
\r
921 case usbCLASS_INTERFACE_REQUEST:
\r
922 /* Class Interface request */
\r
923 prvHandleClassInterfaceRequest( &xRequest );
\r
926 default: /* This is not something we want to respond to. */
\r
932 /*-----------------------------------------------------------*/
\r
934 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
936 /* The type is in the high byte. Return whatever has been requested. */
\r
937 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
939 case usbDESCRIPTOR_TYPE_DEVICE:
\r
940 prvSendControlData( ( unsigned portCHAR * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
943 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
944 prvSendControlData( ( unsigned portCHAR * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
947 case usbDESCRIPTOR_TYPE_STRING:
\r
949 /* The index to the string descriptor is the lower byte. */
\r
950 switch( pxRequest->usValue & 0xff )
\r
952 case usbLANGUAGE_STRING:
\r
953 prvSendControlData( ( unsigned portCHAR * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
956 case usbMANUFACTURER_STRING:
\r
957 prvSendControlData( ( unsigned portCHAR * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
960 case usbPRODUCT_STRING:
\r
961 prvSendControlData( ( unsigned portCHAR * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
964 case usbCONFIGURATION_STRING:
\r
965 prvSendControlData( ( unsigned portCHAR * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
968 case usbINTERFACE_STRING:
\r
969 prvSendControlData( ( unsigned portCHAR * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
973 /* Don't know what this string is. */
\r
981 /* We are not responding to anything else. */
\r
986 /*-----------------------------------------------------------*/
\r
988 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
990 unsigned portSHORT usStatus = 0;
\r
992 switch( pxRequest->ucRequest )
\r
994 case usbGET_STATUS_REQUEST:
\r
995 /* Just send two byte dummy status. */
\r
996 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
999 case usbGET_DESCRIPTOR_REQUEST:
\r
1000 /* Send device descriptor */
\r
1001 prvGetStandardDeviceDescriptor( pxRequest );
\r
1004 case usbGET_CONFIGURATION_REQUEST:
\r
1005 /* Send selected device configuration */
\r
1006 prvSendControlData( ( unsigned portCHAR * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
1009 case usbSET_FEATURE_REQUEST:
\r
1010 prvUSBTransmitNull();
\r
1013 case usbSET_ADDRESS_REQUEST:
\r
1015 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
1016 cannot actually move to the addressed state until we get a TXCOMP
\r
1017 interrupt from this NULL packet. Therefore we just remember the
\r
1018 address and set our state so we know we have received the address. */
\r
1019 prvUSBTransmitNull();
\r
1020 eDriverState = eJUST_GOT_ADDRESS;
\r
1021 ulReceivedAddress = ( unsigned portLONG ) pxRequest->usValue;
\r
1024 case usbSET_CONFIGURATION_REQUEST:
\r
1026 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
1027 we cannot actually move to the configured state until we get a
\r
1028 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
1029 config and set our state so we know we have received the go ahead. */
\r
1030 ucUSBConfig = ( unsigned portCHAR ) ( pxRequest->usValue & 0xff );
\r
1031 eDriverState = eJUST_GOT_CONFIG;
\r
1032 prvUSBTransmitNull();
\r
1037 /* We don't answer to anything else. */
\r
1042 /*-----------------------------------------------------------*/
\r
1044 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1046 switch( pxRequest->ucRequest )
\r
1048 case usbSET_IDLE_REQUEST:
\r
1049 prvUSBTransmitNull();
\r
1052 /* This minimal implementation ignores these. */
\r
1053 case usbGET_REPORT_REQUEST:
\r
1054 case usbGET_IDLE_REQUEST:
\r
1055 case usbGET_PROTOCOL_REQUEST:
\r
1056 case usbSET_REPORT_REQUEST:
\r
1057 case usbSET_PROTOCOL_REQUEST:
\r
1064 /*-----------------------------------------------------------*/
\r
1066 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
1068 switch( ( pxRequest->usValue & ( unsigned portSHORT ) 0xff00 ) >> 8 )
\r
1070 case usbHID_REPORT_DESCRIPTOR:
\r
1071 prvSendControlData( ( unsigned portCHAR * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
1076 /* Don't expect to send any others. */
\r
1081 /*-----------------------------------------------------------*/
\r
1083 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1085 unsigned portSHORT usStatus = 0;
\r
1087 switch( pxRequest->ucRequest )
\r
1089 case usbGET_STATUS_REQUEST:
\r
1090 /* Send dummy 2 bytes. */
\r
1091 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1094 case usbGET_DESCRIPTOR_REQUEST:
\r
1095 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1098 /* This minimal implementation does not respond to these. */
\r
1099 case usbGET_INTERFACE_REQUEST:
\r
1100 case usbSET_FEATURE_REQUEST:
\r
1101 case usbSET_INTERFACE_REQUEST:
\r
1108 /*-----------------------------------------------------------*/
\r
1110 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1112 switch( pxRequest->ucRequest )
\r
1114 /* This minimal implementation does not expect to respond to these. */
\r
1115 case usbGET_STATUS_REQUEST:
\r
1116 case usbCLEAR_FEATURE_REQUEST:
\r
1117 case usbSET_FEATURE_REQUEST:
\r
1124 /*-----------------------------------------------------------*/
\r
1126 static void vInitUSBInterface( void )
\r
1128 volatile unsigned portLONG ulTemp;
\r
1130 /* Create the queue used to communicate between the USB ISR and task. */
\r
1131 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
1133 /* Initialise a few state variables. */
\r
1134 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1135 ucUSBConfig = ( unsigned portCHAR ) 0;
\r
1136 eDriverState = eNOTHING;
\r
1138 /* HARDWARE SETUP */
\r
1140 /* Set the PLL USB Divider */
\r
1141 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1143 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1144 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1145 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1147 /* Setup the PIO for the USB pull up resistor. */
\r
1148 AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,AT91C_PIO_PA16);
\r
1150 /* Start without the pullup - this will get set at the end of this
\r
1152 AT91F_PIO_SetOutput( AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1154 /* When using the USB debugger the peripheral registers do not always get
\r
1155 set to the correct default values. To make sure set the relevant registers
\r
1157 AT91C_BASE_UDP->UDP_IDR = ( unsigned portLONG ) 0xffffffff;
\r
1158 AT91C_BASE_UDP->UDP_ICR = ( unsigned portLONG ) 0xffffffff;
\r
1159 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned portLONG ) 0x00;
\r
1160 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned portLONG ) 0x00;
\r
1161 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1162 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1164 /* Enable the transceiver. */
\r
1165 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1167 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1168 enumeration process progresses. */
\r
1169 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vUSBISREntry );
\r
1170 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_UDP );
\r
1172 /* Wait a short while before making our presence known. */
\r
1173 vTaskDelay( usbINIT_DELAY );
\r
1174 AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1176 /*-----------------------------------------------------------*/
\r
1178 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthToSend, portLONG lSendingDescriptor )
\r
1180 if( ( ( unsigned portLONG ) usRequestedLength < ulLengthToSend ) )
\r
1182 /* Cap the data length to that requested. */
\r
1183 ulLengthToSend = ( unsigned portSHORT ) usRequestedLength;
\r
1185 else if( ( ulLengthToSend < ( unsigned portLONG ) usRequestedLength ) && lSendingDescriptor )
\r
1187 /* We are sending a descriptor. If the descriptor is an exact
\r
1188 multiple of the FIFO length then it will have to be terminated
\r
1189 with a NULL packet. Set the state to indicate this if
\r
1191 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1193 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1197 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1198 BUFFER OVERFLOW PROTECTION HERE.
\r
1200 Copy the data to send into the buffer as we cannot send it all at once
\r
1201 (if it is greater than 8 bytes in length). */
\r
1202 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1204 /* Reinitialise the buffer index so we start sending from the start of
\r
1206 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1207 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1209 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1210 TXCOMP interrupts. */
\r
1211 prvSendNextSegment();
\r
1213 /*-----------------------------------------------------------*/
\r
1215 static void prvSendNextSegment( void )
\r
1217 volatile unsigned portLONG ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1219 /* Is there any data to send? */
\r
1220 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1222 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1224 /* We can only send 8 bytes to the fifo at a time. */
\r
1225 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1227 ulNextLength = usbFIFO_LENGTH;
\r
1231 ulNextLength = ulLengthLeftToSend;
\r
1234 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1236 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1238 vTaskDelay( usbSHORTEST_DELAY );
\r
1241 /* Write the data to the FIFO. */
\r
1242 while( ulNextLength > ( unsigned portLONG ) 0 )
\r
1244 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1247 pxCharsForTx.ulNextCharIndex++;
\r
1250 /* Start the transmission. */
\r
1251 portENTER_CRITICAL();
\r
1253 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1254 usbCSR_SET_BIT( &ulStatus, ( ( unsigned portLONG ) 0x10 ) );
\r
1255 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1257 portEXIT_CRITICAL();
\r
1261 /* There is no data to send. If we were sending a descriptor and the
\r
1262 descriptor was an exact multiple of the max packet size then we need
\r
1263 to send a null to terminate the transmission. */
\r
1264 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1266 prvUSBTransmitNull();
\r
1267 eDriverState = eNOTHING;
\r