2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 Sample interrupt driven mouse device driver. This is a minimal implementation
\r
68 for demonstration only. Although functional, it may not be a fully and
\r
69 compliant implementation. The small joystick on the SAM7X EK can be used to
\r
70 move the mouse cursor, pressing the joystick transmits mouse clicks. Note
\r
71 that it might be necessary to run the demo stand along (without the
\r
72 debugger) in order for the USB device to be recognised by the host computer.
\r
74 The interrupt handler itself is contained within USB_ISR.c.
\r
76 See the FreeRTOS.org online documentation for more information.
\r
79 /* Standard includes. */
\r
82 /* Scheduler includes. */
\r
83 #include "FreeRTOS.h"
\r
87 /* Demo application includes. */
\r
88 #include "USBSample.h"
\r
90 /* Joystick inputs used to move the 'mouse' cursor. */
\r
91 #define usbSW1 ( 1 << 21 ) /* PA21 */
\r
92 #define usbSW2 ( 1 << 22 ) /* PA22 */
\r
93 #define usbSW3 ( 1 << 23 ) /* PA23 */
\r
94 #define usbSW4 ( 1 << 24 ) /* PA24 */
\r
95 #define usbSW_CLICK ( 1 << 25 ) /* PA25 */
\r
97 /* Descriptor type definitions. */
\r
98 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
99 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
100 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
102 /* USB request type definitions. */
\r
103 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
104 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
105 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
106 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
107 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
108 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
109 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
110 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
111 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
112 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
113 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
114 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
115 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
116 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
117 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
120 /* Misc USB definitions. */
\r
121 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
122 #define usbBUS_POWERED ( 0x80 )
\r
123 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
124 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
126 /* Index to the various string. */
\r
127 #define usbLANGUAGE_STRING ( 0 )
\r
128 #define usbMANUFACTURER_STRING ( 1 )
\r
129 #define usbPRODUCT_STRING ( 2 )
\r
130 #define usbCONFIGURATION_STRING ( 3 )
\r
131 #define usbINTERFACE_STRING ( 4 )
\r
133 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
134 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
136 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
137 #define usbREQUEST_INDEX ( 6 )
\r
138 #define usbVALUE_HIGH_BYTE ( 4 )
\r
139 #define usbVALUE_LOW_BYTE ( 5 )
\r
140 #define usbINDEX_HIGH_BYTE ( 2 )
\r
141 #define usbINDEX_LOW_BYTE ( 3 )
\r
142 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
143 #define usbLENGTH_LOW_BYTE ( 1 )
\r
145 /* Misc application definitions. */
\r
146 #define usbINTERRUPT_PRIORITY ( 3 )
\r
147 #define usbFIFO_LENGTH ( ( unsigned long ) 8 )
\r
148 #define usbXUP ( 1 )
\r
149 #define usbXDOWN ( 2 )
\r
150 #define usbYUP ( 3 )
\r
151 #define usbYDOWN ( 4 )
\r
152 #define usbMAX_COORD ( 120 )
\r
153 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
154 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
155 #define usbINIT_DELAY ( ( portTickType ) 1000 / portTICK_RATE_MS )
\r
156 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
157 #define usbEND_POINT_RESET_MASK ( ( unsigned long ) 0x0f )
\r
158 #define usbDATA_INC ( ( char ) 5 )
\r
159 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned long ) 8 )
\r
161 /* Control request types. */
\r
162 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
163 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
164 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
165 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
167 /* Structure used to hold the received requests. */
\r
170 unsigned char ucReqType;
\r
171 unsigned char ucRequest;
\r
172 unsigned short usValue;
\r
173 unsigned short usIndex;
\r
174 unsigned short usLength;
\r
183 eSENDING_EVEN_DESCRIPTOR,
\r
187 /* Structure used to control the data being sent to the host. */
\r
190 unsigned char ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
191 unsigned long ulNextCharIndex;
\r
192 unsigned long ulTotalDataLength;
\r
195 /*-----------------------------------------------------------*/
\r
198 * The USB interrupt service routine. This takes a snapshot of the USB
\r
199 * device at the time of the interrupt, clears the interrupts, and posts
\r
200 * the data to the USB processing task. This is implemented in USB_ISR.c.
\r
202 extern void vUSB_ISR_Wrapper( void );
\r
205 * Called after the bus reset interrupt - this function readies all the
\r
206 * end points for communication.
\r
208 static void prvResetEndPoints( void );
\r
211 * Setup the USB hardware, install the interrupt service routine and
\r
212 * initialise all the state variables.
\r
214 static void vInitUSBInterface( void );
\r
217 * Decode and act upon an interrupt generated by the control end point.
\r
219 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
222 * For simplicity requests are separated into device, interface, class
\r
223 * interface and end point requests.
\r
225 * Decode and handle standard device requests originating on the control
\r
228 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
231 * For simplicity requests are separated into device, interface, class
\r
232 * interface and end point requests.
\r
234 * Decode and handle standard interface requests originating on the control
\r
237 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
240 * For simplicity requests are separated into device, interface, class
\r
241 * interface and end point requests.
\r
243 * Decode and handle standard end point requests originating on the control
\r
246 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
249 * For simplicity requests are separated into device, interface, class
\r
250 * interface and end point requests.
\r
252 * Decode and handle the class interface requests.
\r
254 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
257 * Setup the Tx buffer to send data in response to a control request.
\r
259 * The data to be transmitted is buffered, the state variables are updated,
\r
260 * then prvSendNextSegment() is called to start the transmission off. Once
\r
261 * the first segment has been sent the remaining segments are transmitted
\r
262 * in response to TXCOMP interrupts until the entire buffer has been
\r
265 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthLeftToSend, long lSendingDescriptor );
\r
268 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
270 * If there is data to be transmitted then send the next segment. A segment
\r
271 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
272 * point by the descriptor). The final segment may be less than 8 bytes if
\r
273 * the total data length was not an exact multiple of 8.
\r
275 static void prvSendNextSegment( void );
\r
278 * A stall condition is forced each time the host makes a request that is not
\r
279 * supported by this minimal implementation.
\r
281 * A stall is forced by setting the appropriate bit in the end points control
\r
282 * and status register.
\r
284 static void prvSendStall( void );
\r
287 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
288 * of certain events from the host.
\r
290 static void prvUSBTransmitNull( void );
\r
293 * When the host requests a descriptor this function is called to determine
\r
294 * which descriptor is being requested and start its transmission.
\r
296 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
299 * Transmit movement and clicks on the EK joystick as mouse inputs.
\r
301 static void prvTransmitSampleValues( void );
\r
304 * The created task to handle the USB demo functionality.
\r
306 static void vUSBDemoTask( void *pvParameters );
\r
309 * Simple algorithm to ramp up the mouse cursor speed to make it easier to
\r
312 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 );
\r
313 /*-----------------------------------------------------------*/
\r
316 - DESCRIPTOR DEFINITIONS -
\r
319 /* String descriptors used during the enumeration process.
\r
320 These take the form:
\r
323 Length of descriptor,
\r
328 const char pxLanguageStringDescriptor[] =
\r
331 usbDESCRIPTOR_TYPE_STRING,
\r
335 const char pxManufacturerStringDescriptor[] =
\r
338 usbDESCRIPTOR_TYPE_STRING,
\r
350 const char pxProductStringDescriptor[] =
\r
353 usbDESCRIPTOR_TYPE_STRING,
\r
375 const char pxConfigurationStringDescriptor[] =
\r
378 usbDESCRIPTOR_TYPE_STRING,
\r
400 const char pxInterfaceStringDescriptor[] =
\r
403 usbDESCRIPTOR_TYPE_STRING,
\r
421 /* Enumeration descriptors. */
\r
422 const char pxReportDescriptor[] =
\r
424 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
425 0x09, 0x02, /* USAGE (Mouse) */
\r
426 0xa1, 0x01, /* COLLECTION (Application) */
\r
427 0x09, 0x01, /* USAGE (Pointer) */
\r
428 0xa1, 0x00, /* COLLECTION (Physical) */
\r
429 0x95, 0x03, /* REPORT_COUNT (3) */
\r
430 0x75, 0x01, /* REPORT_SIZE (1) */
\r
431 0x05, 0x09, /* USAGE_PAGE (Button) */
\r
432 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
\r
433 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
\r
434 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
\r
435 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
\r
436 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
437 0x95, 0x01, /* REPORT_COUNT (1) */
\r
438 0x75, 0x05, /* REPORT_SIZE (5) */
\r
439 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */
\r
440 0x75, 0x08, /* REPORT_SIZE (8) */
\r
441 0x95, 0x02, /* REPORT_COUNT (2) */
\r
442 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
443 0x09, 0x30, /* USAGE (X) */
\r
444 0x09, 0x31, /* USAGE (Y) */
\r
445 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
446 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
447 0x81, 0x06, /* INPUT (Data,Var,Rel) */
\r
448 0xc0, /* END_COLLECTION */
\r
449 0xc0 /* END_COLLECTION */
\r
454 const char pxDeviceDescriptor[] =
\r
456 /* Device descriptor */
\r
457 0x12, /* bLength */
\r
458 0x01, /* bDescriptorType */
\r
459 0x10, 0x01, /* bcdUSBL */
\r
460 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
461 0x00, /* bDeviceSubclass: */
\r
462 0x00, /* bDeviceProtocol: */
\r
463 0x08, /* bMaxPacketSize0 */
\r
464 0xFF, 0xFF, /* idVendorL */
\r
465 0x02, 0x00, /* idProductL */
\r
466 0x00, 0x01, /* bcdDeviceL */
\r
467 usbMANUFACTURER_STRING, /* iManufacturer */
\r
468 usbPRODUCT_STRING, /* iProduct */
\r
469 0x00, /* SerialNumber */
\r
470 0x01 /* bNumConfigs */
\r
474 const char pxConfigDescriptor[] = {
\r
475 /* Configuration 1 descriptor */
\r
476 0x09, /* CbLength */
\r
477 0x02, /* CbDescriptorType */
\r
478 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
479 0x01, /* CbNumInterfaces */
\r
480 0x01, /* CbConfigurationValue */
\r
481 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
482 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
483 0x32, /* CMaxPower: 100mA */
\r
485 /* Mouse Interface Descriptor Requirement */
\r
486 0x09, /* bLength */
\r
487 0x04, /* bDescriptorType */
\r
488 0x00, /* bInterfaceNumber */
\r
489 0x00, /* bAlternateSetting */
\r
490 0x01, /* bNumEndpoints */
\r
491 0x03, /* bInterfaceClass: HID code */
\r
492 0x01, /* bInterfaceSubclass boot */
\r
493 0x02, /* bInterfaceProtocol mouse boot */
\r
494 usbINTERFACE_STRING,/* iInterface */
\r
496 /* HID Descriptor */
\r
497 0x09, /* bLength */
\r
498 0x21, /* bDescriptor type: HID Descriptor Type */
\r
499 0x00, 0x01, /* bcdHID */
\r
500 0x00, /* bCountryCode */
\r
501 0x01, /* bNumDescriptors */
\r
502 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
503 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
505 /* Endpoint 1 descriptor */
\r
506 0x07, /* bLength */
\r
507 0x05, /* bDescriptorType */
\r
508 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
509 0x03, /* bmAttributes INT */
\r
510 0x08, 0x00, /* wMaxPacketSize: 8? */
\r
511 0x0A /* bInterval */
\r
514 /*-----------------------------------------------------------*/
\r
516 /* File scope state variables. */
\r
517 static unsigned char ucUSBConfig = ( unsigned char ) 0;
\r
518 static unsigned long ulReceivedAddress = ( unsigned long ) 0;
\r
519 static eDRIVER_STATE eDriverState = eNOTHING;
\r
521 /* Structure used to control the characters being sent to the host. */
\r
522 static xTX_MESSAGE pxCharsForTx;
\r
524 /* Queue used to pass messages between the ISR and the task. */
\r
525 xQueueHandle xUSBInterruptQueue;
\r
527 /*-----------------------------------------------------------*/
\r
529 void vStartUSBTask( unsigned portBASE_TYPE uxPriority )
\r
531 /* Create the queue used to communicate between the USB ISR and task. */
\r
532 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
534 /* Create the task itself. */
\r
535 xTaskCreate( vUSBDemoTask, "USB", configMINIMAL_STACK_SIZE, NULL, uxPriority, NULL );
\r
537 /*-----------------------------------------------------------*/
\r
539 static void vUSBDemoTask( void *pvParameters )
\r
541 xISRStatus *pxMessage;
\r
543 /* The parameters are not used in this task. */
\r
544 ( void ) pvParameters;
\r
546 /* Init USB device */
\r
547 portENTER_CRITICAL();
\r
548 vInitUSBInterface();
\r
549 portEXIT_CRITICAL();
\r
551 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
552 interrupt status then posts the information on this queue for processing
\r
553 at the task level. This simple demo implementation only processes
\r
554 a few interrupt sources. */
\r
557 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
559 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
561 /* Process end point 0 interrupt. */
\r
562 prvProcessEndPoint0Interrupt( pxMessage );
\r
565 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
567 /* Process an end of bus reset interrupt. */
\r
568 prvResetEndPoints();
\r
573 /* The ISR did not post any data for us to process on the queue, so
\r
574 just generate and send some sample data. */
\r
575 if( eDriverState == eREADY_TO_SEND )
\r
577 prvTransmitSampleValues();
\r
582 /*-----------------------------------------------------------*/
\r
584 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 )
\r
586 const char cSpeed = 20;
\r
588 if( !( ulInput & ulSwitch1 ) )
\r
590 /* We are going in the decreasing y direction. */
\r
593 /* We have changed direction since last time so start from
\r
598 if( *cVal > -cSpeed )
\r
600 /* Ramp y down to the max speed. */
\r
604 else if( !( ulInput & ulSwitch2 ) )
\r
606 /* We are going in the increasing y direction. */
\r
609 /* We have changed direction since last time, so start from
\r
614 if( *cVal < cSpeed )
\r
616 /* Ramp y up to the max speed again. */
\r
625 /*-----------------------------------------------------------*/
\r
627 static void prvTransmitSampleValues( void )
\r
629 /* Variables to hold dummy x, y and z joystick axis data. */
\r
630 static signed char x = 0, y = 0, z = 0;
\r
631 unsigned long ulStatus;
\r
633 ulStatus = AT91C_BASE_PIOA->PIO_PDSR;
\r
635 prvControlCursorSpeed( &y, ulStatus, ( unsigned long ) usbSW1, ( unsigned long ) usbSW2 );
\r
636 prvControlCursorSpeed( &x, ulStatus, ( unsigned long ) usbSW3, ( unsigned long ) usbSW4 );
\r
638 /* Just make the z axis go up and down. */
\r
639 z = ( ( ulStatus & usbSW_CLICK ) == 0 );
\r
641 /* Can we place data in the fifo? */
\r
642 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
644 /* Write our sample data to the fifo. */
\r
645 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
646 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
647 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
649 /* Send the data. */
\r
650 portENTER_CRITICAL();
\r
652 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
653 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
654 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
656 portEXIT_CRITICAL();
\r
659 /*-----------------------------------------------------------*/
\r
661 static void prvUSBTransmitNull( void )
\r
663 unsigned long ulStatus;
\r
665 /* Wait until the FIFO is free - even though we are not going to use it.
\r
666 THERE IS NO TIMEOUT HERE! */
\r
667 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
669 vTaskDelay( usbSHORTEST_DELAY );
\r
672 portENTER_CRITICAL();
\r
674 /* Set the length of data to send to equal the index of the next byte
\r
675 to send. This will prevent the ACK to this NULL packet causing any
\r
676 further data transmissions. */
\r
677 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
679 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
680 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
681 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
682 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
684 portEXIT_CRITICAL();
\r
686 /*-----------------------------------------------------------*/
\r
688 static void prvSendStall( void )
\r
690 unsigned long ulStatus;
\r
692 portENTER_CRITICAL();
\r
694 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
695 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
696 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
697 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
699 portEXIT_CRITICAL();
\r
701 /*-----------------------------------------------------------*/
\r
703 static void prvResetEndPoints( void )
\r
705 unsigned long ulTemp;
\r
707 eDriverState = eJUST_RESET;
\r
709 /* Reset all the end points. */
\r
710 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
711 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned long ) 0x00;
\r
713 /* Enable data to be sent and received. */
\r
714 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
716 /* Repair the configuration end point. */
\r
717 portENTER_CRITICAL();
\r
719 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
720 usbCSR_SET_BIT( &ulTemp, ( ( unsigned long ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
721 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
722 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT0;
\r
724 portEXIT_CRITICAL();
\r
726 /*-----------------------------------------------------------*/
\r
728 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
730 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
732 /* We only expect to receive zero length data here as ACK's.
\r
733 Set the data pointer to the end of the current Tx packet to
\r
734 ensure we don't send out any more data. */
\r
735 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
738 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
740 /* We received a TX complete interrupt. What we do depends on
\r
741 what we sent to get this interrupt. */
\r
743 if( eDriverState == eJUST_GOT_CONFIG )
\r
745 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
746 are now at the end of the enumeration. */
\r
747 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
749 /* Read the end point for data transfer. */
\r
750 portENTER_CRITICAL();
\r
752 unsigned long ulTemp;
\r
754 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
755 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
756 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
757 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT1;
\r
759 portEXIT_CRITICAL();
\r
761 eDriverState = eREADY_TO_SEND;
\r
763 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
765 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
766 to the addressed state. */
\r
767 if( ulReceivedAddress != ( unsigned long ) 0 )
\r
769 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
773 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
776 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
777 eDriverState = eNOTHING;
\r
781 /* The TXCOMP was not for any special type of transmission. See
\r
782 if there is any more data to send. */
\r
783 prvSendNextSegment();
\r
787 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
789 xUSB_REQUEST xRequest;
\r
790 unsigned char ucRequest;
\r
791 unsigned long ulRxBytes;
\r
793 /* A data packet is available. */
\r
794 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
795 ulRxBytes &= usbRX_COUNT_MASK;
\r
797 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
799 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
801 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
802 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
804 /* NOT PORTABLE CODE! */
\r
805 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
806 xRequest.usValue <<= 8;
\r
807 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
809 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
810 xRequest.usIndex <<= 8;
\r
811 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
813 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
814 xRequest.usLength <<= 8;
\r
815 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
817 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
818 generate a zero based request selection. This is just done to
\r
819 break up the requests into subsections for clarity. The
\r
820 alternative would be to have more huge switch statement that would
\r
821 be difficult to optimise. */
\r
822 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
823 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
825 switch( ucRequest )
\r
827 case usbSTANDARD_DEVICE_REQUEST:
\r
828 /* Standard Device request */
\r
829 prvHandleStandardDeviceRequest( &xRequest );
\r
832 case usbSTANDARD_INTERFACE_REQUEST:
\r
833 /* Standard Interface request */
\r
834 prvHandleStandardInterfaceRequest( &xRequest );
\r
837 case usbSTANDARD_END_POINT_REQUEST:
\r
838 /* Standard Endpoint request */
\r
839 prvHandleStandardEndPointRequest( &xRequest );
\r
842 case usbCLASS_INTERFACE_REQUEST:
\r
843 /* Class Interface request */
\r
844 prvHandleClassInterfaceRequest( &xRequest );
\r
847 default: /* This is not something we want to respond to. */
\r
853 /*-----------------------------------------------------------*/
\r
855 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
857 /* The type is in the high byte. Return whatever has been requested. */
\r
858 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
860 case usbDESCRIPTOR_TYPE_DEVICE:
\r
861 prvSendControlData( ( unsigned char * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
864 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
865 prvSendControlData( ( unsigned char * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
868 case usbDESCRIPTOR_TYPE_STRING:
\r
870 /* The index to the string descriptor is the lower byte. */
\r
871 switch( pxRequest->usValue & 0xff )
\r
873 case usbLANGUAGE_STRING:
\r
874 prvSendControlData( ( unsigned char * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
877 case usbMANUFACTURER_STRING:
\r
878 prvSendControlData( ( unsigned char * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
881 case usbPRODUCT_STRING:
\r
882 prvSendControlData( ( unsigned char * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
885 case usbCONFIGURATION_STRING:
\r
886 prvSendControlData( ( unsigned char * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
889 case usbINTERFACE_STRING:
\r
890 prvSendControlData( ( unsigned char * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
894 /* Don't know what this string is. */
\r
902 /* We are not responding to anything else. */
\r
907 /*-----------------------------------------------------------*/
\r
909 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
911 unsigned short usStatus = 0;
\r
913 switch( pxRequest->ucRequest )
\r
915 case usbGET_STATUS_REQUEST:
\r
916 /* Just send two byte dummy status. */
\r
917 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
920 case usbGET_DESCRIPTOR_REQUEST:
\r
921 /* Send device descriptor */
\r
922 prvGetStandardDeviceDescriptor( pxRequest );
\r
925 case usbGET_CONFIGURATION_REQUEST:
\r
926 /* Send selected device configuration */
\r
927 prvSendControlData( ( unsigned char * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
930 case usbSET_FEATURE_REQUEST:
\r
931 prvUSBTransmitNull();
\r
934 case usbSET_ADDRESS_REQUEST:
\r
936 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
937 cannot actually move to the addressed state until we get a TXCOMP
\r
938 interrupt from this NULL packet. Therefore we just remember the
\r
939 address and set our state so we know we have received the address. */
\r
940 prvUSBTransmitNull();
\r
941 eDriverState = eJUST_GOT_ADDRESS;
\r
942 ulReceivedAddress = ( unsigned long ) pxRequest->usValue;
\r
945 case usbSET_CONFIGURATION_REQUEST:
\r
947 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
948 we cannot actually move to the configured state until we get a
\r
949 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
950 config and set our state so we know we have received the go ahead. */
\r
951 ucUSBConfig = ( unsigned char ) ( pxRequest->usValue & 0xff );
\r
952 eDriverState = eJUST_GOT_CONFIG;
\r
953 prvUSBTransmitNull();
\r
958 /* We don't answer to anything else. */
\r
963 /*-----------------------------------------------------------*/
\r
965 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
967 switch( pxRequest->ucRequest )
\r
969 case usbSET_IDLE_REQUEST:
\r
970 prvUSBTransmitNull();
\r
973 /* This minimal implementation ignores these. */
\r
974 case usbGET_REPORT_REQUEST:
\r
975 case usbGET_IDLE_REQUEST:
\r
976 case usbGET_PROTOCOL_REQUEST:
\r
977 case usbSET_REPORT_REQUEST:
\r
978 case usbSET_PROTOCOL_REQUEST:
\r
985 /*-----------------------------------------------------------*/
\r
987 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
989 switch( ( pxRequest->usValue & ( unsigned short ) 0xff00 ) >> 8 )
\r
991 case usbHID_REPORT_DESCRIPTOR:
\r
992 prvSendControlData( ( unsigned char * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
997 /* Don't expect to send any others. */
\r
1002 /*-----------------------------------------------------------*/
\r
1004 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1006 unsigned short usStatus = 0;
\r
1008 switch( pxRequest->ucRequest )
\r
1010 case usbGET_STATUS_REQUEST:
\r
1011 /* Send dummy 2 bytes. */
\r
1012 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1015 case usbGET_DESCRIPTOR_REQUEST:
\r
1016 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1019 /* This minimal implementation does not respond to these. */
\r
1020 case usbGET_INTERFACE_REQUEST:
\r
1021 case usbSET_FEATURE_REQUEST:
\r
1022 case usbSET_INTERFACE_REQUEST:
\r
1029 /*-----------------------------------------------------------*/
\r
1031 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1033 switch( pxRequest->ucRequest )
\r
1035 /* This minimal implementation does not expect to respond to these. */
\r
1036 case usbGET_STATUS_REQUEST:
\r
1037 case usbCLEAR_FEATURE_REQUEST:
\r
1038 case usbSET_FEATURE_REQUEST:
\r
1045 /*-----------------------------------------------------------*/
\r
1047 static void vInitUSBInterface( void )
\r
1049 volatile unsigned long ulTemp;
\r
1051 /* Initialise a few state variables. */
\r
1052 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1053 ucUSBConfig = ( unsigned char ) 0;
\r
1054 eDriverState = eNOTHING;
\r
1056 /* HARDWARE SETUP */
\r
1058 /* Set the PLL USB Divider */
\r
1059 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1061 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1062 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1063 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1065 /* Setup the PIO for the USB pull up resistor. */
\r
1066 AT91C_BASE_PIOA->PIO_PER = AT91C_PIO_PA16;
\r
1067 AT91C_BASE_PIOA->PIO_OER = AT91C_PIO_PA16;
\r
1070 /* Start without the pullup - this will get set at the end of this
\r
1072 AT91C_BASE_PIOA->PIO_SODR = AT91C_PIO_PA16;
\r
1074 /* When using the USB debugger the peripheral registers do not always get
\r
1075 set to the correct default values. To make sure set the relevant registers
\r
1077 AT91C_BASE_UDP->UDP_IDR = ( unsigned long ) 0xffffffff;
\r
1078 AT91C_BASE_UDP->UDP_ICR = ( unsigned long ) 0xffffffff;
\r
1079 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned long ) 0x00;
\r
1080 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned long ) 0x00;
\r
1081 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1082 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1084 /* Enable the transceiver. */
\r
1085 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1087 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1088 enumeration process progresses. */
\r
1089 AT91F_AIC_ConfigureIt( AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vUSB_ISR_Wrapper );
\r
1090 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_UDP;
\r
1092 /* Wait a short while before making our presence known. */
\r
1093 vTaskDelay( usbINIT_DELAY );
\r
1094 AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA16;
\r
1096 /*-----------------------------------------------------------*/
\r
1098 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthToSend, long lSendingDescriptor )
\r
1100 if( ( ( unsigned long ) usRequestedLength < ulLengthToSend ) )
\r
1102 /* Cap the data length to that requested. */
\r
1103 ulLengthToSend = ( unsigned short ) usRequestedLength;
\r
1105 else if( ( ulLengthToSend < ( unsigned long ) usRequestedLength ) && lSendingDescriptor )
\r
1107 /* We are sending a descriptor. If the descriptor is an exact
\r
1108 multiple of the FIFO length then it will have to be terminated
\r
1109 with a NULL packet. Set the state to indicate this if
\r
1111 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1113 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1117 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1118 BUFFER OVERFLOW PROTECTION HERE.
\r
1120 Copy the data to send into the buffer as we cannot send it all at once
\r
1121 (if it is greater than 8 bytes in length). */
\r
1122 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1124 /* Reinitialise the buffer index so we start sending from the start of
\r
1126 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1127 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1129 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1130 TXCOMP interrupts. */
\r
1131 prvSendNextSegment();
\r
1133 /*-----------------------------------------------------------*/
\r
1135 static void prvSendNextSegment( void )
\r
1137 volatile unsigned long ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1139 /* Is there any data to send? */
\r
1140 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1142 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1144 /* We can only send 8 bytes to the fifo at a time. */
\r
1145 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1147 ulNextLength = usbFIFO_LENGTH;
\r
1151 ulNextLength = ulLengthLeftToSend;
\r
1154 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1156 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1158 vTaskDelay( usbSHORTEST_DELAY );
\r
1161 /* Write the data to the FIFO. */
\r
1162 while( ulNextLength > ( unsigned long ) 0 )
\r
1164 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1167 pxCharsForTx.ulNextCharIndex++;
\r
1170 /* Start the transmission. */
\r
1171 portENTER_CRITICAL();
\r
1173 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1174 usbCSR_SET_BIT( &ulStatus, ( ( unsigned long ) 0x10 ) );
\r
1175 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1177 portEXIT_CRITICAL();
\r
1181 /* There is no data to send. If we were sending a descriptor and the
\r
1182 descriptor was an exact multiple of the max packet size then we need
\r
1183 to send a null to terminate the transmission. */
\r
1184 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1186 prvUSBTransmitNull();
\r
1187 eDriverState = eNOTHING;
\r