2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 Sample interrupt driven USB device driver. This is a minimal implementation
\r
98 for demonstration only. Although functional, it is not a full and compliant
\r
101 The USB device enumerates as a simple 3 axis joystick, and once configured
\r
102 transmits 3 axis of data which can be viewed from the USB host machine.
\r
104 This file implements the USB interrupt service routine, and a demo FreeRTOS
\r
105 task. The interrupt service routine handles the USB hardware - taking a
\r
106 snapshot of the USB status at the point of the interrupt. The task receives
\r
107 the status information from the interrupt for processing at the task level.
\r
109 See the FreeRTOS.org WEB documentation for more information.
\r
113 Changes from V2.5.5
\r
115 + Descriptors that have a length that is an exact multiple of usbFIFO_LENGTH
\r
116 can now be transmitted. To this end an extra parameter has been
\r
117 added to the prvSendControlData() function, and the state
\r
118 eSENDING_EVEN_DESCRIPTOR has been introduced. Thanks to Scott Miller for
\r
119 assisting with this contribution.
\r
121 Changes from V2.6.0
\r
123 + Replaced the duplicated RX_DATA_BK0 in the interrupt mask with the
\r
127 /* Standard includes. */
\r
128 #include <string.h>
\r
130 /* Demo board includes. */
\r
133 /* Scheduler includes. */
\r
134 #include "FreeRTOS.h"
\r
139 /* Descriptor type definitions. */
\r
140 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
141 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
142 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
144 /* USB request type definitions. */
\r
145 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
146 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
147 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
148 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
149 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
150 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
151 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
152 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
153 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
154 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
155 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
156 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
157 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
158 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
159 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
162 /* Misc USB definitions. */
\r
163 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
164 #define usbBUS_POWERED ( 0x80 )
\r
165 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
166 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
168 /* Index to the various string. */
\r
169 #define usbLANGUAGE_STRING ( 0 )
\r
170 #define usbMANUFACTURER_STRING ( 1 )
\r
171 #define usbPRODUCT_STRING ( 2 )
\r
172 #define usbCONFIGURATION_STRING ( 3 )
\r
173 #define usbINTERFACE_STRING ( 4 )
\r
175 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
176 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
178 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
179 #define usbREQUEST_INDEX ( 6 )
\r
180 #define usbVALUE_HIGH_BYTE ( 4 )
\r
181 #define usbVALUE_LOW_BYTE ( 5 )
\r
182 #define usbINDEX_HIGH_BYTE ( 2 )
\r
183 #define usbINDEX_LOW_BYTE ( 3 )
\r
184 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
185 #define usbLENGTH_LOW_BYTE ( 1 )
\r
187 /* Misc application definitions. */
\r
188 #define usbINTERRUPT_PRIORITY ( 3 )
\r
189 #define usbQUEUE_LENGTH ( 0x3 ) /* Must have all bits set! */
\r
190 #define usbFIFO_LENGTH ( ( unsigned long ) 8 )
\r
191 #define usbEND_POINT_0 ( 0 )
\r
192 #define usbEND_POINT_1 ( 1 )
\r
193 #define usbXUP ( 1 )
\r
194 #define usbXDOWN ( 2 )
\r
195 #define usbYUP ( 3 )
\r
196 #define usbYDOWN ( 4 )
\r
197 #define usbMAX_COORD ( 120 )
\r
198 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
199 #define usbRX_COUNT_MASK ( ( unsigned long ) 0x7ff )
\r
200 #define AT91C_UDP_STALLSENT AT91C_UDP_ISOERROR
\r
201 #define usbSHORTEST_DELAY ( ( TickType_t ) 1 )
\r
202 #define usbINIT_DELAY ( ( TickType_t ) 500 / portTICK_PERIOD_MS )
\r
203 #define usbSHORT_DELAY ( ( TickType_t ) 50 / portTICK_PERIOD_MS )
\r
204 #define usbEND_POINT_RESET_MASK ( ( unsigned long ) 0x0f )
\r
205 #define usbDATA_INC ( ( char ) 5 )
\r
206 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned long ) 8 )
\r
208 /* Control request types. */
\r
209 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
210 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
211 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
212 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
214 /*-----------------------------------------------------------*/
\r
216 /* Structure used to take a snapshot of the USB status from within the ISR. */
\r
217 typedef struct X_ISR_STATUS
\r
219 unsigned long ulISR;
\r
220 unsigned long ulCSR0;
\r
221 unsigned char ucFifoData[ 8 ];
\r
224 /* Structure used to hold the received requests. */
\r
227 unsigned char ucReqType;
\r
228 unsigned char ucRequest;
\r
229 unsigned short usValue;
\r
230 unsigned short usIndex;
\r
231 unsigned short usLength;
\r
240 eSENDING_EVEN_DESCRIPTOR,
\r
244 /* Structure used to control the data being sent to the host. */
\r
247 unsigned char ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
248 unsigned long ulNextCharIndex;
\r
249 unsigned long ulTotalDataLength;
\r
252 /*-----------------------------------------------------------*/
\r
255 * The USB interrupt service routine. This takes a snapshot of the USB
\r
256 * device at the time of the interrupt, clears the interrupts, and posts
\r
257 * the data to the USB processing task.
\r
259 __arm void vUSB_ISR( void );
\r
262 * Called after the bus reset interrupt - this function readies all the
\r
263 * end points for communication.
\r
265 static void prvResetEndPoints( void );
\r
268 * Setup the USB hardware, install the interrupt service routine and
\r
269 * initialise all the state variables.
\r
271 static void vInitUSBInterface( void );
\r
274 * Decode and act upon an interrupt generated by the control end point.
\r
276 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
279 * For simplicity requests are separated into device, interface, class
\r
280 * interface and end point requests.
\r
282 * Decode and handle standard device requests originating on the control
\r
285 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
288 * For simplicity requests are separated into device, interface, class
\r
289 * interface and end point requests.
\r
291 * Decode and handle standard interface requests originating on the control
\r
294 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
297 * For simplicity requests are separated into device, interface, class
\r
298 * interface and end point requests.
\r
300 * Decode and handle standard end point requests originating on the control
\r
303 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
306 * For simplicity requests are separated into device, interface, class
\r
307 * interface and end point requests.
\r
309 * Decode and handle the class interface requests.
\r
311 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
314 * Setup the Tx buffer to send data in response to a control request.
\r
316 * The data to be transmitted is buffered, the state variables are updated,
\r
317 * then prvSendNextSegment() is called to start the transmission off. Once
\r
318 * the first segment has been sent the remaining segments are transmitted
\r
319 * in response to TXCOMP interrupts until the entire buffer has been
\r
322 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthLeftToSend, long lSendingDescriptor );
\r
325 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
327 * If there is data to be transmitted then send the next segment. A segment
\r
328 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
329 * point by the descriptor). The final segment may be less than 8 bytes if
\r
330 * the total data length was not an exact multiple of 8.
\r
332 static void prvSendNextSegment( void );
\r
335 * A stall condition is forced each time the host makes a request that is not
\r
336 * supported by this minimal implementation.
\r
338 * A stall is forced by setting the appropriate bit in the end points control
\r
339 * and status register.
\r
341 static void prvSendStall( void );
\r
344 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
345 * of certain events from the host.
\r
347 static void prvUSBTransmitNull( void );
\r
350 * When the host requests a descriptor this function is called to determine
\r
351 * which descriptor is being requested and start its transmission.
\r
353 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
356 * This demo USB device enumerates as a simple 3 axis joystick. Once
\r
357 * configured this function is periodically called to generate some sample
\r
360 * The x and y axis are made to move in a square. The z axis is made to
\r
361 * repeatedly increment up to its maximum.
\r
363 static void prvTransmitSampleValues( void );
\r
366 * The created task to handle the USB demo functionality.
\r
368 void vUSBDemoTask( void *pvParameters );
\r
370 /*-----------------------------------------------------------*/
\r
373 - DESCRIPTOR DEFINITIONS -
\r
376 /* String descriptors used during the enumeration process.
\r
377 These take the form:
\r
380 Length of descriptor,
\r
385 const char pxLanguageStringDescriptor[] =
\r
388 usbDESCRIPTOR_TYPE_STRING,
\r
392 const char pxManufacturerStringDescriptor[] =
\r
395 usbDESCRIPTOR_TYPE_STRING,
\r
407 const char pxProductStringDescriptor[] =
\r
410 usbDESCRIPTOR_TYPE_STRING,
\r
435 const char pxConfigurationStringDescriptor[] =
\r
438 usbDESCRIPTOR_TYPE_STRING,
\r
460 const char pxInterfaceStringDescriptor[] =
\r
463 usbDESCRIPTOR_TYPE_STRING,
\r
481 /* Enumeration descriptors. */
\r
482 const char pxReportDescriptor[] =
\r
484 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
485 0x09, 0x04, /* USAGE (Joystick) */
\r
486 0xa1, 0x01, /* COLLECTION (Application) */
\r
487 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
488 0x09, 0x01, /* USAGE (Pointer) */
\r
489 0xa1, 0x00, /* COLLECTION (Physical) */
\r
490 0x09, 0x30, /* USAGE (X) */
\r
491 0x09, 0x31, /* USAGE (Y) */
\r
492 0x09, 0x32, /* USAGE (Z) */
\r
493 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
494 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
495 0x75, 0x08, /* REPORT_SIZE (8) */
\r
496 0x95, 0x03, /* REPORT_COUNT (3) */
\r
497 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
498 0xc0, /* END_COLLECTION */
\r
499 0xc0 /* END_COLLECTION */
\r
502 const char pxDeviceDescriptor[] =
\r
504 /* Device descriptor */
\r
505 0x12, /* bLength */
\r
506 0x01, /* bDescriptorType */
\r
507 0x10, 0x01, /* bcdUSBL */
\r
508 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
509 0x00, /* bDeviceSubclass: */
\r
510 0x00, /* bDeviceProtocol: */
\r
511 0x08, /* bMaxPacketSize0 */
\r
512 0xFF, 0xFF, /* idVendorL */
\r
513 0x01, 0x00, /* idProductL */
\r
514 0x00, 0x01, /* bcdDeviceL */
\r
515 usbMANUFACTURER_STRING, /* iManufacturer */
\r
516 usbPRODUCT_STRING, /* iProduct */
\r
517 0x00, /* SerialNumber */
\r
518 0x01 /* bNumConfigs */
\r
521 const char pxConfigDescriptor[] = {
\r
522 /* Configuration 1 descriptor */
\r
523 0x09, /* CbLength */
\r
524 0x02, /* CbDescriptorType */
\r
525 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
526 0x01, /* CbNumInterfaces */
\r
527 0x01, /* CbConfigurationValue */
\r
528 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
529 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
530 0x32, /* CMaxPower: 100mA */
\r
532 /* Joystick Interface Descriptor Requirement */
\r
533 0x09, /* bLength */
\r
534 0x04, /* bDescriptorType */
\r
535 0x00, /* bInterfaceNumber */
\r
536 0x00, /* bAlternateSetting */
\r
537 0x01, /* bNumEndpoints */
\r
538 0x03, /* bInterfaceClass: HID code */
\r
539 0x00, /* bInterfaceSubclass */
\r
540 0x00, /* bInterfaceProtocol */
\r
541 usbINTERFACE_STRING,/* iInterface */
\r
543 /* HID Descriptor */
\r
544 0x09, /* bLength */
\r
545 0x21, /* bDescriptor type: HID Descriptor Type */
\r
546 0x00, 0x01, /* bcdHID */
\r
547 0x00, /* bCountryCode */
\r
548 0x01, /* bNumDescriptors */
\r
549 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
550 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
552 /* Endpoint 1 descriptor */
\r
553 0x07, /* bLength */
\r
554 0x05, /* bDescriptorType */
\r
555 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
556 0x03, /* bmAttributes INT */
\r
557 0x03, 0x00, /* wMaxPacketSize: 3 bytes (x, y, z) */
\r
558 0x0A /* bInterval */
\r
561 /*-----------------------------------------------------------*/
\r
563 /* File scope state variables. */
\r
564 static unsigned char ucUSBConfig = ( unsigned char ) 0;
\r
565 static unsigned long ulReceivedAddress = ( unsigned long ) 0;
\r
566 static eDRIVER_STATE eDriverState = eNOTHING;
\r
568 /* Array in which the USB interrupt status is passed between the ISR and task. */
\r
569 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
571 /* Structure used to control the characters being sent to the host. */
\r
572 static xTX_MESSAGE pxCharsForTx;
\r
574 /* Queue used to pass messages between the ISR and the task. */
\r
575 static QueueHandle_t xUSBInterruptQueue;
\r
577 /* ISR entry has to be written in the asm file as we want a context switch
\r
578 to occur from within the ISR. See the port documentation on the FreeRTOS.org
\r
579 WEB site for more information. */
\r
580 extern void vUSBISREntry( void );
\r
582 /*-----------------------------------------------------------*/
\r
584 /* Macros to manipulate the control and status registers. These registers
\r
585 cannot be accessed using a direct read modify write operation outside of the
\r
586 ISR as some bits are left unchanged by writing with a 0, and some are left
\r
587 unchanged by writing with a 1. */
\r
589 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
591 #define usbCSR_SET_BIT( pulValueNow, ulBit ) \
\r
593 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
594 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
595 /* write has no effect. */ \
\r
596 ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f; \
\r
598 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
599 /* so the write has no effect. */ \
\r
600 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf; \
\r
602 /* Set whichever bit we want set. */ \
\r
603 ( * ( ( unsigned long * ) pulValueNow ) ) |= ( ulBit ); \
\r
606 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit ) \
\r
608 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
609 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
610 /* write has no effect. */ \
\r
611 ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f; \
\r
613 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
614 /* so the write has no effect. */ \
\r
615 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf; \
\r
617 /* Clear whichever bit we want clear. */ \
\r
618 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( ~ulBit ); \
\r
621 /*-----------------------------------------------------------*/
\r
623 __arm void vUSB_ISR( void )
\r
625 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
626 static volatile unsigned long ulNextMessage = 0;
\r
627 xISRStatus *pxMessage;
\r
628 unsigned long ulTemp, ulRxBytes;
\r
630 /* Take the next message from the queue. Note that usbQUEUE_LENGTH *must*
\r
631 be all 1's, as in 0x01, 0x03, 0x07, etc. */
\r
632 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
635 /* Take a snapshot of the current USB state for processing at the task
\r
637 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
638 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
640 /* Clear the interrupts from the ICR register. The bus end interrupt is
\r
641 cleared separately as it does not appear in the mask register. */
\r
642 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
644 /* If there are bytes in the FIFO then we have to retrieve them here.
\r
645 Ideally this would be done at the task level. However we need to clear the
\r
646 RXSETUP interrupt before leaving the ISR, and this may cause the data in
\r
647 the FIFO to be overwritten. Also the DIR bit has to be changed before the
\r
648 RXSETUP bit is cleared (as per the SAM7 manual). */
\r
649 ulTemp = pxMessage->ulCSR0;
\r
651 /* Are there any bytes in the FIFO? */
\r
652 ulRxBytes = ulTemp >> 16;
\r
653 ulRxBytes &= usbRX_COUNT_MASK;
\r
655 /* With this minimal implementation we are only interested in receiving
\r
656 setup bytes on the control end point. */
\r
657 if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )
\r
659 /* Take off 1 for a zero based index. */
\r
660 while( ulRxBytes > 0 )
\r
663 pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
666 /* The direction must be changed first. */
\r
667 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );
\r
668 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
671 /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA
\r
672 registers to clear the interrupts in the CSR register. */
\r
673 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
674 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
676 /* Also clear the interrupts in the CSR1 register. */
\r
677 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
678 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
679 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
681 /* The message now contains the entire state and optional data from
\r
682 the USB interrupt. This can now be posted on the Rx queue ready for
\r
683 processing at the task level. */
\r
684 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
686 /* We may want to switch to the USB task, if this message has made
\r
687 it the highest priority task that is ready to execute. */
\r
688 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r
690 /* Clear the AIC ready for the next interrupt. */
\r
691 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
693 /*-----------------------------------------------------------*/
\r
695 void vUSBDemoTask( void *pvParameters )
\r
697 xISRStatus *pxMessage;
\r
699 /* The parameters are not used in this task. */
\r
700 ( void ) pvParameters;
\r
702 /* Init USB device */
\r
703 portENTER_CRITICAL();
\r
704 vInitUSBInterface();
\r
705 portEXIT_CRITICAL();
\r
707 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
708 interrupt status then posts the information on this queue for processing
\r
709 at the task level. This simple demo implementation only processes
\r
710 a few interrupt sources. */
\r
713 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
715 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
717 /* Process end point 0 interrupt. */
\r
718 prvProcessEndPoint0Interrupt( pxMessage );
\r
721 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
723 /* Process an end of bus reset interrupt. */
\r
724 prvResetEndPoints();
\r
729 /* The ISR did not post any data for us to process on the queue, so
\r
730 just generate and send some sample data. */
\r
731 if( eDriverState == eREADY_TO_SEND )
\r
733 prvTransmitSampleValues();
\r
738 /*-----------------------------------------------------------*/
\r
740 static void prvTransmitSampleValues( void )
\r
742 unsigned long ulStatus;
\r
743 static long lState = usbXUP;
\r
745 /* Variables to hold dummy x, y and z joystick axis data. */
\r
746 static signed char x = 0, y = 0, z = 0;
\r
748 /* Generate some sample data in the x and y axis - draw a square. */
\r
751 case usbXUP : x += usbDATA_INC;
\r
752 if( x >= usbMAX_COORD )
\r
758 case usbXDOWN : x -= usbDATA_INC;
\r
759 if( x <= -usbMAX_COORD )
\r
765 case usbYUP : y += usbDATA_INC;
\r
766 if( y >= usbMAX_COORD )
\r
772 case usbYDOWN : y -= usbDATA_INC;
\r
773 if( y <= -usbMAX_COORD )
\r
780 /* Just make the z axis go up and down. */
\r
783 /* Can we place data in the fifo? */
\r
784 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
786 /* Write our sample data to the fifo. */
\r
787 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
788 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
789 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
791 /* Send the data. */
\r
792 portENTER_CRITICAL();
\r
794 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
795 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
796 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
798 portEXIT_CRITICAL();
\r
801 /*-----------------------------------------------------------*/
\r
803 static void prvUSBTransmitNull( void )
\r
805 unsigned long ulStatus;
\r
807 /* Wait until the FIFO is free - even though we are not going to use it.
\r
808 THERE IS NO TIMEOUT HERE! */
\r
809 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
811 vTaskDelay( usbSHORTEST_DELAY );
\r
814 portENTER_CRITICAL();
\r
816 /* Set the length of data to send to equal the index of the next byte
\r
817 to send. This will prevent the ACK to this NULL packet causing any
\r
818 further data transmissions. */
\r
819 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
821 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
822 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
823 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
824 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
826 portEXIT_CRITICAL();
\r
828 /*-----------------------------------------------------------*/
\r
830 static void prvSendStall( void )
\r
832 unsigned long ulStatus;
\r
834 portENTER_CRITICAL();
\r
836 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
837 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
838 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
839 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
841 portEXIT_CRITICAL();
\r
843 /*-----------------------------------------------------------*/
\r
845 static void prvResetEndPoints( void )
\r
847 unsigned long ulTemp;
\r
849 eDriverState = eJUST_RESET;
\r
851 /* Reset all the end points. */
\r
852 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
853 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned long ) 0x00;
\r
855 /* Enable data to be sent and received. */
\r
856 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
858 /* Repair the configuration end point. */
\r
859 portENTER_CRITICAL();
\r
861 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
862 usbCSR_SET_BIT( &ulTemp, ( ( unsigned long ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
863 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
864 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT0 );
\r
866 portEXIT_CRITICAL();
\r
868 /*-----------------------------------------------------------*/
\r
870 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
872 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
874 /* We only expect to receive zero length data here as ACK's.
\r
875 Set the data pointer to the end of the current Tx packet to
\r
876 ensure we don't send out any more data. */
\r
877 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
880 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
882 /* We received a TX complete interrupt. What we do depends on
\r
883 what we sent to get this interrupt. */
\r
885 if( eDriverState == eJUST_GOT_CONFIG )
\r
887 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
888 are now at the end of the enumeration. */
\r
889 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
891 /* Read the end point for data transfer. */
\r
892 portENTER_CRITICAL();
\r
894 unsigned long ulTemp;
\r
896 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
897 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
898 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
899 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT1 );
\r
901 portEXIT_CRITICAL();
\r
903 eDriverState = eREADY_TO_SEND;
\r
905 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
907 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
908 to the addressed state. */
\r
909 if( ulReceivedAddress != ( unsigned long ) 0 )
\r
911 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
915 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
918 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
919 eDriverState = eNOTHING;
\r
923 /* The TXCOMP was not for any special type of transmission. See
\r
924 if there is any more data to send. */
\r
925 prvSendNextSegment();
\r
929 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
931 xUSB_REQUEST xRequest;
\r
932 unsigned char ucRequest;
\r
933 unsigned long ulRxBytes;
\r
935 /* A data packet is available. */
\r
936 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
937 ulRxBytes &= usbRX_COUNT_MASK;
\r
939 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
941 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
943 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
944 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
946 /* NOT PORTABLE CODE! */
\r
947 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
948 xRequest.usValue <<= 8;
\r
949 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
951 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
952 xRequest.usIndex <<= 8;
\r
953 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
955 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
956 xRequest.usLength <<= 8;
\r
957 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
959 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
960 generate a zero based request selection. This is just done to
\r
961 break up the requests into subsections for clarity. The
\r
962 alternative would be to have more huge switch statement that would
\r
963 be difficult to optimise. */
\r
964 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
965 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
967 switch( ucRequest )
\r
969 case usbSTANDARD_DEVICE_REQUEST:
\r
970 /* Standard Device request */
\r
971 prvHandleStandardDeviceRequest( &xRequest );
\r
974 case usbSTANDARD_INTERFACE_REQUEST:
\r
975 /* Standard Interface request */
\r
976 prvHandleStandardInterfaceRequest( &xRequest );
\r
979 case usbSTANDARD_END_POINT_REQUEST:
\r
980 /* Standard Endpoint request */
\r
981 prvHandleStandardEndPointRequest( &xRequest );
\r
984 case usbCLASS_INTERFACE_REQUEST:
\r
985 /* Class Interface request */
\r
986 prvHandleClassInterfaceRequest( &xRequest );
\r
989 default: /* This is not something we want to respond to. */
\r
995 /*-----------------------------------------------------------*/
\r
997 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
999 /* The type is in the high byte. Return whatever has been requested. */
\r
1000 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
1002 case usbDESCRIPTOR_TYPE_DEVICE:
\r
1003 prvSendControlData( ( unsigned char * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
1006 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
1007 prvSendControlData( ( unsigned char * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
1010 case usbDESCRIPTOR_TYPE_STRING:
\r
1012 /* The index to the string descriptor is the lower byte. */
\r
1013 switch( pxRequest->usValue & 0xff )
\r
1015 case usbLANGUAGE_STRING:
\r
1016 prvSendControlData( ( unsigned char * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
1019 case usbMANUFACTURER_STRING:
\r
1020 prvSendControlData( ( unsigned char * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
1023 case usbPRODUCT_STRING:
\r
1024 prvSendControlData( ( unsigned char * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
1027 case usbCONFIGURATION_STRING:
\r
1028 prvSendControlData( ( unsigned char * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
1031 case usbINTERFACE_STRING:
\r
1032 prvSendControlData( ( unsigned char * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
1036 /* Don't know what this string is. */
\r
1044 /* We are not responding to anything else. */
\r
1049 /*-----------------------------------------------------------*/
\r
1051 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
1053 unsigned short usStatus = 0;
\r
1055 switch( pxRequest->ucRequest )
\r
1057 case usbGET_STATUS_REQUEST:
\r
1058 /* Just send two byte dummy status. */
\r
1059 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1062 case usbGET_DESCRIPTOR_REQUEST:
\r
1063 /* Send device descriptor */
\r
1064 prvGetStandardDeviceDescriptor( pxRequest );
\r
1067 case usbGET_CONFIGURATION_REQUEST:
\r
1068 /* Send selected device configuration */
\r
1069 prvSendControlData( ( unsigned char * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
1072 case usbSET_FEATURE_REQUEST:
\r
1073 prvUSBTransmitNull();
\r
1076 case usbSET_ADDRESS_REQUEST:
\r
1078 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
1079 cannot actually move to the addressed state until we get a TXCOMP
\r
1080 interrupt from this NULL packet. Therefore we just remember the
\r
1081 address and set our state so we know we have received the address. */
\r
1082 prvUSBTransmitNull();
\r
1083 eDriverState = eJUST_GOT_ADDRESS;
\r
1084 ulReceivedAddress = ( unsigned long ) pxRequest->usValue;
\r
1087 case usbSET_CONFIGURATION_REQUEST:
\r
1089 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
1090 we cannot actually move to the configured state until we get a
\r
1091 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
1092 config and set our state so we know we have received the go ahead. */
\r
1093 ucUSBConfig = ( unsigned char ) ( pxRequest->usValue & 0xff );
\r
1094 eDriverState = eJUST_GOT_CONFIG;
\r
1095 prvUSBTransmitNull();
\r
1100 /* We don't answer to anything else. */
\r
1105 /*-----------------------------------------------------------*/
\r
1107 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1109 switch( pxRequest->ucRequest )
\r
1111 case usbSET_IDLE_REQUEST:
\r
1112 prvUSBTransmitNull();
\r
1115 /* This minimal implementation ignores these. */
\r
1116 case usbGET_REPORT_REQUEST:
\r
1117 case usbGET_IDLE_REQUEST:
\r
1118 case usbGET_PROTOCOL_REQUEST:
\r
1119 case usbSET_REPORT_REQUEST:
\r
1120 case usbSET_PROTOCOL_REQUEST:
\r
1127 /*-----------------------------------------------------------*/
\r
1129 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
1131 switch( ( pxRequest->usValue & ( unsigned short ) 0xff00 ) >> 8 )
\r
1133 case usbHID_REPORT_DESCRIPTOR:
\r
1134 prvSendControlData( ( unsigned char * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
1139 /* Don't expect to send any others. */
\r
1144 /*-----------------------------------------------------------*/
\r
1146 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1148 unsigned short usStatus = 0;
\r
1150 switch( pxRequest->ucRequest )
\r
1152 case usbGET_STATUS_REQUEST:
\r
1153 /* Send dummy 2 bytes. */
\r
1154 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1157 case usbGET_DESCRIPTOR_REQUEST:
\r
1158 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1161 /* This minimal implementation does not respond to these. */
\r
1162 case usbGET_INTERFACE_REQUEST:
\r
1163 case usbSET_FEATURE_REQUEST:
\r
1164 case usbSET_INTERFACE_REQUEST:
\r
1171 /*-----------------------------------------------------------*/
\r
1173 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1175 switch( pxRequest->ucRequest )
\r
1177 /* This minimal implementation does not expect to respond to these. */
\r
1178 case usbGET_STATUS_REQUEST:
\r
1179 case usbCLEAR_FEATURE_REQUEST:
\r
1180 case usbSET_FEATURE_REQUEST:
\r
1187 /*-----------------------------------------------------------*/
\r
1189 static void vInitUSBInterface( void )
\r
1191 volatile unsigned long ulTemp;
\r
1193 /* Create the queue used to communicate between the USB ISR and task. */
\r
1194 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
1196 /* Initialise a few state variables. */
\r
1197 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1198 ucUSBConfig = ( unsigned char ) 0;
\r
1199 eDriverState = eNOTHING;
\r
1201 /* HARDWARE SETUP */
\r
1203 /* Set the PLL USB Divider */
\r
1204 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1206 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1207 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1208 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1210 /* Setup the PIO for the USB pull up resistor. */
\r
1211 AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,AT91C_PIO_PA16);
\r
1213 /* Start without the pullup - this will get set at the end of this
\r
1215 AT91F_PIO_SetOutput( AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1217 /* When using the USB debugger the peripheral registers do not always get
\r
1218 set to the correct default values. To make sure set the relevant registers
\r
1220 AT91C_BASE_UDP->UDP_IDR = ( unsigned long ) 0xffffffff;
\r
1221 AT91C_BASE_UDP->UDP_ICR = ( unsigned long ) 0xffffffff;
\r
1222 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned long ) 0x00;
\r
1223 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned long ) 0x00;
\r
1224 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1225 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1227 /* Enable the transceiver. */
\r
1228 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1230 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1231 enumeration process progresses. */
\r
1232 AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vUSBISREntry );
\r
1233 AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_UDP );
\r
1235 /* Wait a short while before making our presence known. */
\r
1236 vTaskDelay( usbINIT_DELAY );
\r
1237 AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA16 );
\r
1239 /*-----------------------------------------------------------*/
\r
1241 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthToSend, long lSendingDescriptor )
\r
1243 if( ( ( unsigned long ) usRequestedLength < ulLengthToSend ) )
\r
1245 /* Cap the data length to that requested. */
\r
1246 ulLengthToSend = ( unsigned short ) usRequestedLength;
\r
1248 else if( ( ulLengthToSend < ( unsigned long ) usRequestedLength ) && lSendingDescriptor )
\r
1250 /* We are sending a descriptor. If the descriptor is an exact
\r
1251 multiple of the FIFO length then it will have to be terminated
\r
1252 with a NULL packet. Set the state to indicate this if
\r
1254 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1256 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1260 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1261 BUFFER OVERFLOW PROTECTION HERE.
\r
1263 Copy the data to send into the buffer as we cannot send it all at once
\r
1264 (if it is greater than 8 bytes in length). */
\r
1265 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1267 /* Reinitialise the buffer index so we start sending from the start of
\r
1269 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1270 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1272 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1273 TXCOMP interrupts. */
\r
1274 prvSendNextSegment();
\r
1276 /*-----------------------------------------------------------*/
\r
1278 static void prvSendNextSegment( void )
\r
1280 volatile unsigned long ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1282 /* Is there any data to send? */
\r
1283 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1285 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1287 /* We can only send 8 bytes to the fifo at a time. */
\r
1288 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1290 ulNextLength = usbFIFO_LENGTH;
\r
1294 ulNextLength = ulLengthLeftToSend;
\r
1297 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1299 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1301 vTaskDelay( usbSHORTEST_DELAY );
\r
1304 /* Write the data to the FIFO. */
\r
1305 while( ulNextLength > ( unsigned long ) 0 )
\r
1307 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1310 pxCharsForTx.ulNextCharIndex++;
\r
1313 /* Start the transmission. */
\r
1314 portENTER_CRITICAL();
\r
1316 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1317 usbCSR_SET_BIT( &ulStatus, ( ( unsigned long ) 0x10 ) );
\r
1318 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1320 portEXIT_CRITICAL();
\r
1324 /* There is no data to send. If we were sending a descriptor and the
\r
1325 descriptor was an exact multiple of the max packet size then we need
\r
1326 to send a null to terminate the transmission. */
\r
1327 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1329 prvUSBTransmitNull();
\r
1330 eDriverState = eNOTHING;
\r