2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 Sample interrupt driven mouse device driver. This is a minimal implementation
\r
71 for demonstration only. Although functional, it may not be a fully and
\r
72 compliant implementation. The small joystick on the SAM7X EK can be used to
\r
73 move the mouse cursor, pressing the joystick transmits mouse clicks. Note
\r
74 that it might be necessary to run the demo stand along (without the
\r
75 debugger) in order for the USB device to be recognised by the host computer.
\r
77 The interrupt handler itself is contained within USB_ISR.c.
\r
79 See the FreeRTOS.org online documentation for more information.
\r
82 /* Standard includes. */
\r
85 /* Scheduler includes. */
\r
86 #include "FreeRTOS.h"
\r
90 /* Demo application includes. */
\r
91 #include "USBSample.h"
\r
93 /* Joystick inputs used to move the 'mouse' cursor. */
\r
94 #define usbSW1 ( 1 << 21 ) /* PA21 */
\r
95 #define usbSW2 ( 1 << 22 ) /* PA22 */
\r
96 #define usbSW3 ( 1 << 23 ) /* PA23 */
\r
97 #define usbSW4 ( 1 << 24 ) /* PA24 */
\r
98 #define usbSW_CLICK ( 1 << 25 ) /* PA25 */
\r
100 /* Descriptor type definitions. */
\r
101 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
102 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
103 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
105 /* USB request type definitions. */
\r
106 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
107 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
108 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
109 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
110 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
111 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
112 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
113 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
114 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
115 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
116 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
117 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
118 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
119 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
120 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
123 /* Misc USB definitions. */
\r
124 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
125 #define usbBUS_POWERED ( 0x80 )
\r
126 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
127 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
129 /* Index to the various string. */
\r
130 #define usbLANGUAGE_STRING ( 0 )
\r
131 #define usbMANUFACTURER_STRING ( 1 )
\r
132 #define usbPRODUCT_STRING ( 2 )
\r
133 #define usbCONFIGURATION_STRING ( 3 )
\r
134 #define usbINTERFACE_STRING ( 4 )
\r
136 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
137 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
139 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
140 #define usbREQUEST_INDEX ( 6 )
\r
141 #define usbVALUE_HIGH_BYTE ( 4 )
\r
142 #define usbVALUE_LOW_BYTE ( 5 )
\r
143 #define usbINDEX_HIGH_BYTE ( 2 )
\r
144 #define usbINDEX_LOW_BYTE ( 3 )
\r
145 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
146 #define usbLENGTH_LOW_BYTE ( 1 )
\r
148 /* Misc application definitions. */
\r
149 #define usbINTERRUPT_PRIORITY ( 3 )
\r
150 #define usbFIFO_LENGTH ( ( unsigned long ) 8 )
\r
151 #define usbXUP ( 1 )
\r
152 #define usbXDOWN ( 2 )
\r
153 #define usbYUP ( 3 )
\r
154 #define usbYDOWN ( 4 )
\r
155 #define usbMAX_COORD ( 120 )
\r
156 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
157 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
158 #define usbINIT_DELAY ( ( portTickType ) 1000 / portTICK_RATE_MS )
\r
159 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
160 #define usbEND_POINT_RESET_MASK ( ( unsigned long ) 0x0f )
\r
161 #define usbDATA_INC ( ( char ) 5 )
\r
162 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned long ) 8 )
\r
164 /* Control request types. */
\r
165 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
166 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
167 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
168 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
170 /* Structure used to hold the received requests. */
\r
173 unsigned char ucReqType;
\r
174 unsigned char ucRequest;
\r
175 unsigned short usValue;
\r
176 unsigned short usIndex;
\r
177 unsigned short usLength;
\r
186 eSENDING_EVEN_DESCRIPTOR,
\r
190 /* Structure used to control the data being sent to the host. */
\r
193 unsigned char ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
194 unsigned long ulNextCharIndex;
\r
195 unsigned long ulTotalDataLength;
\r
198 /*-----------------------------------------------------------*/
\r
201 * The USB interrupt service routine. This takes a snapshot of the USB
\r
202 * device at the time of the interrupt, clears the interrupts, and posts
\r
203 * the data to the USB processing task. This is implemented in USB_ISR.c.
\r
205 extern void vUSB_ISR_Wrapper( void );
\r
208 * Called after the bus reset interrupt - this function readies all the
\r
209 * end points for communication.
\r
211 static void prvResetEndPoints( void );
\r
214 * Setup the USB hardware, install the interrupt service routine and
\r
215 * initialise all the state variables.
\r
217 static void vInitUSBInterface( void );
\r
220 * Decode and act upon an interrupt generated by the control end point.
\r
222 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
225 * For simplicity requests are separated into device, interface, class
\r
226 * interface and end point requests.
\r
228 * Decode and handle standard device requests originating on the control
\r
231 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
234 * For simplicity requests are separated into device, interface, class
\r
235 * interface and end point requests.
\r
237 * Decode and handle standard interface requests originating on the control
\r
240 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
243 * For simplicity requests are separated into device, interface, class
\r
244 * interface and end point requests.
\r
246 * Decode and handle standard end point requests originating on the control
\r
249 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
252 * For simplicity requests are separated into device, interface, class
\r
253 * interface and end point requests.
\r
255 * Decode and handle the class interface requests.
\r
257 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
260 * Setup the Tx buffer to send data in response to a control request.
\r
262 * The data to be transmitted is buffered, the state variables are updated,
\r
263 * then prvSendNextSegment() is called to start the transmission off. Once
\r
264 * the first segment has been sent the remaining segments are transmitted
\r
265 * in response to TXCOMP interrupts until the entire buffer has been
\r
268 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthLeftToSend, long lSendingDescriptor );
\r
271 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
273 * If there is data to be transmitted then send the next segment. A segment
\r
274 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
275 * point by the descriptor). The final segment may be less than 8 bytes if
\r
276 * the total data length was not an exact multiple of 8.
\r
278 static void prvSendNextSegment( void );
\r
281 * A stall condition is forced each time the host makes a request that is not
\r
282 * supported by this minimal implementation.
\r
284 * A stall is forced by setting the appropriate bit in the end points control
\r
285 * and status register.
\r
287 static void prvSendStall( void );
\r
290 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
291 * of certain events from the host.
\r
293 static void prvUSBTransmitNull( void );
\r
296 * When the host requests a descriptor this function is called to determine
\r
297 * which descriptor is being requested and start its transmission.
\r
299 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
302 * Transmit movement and clicks on the EK joystick as mouse inputs.
\r
304 static void prvTransmitSampleValues( void );
\r
307 * The created task to handle the USB demo functionality.
\r
309 static void vUSBDemoTask( void *pvParameters );
\r
312 * Simple algorithm to ramp up the mouse cursor speed to make it easier to
\r
315 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 );
\r
316 /*-----------------------------------------------------------*/
\r
319 - DESCRIPTOR DEFINITIONS -
\r
322 /* String descriptors used during the enumeration process.
\r
323 These take the form:
\r
326 Length of descriptor,
\r
331 const char pxLanguageStringDescriptor[] =
\r
334 usbDESCRIPTOR_TYPE_STRING,
\r
338 const char pxManufacturerStringDescriptor[] =
\r
341 usbDESCRIPTOR_TYPE_STRING,
\r
353 const char pxProductStringDescriptor[] =
\r
356 usbDESCRIPTOR_TYPE_STRING,
\r
378 const char pxConfigurationStringDescriptor[] =
\r
381 usbDESCRIPTOR_TYPE_STRING,
\r
403 const char pxInterfaceStringDescriptor[] =
\r
406 usbDESCRIPTOR_TYPE_STRING,
\r
424 /* Enumeration descriptors. */
\r
425 const char pxReportDescriptor[] =
\r
427 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
428 0x09, 0x02, /* USAGE (Mouse) */
\r
429 0xa1, 0x01, /* COLLECTION (Application) */
\r
430 0x09, 0x01, /* USAGE (Pointer) */
\r
431 0xa1, 0x00, /* COLLECTION (Physical) */
\r
432 0x95, 0x03, /* REPORT_COUNT (3) */
\r
433 0x75, 0x01, /* REPORT_SIZE (1) */
\r
434 0x05, 0x09, /* USAGE_PAGE (Button) */
\r
435 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
\r
436 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
\r
437 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
\r
438 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
\r
439 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
440 0x95, 0x01, /* REPORT_COUNT (1) */
\r
441 0x75, 0x05, /* REPORT_SIZE (5) */
\r
442 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */
\r
443 0x75, 0x08, /* REPORT_SIZE (8) */
\r
444 0x95, 0x02, /* REPORT_COUNT (2) */
\r
445 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
446 0x09, 0x30, /* USAGE (X) */
\r
447 0x09, 0x31, /* USAGE (Y) */
\r
448 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
449 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
450 0x81, 0x06, /* INPUT (Data,Var,Rel) */
\r
451 0xc0, /* END_COLLECTION */
\r
452 0xc0 /* END_COLLECTION */
\r
457 const char pxDeviceDescriptor[] =
\r
459 /* Device descriptor */
\r
460 0x12, /* bLength */
\r
461 0x01, /* bDescriptorType */
\r
462 0x10, 0x01, /* bcdUSBL */
\r
463 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
464 0x00, /* bDeviceSubclass: */
\r
465 0x00, /* bDeviceProtocol: */
\r
466 0x08, /* bMaxPacketSize0 */
\r
467 0xFF, 0xFF, /* idVendorL */
\r
468 0x02, 0x00, /* idProductL */
\r
469 0x00, 0x01, /* bcdDeviceL */
\r
470 usbMANUFACTURER_STRING, /* iManufacturer */
\r
471 usbPRODUCT_STRING, /* iProduct */
\r
472 0x00, /* SerialNumber */
\r
473 0x01 /* bNumConfigs */
\r
477 const char pxConfigDescriptor[] = {
\r
478 /* Configuration 1 descriptor */
\r
479 0x09, /* CbLength */
\r
480 0x02, /* CbDescriptorType */
\r
481 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
482 0x01, /* CbNumInterfaces */
\r
483 0x01, /* CbConfigurationValue */
\r
484 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
485 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
486 0x32, /* CMaxPower: 100mA */
\r
488 /* Mouse Interface Descriptor Requirement */
\r
489 0x09, /* bLength */
\r
490 0x04, /* bDescriptorType */
\r
491 0x00, /* bInterfaceNumber */
\r
492 0x00, /* bAlternateSetting */
\r
493 0x01, /* bNumEndpoints */
\r
494 0x03, /* bInterfaceClass: HID code */
\r
495 0x01, /* bInterfaceSubclass boot */
\r
496 0x02, /* bInterfaceProtocol mouse boot */
\r
497 usbINTERFACE_STRING,/* iInterface */
\r
499 /* HID Descriptor */
\r
500 0x09, /* bLength */
\r
501 0x21, /* bDescriptor type: HID Descriptor Type */
\r
502 0x00, 0x01, /* bcdHID */
\r
503 0x00, /* bCountryCode */
\r
504 0x01, /* bNumDescriptors */
\r
505 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
506 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
508 /* Endpoint 1 descriptor */
\r
509 0x07, /* bLength */
\r
510 0x05, /* bDescriptorType */
\r
511 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
512 0x03, /* bmAttributes INT */
\r
513 0x08, 0x00, /* wMaxPacketSize: 8? */
\r
514 0x0A /* bInterval */
\r
517 /*-----------------------------------------------------------*/
\r
519 /* File scope state variables. */
\r
520 static unsigned char ucUSBConfig = ( unsigned char ) 0;
\r
521 static unsigned long ulReceivedAddress = ( unsigned long ) 0;
\r
522 static eDRIVER_STATE eDriverState = eNOTHING;
\r
524 /* Structure used to control the characters being sent to the host. */
\r
525 static xTX_MESSAGE pxCharsForTx;
\r
527 /* Queue used to pass messages between the ISR and the task. */
\r
528 xQueueHandle xUSBInterruptQueue;
\r
530 /*-----------------------------------------------------------*/
\r
532 void vStartUSBTask( unsigned portBASE_TYPE uxPriority )
\r
534 /* Create the queue used to communicate between the USB ISR and task. */
\r
535 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
537 /* Create the task itself. */
\r
538 xTaskCreate( vUSBDemoTask, "USB", configMINIMAL_STACK_SIZE, NULL, uxPriority, NULL );
\r
540 /*-----------------------------------------------------------*/
\r
542 static void vUSBDemoTask( void *pvParameters )
\r
544 xISRStatus *pxMessage;
\r
546 /* The parameters are not used in this task. */
\r
547 ( void ) pvParameters;
\r
549 /* Init USB device */
\r
550 portENTER_CRITICAL();
\r
551 vInitUSBInterface();
\r
552 portEXIT_CRITICAL();
\r
554 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
555 interrupt status then posts the information on this queue for processing
\r
556 at the task level. This simple demo implementation only processes
\r
557 a few interrupt sources. */
\r
560 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
562 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
564 /* Process end point 0 interrupt. */
\r
565 prvProcessEndPoint0Interrupt( pxMessage );
\r
568 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
570 /* Process an end of bus reset interrupt. */
\r
571 prvResetEndPoints();
\r
576 /* The ISR did not post any data for us to process on the queue, so
\r
577 just generate and send some sample data. */
\r
578 if( eDriverState == eREADY_TO_SEND )
\r
580 prvTransmitSampleValues();
\r
585 /*-----------------------------------------------------------*/
\r
587 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 )
\r
589 const char cSpeed = 20;
\r
591 if( !( ulInput & ulSwitch1 ) )
\r
593 /* We are going in the decreasing y direction. */
\r
596 /* We have changed direction since last time so start from
\r
601 if( *cVal > -cSpeed )
\r
603 /* Ramp y down to the max speed. */
\r
607 else if( !( ulInput & ulSwitch2 ) )
\r
609 /* We are going in the increasing y direction. */
\r
612 /* We have changed direction since last time, so start from
\r
617 if( *cVal < cSpeed )
\r
619 /* Ramp y up to the max speed again. */
\r
628 /*-----------------------------------------------------------*/
\r
630 static void prvTransmitSampleValues( void )
\r
632 /* Variables to hold dummy x, y and z joystick axis data. */
\r
633 static signed char x = 0, y = 0, z = 0;
\r
634 unsigned long ulStatus;
\r
636 ulStatus = AT91C_BASE_PIOA->PIO_PDSR;
\r
638 prvControlCursorSpeed( &y, ulStatus, ( unsigned long ) usbSW1, ( unsigned long ) usbSW2 );
\r
639 prvControlCursorSpeed( &x, ulStatus, ( unsigned long ) usbSW3, ( unsigned long ) usbSW4 );
\r
641 /* Just make the z axis go up and down. */
\r
642 z = ( ( ulStatus & usbSW_CLICK ) == 0 );
\r
644 /* Can we place data in the fifo? */
\r
645 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
647 /* Write our sample data to the fifo. */
\r
648 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
649 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
650 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
652 /* Send the data. */
\r
653 portENTER_CRITICAL();
\r
655 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
656 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
657 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
659 portEXIT_CRITICAL();
\r
662 /*-----------------------------------------------------------*/
\r
664 static void prvUSBTransmitNull( void )
\r
666 unsigned long ulStatus;
\r
668 /* Wait until the FIFO is free - even though we are not going to use it.
\r
669 THERE IS NO TIMEOUT HERE! */
\r
670 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
672 vTaskDelay( usbSHORTEST_DELAY );
\r
675 portENTER_CRITICAL();
\r
677 /* Set the length of data to send to equal the index of the next byte
\r
678 to send. This will prevent the ACK to this NULL packet causing any
\r
679 further data transmissions. */
\r
680 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
682 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
683 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
684 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
685 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
687 portEXIT_CRITICAL();
\r
689 /*-----------------------------------------------------------*/
\r
691 static void prvSendStall( void )
\r
693 unsigned long ulStatus;
\r
695 portENTER_CRITICAL();
\r
697 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
698 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
699 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
700 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
702 portEXIT_CRITICAL();
\r
704 /*-----------------------------------------------------------*/
\r
706 static void prvResetEndPoints( void )
\r
708 unsigned long ulTemp;
\r
710 eDriverState = eJUST_RESET;
\r
712 /* Reset all the end points. */
\r
713 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
714 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned long ) 0x00;
\r
716 /* Enable data to be sent and received. */
\r
717 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
719 /* Repair the configuration end point. */
\r
720 portENTER_CRITICAL();
\r
722 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
723 usbCSR_SET_BIT( &ulTemp, ( ( unsigned long ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
724 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
725 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT0;
\r
727 portEXIT_CRITICAL();
\r
729 /*-----------------------------------------------------------*/
\r
731 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
733 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
735 /* We only expect to receive zero length data here as ACK's.
\r
736 Set the data pointer to the end of the current Tx packet to
\r
737 ensure we don't send out any more data. */
\r
738 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
741 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
743 /* We received a TX complete interrupt. What we do depends on
\r
744 what we sent to get this interrupt. */
\r
746 if( eDriverState == eJUST_GOT_CONFIG )
\r
748 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
749 are now at the end of the enumeration. */
\r
750 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
752 /* Read the end point for data transfer. */
\r
753 portENTER_CRITICAL();
\r
755 unsigned long ulTemp;
\r
757 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
758 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
759 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
760 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT1;
\r
762 portEXIT_CRITICAL();
\r
764 eDriverState = eREADY_TO_SEND;
\r
766 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
768 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
769 to the addressed state. */
\r
770 if( ulReceivedAddress != ( unsigned long ) 0 )
\r
772 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
776 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
779 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
780 eDriverState = eNOTHING;
\r
784 /* The TXCOMP was not for any special type of transmission. See
\r
785 if there is any more data to send. */
\r
786 prvSendNextSegment();
\r
790 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
792 xUSB_REQUEST xRequest;
\r
793 unsigned char ucRequest;
\r
794 unsigned long ulRxBytes;
\r
796 /* A data packet is available. */
\r
797 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
798 ulRxBytes &= usbRX_COUNT_MASK;
\r
800 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
802 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
804 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
805 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
807 /* NOT PORTABLE CODE! */
\r
808 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
809 xRequest.usValue <<= 8;
\r
810 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
812 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
813 xRequest.usIndex <<= 8;
\r
814 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
816 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
817 xRequest.usLength <<= 8;
\r
818 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
820 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
821 generate a zero based request selection. This is just done to
\r
822 break up the requests into subsections for clarity. The
\r
823 alternative would be to have more huge switch statement that would
\r
824 be difficult to optimise. */
\r
825 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
826 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
828 switch( ucRequest )
\r
830 case usbSTANDARD_DEVICE_REQUEST:
\r
831 /* Standard Device request */
\r
832 prvHandleStandardDeviceRequest( &xRequest );
\r
835 case usbSTANDARD_INTERFACE_REQUEST:
\r
836 /* Standard Interface request */
\r
837 prvHandleStandardInterfaceRequest( &xRequest );
\r
840 case usbSTANDARD_END_POINT_REQUEST:
\r
841 /* Standard Endpoint request */
\r
842 prvHandleStandardEndPointRequest( &xRequest );
\r
845 case usbCLASS_INTERFACE_REQUEST:
\r
846 /* Class Interface request */
\r
847 prvHandleClassInterfaceRequest( &xRequest );
\r
850 default: /* This is not something we want to respond to. */
\r
856 /*-----------------------------------------------------------*/
\r
858 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
860 /* The type is in the high byte. Return whatever has been requested. */
\r
861 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
863 case usbDESCRIPTOR_TYPE_DEVICE:
\r
864 prvSendControlData( ( unsigned char * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
867 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
868 prvSendControlData( ( unsigned char * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
871 case usbDESCRIPTOR_TYPE_STRING:
\r
873 /* The index to the string descriptor is the lower byte. */
\r
874 switch( pxRequest->usValue & 0xff )
\r
876 case usbLANGUAGE_STRING:
\r
877 prvSendControlData( ( unsigned char * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
880 case usbMANUFACTURER_STRING:
\r
881 prvSendControlData( ( unsigned char * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
884 case usbPRODUCT_STRING:
\r
885 prvSendControlData( ( unsigned char * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
888 case usbCONFIGURATION_STRING:
\r
889 prvSendControlData( ( unsigned char * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
892 case usbINTERFACE_STRING:
\r
893 prvSendControlData( ( unsigned char * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
897 /* Don't know what this string is. */
\r
905 /* We are not responding to anything else. */
\r
910 /*-----------------------------------------------------------*/
\r
912 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
914 unsigned short usStatus = 0;
\r
916 switch( pxRequest->ucRequest )
\r
918 case usbGET_STATUS_REQUEST:
\r
919 /* Just send two byte dummy status. */
\r
920 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
923 case usbGET_DESCRIPTOR_REQUEST:
\r
924 /* Send device descriptor */
\r
925 prvGetStandardDeviceDescriptor( pxRequest );
\r
928 case usbGET_CONFIGURATION_REQUEST:
\r
929 /* Send selected device configuration */
\r
930 prvSendControlData( ( unsigned char * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
933 case usbSET_FEATURE_REQUEST:
\r
934 prvUSBTransmitNull();
\r
937 case usbSET_ADDRESS_REQUEST:
\r
939 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
940 cannot actually move to the addressed state until we get a TXCOMP
\r
941 interrupt from this NULL packet. Therefore we just remember the
\r
942 address and set our state so we know we have received the address. */
\r
943 prvUSBTransmitNull();
\r
944 eDriverState = eJUST_GOT_ADDRESS;
\r
945 ulReceivedAddress = ( unsigned long ) pxRequest->usValue;
\r
948 case usbSET_CONFIGURATION_REQUEST:
\r
950 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
951 we cannot actually move to the configured state until we get a
\r
952 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
953 config and set our state so we know we have received the go ahead. */
\r
954 ucUSBConfig = ( unsigned char ) ( pxRequest->usValue & 0xff );
\r
955 eDriverState = eJUST_GOT_CONFIG;
\r
956 prvUSBTransmitNull();
\r
961 /* We don't answer to anything else. */
\r
966 /*-----------------------------------------------------------*/
\r
968 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
970 switch( pxRequest->ucRequest )
\r
972 case usbSET_IDLE_REQUEST:
\r
973 prvUSBTransmitNull();
\r
976 /* This minimal implementation ignores these. */
\r
977 case usbGET_REPORT_REQUEST:
\r
978 case usbGET_IDLE_REQUEST:
\r
979 case usbGET_PROTOCOL_REQUEST:
\r
980 case usbSET_REPORT_REQUEST:
\r
981 case usbSET_PROTOCOL_REQUEST:
\r
988 /*-----------------------------------------------------------*/
\r
990 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
992 switch( ( pxRequest->usValue & ( unsigned short ) 0xff00 ) >> 8 )
\r
994 case usbHID_REPORT_DESCRIPTOR:
\r
995 prvSendControlData( ( unsigned char * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
1000 /* Don't expect to send any others. */
\r
1005 /*-----------------------------------------------------------*/
\r
1007 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
1009 unsigned short usStatus = 0;
\r
1011 switch( pxRequest->ucRequest )
\r
1013 case usbGET_STATUS_REQUEST:
\r
1014 /* Send dummy 2 bytes. */
\r
1015 prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1018 case usbGET_DESCRIPTOR_REQUEST:
\r
1019 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1022 /* This minimal implementation does not respond to these. */
\r
1023 case usbGET_INTERFACE_REQUEST:
\r
1024 case usbSET_FEATURE_REQUEST:
\r
1025 case usbSET_INTERFACE_REQUEST:
\r
1032 /*-----------------------------------------------------------*/
\r
1034 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1036 switch( pxRequest->ucRequest )
\r
1038 /* This minimal implementation does not expect to respond to these. */
\r
1039 case usbGET_STATUS_REQUEST:
\r
1040 case usbCLEAR_FEATURE_REQUEST:
\r
1041 case usbSET_FEATURE_REQUEST:
\r
1048 /*-----------------------------------------------------------*/
\r
1050 static void vInitUSBInterface( void )
\r
1052 volatile unsigned long ulTemp;
\r
1054 /* Initialise a few state variables. */
\r
1055 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1056 ucUSBConfig = ( unsigned char ) 0;
\r
1057 eDriverState = eNOTHING;
\r
1059 /* HARDWARE SETUP */
\r
1061 /* Set the PLL USB Divider */
\r
1062 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1064 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1065 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1066 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1068 /* Setup the PIO for the USB pull up resistor. */
\r
1069 AT91C_BASE_PIOA->PIO_PER = AT91C_PIO_PA16;
\r
1070 AT91C_BASE_PIOA->PIO_OER = AT91C_PIO_PA16;
\r
1073 /* Start without the pullup - this will get set at the end of this
\r
1075 AT91C_BASE_PIOA->PIO_SODR = AT91C_PIO_PA16;
\r
1077 /* When using the USB debugger the peripheral registers do not always get
\r
1078 set to the correct default values. To make sure set the relevant registers
\r
1080 AT91C_BASE_UDP->UDP_IDR = ( unsigned long ) 0xffffffff;
\r
1081 AT91C_BASE_UDP->UDP_ICR = ( unsigned long ) 0xffffffff;
\r
1082 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned long ) 0x00;
\r
1083 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned long ) 0x00;
\r
1084 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1085 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1087 /* Enable the transceiver. */
\r
1088 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1090 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1091 enumeration process progresses. */
\r
1092 AT91F_AIC_ConfigureIt( AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vUSB_ISR_Wrapper );
\r
1093 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_UDP;
\r
1095 /* Wait a short while before making our presence known. */
\r
1096 vTaskDelay( usbINIT_DELAY );
\r
1097 AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA16;
\r
1099 /*-----------------------------------------------------------*/
\r
1101 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthToSend, long lSendingDescriptor )
\r
1103 if( ( ( unsigned long ) usRequestedLength < ulLengthToSend ) )
\r
1105 /* Cap the data length to that requested. */
\r
1106 ulLengthToSend = ( unsigned short ) usRequestedLength;
\r
1108 else if( ( ulLengthToSend < ( unsigned long ) usRequestedLength ) && lSendingDescriptor )
\r
1110 /* We are sending a descriptor. If the descriptor is an exact
\r
1111 multiple of the FIFO length then it will have to be terminated
\r
1112 with a NULL packet. Set the state to indicate this if
\r
1114 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1116 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1120 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1121 BUFFER OVERFLOW PROTECTION HERE.
\r
1123 Copy the data to send into the buffer as we cannot send it all at once
\r
1124 (if it is greater than 8 bytes in length). */
\r
1125 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1127 /* Reinitialise the buffer index so we start sending from the start of
\r
1129 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1130 pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;
\r
1132 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1133 TXCOMP interrupts. */
\r
1134 prvSendNextSegment();
\r
1136 /*-----------------------------------------------------------*/
\r
1138 static void prvSendNextSegment( void )
\r
1140 volatile unsigned long ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1142 /* Is there any data to send? */
\r
1143 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1145 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1147 /* We can only send 8 bytes to the fifo at a time. */
\r
1148 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1150 ulNextLength = usbFIFO_LENGTH;
\r
1154 ulNextLength = ulLengthLeftToSend;
\r
1157 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1159 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1161 vTaskDelay( usbSHORTEST_DELAY );
\r
1164 /* Write the data to the FIFO. */
\r
1165 while( ulNextLength > ( unsigned long ) 0 )
\r
1167 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1170 pxCharsForTx.ulNextCharIndex++;
\r
1173 /* Start the transmission. */
\r
1174 portENTER_CRITICAL();
\r
1176 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1177 usbCSR_SET_BIT( &ulStatus, ( ( unsigned long ) 0x10 ) );
\r
1178 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1180 portEXIT_CRITICAL();
\r
1184 /* There is no data to send. If we were sending a descriptor and the
\r
1185 descriptor was an exact multiple of the max packet size then we need
\r
1186 to send a null to terminate the transmission. */
\r
1187 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1189 prvUSBTransmitNull();
\r
1190 eDriverState = eNOTHING;
\r