2 FreeRTOS.org V5.3.0 - 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 Sample interrupt driven USB device driver. This is a minimal implementation
\r
54 for demonstration only. Although functional, it is not a full and compliant
\r
57 The USB device enumerates as a simple 3 axis joystick, and once configured
\r
58 transmits 3 axis of data which can be viewed from the USB host machine.
\r
60 This file implements the USB interrupt service routine, and a demo FreeRTOS
\r
61 task. The interrupt service routine handles the USB hardware - taking a
\r
62 snapshot of the USB status at the point of the interrupt. The task receives
\r
63 the status information from the interrupt for processing at the task level.
\r
65 See the FreeRTOS.org WEB documentation for more information.
\r
71 + Descriptors that have a length that is an exact multiple of usbFIFO_LENGTH
\r
72 can now be transmitted. To this end an extra parameter has been
\r
73 added to the prvSendControlData() function, and the state
\r
74 eSENDING_EVEN_DESCRIPTOR has been introduced. Thanks to Scott Miller for
\r
75 assisting with this contribution.
\r
79 + Replaced the duplicated RX_DATA_BK0 in the interrupt mask with the
\r
83 /* Standard includes. */
\r
86 /* Demo board includes. */
\r
89 /* Scheduler includes. */
\r
90 #include "FreeRTOS.h"
\r
95 /* Descriptor type definitions. */
\r
96 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
97 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
98 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
100 /* USB request type definitions. */
\r
101 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
102 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
103 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
104 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
105 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
106 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
107 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
108 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
109 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
110 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
111 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
112 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
113 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
114 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
115 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
118 /* Misc USB definitions. */
\r
119 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
120 #define usbBUS_POWERED ( 0x80 )
\r
121 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
122 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
124 /* Index to the various string. */
\r
125 #define usbLANGUAGE_STRING ( 0 )
\r
126 #define usbMANUFACTURER_STRING ( 1 )
\r
127 #define usbPRODUCT_STRING ( 2 )
\r
128 #define usbCONFIGURATION_STRING ( 3 )
\r
129 #define usbINTERFACE_STRING ( 4 )
\r
131 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
132 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
134 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
135 #define usbREQUEST_INDEX ( 6 )
\r
136 #define usbVALUE_HIGH_BYTE ( 4 )
\r
137 #define usbVALUE_LOW_BYTE ( 5 )
\r
138 #define usbINDEX_HIGH_BYTE ( 2 )
\r
139 #define usbINDEX_LOW_BYTE ( 3 )
\r
140 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
141 #define usbLENGTH_LOW_BYTE ( 1 )
\r
143 /* Misc application definitions. */
\r
144 #define usbINTERRUPT_PRIORITY ( 3 )
\r
145 #define usbQUEUE_LENGTH ( 0x3 ) /* Must have all bits set! */
\r
146 #define usbFIFO_LENGTH ( ( unsigned portLONG ) 8 )
\r
147 #define usbEND_POINT_0 ( 0 )
\r
148 #define usbEND_POINT_1 ( 1 )
\r
149 #define usbXUP ( 1 )
\r
150 #define usbXDOWN ( 2 )
\r
151 #define usbYUP ( 3 )
\r
152 #define usbYDOWN ( 4 )
\r
153 #define usbMAX_COORD ( 120 )
\r
154 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
155 #define usbRX_COUNT_MASK ( ( unsigned portLONG ) 0x7ff )
\r
156 #define AT91C_UDP_STALLSENT AT91C_UDP_ISOERROR
\r
157 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
158 #define usbINIT_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
159 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
160 #define usbEND_POINT_RESET_MASK ( ( unsigned portLONG ) 0x0f )
\r
161 #define usbDATA_INC ( ( portCHAR ) 5 )
\r
162 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned portLONG ) 8 )
\r
164 /* Control request types. */
\r
165 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
166 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
167 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
168 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
170 /*-----------------------------------------------------------*/
\r
172 /* Structure used to take a snapshot of the USB status from within the ISR. */
\r
173 typedef struct X_ISR_STATUS
\r
175 unsigned portLONG ulISR;
\r
176 unsigned portLONG ulCSR0;
\r
177 unsigned portCHAR ucFifoData[ 8 ];
\r
180 /* Structure used to hold the received requests. */
\r
183 unsigned portCHAR ucReqType;
\r
184 unsigned portCHAR ucRequest;
\r
185 unsigned portSHORT usValue;
\r
186 unsigned portSHORT usIndex;
\r
187 unsigned portSHORT usLength;
\r
196 eSENDING_EVEN_DESCRIPTOR,
\r
200 /* Structure used to control the data being sent to the host. */
\r
203 unsigned portCHAR ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
204 unsigned portLONG ulNextCharIndex;
\r
205 unsigned portLONG ulTotalDataLength;
\r
208 /*-----------------------------------------------------------*/
\r
211 * The USB interrupt service routine. This takes a snapshot of the USB
\r
212 * device at the time of the interrupt, clears the interrupts, and posts
\r
213 * the data to the USB processing task.
\r
215 __arm void vUSB_ISR( void );
\r
218 * Called after the bus reset interrupt - this function readies all the
\r
219 * end points for communication.
\r
221 static void prvResetEndPoints( void );
\r
224 * Setup the USB hardware, install the interrupt service routine and
\r
225 * initialise all the state variables.
\r
227 static void vInitUSBInterface( void );
\r
230 * Decode and act upon an interrupt generated by the control end point.
\r
232 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
235 * For simplicity requests are separated into device, interface, class
\r
236 * interface and end point requests.
\r
238 * Decode and handle standard device requests originating on the control
\r
241 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
244 * For simplicity requests are separated into device, interface, class
\r
245 * interface and end point requests.
\r
247 * Decode and handle standard interface requests originating on the control
\r
250 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
253 * For simplicity requests are separated into device, interface, class
\r
254 * interface and end point requests.
\r
256 * Decode and handle standard end point requests originating on the control
\r
259 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
262 * For simplicity requests are separated into device, interface, class
\r
263 * interface and end point requests.
\r
265 * Decode and handle the class interface requests.
\r
267 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
270 * Setup the Tx buffer to send data in response to a control request.
\r
272 * The data to be transmitted is buffered, the state variables are updated,
\r
273 * then prvSendNextSegment() is called to start the transmission off. Once
\r
274 * the first segment has been sent the remaining segments are transmitted
\r
275 * in response to TXCOMP interrupts until the entire buffer has been
\r
278 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthLeftToSend, portLONG lSendingDescriptor );
\r
281 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
283 * If there is data to be transmitted then send the next segment. A segment
\r
284 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
285 * point by the descriptor). The final segment may be less than 8 bytes if
\r
286 * the total data length was not an exact multiple of 8.
\r
288 static void prvSendNextSegment( void );
\r
291 * A stall condition is forced each time the host makes a request that is not
\r
292 * supported by this minimal implementation.
\r
294 * A stall is forced by setting the appropriate bit in the end points control
\r
295 * and status register.
\r
297 static void prvSendStall( void );
\r
300 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
301 * of certain events from the host.
\r
303 static void prvUSBTransmitNull( void );
\r
306 * When the host requests a descriptor this function is called to determine
\r
307 * which descriptor is being requested and start its transmission.
\r
309 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
312 * This demo USB device enumerates as a simple 3 axis joystick. Once
\r
313 * configured this function is periodically called to generate some sample
\r
316 * The x and y axis are made to move in a square. The z axis is made to
\r
317 * repeatedly increment up to its maximum.
\r
319 static void prvTransmitSampleValues( void );
\r
322 * The created task to handle the USB demo functionality.
\r
324 void vUSBDemoTask( void *pvParameters );
\r
326 /*-----------------------------------------------------------*/
\r
329 - DESCRIPTOR DEFINITIONS -
\r
332 /* String descriptors used during the enumeration process.
\r
333 These take the form:
\r
336 Length of descriptor,
\r
341 const portCHAR pxLanguageStringDescriptor[] =
\r
344 usbDESCRIPTOR_TYPE_STRING,
\r
348 const portCHAR pxManufacturerStringDescriptor[] =
\r
351 usbDESCRIPTOR_TYPE_STRING,
\r
363 const portCHAR pxProductStringDescriptor[] =
\r
366 usbDESCRIPTOR_TYPE_STRING,
\r
391 const portCHAR pxConfigurationStringDescriptor[] =
\r
394 usbDESCRIPTOR_TYPE_STRING,
\r
416 const portCHAR pxInterfaceStringDescriptor[] =
\r
419 usbDESCRIPTOR_TYPE_STRING,
\r
437 /* Enumeration descriptors. */
\r
438 const portCHAR pxReportDescriptor[] =
\r
440 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
441 0x09, 0x04, /* USAGE (Joystick) */
\r
442 0xa1, 0x01, /* COLLECTION (Application) */
\r
443 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
444 0x09, 0x01, /* USAGE (Pointer) */
\r
445 0xa1, 0x00, /* COLLECTION (Physical) */
\r
446 0x09, 0x30, /* USAGE (X) */
\r
447 0x09, 0x31, /* USAGE (Y) */
\r
448 0x09, 0x32, /* USAGE (Z) */
\r
449 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
450 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
451 0x75, 0x08, /* REPORT_SIZE (8) */
\r
452 0x95, 0x03, /* REPORT_COUNT (3) */
\r
453 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
454 0xc0, /* END_COLLECTION */
\r
455 0xc0 /* END_COLLECTION */
\r
458 const char pxDeviceDescriptor[] =
\r
460 /* Device descriptor */
\r
461 0x12, /* bLength */
\r
462 0x01, /* bDescriptorType */
\r
463 0x10, 0x01, /* bcdUSBL */
\r
464 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
465 0x00, /* bDeviceSubclass: */
\r
466 0x00, /* bDeviceProtocol: */
\r
467 0x08, /* bMaxPacketSize0 */
\r
468 0xFF, 0xFF, /* idVendorL */
\r
469 0x01, 0x00, /* idProductL */
\r
470 0x00, 0x01, /* bcdDeviceL */
\r
471 usbMANUFACTURER_STRING, /* iManufacturer */
\r
472 usbPRODUCT_STRING, /* iProduct */
\r
473 0x00, /* SerialNumber */
\r
474 0x01 /* bNumConfigs */
\r
477 const char pxConfigDescriptor[] = {
\r
478 /* Configuration 1 descriptor */
\r
479 0x09, /* CbLength */
\r
480 0x02, /* CbDescriptorType */
\r
481 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
482 0x01, /* CbNumInterfaces */
\r
483 0x01, /* CbConfigurationValue */
\r
484 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
485 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
486 0x32, /* CMaxPower: 100mA */
\r
488 /* Joystick Interface Descriptor Requirement */
\r
489 0x09, /* bLength */
\r
490 0x04, /* bDescriptorType */
\r
491 0x00, /* bInterfaceNumber */
\r
492 0x00, /* bAlternateSetting */
\r
493 0x01, /* bNumEndpoints */
\r
494 0x03, /* bInterfaceClass: HID code */
\r
495 0x00, /* bInterfaceSubclass */
\r
496 0x00, /* bInterfaceProtocol */
\r
497 usbINTERFACE_STRING,/* iInterface */
\r
499 /* HID Descriptor */
\r
500 0x09, /* bLength */
\r
501 0x21, /* bDescriptor type: HID Descriptor Type */
\r
502 0x00, 0x01, /* bcdHID */
\r
503 0x00, /* bCountryCode */
\r
504 0x01, /* bNumDescriptors */
\r
505 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
506 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
508 /* Endpoint 1 descriptor */
\r
509 0x07, /* bLength */
\r
510 0x05, /* bDescriptorType */
\r
511 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
512 0x03, /* bmAttributes INT */
\r
513 0x03, 0x00, /* wMaxPacketSize: 3 bytes (x, y, z) */
\r
514 0x0A /* bInterval */
\r
517 /*-----------------------------------------------------------*/
\r
519 /* File scope state variables. */
\r
520 static unsigned portCHAR ucUSBConfig = ( unsigned portCHAR ) 0;
\r
521 static unsigned portLONG ulReceivedAddress = ( unsigned portLONG ) 0;
\r
522 static eDRIVER_STATE eDriverState = eNOTHING;
\r
524 /* Array in which the USB interrupt status is passed between the ISR and task. */
\r
525 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
527 /* Structure used to control the characters being sent to the host. */
\r
528 static xTX_MESSAGE pxCharsForTx;
\r
530 /* Queue used to pass messages between the ISR and the task. */
\r
531 static xQueueHandle xUSBInterruptQueue;
\r
533 /* ISR entry has to be written in the asm file as we want a context switch
\r
534 to occur from within the ISR. See the port documentation on the FreeRTOS.org
\r
535 WEB site for more information. */
\r
536 extern void vUSBISREntry( void );
\r
538 /*-----------------------------------------------------------*/
\r
540 /* Macros to manipulate the control and status registers. These registers
\r
541 cannot be accessed using a direct read modify write operation outside of the
\r
542 ISR as some bits are left unchanged by writing with a 0, and some are left
\r
543 unchanged by writing with a 1. */
\r
545 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
547 #define usbCSR_SET_BIT( pulValueNow, ulBit ) \
\r
549 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
550 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
551 /* write has no effect. */ \
\r
552 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f; \
\r
554 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
555 /* so the write has no effect. */ \
\r
556 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf; \
\r
558 /* Set whichever bit we want set. */ \
\r
559 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( ulBit ); \
\r
562 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit ) \
\r
564 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
565 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
566 /* write has no effect. */ \
\r
567 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f; \
\r
569 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
570 /* so the write has no effect. */ \
\r
571 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf; \
\r
573 /* Clear whichever bit we want clear. */ \
\r
574 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( ~ulBit ); \
\r
577 /*-----------------------------------------------------------*/
\r
579 __arm void vUSB_ISR( void )
\r
581 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
582 static volatile unsigned portLONG ulNextMessage = 0;
\r
583 xISRStatus *pxMessage;
\r
584 unsigned portLONG ulTemp, ulRxBytes;
\r
586 /* Take the next message from the queue. Note that usbQUEUE_LENGTH *must*
\r
587 be all 1's, as in 0x01, 0x03, 0x07, etc. */
\r
588 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
591 /* Take a snapshot of the current USB state for processing at the task
\r
593 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
594 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
596 /* Clear the interrupts from the ICR register. The bus end interrupt is
\r
597 cleared separately as it does not appear in the mask register. */
\r
598 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
600 /* If there are bytes in the FIFO then we have to retrieve them here.
\r
601 Ideally this would be done at the task level. However we need to clear the
\r
602 RXSETUP interrupt before leaving the ISR, and this may cause the data in
\r
603 the FIFO to be overwritten. Also the DIR bit has to be changed before the
\r
604 RXSETUP bit is cleared (as per the SAM7 manual). */
\r
605 ulTemp = pxMessage->ulCSR0;
\r
607 /* Are there any bytes in the FIFO? */
\r
608 ulRxBytes = ulTemp >> 16;
\r
609 ulRxBytes &= usbRX_COUNT_MASK;
\r
611 /* With this minimal implementation we are only interested in receiving
\r
612 setup bytes on the control end point. */
\r
613 if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )
\r
615 /* Take off 1 for a zero based index. */
\r
616 while( ulRxBytes > 0 )
\r
619 pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
622 /* The direction must be changed first. */
\r
623 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );
\r
624 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
627 /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA
\r
628 registers to clear the interrupts in the CSR register. */
\r
629 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
630 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
632 /* Also clear the interrupts in the CSR1 register. */
\r
633 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
634 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
635 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
637 /* The message now contains the entire state and optional data from
\r
638 the USB interrupt. This can now be posted on the Rx queue ready for
\r
639 processing at the task level. */
\r
640 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
642 /* We may want to switch to the USB task, if this message has made
\r
643 it the highest priority task that is ready to execute. */
\r
644 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
646 /* Clear the AIC ready for the next interrupt. */
\r
647 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
649 /*-----------------------------------------------------------*/
\r
651 void vUSBDemoTask( void *pvParameters )
\r
653 xISRStatus *pxMessage;
\r
655 /* The parameters are not used in this task. */
\r
656 ( void ) pvParameters;
\r
658 /* Init USB device */
\r
659 portENTER_CRITICAL();
\r
660 vInitUSBInterface();
\r
661 portEXIT_CRITICAL();
\r
663 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
664 interrupt status then posts the information on this queue for processing
\r
665 at the task level. This simple demo implementation only processes
\r
666 a few interrupt sources. */
\r
669 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
671 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
673 /* Process end point 0 interrupt. */
\r
674 prvProcessEndPoint0Interrupt( pxMessage );
\r
677 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
679 /* Process an end of bus reset interrupt. */
\r
680 prvResetEndPoints();
\r
685 /* The ISR did not post any data for us to process on the queue, so
\r
686 just generate and send some sample data. */
\r
687 if( eDriverState == eREADY_TO_SEND )
\r
689 prvTransmitSampleValues();
\r
694 /*-----------------------------------------------------------*/
\r
696 static void prvTransmitSampleValues( void )
\r
698 unsigned portLONG ulStatus;
\r
699 static portLONG lState = usbXUP;
\r
701 /* Variables to hold dummy x, y and z joystick axis data. */
\r
702 static signed portCHAR x = 0, y = 0, z = 0;
\r
704 /* Generate some sample data in the x and y axis - draw a square. */
\r
707 case usbXUP : x += usbDATA_INC;
\r
708 if( x >= usbMAX_COORD )
\r
714 case usbXDOWN : x -= usbDATA_INC;
\r
715 if( x <= -usbMAX_COORD )
\r
721 case usbYUP : y += usbDATA_INC;
\r
722 if( y >= usbMAX_COORD )
\r
728 case usbYDOWN : y -= usbDATA_INC;
\r
729 if( y <= -usbMAX_COORD )
\r
736 /* Just make the z axis go up and down. */
\r
739 /* Can we place data in the fifo? */
\r
740 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
742 /* Write our sample data to the fifo. */
\r
743 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
744 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
745 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
747 /* Send the data. */
\r
748 portENTER_CRITICAL();
\r
750 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
751 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
752 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
754 portEXIT_CRITICAL();
\r
757 /*-----------------------------------------------------------*/
\r
759 static void prvUSBTransmitNull( void )
\r
761 unsigned portLONG ulStatus;
\r
763 /* Wait until the FIFO is free - even though we are not going to use it.
\r
764 THERE IS NO TIMEOUT HERE! */
\r
765 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
767 vTaskDelay( usbSHORTEST_DELAY );
\r
770 portENTER_CRITICAL();
\r
772 /* Set the length of data to send to equal the index of the next byte
\r
773 to send. This will prevent the ACK to this NULL packet causing any
\r
774 further data transmissions. */
\r
775 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
777 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
778 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
779 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
780 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
782 portEXIT_CRITICAL();
\r
784 /*-----------------------------------------------------------*/
\r
786 static void prvSendStall( void )
\r
788 unsigned portLONG ulStatus;
\r
790 portENTER_CRITICAL();
\r
792 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
793 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
794 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
795 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
797 portEXIT_CRITICAL();
\r
799 /*-----------------------------------------------------------*/
\r
801 static void prvResetEndPoints( void )
\r
803 unsigned portLONG ulTemp;
\r
805 eDriverState = eJUST_RESET;
\r
807 /* Reset all the end points. */
\r
808 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
809 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned portLONG ) 0x00;
\r
811 /* Enable data to be sent and received. */
\r
812 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
814 /* Repair the configuration end point. */
\r
815 portENTER_CRITICAL();
\r
817 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
818 usbCSR_SET_BIT( &ulTemp, ( ( unsigned portLONG ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
819 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
820 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT0 );
\r
822 portEXIT_CRITICAL();
\r
824 /*-----------------------------------------------------------*/
\r
826 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
828 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
830 /* We only expect to receive zero length data here as ACK's.
\r
831 Set the data pointer to the end of the current Tx packet to
\r
832 ensure we don't send out any more data. */
\r
833 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
836 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
838 /* We received a TX complete interrupt. What we do depends on
\r
839 what we sent to get this interrupt. */
\r
841 if( eDriverState == eJUST_GOT_CONFIG )
\r
843 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
844 are now at the end of the enumeration. */
\r
845 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
847 /* Read the end point for data transfer. */
\r
848 portENTER_CRITICAL();
\r
850 unsigned portLONG ulTemp;
\r
852 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
853 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
854 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
855 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT1 );
\r
857 portEXIT_CRITICAL();
\r
859 eDriverState = eREADY_TO_SEND;
\r
861 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
863 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
864 to the addressed state. */
\r
865 if( ulReceivedAddress != ( unsigned portLONG ) 0 )
\r
867 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
871 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
874 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
875 eDriverState = eNOTHING;
\r
879 /* The TXCOMP was not for any special type of transmission. See
\r
880 if there is any more data to send. */
\r
881 prvSendNextSegment();
\r
885 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
887 xUSB_REQUEST xRequest;
\r
888 unsigned portCHAR ucRequest;
\r
889 unsigned portLONG ulRxBytes;
\r
891 /* A data packet is available. */
\r
892 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
893 ulRxBytes &= usbRX_COUNT_MASK;
\r
895 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
897 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
899 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
900 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
902 /* NOT PORTABLE CODE! */
\r
903 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
904 xRequest.usValue <<= 8;
\r
905 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
907 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
908 xRequest.usIndex <<= 8;
\r
909 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
911 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
912 xRequest.usLength <<= 8;
\r
913 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
915 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
916 generate a zero based request selection. This is just done to
\r
917 break up the requests into subsections for clarity. The
\r
918 alternative would be to have more huge switch statement that would
\r
919 be difficult to optimise. */
\r
920 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
921 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
923 switch( ucRequest )
\r
925 case usbSTANDARD_DEVICE_REQUEST:
\r
926 /* Standard Device request */
\r
927 prvHandleStandardDeviceRequest( &xRequest );
\r
930 case usbSTANDARD_INTERFACE_REQUEST:
\r
931 /* Standard Interface request */
\r
932 prvHandleStandardInterfaceRequest( &xRequest );
\r
935 case usbSTANDARD_END_POINT_REQUEST:
\r
936 /* Standard Endpoint request */
\r
937 prvHandleStandardEndPointRequest( &xRequest );
\r
940 case usbCLASS_INTERFACE_REQUEST:
\r
941 /* Class Interface request */
\r
942 prvHandleClassInterfaceRequest( &xRequest );
\r
945 default: /* This is not something we want to respond to. */
\r
951 /*-----------------------------------------------------------*/
\r
953 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
955 /* The type is in the high byte. Return whatever has been requested. */
\r
956 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
958 case usbDESCRIPTOR_TYPE_DEVICE:
\r
959 prvSendControlData( ( unsigned portCHAR * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
962 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
963 prvSendControlData( ( unsigned portCHAR * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
966 case usbDESCRIPTOR_TYPE_STRING:
\r
968 /* The index to the string descriptor is the lower byte. */
\r
969 switch( pxRequest->usValue & 0xff )
\r
971 case usbLANGUAGE_STRING:
\r
972 prvSendControlData( ( unsigned portCHAR * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
975 case usbMANUFACTURER_STRING:
\r
976 prvSendControlData( ( unsigned portCHAR * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
979 case usbPRODUCT_STRING:
\r
980 prvSendControlData( ( unsigned portCHAR * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
983 case usbCONFIGURATION_STRING:
\r
984 prvSendControlData( ( unsigned portCHAR * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
987 case usbINTERFACE_STRING:
\r
988 prvSendControlData( ( unsigned portCHAR * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
992 /* Don't know what this string is. */
\r
1000 /* We are not responding to anything else. */
\r
1005 /*-----------------------------------------------------------*/
\r
1007 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
1009 unsigned portSHORT usStatus = 0;
\r
1011 switch( pxRequest->ucRequest )
\r
1013 case usbGET_STATUS_REQUEST:
\r
1014 /* Just send two byte dummy status. */
\r
1015 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1018 case usbGET_DESCRIPTOR_REQUEST:
\r
1019 /* Send device descriptor */
\r
1020 prvGetStandardDeviceDescriptor( pxRequest );
\r
1023 case usbGET_CONFIGURATION_REQUEST:
\r
1024 /* Send selected device configuration */
\r
1025 prvSendControlData( ( unsigned portCHAR * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
1028 case usbSET_FEATURE_REQUEST:
\r
1029 prvUSBTransmitNull();
\r
1032 case usbSET_ADDRESS_REQUEST:
\r
1034 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
1035 cannot actually move to the addressed state until we get a TXCOMP
\r
1036 interrupt from this NULL packet. Therefore we just remember the
\r
1037 address and set our state so we know we have received the address. */
\r
1038 prvUSBTransmitNull();
\r
1039 eDriverState = eJUST_GOT_ADDRESS;
\r
1040 ulReceivedAddress = ( unsigned portLONG ) pxRequest->usValue;
\r
1043 case usbSET_CONFIGURATION_REQUEST:
\r
1045 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
1046 we cannot actually move to the configured state until we get a
\r
1047 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
1048 config and set our state so we know we have received the go ahead. */
\r
1049 ucUSBConfig = ( unsigned portCHAR ) ( pxRequest->usValue & 0xff );
\r
1050 eDriverState = eJUST_GOT_CONFIG;
\r
1051 prvUSBTransmitNull();
\r
1056 /* We don't answer to anything else. */
\r
1061 /*-----------------------------------------------------------*/
\r
1063 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1065 switch( pxRequest->ucRequest )
\r
1067 case usbSET_IDLE_REQUEST:
\r
1068 prvUSBTransmitNull();
\r
1071 /* This minimal implementation ignores these. */
\r
1072 case usbGET_REPORT_REQUEST:
\r
1073 case usbGET_IDLE_REQUEST:
\r
1074 case usbGET_PROTOCOL_REQUEST:
\r
1075 case usbSET_REPORT_REQUEST:
\r
1076 case usbSET_PROTOCOL_REQUEST:
\r
1083 /*-----------------------------------------------------------*/
\r
1085 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
1087 switch( ( pxRequest->usValue & ( unsigned portSHORT ) 0xff00 ) >> 8 )
\r
1089 case usbHID_REPORT_DESCRIPTOR:
\r
1090 prvSendControlData( ( unsigned portCHAR * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
1095 /* Don't expect to send any others. */
\r
1100 /*-----------------------------------------------------------*/
\r
1102 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1104 unsigned portSHORT usStatus = 0;
\r
1106 switch( pxRequest->ucRequest )
\r
1108 case usbGET_STATUS_REQUEST:
\r
1109 /* Send dummy 2 bytes. */
\r
1110 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1113 case usbGET_DESCRIPTOR_REQUEST:
\r
1114 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1117 /* This minimal implementation does not respond to these. */
\r
1118 case usbGET_INTERFACE_REQUEST:
\r
1119 case usbSET_FEATURE_REQUEST:
\r
1120 case usbSET_INTERFACE_REQUEST:
\r
1127 /*-----------------------------------------------------------*/
\r
1129 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1131 switch( pxRequest->ucRequest )
\r
1133 /* This minimal implementation does not expect to respond to these. */
\r
1134 case usbGET_STATUS_REQUEST:
\r
1135 case usbCLEAR_FEATURE_REQUEST:
\r
1136 case usbSET_FEATURE_REQUEST:
\r
1143 /*-----------------------------------------------------------*/
\r
1145 static void vInitUSBInterface( void )
\r
1147 volatile unsigned portLONG ulTemp;
\r
1149 /* Create the queue used to communicate between the USB ISR and task. */
\r
1150 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
1152 /* Initialise a few state variables. */
\r
1153 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1154 ucUSBConfig = ( unsigned portCHAR ) 0;
\r
1155 eDriverState = eNOTHING;
\r
1157 /* HARDWARE SETUP */
\r
1159 /* Set the PLL USB Divider */
\r
1160 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1162 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1163 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1164 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1166 /* Setup the PIO for the USB pull up resistor. */
\r
1167 AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,AT91C_PIO_PA16);
\r
1169 /* Start without the pullup - this will get set at the end of this
\r
1171 AT91F_PIO_SetOutput( AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1173 /* When using the USB debugger the peripheral registers do not always get
\r
1174 set to the correct default values. To make sure set the relevant registers
\r
1176 AT91C_BASE_UDP->UDP_IDR = ( unsigned portLONG ) 0xffffffff;
\r
1177 AT91C_BASE_UDP->UDP_ICR = ( unsigned portLONG ) 0xffffffff;
\r
1178 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned portLONG ) 0x00;
\r
1179 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned portLONG ) 0x00;
\r
1180 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1181 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1183 /* Enable the transceiver. */
\r
1184 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1186 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1187 enumeration process progresses. */
\r
1188 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vUSBISREntry );
\r
1189 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_UDP );
\r
1191 /* Wait a short while before making our presence known. */
\r
1192 vTaskDelay( usbINIT_DELAY );
\r
1193 AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1195 /*-----------------------------------------------------------*/
\r
1197 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthToSend, portLONG lSendingDescriptor )
\r
1199 if( ( ( unsigned portLONG ) usRequestedLength < ulLengthToSend ) )
\r
1201 /* Cap the data length to that requested. */
\r
1202 ulLengthToSend = ( unsigned portSHORT ) usRequestedLength;
\r
1204 else if( ( ulLengthToSend < ( unsigned portLONG ) usRequestedLength ) && lSendingDescriptor )
\r
1206 /* We are sending a descriptor. If the descriptor is an exact
\r
1207 multiple of the FIFO length then it will have to be terminated
\r
1208 with a NULL packet. Set the state to indicate this if
\r
1210 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1212 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1216 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1217 BUFFER OVERFLOW PROTECTION HERE.
\r
1219 Copy the data to send into the buffer as we cannot send it all at once
\r
1220 (if it is greater than 8 bytes in length). */
\r
1221 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1223 /* Reinitialise the buffer index so we start sending from the start of
\r
1225 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1226 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1228 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1229 TXCOMP interrupts. */
\r
1230 prvSendNextSegment();
\r
1232 /*-----------------------------------------------------------*/
\r
1234 static void prvSendNextSegment( void )
\r
1236 volatile unsigned portLONG ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1238 /* Is there any data to send? */
\r
1239 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1241 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1243 /* We can only send 8 bytes to the fifo at a time. */
\r
1244 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1246 ulNextLength = usbFIFO_LENGTH;
\r
1250 ulNextLength = ulLengthLeftToSend;
\r
1253 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1255 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1257 vTaskDelay( usbSHORTEST_DELAY );
\r
1260 /* Write the data to the FIFO. */
\r
1261 while( ulNextLength > ( unsigned portLONG ) 0 )
\r
1263 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1266 pxCharsForTx.ulNextCharIndex++;
\r
1269 /* Start the transmission. */
\r
1270 portENTER_CRITICAL();
\r
1272 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1273 usbCSR_SET_BIT( &ulStatus, ( ( unsigned portLONG ) 0x10 ) );
\r
1274 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1276 portEXIT_CRITICAL();
\r
1280 /* There is no data to send. If we were sending a descriptor and the
\r
1281 descriptor was an exact multiple of the max packet size then we need
\r
1282 to send a null to terminate the transmission. */
\r
1283 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1285 prvUSBTransmitNull();
\r
1286 eDriverState = eNOTHING;
\r