2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
50 Sample interrupt driven mouse device driver. This is a minimal implementation
\r
51 for demonstration only. Although functional, it may not be a fully and
\r
52 compliant implementation. The small joystick on the SAM7X EK can be used to
\r
53 move the mouse cursor, pressing the joystick transmits mouse clicks. Note
\r
54 that it might be necessary to run the demo stand along (without the
\r
55 debugger) in order for the USB device to be recognised by the host computer.
\r
57 The interrupt handler itself is contained within USB_ISR.c.
\r
59 See the FreeRTOS.org online documentation for more information.
\r
62 /* Standard includes. */
\r
65 /* Scheduler includes. */
\r
66 #include "FreeRTOS.h"
\r
70 /* Demo application includes. */
\r
71 #include "USBSample.h"
\r
73 /* Joystick inputs used to move the 'mouse' cursor. */
\r
74 #define usbSW1 ( 1 << 21 ) /* PA21 */
\r
75 #define usbSW2 ( 1 << 22 ) /* PA22 */
\r
76 #define usbSW3 ( 1 << 23 ) /* PA23 */
\r
77 #define usbSW4 ( 1 << 24 ) /* PA24 */
\r
78 #define usbSW_CLICK ( 1 << 25 ) /* PA25 */
\r
80 /* Descriptor type definitions. */
\r
81 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
82 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
83 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
85 /* USB request type definitions. */
\r
86 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
87 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
88 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
89 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
90 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
91 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
92 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
93 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
94 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
95 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
96 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
97 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
98 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
99 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
100 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
103 /* Misc USB definitions. */
\r
104 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
105 #define usbBUS_POWERED ( 0x80 )
\r
106 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
107 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
109 /* Index to the various string. */
\r
110 #define usbLANGUAGE_STRING ( 0 )
\r
111 #define usbMANUFACTURER_STRING ( 1 )
\r
112 #define usbPRODUCT_STRING ( 2 )
\r
113 #define usbCONFIGURATION_STRING ( 3 )
\r
114 #define usbINTERFACE_STRING ( 4 )
\r
116 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
117 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
119 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
120 #define usbREQUEST_INDEX ( 6 )
\r
121 #define usbVALUE_HIGH_BYTE ( 4 )
\r
122 #define usbVALUE_LOW_BYTE ( 5 )
\r
123 #define usbINDEX_HIGH_BYTE ( 2 )
\r
124 #define usbINDEX_LOW_BYTE ( 3 )
\r
125 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
126 #define usbLENGTH_LOW_BYTE ( 1 )
\r
128 /* Misc application definitions. */
\r
129 #define usbINTERRUPT_PRIORITY ( 3 )
\r
130 #define usbFIFO_LENGTH ( ( unsigned long ) 8 )
\r
131 #define usbXUP ( 1 )
\r
132 #define usbXDOWN ( 2 )
\r
133 #define usbYUP ( 3 )
\r
134 #define usbYDOWN ( 4 )
\r
135 #define usbMAX_COORD ( 120 )
\r
136 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
137 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
138 #define usbINIT_DELAY ( ( portTickType ) 1000 / portTICK_RATE_MS )
\r
139 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
140 #define usbEND_POINT_RESET_MASK ( ( unsigned long ) 0x0f )
\r
141 #define usbDATA_INC ( ( char ) 5 )
\r
142 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned long ) 8 )
\r
144 /* Control request types. */
\r
145 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
146 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
147 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
148 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
150 /* Structure used to hold the received requests. */
\r
153 unsigned char ucReqType;
\r
154 unsigned char ucRequest;
\r
155 unsigned short usValue;
\r
156 unsigned short usIndex;
\r
157 unsigned short usLength;
\r
166 eSENDING_EVEN_DESCRIPTOR,
\r
170 /* Structure used to control the data being sent to the host. */
\r
173 unsigned char ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
174 unsigned long ulNextCharIndex;
\r
175 unsigned long ulTotalDataLength;
\r
178 /*-----------------------------------------------------------*/
\r
181 * The USB interrupt service routine. This takes a snapshot of the USB
\r
182 * device at the time of the interrupt, clears the interrupts, and posts
\r
183 * the data to the USB processing task. This is implemented in USB_ISR.c.
\r
185 extern void vUSB_ISR_Wrapper( void );
\r
188 * Called after the bus reset interrupt - this function readies all the
\r
189 * end points for communication.
\r
191 static void prvResetEndPoints( void );
\r
194 * Setup the USB hardware, install the interrupt service routine and
\r
195 * initialise all the state variables.
\r
197 static void vInitUSBInterface( void );
\r
200 * Decode and act upon an interrupt generated by the control end point.
\r
202 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
205 * For simplicity requests are separated into device, interface, class
\r
206 * interface and end point requests.
\r
208 * Decode and handle standard device requests originating on the control
\r
211 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
214 * For simplicity requests are separated into device, interface, class
\r
215 * interface and end point requests.
\r
217 * Decode and handle standard interface requests originating on the control
\r
220 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
223 * For simplicity requests are separated into device, interface, class
\r
224 * interface and end point requests.
\r
226 * Decode and handle standard end point requests originating on the control
\r
229 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
232 * For simplicity requests are separated into device, interface, class
\r
233 * interface and end point requests.
\r
235 * Decode and handle the class interface requests.
\r
237 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
240 * Setup the Tx buffer to send data in response to a control request.
\r
242 * The data to be transmitted is buffered, the state variables are updated,
\r
243 * then prvSendNextSegment() is called to start the transmission off. Once
\r
244 * the first segment has been sent the remaining segments are transmitted
\r
245 * in response to TXCOMP interrupts until the entire buffer has been
\r
248 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthLeftToSend, long lSendingDescriptor );
\r
251 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
253 * If there is data to be transmitted then send the next segment. A segment
\r
254 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
255 * point by the descriptor). The final segment may be less than 8 bytes if
\r
256 * the total data length was not an exact multiple of 8.
\r
258 static void prvSendNextSegment( void );
\r
261 * A stall condition is forced each time the host makes a request that is not
\r
262 * supported by this minimal implementation.
\r
264 * A stall is forced by setting the appropriate bit in the end points control
\r
265 * and status register.
\r
267 static void prvSendStall( void );
\r
270 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
271 * of certain events from the host.
\r
273 static void prvUSBTransmitNull( void );
\r
276 * When the host requests a descriptor this function is called to determine
\r
277 * which descriptor is being requested and start its transmission.
\r
279 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
282 * Transmit movement and clicks on the EK joystick as mouse inputs.
\r
284 static void prvTransmitSampleValues( void );
\r
287 * The created task to handle the USB demo functionality.
\r
289 static void vUSBDemoTask( void *pvParameters );
\r
292 * Simple algorithm to ramp up the mouse cursor speed to make it easier to
\r
295 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 );
\r
296 /*-----------------------------------------------------------*/
\r
299 - DESCRIPTOR DEFINITIONS -
\r
302 /* String descriptors used during the enumeration process.
\r
303 These take the form:
\r
306 Length of descriptor,
\r
311 const char pxLanguageStringDescriptor[] =
\r
314 usbDESCRIPTOR_TYPE_STRING,
\r
318 const char pxManufacturerStringDescriptor[] =
\r
321 usbDESCRIPTOR_TYPE_STRING,
\r
333 const char pxProductStringDescriptor[] =
\r
336 usbDESCRIPTOR_TYPE_STRING,
\r
358 const char pxConfigurationStringDescriptor[] =
\r
361 usbDESCRIPTOR_TYPE_STRING,
\r
383 const char pxInterfaceStringDescriptor[] =
\r
386 usbDESCRIPTOR_TYPE_STRING,
\r
404 /* Enumeration descriptors. */
\r
405 const char pxReportDescriptor[] =
\r
407 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
408 0x09, 0x02, /* USAGE (Mouse) */
\r
409 0xa1, 0x01, /* COLLECTION (Application) */
\r
410 0x09, 0x01, /* USAGE (Pointer) */
\r
411 0xa1, 0x00, /* COLLECTION (Physical) */
\r
412 0x95, 0x03, /* REPORT_COUNT (3) */
\r
413 0x75, 0x01, /* REPORT_SIZE (1) */
\r
414 0x05, 0x09, /* USAGE_PAGE (Button) */
\r
415 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
\r
416 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
\r
417 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
\r
418 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
\r
419 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
420 0x95, 0x01, /* REPORT_COUNT (1) */
\r
421 0x75, 0x05, /* REPORT_SIZE (5) */
\r
422 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */
\r
423 0x75, 0x08, /* REPORT_SIZE (8) */
\r
424 0x95, 0x02, /* REPORT_COUNT (2) */
\r
425 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
426 0x09, 0x30, /* USAGE (X) */
\r
427 0x09, 0x31, /* USAGE (Y) */
\r
428 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
429 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
430 0x81, 0x06, /* INPUT (Data,Var,Rel) */
\r
431 0xc0, /* END_COLLECTION */
\r
432 0xc0 /* END_COLLECTION */
\r
437 const char pxDeviceDescriptor[] =
\r
439 /* Device descriptor */
\r
440 0x12, /* bLength */
\r
441 0x01, /* bDescriptorType */
\r
442 0x10, 0x01, /* bcdUSBL */
\r
443 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
444 0x00, /* bDeviceSubclass: */
\r
445 0x00, /* bDeviceProtocol: */
\r
446 0x08, /* bMaxPacketSize0 */
\r
447 0xFF, 0xFF, /* idVendorL */
\r
448 0x02, 0x00, /* idProductL */
\r
449 0x00, 0x01, /* bcdDeviceL */
\r
450 usbMANUFACTURER_STRING, /* iManufacturer */
\r
451 usbPRODUCT_STRING, /* iProduct */
\r
452 0x00, /* SerialNumber */
\r
453 0x01 /* bNumConfigs */
\r
457 const char pxConfigDescriptor[] = {
\r
458 /* Configuration 1 descriptor */
\r
459 0x09, /* CbLength */
\r
460 0x02, /* CbDescriptorType */
\r
461 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
462 0x01, /* CbNumInterfaces */
\r
463 0x01, /* CbConfigurationValue */
\r
464 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
465 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
466 0x32, /* CMaxPower: 100mA */
\r
468 /* Mouse Interface Descriptor Requirement */
\r
469 0x09, /* bLength */
\r
470 0x04, /* bDescriptorType */
\r
471 0x00, /* bInterfaceNumber */
\r
472 0x00, /* bAlternateSetting */
\r
473 0x01, /* bNumEndpoints */
\r
474 0x03, /* bInterfaceClass: HID code */
\r
475 0x01, /* bInterfaceSubclass boot */
\r
476 0x02, /* bInterfaceProtocol mouse boot */
\r
477 usbINTERFACE_STRING,/* iInterface */
\r
479 /* HID Descriptor */
\r
480 0x09, /* bLength */
\r
481 0x21, /* bDescriptor type: HID Descriptor Type */
\r
482 0x00, 0x01, /* bcdHID */
\r
483 0x00, /* bCountryCode */
\r
484 0x01, /* bNumDescriptors */
\r
485 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
486 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
488 /* Endpoint 1 descriptor */
\r
489 0x07, /* bLength */
\r
490 0x05, /* bDescriptorType */
\r
491 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
492 0x03, /* bmAttributes INT */
\r
493 0x08, 0x00, /* wMaxPacketSize: 8? */
\r
494 0x0A /* bInterval */
\r
497 /*-----------------------------------------------------------*/
\r
499 /* File scope state variables. */
\r
500 static unsigned char ucUSBConfig = ( unsigned char ) 0;
\r
501 static unsigned long ulReceivedAddress = ( unsigned long ) 0;
\r
502 static eDRIVER_STATE eDriverState = eNOTHING;
\r
504 /* Structure used to control the characters being sent to the host. */
\r
505 static xTX_MESSAGE pxCharsForTx;
\r
507 /* Queue used to pass messages between the ISR and the task. */
\r
508 xQueueHandle xUSBInterruptQueue;
\r
510 /*-----------------------------------------------------------*/
\r
512 void vStartUSBTask( unsigned portBASE_TYPE uxPriority )
\r
514 /* Create the queue used to communicate between the USB ISR and task. */
\r
515 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
517 /* Create the task itself. */
\r
518 xTaskCreate( vUSBDemoTask, "USB", configMINIMAL_STACK_SIZE, NULL, uxPriority, NULL );
\r
520 /*-----------------------------------------------------------*/
\r
522 static void vUSBDemoTask( void *pvParameters )
\r
524 xISRStatus *pxMessage;
\r
526 /* The parameters are not used in this task. */
\r
527 ( void ) pvParameters;
\r
529 /* Init USB device */
\r
530 portENTER_CRITICAL();
\r
531 vInitUSBInterface();
\r
532 portEXIT_CRITICAL();
\r
534 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
535 interrupt status then posts the information on this queue for processing
\r
536 at the task level. This simple demo implementation only processes
\r
537 a few interrupt sources. */
\r
540 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
542 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
544 /* Process end point 0 interrupt. */
\r
545 prvProcessEndPoint0Interrupt( pxMessage );
\r
548 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
550 /* Process an end of bus reset interrupt. */
\r
551 prvResetEndPoints();
\r
556 /* The ISR did not post any data for us to process on the queue, so
\r
557 just generate and send some sample data. */
\r
558 if( eDriverState == eREADY_TO_SEND )
\r
560 prvTransmitSampleValues();
\r
565 /*-----------------------------------------------------------*/
\r
567 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 )
\r
569 const char cSpeed = 20;
\r
571 if( !( ulInput & ulSwitch1 ) )
\r
573 /* We are going in the decreasing y direction. */
\r
576 /* We have changed direction since last time so start from
\r
581 if( *cVal > -cSpeed )
\r
583 /* Ramp y down to the max speed. */
\r
587 else if( !( ulInput & ulSwitch2 ) )
\r
589 /* We are going in the increasing y direction. */
\r
592 /* We have changed direction since last time, so start from
\r
597 if( *cVal < cSpeed )
\r
599 /* Ramp y up to the max speed again. */
\r
608 /*-----------------------------------------------------------*/
\r
610 static void prvTransmitSampleValues( void )
\r
612 /* Variables to hold dummy x, y and z joystick axis data. */
\r
613 static signed char x = 0, y = 0, z = 0;
\r
614 unsigned long ulStatus;
\r
616 ulStatus = AT91C_BASE_PIOA->PIO_PDSR;
\r
618 prvControlCursorSpeed( &y, ulStatus, ( unsigned long ) usbSW1, ( unsigned long ) usbSW2 );
\r
619 prvControlCursorSpeed( &x, ulStatus, ( unsigned long ) usbSW3, ( unsigned long ) usbSW4 );
\r
621 /* Just make the z axis go up and down. */
\r
622 z = ( ( ulStatus & usbSW_CLICK ) == 0 );
\r
624 /* Can we place data in the fifo? */
\r
625 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
627 /* Write our sample data to the fifo. */
\r
628 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
629 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
630 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
632 /* Send the data. */
\r
633 portENTER_CRITICAL();
\r
635 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
636 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
637 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
639 portEXIT_CRITICAL();
\r
642 /*-----------------------------------------------------------*/
\r
644 static void prvUSBTransmitNull( void )
\r
646 unsigned long ulStatus;
\r
648 /* Wait until the FIFO is free - even though we are not going to use it.
\r
649 THERE IS NO TIMEOUT HERE! */
\r
650 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
652 vTaskDelay( usbSHORTEST_DELAY );
\r
655 portENTER_CRITICAL();
\r
657 /* Set the length of data to send to equal the index of the next byte
\r
658 to send. This will prevent the ACK to this NULL packet causing any
\r
659 further data transmissions. */
\r
660 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
662 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
663 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
664 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
665 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
667 portEXIT_CRITICAL();
\r
669 /*-----------------------------------------------------------*/
\r
671 static void prvSendStall( void )
\r
673 unsigned long ulStatus;
\r
675 portENTER_CRITICAL();
\r
677 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
678 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
679 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
680 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
682 portEXIT_CRITICAL();
\r
684 /*-----------------------------------------------------------*/
\r
686 static void prvResetEndPoints( void )
\r
688 unsigned long ulTemp;
\r
690 eDriverState = eJUST_RESET;
\r
692 /* Reset all the end points. */
\r
693 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
694 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned long ) 0x00;
\r
696 /* Enable data to be sent and received. */
\r
697 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
699 /* Repair the configuration end point. */
\r
700 portENTER_CRITICAL();
\r
702 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
703 usbCSR_SET_BIT( &ulTemp, ( ( unsigned long ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
704 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
705 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT0;
\r
707 portEXIT_CRITICAL();
\r
709 /*-----------------------------------------------------------*/
\r
711 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
713 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
715 /* We only expect to receive zero length data here as ACK's.
\r
716 Set the data pointer to the end of the current Tx packet to
\r
717 ensure we don't send out any more data. */
\r
718 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
721 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
723 /* We received a TX complete interrupt. What we do depends on
\r
724 what we sent to get this interrupt. */
\r
726 if( eDriverState == eJUST_GOT_CONFIG )
\r
728 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
729 are now at the end of the enumeration. */
\r
730 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
732 /* Read the end point for data transfer. */
\r
733 portENTER_CRITICAL();
\r
735 unsigned long ulTemp;
\r
737 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
738 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
739 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
740 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT1;
\r
742 portEXIT_CRITICAL();
\r
744 eDriverState = eREADY_TO_SEND;
\r
746 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
748 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
749 to the addressed state. */
\r
750 if( ulReceivedAddress != ( unsigned long ) 0 )
\r
752 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
756 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
759 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
760 eDriverState = eNOTHING;
\r
764 /* The TXCOMP was not for any special type of transmission. See
\r
765 if there is any more data to send. */
\r
766 prvSendNextSegment();
\r
770 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
772 xUSB_REQUEST xRequest;
\r
773 unsigned char ucRequest;
\r
774 unsigned long ulRxBytes;
\r
776 /* A data packet is available. */
\r
777 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
778 ulRxBytes &= usbRX_COUNT_MASK;
\r
780 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
782 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
784 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
785 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
787 /* NOT PORTABLE CODE! */
\r
788 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
789 xRequest.usValue <<= 8;
\r
790 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
792 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
793 xRequest.usIndex <<= 8;
\r
794 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
796 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
797 xRequest.usLength <<= 8;
\r
798 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
800 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
801 generate a zero based request selection. This is just done to
\r
802 break up the requests into subsections for clarity. The
\r
803 alternative would be to have more huge switch statement that would
\r
804 be difficult to optimise. */
\r
805 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
806 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
808 switch( ucRequest )
\r
810 case usbSTANDARD_DEVICE_REQUEST:
\r
811 /* Standard Device request */
\r
812 prvHandleStandardDeviceRequest( &xRequest );
\r
815 case usbSTANDARD_INTERFACE_REQUEST:
\r
816 /* Standard Interface request */
\r
817 prvHandleStandardInterfaceRequest( &xRequest );
\r
820 case usbSTANDARD_END_POINT_REQUEST:
\r
821 /* Standard Endpoint request */
\r
822 prvHandleStandardEndPointRequest( &xRequest );
\r
825 case usbCLASS_INTERFACE_REQUEST:
\r
826 /* Class Interface request */
\r
827 prvHandleClassInterfaceRequest( &xRequest );
\r
830 default: /* This is not something we want to respond to. */
\r
836 /*-----------------------------------------------------------*/
\r
838 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
840 /* The type is in the high byte. Return whatever has been requested. */
\r
841 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
843 case usbDESCRIPTOR_TYPE_DEVICE:
\r
844 prvSendControlData( ( unsigned char * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
847 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
848 prvSendControlData( ( unsigned char * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
851 case usbDESCRIPTOR_TYPE_STRING:
\r
853 /* The index to the string descriptor is the lower byte. */
\r
854 switch( pxRequest->usValue & 0xff )
\r
856 case usbLANGUAGE_STRING:
\r
857 prvSendControlData( ( unsigned char * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
860 case usbMANUFACTURER_STRING:
\r
861 prvSendControlData( ( unsigned char * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
864 case usbPRODUCT_STRING:
\r
865 prvSendControlData( ( unsigned char * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
868 case usbCONFIGURATION_STRING:
\r
869 prvSendControlData( ( unsigned char * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
872 case usbINTERFACE_STRING:
\r
873 prvSendControlData( ( unsigned char * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
877 /* Don't know what this string is. */
\r
885 /* We are not responding to anything else. */
\r
890 /*-----------------------------------------------------------*/
\r
892 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
894 unsigned short usStatus = 0;
\r
896 switch( pxRequest->ucRequest )
\r
898 case usbGET_STATUS_REQUEST:
\r
899 /* Just send two byte dummy status. */
\r
900 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
903 case usbGET_DESCRIPTOR_REQUEST:
\r
904 /* Send device descriptor */
\r
905 prvGetStandardDeviceDescriptor( pxRequest );
\r
908 case usbGET_CONFIGURATION_REQUEST:
\r
909 /* Send selected device configuration */
\r
910 prvSendControlData( ( unsigned char * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
913 case usbSET_FEATURE_REQUEST:
\r
914 prvUSBTransmitNull();
\r
917 case usbSET_ADDRESS_REQUEST:
\r
919 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
920 cannot actually move to the addressed state until we get a TXCOMP
\r
921 interrupt from this NULL packet. Therefore we just remember the
\r
922 address and set our state so we know we have received the address. */
\r
923 prvUSBTransmitNull();
\r
924 eDriverState = eJUST_GOT_ADDRESS;
\r
925 ulReceivedAddress = ( unsigned long ) pxRequest->usValue;
\r
928 case usbSET_CONFIGURATION_REQUEST:
\r
930 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
931 we cannot actually move to the configured state until we get a
\r
932 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
933 config and set our state so we know we have received the go ahead. */
\r
934 ucUSBConfig = ( unsigned char ) ( pxRequest->usValue & 0xff );
\r
935 eDriverState = eJUST_GOT_CONFIG;
\r
936 prvUSBTransmitNull();
\r
941 /* We don't answer to anything else. */
\r
946 /*-----------------------------------------------------------*/
\r
948 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
950 switch( pxRequest->ucRequest )
\r
952 case usbSET_IDLE_REQUEST:
\r
953 prvUSBTransmitNull();
\r
956 /* This minimal implementation ignores these. */
\r
957 case usbGET_REPORT_REQUEST:
\r
958 case usbGET_IDLE_REQUEST:
\r
959 case usbGET_PROTOCOL_REQUEST:
\r
960 case usbSET_REPORT_REQUEST:
\r
961 case usbSET_PROTOCOL_REQUEST:
\r
968 /*-----------------------------------------------------------*/
\r
970 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
972 switch( ( pxRequest->usValue & ( unsigned short ) 0xff00 ) >> 8 )
\r
974 case usbHID_REPORT_DESCRIPTOR:
\r
975 prvSendControlData( ( unsigned char * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
980 /* Don't expect to send any others. */
\r
985 /*-----------------------------------------------------------*/
\r
987 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
989 unsigned short usStatus = 0;
\r
991 switch( pxRequest->ucRequest )
\r
993 case usbGET_STATUS_REQUEST:
\r
994 /* Send dummy 2 bytes. */
\r
995 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
998 case usbGET_DESCRIPTOR_REQUEST:
\r
999 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1002 /* This minimal implementation does not respond to these. */
\r
1003 case usbGET_INTERFACE_REQUEST:
\r
1004 case usbSET_FEATURE_REQUEST:
\r
1005 case usbSET_INTERFACE_REQUEST:
\r
1012 /*-----------------------------------------------------------*/
\r
1014 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1016 switch( pxRequest->ucRequest )
\r
1018 /* This minimal implementation does not expect to respond to these. */
\r
1019 case usbGET_STATUS_REQUEST:
\r
1020 case usbCLEAR_FEATURE_REQUEST:
\r
1021 case usbSET_FEATURE_REQUEST:
\r
1028 /*-----------------------------------------------------------*/
\r
1030 static void vInitUSBInterface( void )
\r
1032 volatile unsigned long ulTemp;
\r
1034 /* Initialise a few state variables. */
\r
1035 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1036 ucUSBConfig = ( unsigned char ) 0;
\r
1037 eDriverState = eNOTHING;
\r
1039 /* HARDWARE SETUP */
\r
1041 /* Set the PLL USB Divider */
\r
1042 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1044 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1045 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1046 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1048 /* Setup the PIO for the USB pull up resistor. */
\r
1049 AT91C_BASE_PIOA->PIO_PER = AT91C_PIO_PA16;
\r
1050 AT91C_BASE_PIOA->PIO_OER = AT91C_PIO_PA16;
\r
1053 /* Start without the pullup - this will get set at the end of this
\r
1055 AT91C_BASE_PIOA->PIO_SODR = AT91C_PIO_PA16;
\r
1057 /* When using the USB debugger the peripheral registers do not always get
\r
1058 set to the correct default values. To make sure set the relevant registers
\r
1060 AT91C_BASE_UDP->UDP_IDR = ( unsigned long ) 0xffffffff;
\r
1061 AT91C_BASE_UDP->UDP_ICR = ( unsigned long ) 0xffffffff;
\r
1062 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned long ) 0x00;
\r
1063 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned long ) 0x00;
\r
1064 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1065 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1067 /* Enable the transceiver. */
\r
1068 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1070 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1071 enumeration process progresses. */
\r
1072 AT91F_AIC_ConfigureIt( AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vUSB_ISR_Wrapper );
\r
1073 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_UDP;
\r
1075 /* Wait a short while before making our presence known. */
\r
1076 vTaskDelay( usbINIT_DELAY );
\r
1077 AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA16;
\r
1079 /*-----------------------------------------------------------*/
\r
1081 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthToSend, long lSendingDescriptor )
\r
1083 if( ( ( unsigned long ) usRequestedLength < ulLengthToSend ) )
\r
1085 /* Cap the data length to that requested. */
\r
1086 ulLengthToSend = ( unsigned short ) usRequestedLength;
\r
1088 else if( ( ulLengthToSend < ( unsigned long ) usRequestedLength ) && lSendingDescriptor )
\r
1090 /* We are sending a descriptor. If the descriptor is an exact
\r
1091 multiple of the FIFO length then it will have to be terminated
\r
1092 with a NULL packet. Set the state to indicate this if
\r
1094 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1096 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1100 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1101 BUFFER OVERFLOW PROTECTION HERE.
\r
1103 Copy the data to send into the buffer as we cannot send it all at once
\r
1104 (if it is greater than 8 bytes in length). */
\r
1105 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1107 /* Reinitialise the buffer index so we start sending from the start of
\r
1109 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1110 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1112 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1113 TXCOMP interrupts. */
\r
1114 prvSendNextSegment();
\r
1116 /*-----------------------------------------------------------*/
\r
1118 static void prvSendNextSegment( void )
\r
1120 volatile unsigned long ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1122 /* Is there any data to send? */
\r
1123 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1125 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1127 /* We can only send 8 bytes to the fifo at a time. */
\r
1128 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1130 ulNextLength = usbFIFO_LENGTH;
\r
1134 ulNextLength = ulLengthLeftToSend;
\r
1137 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1139 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1141 vTaskDelay( usbSHORTEST_DELAY );
\r
1144 /* Write the data to the FIFO. */
\r
1145 while( ulNextLength > ( unsigned long ) 0 )
\r
1147 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1150 pxCharsForTx.ulNextCharIndex++;
\r
1153 /* Start the transmission. */
\r
1154 portENTER_CRITICAL();
\r
1156 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1157 usbCSR_SET_BIT( &ulStatus, ( ( unsigned long ) 0x10 ) );
\r
1158 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1160 portEXIT_CRITICAL();
\r
1164 /* There is no data to send. If we were sending a descriptor and the
\r
1165 descriptor was an exact multiple of the max packet size then we need
\r
1166 to send a null to terminate the transmission. */
\r
1167 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1169 prvUSBTransmitNull();
\r
1170 eDriverState = eNOTHING;
\r