2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 Sample interrupt driven USB device driver. This is a minimal implementation
\r
67 for demonstration only. Although functional, it is not a full and compliant
\r
70 The USB device enumerates as a simple 3 axis joystick, and once configured
\r
71 transmits 3 axis of data which can be viewed from the USB host machine.
\r
73 This file implements the USB interrupt service routine, and a demo FreeRTOS
\r
74 task. The interrupt service routine handles the USB hardware - taking a
\r
75 snapshot of the USB status at the point of the interrupt. The task receives
\r
76 the status information from the interrupt for processing at the task level.
\r
78 See the FreeRTOS.org WEB documentation for more information.
\r
84 + Descriptors that have a length that is an exact multiple of usbFIFO_LENGTH
\r
85 can now be transmitted. To this end an extra parameter has been
\r
86 added to the prvSendControlData() function, and the state
\r
87 eSENDING_EVEN_DESCRIPTOR has been introduced. Thanks to Scott Miller for
\r
88 assisting with this contribution.
\r
92 + Replaced the duplicated RX_DATA_BK0 in the interrupt mask with the
\r
96 /* Standard includes. */
\r
99 /* Demo board includes. */
\r
102 /* Scheduler includes. */
\r
103 #include "FreeRTOS.h"
\r
108 /* Descriptor type definitions. */
\r
109 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
110 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
111 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
113 /* USB request type definitions. */
\r
114 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
115 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
116 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
117 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
118 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
119 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
120 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
121 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
122 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
123 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
124 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
125 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
126 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
127 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
128 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
131 /* Misc USB definitions. */
\r
132 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
133 #define usbBUS_POWERED ( 0x80 )
\r
134 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
135 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
137 /* Index to the various string. */
\r
138 #define usbLANGUAGE_STRING ( 0 )
\r
139 #define usbMANUFACTURER_STRING ( 1 )
\r
140 #define usbPRODUCT_STRING ( 2 )
\r
141 #define usbCONFIGURATION_STRING ( 3 )
\r
142 #define usbINTERFACE_STRING ( 4 )
\r
144 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
145 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
147 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
148 #define usbREQUEST_INDEX ( 6 )
\r
149 #define usbVALUE_HIGH_BYTE ( 4 )
\r
150 #define usbVALUE_LOW_BYTE ( 5 )
\r
151 #define usbINDEX_HIGH_BYTE ( 2 )
\r
152 #define usbINDEX_LOW_BYTE ( 3 )
\r
153 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
154 #define usbLENGTH_LOW_BYTE ( 1 )
\r
156 /* Misc application definitions. */
\r
157 #define usbINTERRUPT_PRIORITY ( 3 )
\r
158 #define usbQUEUE_LENGTH ( 0x3 ) /* Must have all bits set! */
\r
159 #define usbFIFO_LENGTH ( ( unsigned long ) 8 )
\r
160 #define usbEND_POINT_0 ( 0 )
\r
161 #define usbEND_POINT_1 ( 1 )
\r
162 #define usbXUP ( 1 )
\r
163 #define usbXDOWN ( 2 )
\r
164 #define usbYUP ( 3 )
\r
165 #define usbYDOWN ( 4 )
\r
166 #define usbMAX_COORD ( 120 )
\r
167 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
168 #define usbRX_COUNT_MASK ( ( unsigned long ) 0x7ff )
\r
169 #define AT91C_UDP_STALLSENT AT91C_UDP_ISOERROR
\r
170 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
171 #define usbINIT_DELAY ( ( portTickType ) 500 / portTICK_RATE_MS )
\r
172 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
173 #define usbEND_POINT_RESET_MASK ( ( unsigned long ) 0x0f )
\r
174 #define usbDATA_INC ( ( char ) 5 )
\r
175 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned long ) 8 )
\r
177 /* Control request types. */
\r
178 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
179 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
180 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
181 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
183 /*-----------------------------------------------------------*/
\r
185 /* Structure used to take a snapshot of the USB status from within the ISR. */
\r
186 typedef struct X_ISR_STATUS
\r
188 unsigned long ulISR;
\r
189 unsigned long ulCSR0;
\r
190 unsigned char ucFifoData[ 8 ];
\r
193 /* Structure used to hold the received requests. */
\r
196 unsigned char ucReqType;
\r
197 unsigned char ucRequest;
\r
198 unsigned short usValue;
\r
199 unsigned short usIndex;
\r
200 unsigned short usLength;
\r
209 eSENDING_EVEN_DESCRIPTOR,
\r
213 /* Structure used to control the data being sent to the host. */
\r
216 unsigned char ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
217 unsigned long ulNextCharIndex;
\r
218 unsigned long ulTotalDataLength;
\r
221 /*-----------------------------------------------------------*/
\r
224 * The USB interrupt service routine. This takes a snapshot of the USB
\r
225 * device at the time of the interrupt, clears the interrupts, and posts
\r
226 * the data to the USB processing task.
\r
228 __arm void vUSB_ISR( void );
\r
231 * Called after the bus reset interrupt - this function readies all the
\r
232 * end points for communication.
\r
234 static void prvResetEndPoints( void );
\r
237 * Setup the USB hardware, install the interrupt service routine and
\r
238 * initialise all the state variables.
\r
240 static void vInitUSBInterface( void );
\r
243 * Decode and act upon an interrupt generated by the control end point.
\r
245 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
248 * For simplicity requests are separated into device, interface, class
\r
249 * interface and end point requests.
\r
251 * Decode and handle standard device requests originating on the control
\r
254 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
257 * For simplicity requests are separated into device, interface, class
\r
258 * interface and end point requests.
\r
260 * Decode and handle standard interface requests originating on the control
\r
263 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
266 * For simplicity requests are separated into device, interface, class
\r
267 * interface and end point requests.
\r
269 * Decode and handle standard end point requests originating on the control
\r
272 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
275 * For simplicity requests are separated into device, interface, class
\r
276 * interface and end point requests.
\r
278 * Decode and handle the class interface requests.
\r
280 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
283 * Setup the Tx buffer to send data in response to a control request.
\r
285 * The data to be transmitted is buffered, the state variables are updated,
\r
286 * then prvSendNextSegment() is called to start the transmission off. Once
\r
287 * the first segment has been sent the remaining segments are transmitted
\r
288 * in response to TXCOMP interrupts until the entire buffer has been
\r
291 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthLeftToSend, long lSendingDescriptor );
\r
294 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
296 * If there is data to be transmitted then send the next segment. A segment
\r
297 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
298 * point by the descriptor). The final segment may be less than 8 bytes if
\r
299 * the total data length was not an exact multiple of 8.
\r
301 static void prvSendNextSegment( void );
\r
304 * A stall condition is forced each time the host makes a request that is not
\r
305 * supported by this minimal implementation.
\r
307 * A stall is forced by setting the appropriate bit in the end points control
\r
308 * and status register.
\r
310 static void prvSendStall( void );
\r
313 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
314 * of certain events from the host.
\r
316 static void prvUSBTransmitNull( void );
\r
319 * When the host requests a descriptor this function is called to determine
\r
320 * which descriptor is being requested and start its transmission.
\r
322 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
325 * This demo USB device enumerates as a simple 3 axis joystick. Once
\r
326 * configured this function is periodically called to generate some sample
\r
329 * The x and y axis are made to move in a square. The z axis is made to
\r
330 * repeatedly increment up to its maximum.
\r
332 static void prvTransmitSampleValues( void );
\r
335 * The created task to handle the USB demo functionality.
\r
337 void vUSBDemoTask( void *pvParameters );
\r
339 /*-----------------------------------------------------------*/
\r
342 - DESCRIPTOR DEFINITIONS -
\r
345 /* String descriptors used during the enumeration process.
\r
346 These take the form:
\r
349 Length of descriptor,
\r
354 const char pxLanguageStringDescriptor[] =
\r
357 usbDESCRIPTOR_TYPE_STRING,
\r
361 const char pxManufacturerStringDescriptor[] =
\r
364 usbDESCRIPTOR_TYPE_STRING,
\r
376 const char pxProductStringDescriptor[] =
\r
379 usbDESCRIPTOR_TYPE_STRING,
\r
404 const char pxConfigurationStringDescriptor[] =
\r
407 usbDESCRIPTOR_TYPE_STRING,
\r
429 const char pxInterfaceStringDescriptor[] =
\r
432 usbDESCRIPTOR_TYPE_STRING,
\r
450 /* Enumeration descriptors. */
\r
451 const char pxReportDescriptor[] =
\r
453 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
454 0x09, 0x04, /* USAGE (Joystick) */
\r
455 0xa1, 0x01, /* COLLECTION (Application) */
\r
456 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
457 0x09, 0x01, /* USAGE (Pointer) */
\r
458 0xa1, 0x00, /* COLLECTION (Physical) */
\r
459 0x09, 0x30, /* USAGE (X) */
\r
460 0x09, 0x31, /* USAGE (Y) */
\r
461 0x09, 0x32, /* USAGE (Z) */
\r
462 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
463 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
464 0x75, 0x08, /* REPORT_SIZE (8) */
\r
465 0x95, 0x03, /* REPORT_COUNT (3) */
\r
466 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
467 0xc0, /* END_COLLECTION */
\r
468 0xc0 /* END_COLLECTION */
\r
471 const char pxDeviceDescriptor[] =
\r
473 /* Device descriptor */
\r
474 0x12, /* bLength */
\r
475 0x01, /* bDescriptorType */
\r
476 0x10, 0x01, /* bcdUSBL */
\r
477 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
478 0x00, /* bDeviceSubclass: */
\r
479 0x00, /* bDeviceProtocol: */
\r
480 0x08, /* bMaxPacketSize0 */
\r
481 0xFF, 0xFF, /* idVendorL */
\r
482 0x01, 0x00, /* idProductL */
\r
483 0x00, 0x01, /* bcdDeviceL */
\r
484 usbMANUFACTURER_STRING, /* iManufacturer */
\r
485 usbPRODUCT_STRING, /* iProduct */
\r
486 0x00, /* SerialNumber */
\r
487 0x01 /* bNumConfigs */
\r
490 const char pxConfigDescriptor[] = {
\r
491 /* Configuration 1 descriptor */
\r
492 0x09, /* CbLength */
\r
493 0x02, /* CbDescriptorType */
\r
494 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
495 0x01, /* CbNumInterfaces */
\r
496 0x01, /* CbConfigurationValue */
\r
497 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
498 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
499 0x32, /* CMaxPower: 100mA */
\r
501 /* Joystick Interface Descriptor Requirement */
\r
502 0x09, /* bLength */
\r
503 0x04, /* bDescriptorType */
\r
504 0x00, /* bInterfaceNumber */
\r
505 0x00, /* bAlternateSetting */
\r
506 0x01, /* bNumEndpoints */
\r
507 0x03, /* bInterfaceClass: HID code */
\r
508 0x00, /* bInterfaceSubclass */
\r
509 0x00, /* bInterfaceProtocol */
\r
510 usbINTERFACE_STRING,/* iInterface */
\r
512 /* HID Descriptor */
\r
513 0x09, /* bLength */
\r
514 0x21, /* bDescriptor type: HID Descriptor Type */
\r
515 0x00, 0x01, /* bcdHID */
\r
516 0x00, /* bCountryCode */
\r
517 0x01, /* bNumDescriptors */
\r
518 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
519 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
521 /* Endpoint 1 descriptor */
\r
522 0x07, /* bLength */
\r
523 0x05, /* bDescriptorType */
\r
524 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
525 0x03, /* bmAttributes INT */
\r
526 0x03, 0x00, /* wMaxPacketSize: 3 bytes (x, y, z) */
\r
527 0x0A /* bInterval */
\r
530 /*-----------------------------------------------------------*/
\r
532 /* File scope state variables. */
\r
533 static unsigned char ucUSBConfig = ( unsigned char ) 0;
\r
534 static unsigned long ulReceivedAddress = ( unsigned long ) 0;
\r
535 static eDRIVER_STATE eDriverState = eNOTHING;
\r
537 /* Array in which the USB interrupt status is passed between the ISR and task. */
\r
538 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
540 /* Structure used to control the characters being sent to the host. */
\r
541 static xTX_MESSAGE pxCharsForTx;
\r
543 /* Queue used to pass messages between the ISR and the task. */
\r
544 static xQueueHandle xUSBInterruptQueue;
\r
546 /* ISR entry has to be written in the asm file as we want a context switch
\r
547 to occur from within the ISR. See the port documentation on the FreeRTOS.org
\r
548 WEB site for more information. */
\r
549 extern void vUSBISREntry( void );
\r
551 /*-----------------------------------------------------------*/
\r
553 /* Macros to manipulate the control and status registers. These registers
\r
554 cannot be accessed using a direct read modify write operation outside of the
\r
555 ISR as some bits are left unchanged by writing with a 0, and some are left
\r
556 unchanged by writing with a 1. */
\r
558 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
560 #define usbCSR_SET_BIT( pulValueNow, ulBit ) \
\r
562 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
563 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
564 /* write has no effect. */ \
\r
565 ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f; \
\r
567 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
568 /* so the write has no effect. */ \
\r
569 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf; \
\r
571 /* Set whichever bit we want set. */ \
\r
572 ( * ( ( unsigned long * ) pulValueNow ) ) |= ( ulBit ); \
\r
575 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit ) \
\r
577 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
578 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
579 /* write has no effect. */ \
\r
580 ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f; \
\r
582 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
583 /* so the write has no effect. */ \
\r
584 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf; \
\r
586 /* Clear whichever bit we want clear. */ \
\r
587 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( ~ulBit ); \
\r
590 /*-----------------------------------------------------------*/
\r
592 __arm void vUSB_ISR( void )
\r
594 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
595 static volatile unsigned long ulNextMessage = 0;
\r
596 xISRStatus *pxMessage;
\r
597 unsigned long ulTemp, ulRxBytes;
\r
599 /* Take the next message from the queue. Note that usbQUEUE_LENGTH *must*
\r
600 be all 1's, as in 0x01, 0x03, 0x07, etc. */
\r
601 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
604 /* Take a snapshot of the current USB state for processing at the task
\r
606 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
607 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
609 /* Clear the interrupts from the ICR register. The bus end interrupt is
\r
610 cleared separately as it does not appear in the mask register. */
\r
611 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
613 /* If there are bytes in the FIFO then we have to retrieve them here.
\r
614 Ideally this would be done at the task level. However we need to clear the
\r
615 RXSETUP interrupt before leaving the ISR, and this may cause the data in
\r
616 the FIFO to be overwritten. Also the DIR bit has to be changed before the
\r
617 RXSETUP bit is cleared (as per the SAM7 manual). */
\r
618 ulTemp = pxMessage->ulCSR0;
\r
620 /* Are there any bytes in the FIFO? */
\r
621 ulRxBytes = ulTemp >> 16;
\r
622 ulRxBytes &= usbRX_COUNT_MASK;
\r
624 /* With this minimal implementation we are only interested in receiving
\r
625 setup bytes on the control end point. */
\r
626 if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )
\r
628 /* Take off 1 for a zero based index. */
\r
629 while( ulRxBytes > 0 )
\r
632 pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
635 /* The direction must be changed first. */
\r
636 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );
\r
637 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
640 /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA
\r
641 registers to clear the interrupts in the CSR register. */
\r
642 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
643 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
645 /* Also clear the interrupts in the CSR1 register. */
\r
646 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
647 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
648 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
650 /* The message now contains the entire state and optional data from
\r
651 the USB interrupt. This can now be posted on the Rx queue ready for
\r
652 processing at the task level. */
\r
653 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
655 /* We may want to switch to the USB task, if this message has made
\r
656 it the highest priority task that is ready to execute. */
\r
657 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
659 /* Clear the AIC ready for the next interrupt. */
\r
660 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
662 /*-----------------------------------------------------------*/
\r
664 void vUSBDemoTask( void *pvParameters )
\r
666 xISRStatus *pxMessage;
\r
668 /* The parameters are not used in this task. */
\r
669 ( void ) pvParameters;
\r
671 /* Init USB device */
\r
672 portENTER_CRITICAL();
\r
673 vInitUSBInterface();
\r
674 portEXIT_CRITICAL();
\r
676 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
677 interrupt status then posts the information on this queue for processing
\r
678 at the task level. This simple demo implementation only processes
\r
679 a few interrupt sources. */
\r
682 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
684 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
686 /* Process end point 0 interrupt. */
\r
687 prvProcessEndPoint0Interrupt( pxMessage );
\r
690 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
692 /* Process an end of bus reset interrupt. */
\r
693 prvResetEndPoints();
\r
698 /* The ISR did not post any data for us to process on the queue, so
\r
699 just generate and send some sample data. */
\r
700 if( eDriverState == eREADY_TO_SEND )
\r
702 prvTransmitSampleValues();
\r
707 /*-----------------------------------------------------------*/
\r
709 static void prvTransmitSampleValues( void )
\r
711 unsigned long ulStatus;
\r
712 static long lState = usbXUP;
\r
714 /* Variables to hold dummy x, y and z joystick axis data. */
\r
715 static signed char x = 0, y = 0, z = 0;
\r
717 /* Generate some sample data in the x and y axis - draw a square. */
\r
720 case usbXUP : x += usbDATA_INC;
\r
721 if( x >= usbMAX_COORD )
\r
727 case usbXDOWN : x -= usbDATA_INC;
\r
728 if( x <= -usbMAX_COORD )
\r
734 case usbYUP : y += usbDATA_INC;
\r
735 if( y >= usbMAX_COORD )
\r
741 case usbYDOWN : y -= usbDATA_INC;
\r
742 if( y <= -usbMAX_COORD )
\r
749 /* Just make the z axis go up and down. */
\r
752 /* Can we place data in the fifo? */
\r
753 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
755 /* Write our sample data to the fifo. */
\r
756 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
757 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
758 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
760 /* Send the data. */
\r
761 portENTER_CRITICAL();
\r
763 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
764 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
765 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
767 portEXIT_CRITICAL();
\r
770 /*-----------------------------------------------------------*/
\r
772 static void prvUSBTransmitNull( void )
\r
774 unsigned long ulStatus;
\r
776 /* Wait until the FIFO is free - even though we are not going to use it.
\r
777 THERE IS NO TIMEOUT HERE! */
\r
778 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
780 vTaskDelay( usbSHORTEST_DELAY );
\r
783 portENTER_CRITICAL();
\r
785 /* Set the length of data to send to equal the index of the next byte
\r
786 to send. This will prevent the ACK to this NULL packet causing any
\r
787 further data transmissions. */
\r
788 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
790 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
791 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
792 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
793 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
795 portEXIT_CRITICAL();
\r
797 /*-----------------------------------------------------------*/
\r
799 static void prvSendStall( void )
\r
801 unsigned long ulStatus;
\r
803 portENTER_CRITICAL();
\r
805 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
806 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
807 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
808 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
810 portEXIT_CRITICAL();
\r
812 /*-----------------------------------------------------------*/
\r
814 static void prvResetEndPoints( void )
\r
816 unsigned long ulTemp;
\r
818 eDriverState = eJUST_RESET;
\r
820 /* Reset all the end points. */
\r
821 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
822 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned long ) 0x00;
\r
824 /* Enable data to be sent and received. */
\r
825 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
827 /* Repair the configuration end point. */
\r
828 portENTER_CRITICAL();
\r
830 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
831 usbCSR_SET_BIT( &ulTemp, ( ( unsigned long ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
832 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
833 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT0 );
\r
835 portEXIT_CRITICAL();
\r
837 /*-----------------------------------------------------------*/
\r
839 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
841 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
843 /* We only expect to receive zero length data here as ACK's.
\r
844 Set the data pointer to the end of the current Tx packet to
\r
845 ensure we don't send out any more data. */
\r
846 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
849 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
851 /* We received a TX complete interrupt. What we do depends on
\r
852 what we sent to get this interrupt. */
\r
854 if( eDriverState == eJUST_GOT_CONFIG )
\r
856 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
857 are now at the end of the enumeration. */
\r
858 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
860 /* Read the end point for data transfer. */
\r
861 portENTER_CRITICAL();
\r
863 unsigned long ulTemp;
\r
865 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
866 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
867 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
868 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT1 );
\r
870 portEXIT_CRITICAL();
\r
872 eDriverState = eREADY_TO_SEND;
\r
874 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
876 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
877 to the addressed state. */
\r
878 if( ulReceivedAddress != ( unsigned long ) 0 )
\r
880 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
884 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
887 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
888 eDriverState = eNOTHING;
\r
892 /* The TXCOMP was not for any special type of transmission. See
\r
893 if there is any more data to send. */
\r
894 prvSendNextSegment();
\r
898 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
900 xUSB_REQUEST xRequest;
\r
901 unsigned char ucRequest;
\r
902 unsigned long ulRxBytes;
\r
904 /* A data packet is available. */
\r
905 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
906 ulRxBytes &= usbRX_COUNT_MASK;
\r
908 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
910 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
912 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
913 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
915 /* NOT PORTABLE CODE! */
\r
916 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
917 xRequest.usValue <<= 8;
\r
918 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
920 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
921 xRequest.usIndex <<= 8;
\r
922 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
924 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
925 xRequest.usLength <<= 8;
\r
926 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
928 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
929 generate a zero based request selection. This is just done to
\r
930 break up the requests into subsections for clarity. The
\r
931 alternative would be to have more huge switch statement that would
\r
932 be difficult to optimise. */
\r
933 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
934 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
936 switch( ucRequest )
\r
938 case usbSTANDARD_DEVICE_REQUEST:
\r
939 /* Standard Device request */
\r
940 prvHandleStandardDeviceRequest( &xRequest );
\r
943 case usbSTANDARD_INTERFACE_REQUEST:
\r
944 /* Standard Interface request */
\r
945 prvHandleStandardInterfaceRequest( &xRequest );
\r
948 case usbSTANDARD_END_POINT_REQUEST:
\r
949 /* Standard Endpoint request */
\r
950 prvHandleStandardEndPointRequest( &xRequest );
\r
953 case usbCLASS_INTERFACE_REQUEST:
\r
954 /* Class Interface request */
\r
955 prvHandleClassInterfaceRequest( &xRequest );
\r
958 default: /* This is not something we want to respond to. */
\r
964 /*-----------------------------------------------------------*/
\r
966 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
968 /* The type is in the high byte. Return whatever has been requested. */
\r
969 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
971 case usbDESCRIPTOR_TYPE_DEVICE:
\r
972 prvSendControlData( ( unsigned char * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
975 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
976 prvSendControlData( ( unsigned char * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
979 case usbDESCRIPTOR_TYPE_STRING:
\r
981 /* The index to the string descriptor is the lower byte. */
\r
982 switch( pxRequest->usValue & 0xff )
\r
984 case usbLANGUAGE_STRING:
\r
985 prvSendControlData( ( unsigned char * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
988 case usbMANUFACTURER_STRING:
\r
989 prvSendControlData( ( unsigned char * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
992 case usbPRODUCT_STRING:
\r
993 prvSendControlData( ( unsigned char * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
996 case usbCONFIGURATION_STRING:
\r
997 prvSendControlData( ( unsigned char * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
1000 case usbINTERFACE_STRING:
\r
1001 prvSendControlData( ( unsigned char * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
1005 /* Don't know what this string is. */
\r
1013 /* We are not responding to anything else. */
\r
1018 /*-----------------------------------------------------------*/
\r
1020 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
1022 unsigned short usStatus = 0;
\r
1024 switch( pxRequest->ucRequest )
\r
1026 case usbGET_STATUS_REQUEST:
\r
1027 /* Just send two byte dummy status. */
\r
1028 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1031 case usbGET_DESCRIPTOR_REQUEST:
\r
1032 /* Send device descriptor */
\r
1033 prvGetStandardDeviceDescriptor( pxRequest );
\r
1036 case usbGET_CONFIGURATION_REQUEST:
\r
1037 /* Send selected device configuration */
\r
1038 prvSendControlData( ( unsigned char * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
1041 case usbSET_FEATURE_REQUEST:
\r
1042 prvUSBTransmitNull();
\r
1045 case usbSET_ADDRESS_REQUEST:
\r
1047 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
1048 cannot actually move to the addressed state until we get a TXCOMP
\r
1049 interrupt from this NULL packet. Therefore we just remember the
\r
1050 address and set our state so we know we have received the address. */
\r
1051 prvUSBTransmitNull();
\r
1052 eDriverState = eJUST_GOT_ADDRESS;
\r
1053 ulReceivedAddress = ( unsigned long ) pxRequest->usValue;
\r
1056 case usbSET_CONFIGURATION_REQUEST:
\r
1058 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
1059 we cannot actually move to the configured state until we get a
\r
1060 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
1061 config and set our state so we know we have received the go ahead. */
\r
1062 ucUSBConfig = ( unsigned char ) ( pxRequest->usValue & 0xff );
\r
1063 eDriverState = eJUST_GOT_CONFIG;
\r
1064 prvUSBTransmitNull();
\r
1069 /* We don't answer to anything else. */
\r
1074 /*-----------------------------------------------------------*/
\r
1076 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1078 switch( pxRequest->ucRequest )
\r
1080 case usbSET_IDLE_REQUEST:
\r
1081 prvUSBTransmitNull();
\r
1084 /* This minimal implementation ignores these. */
\r
1085 case usbGET_REPORT_REQUEST:
\r
1086 case usbGET_IDLE_REQUEST:
\r
1087 case usbGET_PROTOCOL_REQUEST:
\r
1088 case usbSET_REPORT_REQUEST:
\r
1089 case usbSET_PROTOCOL_REQUEST:
\r
1096 /*-----------------------------------------------------------*/
\r
1098 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
1100 switch( ( pxRequest->usValue & ( unsigned short ) 0xff00 ) >> 8 )
\r
1102 case usbHID_REPORT_DESCRIPTOR:
\r
1103 prvSendControlData( ( unsigned char * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
1108 /* Don't expect to send any others. */
\r
1113 /*-----------------------------------------------------------*/
\r
1115 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1117 unsigned short usStatus = 0;
\r
1119 switch( pxRequest->ucRequest )
\r
1121 case usbGET_STATUS_REQUEST:
\r
1122 /* Send dummy 2 bytes. */
\r
1123 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1126 case usbGET_DESCRIPTOR_REQUEST:
\r
1127 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1130 /* This minimal implementation does not respond to these. */
\r
1131 case usbGET_INTERFACE_REQUEST:
\r
1132 case usbSET_FEATURE_REQUEST:
\r
1133 case usbSET_INTERFACE_REQUEST:
\r
1140 /*-----------------------------------------------------------*/
\r
1142 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1144 switch( pxRequest->ucRequest )
\r
1146 /* This minimal implementation does not expect to respond to these. */
\r
1147 case usbGET_STATUS_REQUEST:
\r
1148 case usbCLEAR_FEATURE_REQUEST:
\r
1149 case usbSET_FEATURE_REQUEST:
\r
1156 /*-----------------------------------------------------------*/
\r
1158 static void vInitUSBInterface( void )
\r
1160 volatile unsigned long ulTemp;
\r
1162 /* Create the queue used to communicate between the USB ISR and task. */
\r
1163 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
1165 /* Initialise a few state variables. */
\r
1166 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1167 ucUSBConfig = ( unsigned char ) 0;
\r
1168 eDriverState = eNOTHING;
\r
1170 /* HARDWARE SETUP */
\r
1172 /* Set the PLL USB Divider */
\r
1173 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1175 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1176 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1177 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1179 /* Setup the PIO for the USB pull up resistor. */
\r
1180 AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,AT91C_PIO_PA16);
\r
1182 /* Start without the pullup - this will get set at the end of this
\r
1184 AT91F_PIO_SetOutput( AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1186 /* When using the USB debugger the peripheral registers do not always get
\r
1187 set to the correct default values. To make sure set the relevant registers
\r
1189 AT91C_BASE_UDP->UDP_IDR = ( unsigned long ) 0xffffffff;
\r
1190 AT91C_BASE_UDP->UDP_ICR = ( unsigned long ) 0xffffffff;
\r
1191 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned long ) 0x00;
\r
1192 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned long ) 0x00;
\r
1193 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1194 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1196 /* Enable the transceiver. */
\r
1197 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1199 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1200 enumeration process progresses. */
\r
1201 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vUSBISREntry );
\r
1202 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_UDP );
\r
1204 /* Wait a short while before making our presence known. */
\r
1205 vTaskDelay( usbINIT_DELAY );
\r
1206 AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1208 /*-----------------------------------------------------------*/
\r
1210 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthToSend, long lSendingDescriptor )
\r
1212 if( ( ( unsigned long ) usRequestedLength < ulLengthToSend ) )
\r
1214 /* Cap the data length to that requested. */
\r
1215 ulLengthToSend = ( unsigned short ) usRequestedLength;
\r
1217 else if( ( ulLengthToSend < ( unsigned long ) usRequestedLength ) && lSendingDescriptor )
\r
1219 /* We are sending a descriptor. If the descriptor is an exact
\r
1220 multiple of the FIFO length then it will have to be terminated
\r
1221 with a NULL packet. Set the state to indicate this if
\r
1223 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1225 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1229 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1230 BUFFER OVERFLOW PROTECTION HERE.
\r
1232 Copy the data to send into the buffer as we cannot send it all at once
\r
1233 (if it is greater than 8 bytes in length). */
\r
1234 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1236 /* Reinitialise the buffer index so we start sending from the start of
\r
1238 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1239 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1241 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1242 TXCOMP interrupts. */
\r
1243 prvSendNextSegment();
\r
1245 /*-----------------------------------------------------------*/
\r
1247 static void prvSendNextSegment( void )
\r
1249 volatile unsigned long ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1251 /* Is there any data to send? */
\r
1252 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1254 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1256 /* We can only send 8 bytes to the fifo at a time. */
\r
1257 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1259 ulNextLength = usbFIFO_LENGTH;
\r
1263 ulNextLength = ulLengthLeftToSend;
\r
1266 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1268 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1270 vTaskDelay( usbSHORTEST_DELAY );
\r
1273 /* Write the data to the FIFO. */
\r
1274 while( ulNextLength > ( unsigned long ) 0 )
\r
1276 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1279 pxCharsForTx.ulNextCharIndex++;
\r
1282 /* Start the transmission. */
\r
1283 portENTER_CRITICAL();
\r
1285 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1286 usbCSR_SET_BIT( &ulStatus, ( ( unsigned long ) 0x10 ) );
\r
1287 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1289 portEXIT_CRITICAL();
\r
1293 /* There is no data to send. If we were sending a descriptor and the
\r
1294 descriptor was an exact multiple of the max packet size then we need
\r
1295 to send a null to terminate the transmission. */
\r
1296 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1298 prvUSBTransmitNull();
\r
1299 eDriverState = eNOTHING;
\r