2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 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 ***************************************************************************
\r
29 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
54 Sample interrupt driven USB device driver. This is a minimal implementation
\r
55 for demonstration only. Although functional, it is not a full and compliant
\r
58 The USB device enumerates as a simple 3 axis joystick, and once configured
\r
59 transmits 3 axis of data which can be viewed from the USB host machine.
\r
61 This file implements the USB interrupt service routine, and a demo FreeRTOS
\r
62 task. The interrupt service routine handles the USB hardware - taking a
\r
63 snapshot of the USB status at the point of the interrupt. The task receives
\r
64 the status information from the interrupt for processing at the task level.
\r
66 See the FreeRTOS.org WEB documentation for more information.
\r
72 + Descriptors that have a length that is an exact multiple of usbFIFO_LENGTH
\r
73 can now be transmitted. To this end an extra parameter has been
\r
74 added to the prvSendControlData() function, and the state
\r
75 eSENDING_EVEN_DESCRIPTOR has been introduced. Thanks to Scott Miller for
\r
76 assisting with this contribution.
\r
80 + Replaced the duplicated RX_DATA_BK0 in the interrupt mask with the
\r
84 /* Standard includes. */
\r
87 /* Demo board includes. */
\r
90 /* Scheduler includes. */
\r
91 #include "FreeRTOS.h"
\r
96 /* Descriptor type definitions. */
\r
97 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
98 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
99 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
101 /* USB request type definitions. */
\r
102 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
103 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
104 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
105 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
106 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
107 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
108 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
109 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
110 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
111 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
112 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
113 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
114 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
115 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
116 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
119 /* Misc USB definitions. */
\r
120 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
121 #define usbBUS_POWERED ( 0x80 )
\r
122 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
123 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
125 /* Index to the various string. */
\r
126 #define usbLANGUAGE_STRING ( 0 )
\r
127 #define usbMANUFACTURER_STRING ( 1 )
\r
128 #define usbPRODUCT_STRING ( 2 )
\r
129 #define usbCONFIGURATION_STRING ( 3 )
\r
130 #define usbINTERFACE_STRING ( 4 )
\r
132 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
133 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
135 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
136 #define usbREQUEST_INDEX ( 6 )
\r
137 #define usbVALUE_HIGH_BYTE ( 4 )
\r
138 #define usbVALUE_LOW_BYTE ( 5 )
\r
139 #define usbINDEX_HIGH_BYTE ( 2 )
\r
140 #define usbINDEX_LOW_BYTE ( 3 )
\r
141 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
142 #define usbLENGTH_LOW_BYTE ( 1 )
\r
144 /* Misc application definitions. */
\r
145 #define usbINTERRUPT_PRIORITY ( 3 )
\r
146 #define usbQUEUE_LENGTH ( 0x3 ) /* Must have all bits set! */
\r
147 #define usbFIFO_LENGTH ( ( unsigned portLONG ) 8 )
\r
148 #define usbEND_POINT_0 ( 0 )
\r
149 #define usbEND_POINT_1 ( 1 )
\r
150 #define usbXUP ( 1 )
\r
151 #define usbXDOWN ( 2 )
\r
152 #define usbYUP ( 3 )
\r
153 #define usbYDOWN ( 4 )
\r
154 #define usbMAX_COORD ( 120 )
\r
155 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
156 #define usbRX_COUNT_MASK ( ( unsigned portLONG ) 0x7ff )
\r
157 #define AT91C_UDP_STALLSENT AT91C_UDP_ISOERROR
\r
158 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
159 #define usbINIT_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
160 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
161 #define usbEND_POINT_RESET_MASK ( ( unsigned portLONG ) 0x0f )
\r
162 #define usbDATA_INC ( ( portCHAR ) 5 )
\r
163 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned portLONG ) 8 )
\r
165 /* Control request types. */
\r
166 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
167 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
168 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
169 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
171 /*-----------------------------------------------------------*/
\r
173 /* Structure used to take a snapshot of the USB status from within the ISR. */
\r
174 typedef struct X_ISR_STATUS
\r
176 unsigned portLONG ulISR;
\r
177 unsigned portLONG ulCSR0;
\r
178 unsigned portCHAR ucFifoData[ 8 ];
\r
181 /* Structure used to hold the received requests. */
\r
184 unsigned portCHAR ucReqType;
\r
185 unsigned portCHAR ucRequest;
\r
186 unsigned portSHORT usValue;
\r
187 unsigned portSHORT usIndex;
\r
188 unsigned portSHORT usLength;
\r
197 eSENDING_EVEN_DESCRIPTOR,
\r
201 /* Structure used to control the data being sent to the host. */
\r
204 unsigned portCHAR ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
205 unsigned portLONG ulNextCharIndex;
\r
206 unsigned portLONG ulTotalDataLength;
\r
209 /*-----------------------------------------------------------*/
\r
212 * The USB interrupt service routine. This takes a snapshot of the USB
\r
213 * device at the time of the interrupt, clears the interrupts, and posts
\r
214 * the data to the USB processing task.
\r
216 __arm void vUSB_ISR( void );
\r
219 * Called after the bus reset interrupt - this function readies all the
\r
220 * end points for communication.
\r
222 static void prvResetEndPoints( void );
\r
225 * Setup the USB hardware, install the interrupt service routine and
\r
226 * initialise all the state variables.
\r
228 static void vInitUSBInterface( void );
\r
231 * Decode and act upon an interrupt generated by the control end point.
\r
233 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
236 * For simplicity requests are separated into device, interface, class
\r
237 * interface and end point requests.
\r
239 * Decode and handle standard device requests originating on the control
\r
242 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
245 * For simplicity requests are separated into device, interface, class
\r
246 * interface and end point requests.
\r
248 * Decode and handle standard interface requests originating on the control
\r
251 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
254 * For simplicity requests are separated into device, interface, class
\r
255 * interface and end point requests.
\r
257 * Decode and handle standard end point requests originating on the control
\r
260 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
263 * For simplicity requests are separated into device, interface, class
\r
264 * interface and end point requests.
\r
266 * Decode and handle the class interface requests.
\r
268 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
271 * Setup the Tx buffer to send data in response to a control request.
\r
273 * The data to be transmitted is buffered, the state variables are updated,
\r
274 * then prvSendNextSegment() is called to start the transmission off. Once
\r
275 * the first segment has been sent the remaining segments are transmitted
\r
276 * in response to TXCOMP interrupts until the entire buffer has been
\r
279 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthLeftToSend, portLONG lSendingDescriptor );
\r
282 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
284 * If there is data to be transmitted then send the next segment. A segment
\r
285 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
286 * point by the descriptor). The final segment may be less than 8 bytes if
\r
287 * the total data length was not an exact multiple of 8.
\r
289 static void prvSendNextSegment( void );
\r
292 * A stall condition is forced each time the host makes a request that is not
\r
293 * supported by this minimal implementation.
\r
295 * A stall is forced by setting the appropriate bit in the end points control
\r
296 * and status register.
\r
298 static void prvSendStall( void );
\r
301 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
302 * of certain events from the host.
\r
304 static void prvUSBTransmitNull( void );
\r
307 * When the host requests a descriptor this function is called to determine
\r
308 * which descriptor is being requested and start its transmission.
\r
310 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
313 * This demo USB device enumerates as a simple 3 axis joystick. Once
\r
314 * configured this function is periodically called to generate some sample
\r
317 * The x and y axis are made to move in a square. The z axis is made to
\r
318 * repeatedly increment up to its maximum.
\r
320 static void prvTransmitSampleValues( void );
\r
323 * The created task to handle the USB demo functionality.
\r
325 void vUSBDemoTask( void *pvParameters );
\r
327 /*-----------------------------------------------------------*/
\r
330 - DESCRIPTOR DEFINITIONS -
\r
333 /* String descriptors used during the enumeration process.
\r
334 These take the form:
\r
337 Length of descriptor,
\r
342 const portCHAR pxLanguageStringDescriptor[] =
\r
345 usbDESCRIPTOR_TYPE_STRING,
\r
349 const portCHAR pxManufacturerStringDescriptor[] =
\r
352 usbDESCRIPTOR_TYPE_STRING,
\r
364 const portCHAR pxProductStringDescriptor[] =
\r
367 usbDESCRIPTOR_TYPE_STRING,
\r
392 const portCHAR pxConfigurationStringDescriptor[] =
\r
395 usbDESCRIPTOR_TYPE_STRING,
\r
417 const portCHAR pxInterfaceStringDescriptor[] =
\r
420 usbDESCRIPTOR_TYPE_STRING,
\r
438 /* Enumeration descriptors. */
\r
439 const portCHAR pxReportDescriptor[] =
\r
441 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
442 0x09, 0x04, /* USAGE (Joystick) */
\r
443 0xa1, 0x01, /* COLLECTION (Application) */
\r
444 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
445 0x09, 0x01, /* USAGE (Pointer) */
\r
446 0xa1, 0x00, /* COLLECTION (Physical) */
\r
447 0x09, 0x30, /* USAGE (X) */
\r
448 0x09, 0x31, /* USAGE (Y) */
\r
449 0x09, 0x32, /* USAGE (Z) */
\r
450 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
451 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
452 0x75, 0x08, /* REPORT_SIZE (8) */
\r
453 0x95, 0x03, /* REPORT_COUNT (3) */
\r
454 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
455 0xc0, /* END_COLLECTION */
\r
456 0xc0 /* END_COLLECTION */
\r
459 const char pxDeviceDescriptor[] =
\r
461 /* Device descriptor */
\r
462 0x12, /* bLength */
\r
463 0x01, /* bDescriptorType */
\r
464 0x10, 0x01, /* bcdUSBL */
\r
465 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
466 0x00, /* bDeviceSubclass: */
\r
467 0x00, /* bDeviceProtocol: */
\r
468 0x08, /* bMaxPacketSize0 */
\r
469 0xFF, 0xFF, /* idVendorL */
\r
470 0x01, 0x00, /* idProductL */
\r
471 0x00, 0x01, /* bcdDeviceL */
\r
472 usbMANUFACTURER_STRING, /* iManufacturer */
\r
473 usbPRODUCT_STRING, /* iProduct */
\r
474 0x00, /* SerialNumber */
\r
475 0x01 /* bNumConfigs */
\r
478 const char pxConfigDescriptor[] = {
\r
479 /* Configuration 1 descriptor */
\r
480 0x09, /* CbLength */
\r
481 0x02, /* CbDescriptorType */
\r
482 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
483 0x01, /* CbNumInterfaces */
\r
484 0x01, /* CbConfigurationValue */
\r
485 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
486 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
487 0x32, /* CMaxPower: 100mA */
\r
489 /* Joystick Interface Descriptor Requirement */
\r
490 0x09, /* bLength */
\r
491 0x04, /* bDescriptorType */
\r
492 0x00, /* bInterfaceNumber */
\r
493 0x00, /* bAlternateSetting */
\r
494 0x01, /* bNumEndpoints */
\r
495 0x03, /* bInterfaceClass: HID code */
\r
496 0x00, /* bInterfaceSubclass */
\r
497 0x00, /* bInterfaceProtocol */
\r
498 usbINTERFACE_STRING,/* iInterface */
\r
500 /* HID Descriptor */
\r
501 0x09, /* bLength */
\r
502 0x21, /* bDescriptor type: HID Descriptor Type */
\r
503 0x00, 0x01, /* bcdHID */
\r
504 0x00, /* bCountryCode */
\r
505 0x01, /* bNumDescriptors */
\r
506 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
507 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
509 /* Endpoint 1 descriptor */
\r
510 0x07, /* bLength */
\r
511 0x05, /* bDescriptorType */
\r
512 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
513 0x03, /* bmAttributes INT */
\r
514 0x03, 0x00, /* wMaxPacketSize: 3 bytes (x, y, z) */
\r
515 0x0A /* bInterval */
\r
518 /*-----------------------------------------------------------*/
\r
520 /* File scope state variables. */
\r
521 static unsigned portCHAR ucUSBConfig = ( unsigned portCHAR ) 0;
\r
522 static unsigned portLONG ulReceivedAddress = ( unsigned portLONG ) 0;
\r
523 static eDRIVER_STATE eDriverState = eNOTHING;
\r
525 /* Array in which the USB interrupt status is passed between the ISR and task. */
\r
526 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
528 /* Structure used to control the characters being sent to the host. */
\r
529 static xTX_MESSAGE pxCharsForTx;
\r
531 /* Queue used to pass messages between the ISR and the task. */
\r
532 static xQueueHandle xUSBInterruptQueue;
\r
534 /* ISR entry has to be written in the asm file as we want a context switch
\r
535 to occur from within the ISR. See the port documentation on the FreeRTOS.org
\r
536 WEB site for more information. */
\r
537 extern void vUSBISREntry( void );
\r
539 /*-----------------------------------------------------------*/
\r
541 /* Macros to manipulate the control and status registers. These registers
\r
542 cannot be accessed using a direct read modify write operation outside of the
\r
543 ISR as some bits are left unchanged by writing with a 0, and some are left
\r
544 unchanged by writing with a 1. */
\r
546 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
548 #define usbCSR_SET_BIT( pulValueNow, ulBit ) \
\r
550 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
551 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
552 /* write has no effect. */ \
\r
553 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f; \
\r
555 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
556 /* so the write has no effect. */ \
\r
557 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf; \
\r
559 /* Set whichever bit we want set. */ \
\r
560 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( ulBit ); \
\r
563 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit ) \
\r
565 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
566 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
567 /* write has no effect. */ \
\r
568 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f; \
\r
570 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
571 /* so the write has no effect. */ \
\r
572 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf; \
\r
574 /* Clear whichever bit we want clear. */ \
\r
575 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( ~ulBit ); \
\r
578 /*-----------------------------------------------------------*/
\r
580 __arm void vUSB_ISR( void )
\r
582 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
583 static volatile unsigned portLONG ulNextMessage = 0;
\r
584 xISRStatus *pxMessage;
\r
585 unsigned portLONG ulTemp, ulRxBytes;
\r
587 /* Take the next message from the queue. Note that usbQUEUE_LENGTH *must*
\r
588 be all 1's, as in 0x01, 0x03, 0x07, etc. */
\r
589 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
592 /* Take a snapshot of the current USB state for processing at the task
\r
594 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
595 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
597 /* Clear the interrupts from the ICR register. The bus end interrupt is
\r
598 cleared separately as it does not appear in the mask register. */
\r
599 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
601 /* If there are bytes in the FIFO then we have to retrieve them here.
\r
602 Ideally this would be done at the task level. However we need to clear the
\r
603 RXSETUP interrupt before leaving the ISR, and this may cause the data in
\r
604 the FIFO to be overwritten. Also the DIR bit has to be changed before the
\r
605 RXSETUP bit is cleared (as per the SAM7 manual). */
\r
606 ulTemp = pxMessage->ulCSR0;
\r
608 /* Are there any bytes in the FIFO? */
\r
609 ulRxBytes = ulTemp >> 16;
\r
610 ulRxBytes &= usbRX_COUNT_MASK;
\r
612 /* With this minimal implementation we are only interested in receiving
\r
613 setup bytes on the control end point. */
\r
614 if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )
\r
616 /* Take off 1 for a zero based index. */
\r
617 while( ulRxBytes > 0 )
\r
620 pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
623 /* The direction must be changed first. */
\r
624 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );
\r
625 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
628 /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA
\r
629 registers to clear the interrupts in the CSR register. */
\r
630 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
631 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
633 /* Also clear the interrupts in the CSR1 register. */
\r
634 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
635 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
636 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
638 /* The message now contains the entire state and optional data from
\r
639 the USB interrupt. This can now be posted on the Rx queue ready for
\r
640 processing at the task level. */
\r
641 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
643 /* We may want to switch to the USB task, if this message has made
\r
644 it the highest priority task that is ready to execute. */
\r
645 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
647 /* Clear the AIC ready for the next interrupt. */
\r
648 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
650 /*-----------------------------------------------------------*/
\r
652 void vUSBDemoTask( void *pvParameters )
\r
654 xISRStatus *pxMessage;
\r
656 /* The parameters are not used in this task. */
\r
657 ( void ) pvParameters;
\r
659 /* Init USB device */
\r
660 portENTER_CRITICAL();
\r
661 vInitUSBInterface();
\r
662 portEXIT_CRITICAL();
\r
664 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
665 interrupt status then posts the information on this queue for processing
\r
666 at the task level. This simple demo implementation only processes
\r
667 a few interrupt sources. */
\r
670 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
672 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
674 /* Process end point 0 interrupt. */
\r
675 prvProcessEndPoint0Interrupt( pxMessage );
\r
678 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
680 /* Process an end of bus reset interrupt. */
\r
681 prvResetEndPoints();
\r
686 /* The ISR did not post any data for us to process on the queue, so
\r
687 just generate and send some sample data. */
\r
688 if( eDriverState == eREADY_TO_SEND )
\r
690 prvTransmitSampleValues();
\r
695 /*-----------------------------------------------------------*/
\r
697 static void prvTransmitSampleValues( void )
\r
699 unsigned portLONG ulStatus;
\r
700 static portLONG lState = usbXUP;
\r
702 /* Variables to hold dummy x, y and z joystick axis data. */
\r
703 static signed portCHAR x = 0, y = 0, z = 0;
\r
705 /* Generate some sample data in the x and y axis - draw a square. */
\r
708 case usbXUP : x += usbDATA_INC;
\r
709 if( x >= usbMAX_COORD )
\r
715 case usbXDOWN : x -= usbDATA_INC;
\r
716 if( x <= -usbMAX_COORD )
\r
722 case usbYUP : y += usbDATA_INC;
\r
723 if( y >= usbMAX_COORD )
\r
729 case usbYDOWN : y -= usbDATA_INC;
\r
730 if( y <= -usbMAX_COORD )
\r
737 /* Just make the z axis go up and down. */
\r
740 /* Can we place data in the fifo? */
\r
741 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
743 /* Write our sample data to the fifo. */
\r
744 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
745 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
746 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
748 /* Send the data. */
\r
749 portENTER_CRITICAL();
\r
751 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
752 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
753 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
755 portEXIT_CRITICAL();
\r
758 /*-----------------------------------------------------------*/
\r
760 static void prvUSBTransmitNull( void )
\r
762 unsigned portLONG ulStatus;
\r
764 /* Wait until the FIFO is free - even though we are not going to use it.
\r
765 THERE IS NO TIMEOUT HERE! */
\r
766 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
768 vTaskDelay( usbSHORTEST_DELAY );
\r
771 portENTER_CRITICAL();
\r
773 /* Set the length of data to send to equal the index of the next byte
\r
774 to send. This will prevent the ACK to this NULL packet causing any
\r
775 further data transmissions. */
\r
776 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
778 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
779 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
780 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
781 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
783 portEXIT_CRITICAL();
\r
785 /*-----------------------------------------------------------*/
\r
787 static void prvSendStall( void )
\r
789 unsigned portLONG ulStatus;
\r
791 portENTER_CRITICAL();
\r
793 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
794 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
795 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
796 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
798 portEXIT_CRITICAL();
\r
800 /*-----------------------------------------------------------*/
\r
802 static void prvResetEndPoints( void )
\r
804 unsigned portLONG ulTemp;
\r
806 eDriverState = eJUST_RESET;
\r
808 /* Reset all the end points. */
\r
809 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
810 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned portLONG ) 0x00;
\r
812 /* Enable data to be sent and received. */
\r
813 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
815 /* Repair the configuration end point. */
\r
816 portENTER_CRITICAL();
\r
818 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
819 usbCSR_SET_BIT( &ulTemp, ( ( unsigned portLONG ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
820 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
821 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT0 );
\r
823 portEXIT_CRITICAL();
\r
825 /*-----------------------------------------------------------*/
\r
827 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
829 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
831 /* We only expect to receive zero length data here as ACK's.
\r
832 Set the data pointer to the end of the current Tx packet to
\r
833 ensure we don't send out any more data. */
\r
834 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
837 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
839 /* We received a TX complete interrupt. What we do depends on
\r
840 what we sent to get this interrupt. */
\r
842 if( eDriverState == eJUST_GOT_CONFIG )
\r
844 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
845 are now at the end of the enumeration. */
\r
846 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
848 /* Read the end point for data transfer. */
\r
849 portENTER_CRITICAL();
\r
851 unsigned portLONG ulTemp;
\r
853 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
854 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
855 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
856 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT1 );
\r
858 portEXIT_CRITICAL();
\r
860 eDriverState = eREADY_TO_SEND;
\r
862 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
864 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
865 to the addressed state. */
\r
866 if( ulReceivedAddress != ( unsigned portLONG ) 0 )
\r
868 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
872 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
875 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
876 eDriverState = eNOTHING;
\r
880 /* The TXCOMP was not for any special type of transmission. See
\r
881 if there is any more data to send. */
\r
882 prvSendNextSegment();
\r
886 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
888 xUSB_REQUEST xRequest;
\r
889 unsigned portCHAR ucRequest;
\r
890 unsigned portLONG ulRxBytes;
\r
892 /* A data packet is available. */
\r
893 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
894 ulRxBytes &= usbRX_COUNT_MASK;
\r
896 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
898 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
900 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
901 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
903 /* NOT PORTABLE CODE! */
\r
904 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
905 xRequest.usValue <<= 8;
\r
906 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
908 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
909 xRequest.usIndex <<= 8;
\r
910 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
912 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
913 xRequest.usLength <<= 8;
\r
914 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
916 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
917 generate a zero based request selection. This is just done to
\r
918 break up the requests into subsections for clarity. The
\r
919 alternative would be to have more huge switch statement that would
\r
920 be difficult to optimise. */
\r
921 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
922 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
924 switch( ucRequest )
\r
926 case usbSTANDARD_DEVICE_REQUEST:
\r
927 /* Standard Device request */
\r
928 prvHandleStandardDeviceRequest( &xRequest );
\r
931 case usbSTANDARD_INTERFACE_REQUEST:
\r
932 /* Standard Interface request */
\r
933 prvHandleStandardInterfaceRequest( &xRequest );
\r
936 case usbSTANDARD_END_POINT_REQUEST:
\r
937 /* Standard Endpoint request */
\r
938 prvHandleStandardEndPointRequest( &xRequest );
\r
941 case usbCLASS_INTERFACE_REQUEST:
\r
942 /* Class Interface request */
\r
943 prvHandleClassInterfaceRequest( &xRequest );
\r
946 default: /* This is not something we want to respond to. */
\r
952 /*-----------------------------------------------------------*/
\r
954 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
956 /* The type is in the high byte. Return whatever has been requested. */
\r
957 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
959 case usbDESCRIPTOR_TYPE_DEVICE:
\r
960 prvSendControlData( ( unsigned portCHAR * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
963 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
964 prvSendControlData( ( unsigned portCHAR * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
967 case usbDESCRIPTOR_TYPE_STRING:
\r
969 /* The index to the string descriptor is the lower byte. */
\r
970 switch( pxRequest->usValue & 0xff )
\r
972 case usbLANGUAGE_STRING:
\r
973 prvSendControlData( ( unsigned portCHAR * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
976 case usbMANUFACTURER_STRING:
\r
977 prvSendControlData( ( unsigned portCHAR * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
980 case usbPRODUCT_STRING:
\r
981 prvSendControlData( ( unsigned portCHAR * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
984 case usbCONFIGURATION_STRING:
\r
985 prvSendControlData( ( unsigned portCHAR * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
988 case usbINTERFACE_STRING:
\r
989 prvSendControlData( ( unsigned portCHAR * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
993 /* Don't know what this string is. */
\r
1001 /* We are not responding to anything else. */
\r
1006 /*-----------------------------------------------------------*/
\r
1008 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
1010 unsigned portSHORT usStatus = 0;
\r
1012 switch( pxRequest->ucRequest )
\r
1014 case usbGET_STATUS_REQUEST:
\r
1015 /* Just send two byte dummy status. */
\r
1016 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1019 case usbGET_DESCRIPTOR_REQUEST:
\r
1020 /* Send device descriptor */
\r
1021 prvGetStandardDeviceDescriptor( pxRequest );
\r
1024 case usbGET_CONFIGURATION_REQUEST:
\r
1025 /* Send selected device configuration */
\r
1026 prvSendControlData( ( unsigned portCHAR * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
1029 case usbSET_FEATURE_REQUEST:
\r
1030 prvUSBTransmitNull();
\r
1033 case usbSET_ADDRESS_REQUEST:
\r
1035 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
1036 cannot actually move to the addressed state until we get a TXCOMP
\r
1037 interrupt from this NULL packet. Therefore we just remember the
\r
1038 address and set our state so we know we have received the address. */
\r
1039 prvUSBTransmitNull();
\r
1040 eDriverState = eJUST_GOT_ADDRESS;
\r
1041 ulReceivedAddress = ( unsigned portLONG ) pxRequest->usValue;
\r
1044 case usbSET_CONFIGURATION_REQUEST:
\r
1046 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
1047 we cannot actually move to the configured state until we get a
\r
1048 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
1049 config and set our state so we know we have received the go ahead. */
\r
1050 ucUSBConfig = ( unsigned portCHAR ) ( pxRequest->usValue & 0xff );
\r
1051 eDriverState = eJUST_GOT_CONFIG;
\r
1052 prvUSBTransmitNull();
\r
1057 /* We don't answer to anything else. */
\r
1062 /*-----------------------------------------------------------*/
\r
1064 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1066 switch( pxRequest->ucRequest )
\r
1068 case usbSET_IDLE_REQUEST:
\r
1069 prvUSBTransmitNull();
\r
1072 /* This minimal implementation ignores these. */
\r
1073 case usbGET_REPORT_REQUEST:
\r
1074 case usbGET_IDLE_REQUEST:
\r
1075 case usbGET_PROTOCOL_REQUEST:
\r
1076 case usbSET_REPORT_REQUEST:
\r
1077 case usbSET_PROTOCOL_REQUEST:
\r
1084 /*-----------------------------------------------------------*/
\r
1086 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
1088 switch( ( pxRequest->usValue & ( unsigned portSHORT ) 0xff00 ) >> 8 )
\r
1090 case usbHID_REPORT_DESCRIPTOR:
\r
1091 prvSendControlData( ( unsigned portCHAR * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
1096 /* Don't expect to send any others. */
\r
1101 /*-----------------------------------------------------------*/
\r
1103 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1105 unsigned portSHORT usStatus = 0;
\r
1107 switch( pxRequest->ucRequest )
\r
1109 case usbGET_STATUS_REQUEST:
\r
1110 /* Send dummy 2 bytes. */
\r
1111 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1114 case usbGET_DESCRIPTOR_REQUEST:
\r
1115 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1118 /* This minimal implementation does not respond to these. */
\r
1119 case usbGET_INTERFACE_REQUEST:
\r
1120 case usbSET_FEATURE_REQUEST:
\r
1121 case usbSET_INTERFACE_REQUEST:
\r
1128 /*-----------------------------------------------------------*/
\r
1130 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1132 switch( pxRequest->ucRequest )
\r
1134 /* This minimal implementation does not expect to respond to these. */
\r
1135 case usbGET_STATUS_REQUEST:
\r
1136 case usbCLEAR_FEATURE_REQUEST:
\r
1137 case usbSET_FEATURE_REQUEST:
\r
1144 /*-----------------------------------------------------------*/
\r
1146 static void vInitUSBInterface( void )
\r
1148 volatile unsigned portLONG ulTemp;
\r
1150 /* Create the queue used to communicate between the USB ISR and task. */
\r
1151 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
1153 /* Initialise a few state variables. */
\r
1154 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1155 ucUSBConfig = ( unsigned portCHAR ) 0;
\r
1156 eDriverState = eNOTHING;
\r
1158 /* HARDWARE SETUP */
\r
1160 /* Set the PLL USB Divider */
\r
1161 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1163 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1164 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1165 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1167 /* Setup the PIO for the USB pull up resistor. */
\r
1168 AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,AT91C_PIO_PA16);
\r
1170 /* Start without the pullup - this will get set at the end of this
\r
1172 AT91F_PIO_SetOutput( AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1174 /* When using the USB debugger the peripheral registers do not always get
\r
1175 set to the correct default values. To make sure set the relevant registers
\r
1177 AT91C_BASE_UDP->UDP_IDR = ( unsigned portLONG ) 0xffffffff;
\r
1178 AT91C_BASE_UDP->UDP_ICR = ( unsigned portLONG ) 0xffffffff;
\r
1179 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned portLONG ) 0x00;
\r
1180 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned portLONG ) 0x00;
\r
1181 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1182 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1184 /* Enable the transceiver. */
\r
1185 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1187 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1188 enumeration process progresses. */
\r
1189 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vUSBISREntry );
\r
1190 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_UDP );
\r
1192 /* Wait a short while before making our presence known. */
\r
1193 vTaskDelay( usbINIT_DELAY );
\r
1194 AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1196 /*-----------------------------------------------------------*/
\r
1198 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthToSend, portLONG lSendingDescriptor )
\r
1200 if( ( ( unsigned portLONG ) usRequestedLength < ulLengthToSend ) )
\r
1202 /* Cap the data length to that requested. */
\r
1203 ulLengthToSend = ( unsigned portSHORT ) usRequestedLength;
\r
1205 else if( ( ulLengthToSend < ( unsigned portLONG ) usRequestedLength ) && lSendingDescriptor )
\r
1207 /* We are sending a descriptor. If the descriptor is an exact
\r
1208 multiple of the FIFO length then it will have to be terminated
\r
1209 with a NULL packet. Set the state to indicate this if
\r
1211 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1213 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1217 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1218 BUFFER OVERFLOW PROTECTION HERE.
\r
1220 Copy the data to send into the buffer as we cannot send it all at once
\r
1221 (if it is greater than 8 bytes in length). */
\r
1222 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1224 /* Reinitialise the buffer index so we start sending from the start of
\r
1226 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1227 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1229 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1230 TXCOMP interrupts. */
\r
1231 prvSendNextSegment();
\r
1233 /*-----------------------------------------------------------*/
\r
1235 static void prvSendNextSegment( void )
\r
1237 volatile unsigned portLONG ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1239 /* Is there any data to send? */
\r
1240 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1242 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1244 /* We can only send 8 bytes to the fifo at a time. */
\r
1245 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1247 ulNextLength = usbFIFO_LENGTH;
\r
1251 ulNextLength = ulLengthLeftToSend;
\r
1254 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1256 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1258 vTaskDelay( usbSHORTEST_DELAY );
\r
1261 /* Write the data to the FIFO. */
\r
1262 while( ulNextLength > ( unsigned portLONG ) 0 )
\r
1264 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1267 pxCharsForTx.ulNextCharIndex++;
\r
1270 /* Start the transmission. */
\r
1271 portENTER_CRITICAL();
\r
1273 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1274 usbCSR_SET_BIT( &ulStatus, ( ( unsigned portLONG ) 0x10 ) );
\r
1275 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1277 portEXIT_CRITICAL();
\r
1281 /* There is no data to send. If we were sending a descriptor and the
\r
1282 descriptor was an exact multiple of the max packet size then we need
\r
1283 to send a null to terminate the transmission. */
\r
1284 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1286 prvUSBTransmitNull();
\r
1287 eDriverState = eNOTHING;
\r