2 FreeRTOS.org V5.1.2 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
31 * This is a concise, step by step, 'hands on' guide that describes both *
\r
32 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
33 * explains numerous examples that are written using the FreeRTOS API. *
\r
34 * Full source code for all the examples is provided in an accompanying *
\r
37 ***************************************************************************
\r
38 ***************************************************************************
\r
40 Please ensure to read the configuration and relevant port sections of the
\r
41 online documentation.
\r
43 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
46 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
49 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
50 licensing and training services.
\r
54 Sample interrupt driven mouse device driver. This is a minimal implementation
\r
55 for demonstration only. Although functional, it may not be a fully and
\r
56 compliant implementation. The small joystick on the SAM7X EK can be used to
\r
57 move the mouse cursor, pressing the joystick transmits mouse clicks. Note
\r
58 that it might be necessary to run the demo stand along (without the
\r
59 debugger) in order for the USB device to be recognised by the host computer.
\r
61 The interrupt handler itself is contained within USB_ISR.c.
\r
63 See the FreeRTOS.org online documentation for more information.
\r
66 /* Standard includes. */
\r
69 /* Scheduler includes. */
\r
70 #include "FreeRTOS.h"
\r
74 /* Demo application includes. */
\r
75 #include "USBSample.h"
\r
77 /* Joystick inputs used to move the 'mouse' cursor. */
\r
78 #define usbSW1 ( 1 << 21 ) /* PA21 */
\r
79 #define usbSW2 ( 1 << 22 ) /* PA22 */
\r
80 #define usbSW3 ( 1 << 23 ) /* PA23 */
\r
81 #define usbSW4 ( 1 << 24 ) /* PA24 */
\r
82 #define usbSW_CLICK ( 1 << 25 ) /* PA25 */
\r
84 /* Descriptor type definitions. */
\r
85 #define usbDESCRIPTOR_TYPE_DEVICE ( 0x01 )
\r
86 #define usbDESCRIPTOR_TYPE_CONFIGURATION ( 0x02 )
\r
87 #define usbDESCRIPTOR_TYPE_STRING ( 0x03 )
\r
89 /* USB request type definitions. */
\r
90 #define usbGET_REPORT_REQUEST ( 0x01 )
\r
91 #define usbGET_IDLE_REQUEST ( 0x02 )
\r
92 #define usbGET_PROTOCOL_REQUEST ( 0x03 )
\r
93 #define usbSET_REPORT_REQUEST ( 0x09 )
\r
94 #define usbSET_IDLE_REQUEST ( 0x0A )
\r
95 #define usbSET_PROTOCOL_REQUEST ( 0x0B )
\r
96 #define usbGET_CONFIGURATION_REQUEST ( 0x08 )
\r
97 #define usbGET_STATUS_REQUEST ( 0x00 )
\r
98 #define usbCLEAR_FEATURE_REQUEST ( 0x01 )
\r
99 #define usbSET_FEATURE_REQUEST ( 0x03 )
\r
100 #define usbSET_ADDRESS_REQUEST ( 0x05 )
\r
101 #define usbGET_DESCRIPTOR_REQUEST ( 0x06 )
\r
102 #define usbSET_CONFIGURATION_REQUEST ( 0x09 )
\r
103 #define usbGET_INTERFACE_REQUEST ( 0x0A )
\r
104 #define usbSET_INTERFACE_REQUEST ( 0x0B )
\r
107 /* Misc USB definitions. */
\r
108 #define usbDEVICE_CLASS_VENDOR_SPECIFIC ( 0xFF )
\r
109 #define usbBUS_POWERED ( 0x80 )
\r
110 #define usbHID_REPORT_DESCRIPTOR ( 0x22 )
\r
111 #define AT91C_UDP_TRANSCEIVER_ENABLE ( *( ( unsigned long * ) 0xfffb0074 ) )
\r
113 /* Index to the various string. */
\r
114 #define usbLANGUAGE_STRING ( 0 )
\r
115 #define usbMANUFACTURER_STRING ( 1 )
\r
116 #define usbPRODUCT_STRING ( 2 )
\r
117 #define usbCONFIGURATION_STRING ( 3 )
\r
118 #define usbINTERFACE_STRING ( 4 )
\r
120 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]
\r
121 into xUSB_REQUEST. The data order is designed for speed - so looks a
\r
123 #define usbREQUEST_TYPE_INDEX ( 7 )
\r
124 #define usbREQUEST_INDEX ( 6 )
\r
125 #define usbVALUE_HIGH_BYTE ( 4 )
\r
126 #define usbVALUE_LOW_BYTE ( 5 )
\r
127 #define usbINDEX_HIGH_BYTE ( 2 )
\r
128 #define usbINDEX_LOW_BYTE ( 3 )
\r
129 #define usbLENGTH_HIGH_BYTE ( 0 )
\r
130 #define usbLENGTH_LOW_BYTE ( 1 )
\r
132 /* Misc application definitions. */
\r
133 #define usbINTERRUPT_PRIORITY ( 3 )
\r
134 #define usbFIFO_LENGTH ( ( unsigned portLONG ) 8 )
\r
135 #define usbXUP ( 1 )
\r
136 #define usbXDOWN ( 2 )
\r
137 #define usbYUP ( 3 )
\r
138 #define usbYDOWN ( 4 )
\r
139 #define usbMAX_COORD ( 120 )
\r
140 #define usbMAX_TX_MESSAGE_SIZE ( 128 )
\r
141 #define usbSHORTEST_DELAY ( ( portTickType ) 1 )
\r
142 #define usbINIT_DELAY ( ( portTickType ) 1000 / portTICK_RATE_MS )
\r
143 #define usbSHORT_DELAY ( ( portTickType ) 50 / portTICK_RATE_MS )
\r
144 #define usbEND_POINT_RESET_MASK ( ( unsigned portLONG ) 0x0f )
\r
145 #define usbDATA_INC ( ( portCHAR ) 5 )
\r
146 #define usbEXPECTED_NUMBER_OF_BYTES ( ( unsigned portLONG ) 8 )
\r
148 /* Control request types. */
\r
149 #define usbSTANDARD_DEVICE_REQUEST ( 0 )
\r
150 #define usbSTANDARD_INTERFACE_REQUEST ( 1 )
\r
151 #define usbSTANDARD_END_POINT_REQUEST ( 2 )
\r
152 #define usbCLASS_INTERFACE_REQUEST ( 5 )
\r
154 /* Structure used to hold the received requests. */
\r
157 unsigned portCHAR ucReqType;
\r
158 unsigned portCHAR ucRequest;
\r
159 unsigned portSHORT usValue;
\r
160 unsigned portSHORT usIndex;
\r
161 unsigned portSHORT usLength;
\r
170 eSENDING_EVEN_DESCRIPTOR,
\r
174 /* Structure used to control the data being sent to the host. */
\r
177 unsigned portCHAR ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];
\r
178 unsigned portLONG ulNextCharIndex;
\r
179 unsigned portLONG ulTotalDataLength;
\r
182 /*-----------------------------------------------------------*/
\r
185 * The USB interrupt service routine. This takes a snapshot of the USB
\r
186 * device at the time of the interrupt, clears the interrupts, and posts
\r
187 * the data to the USB processing task. This is implemented in USB_ISR.c.
\r
189 extern void vUSB_ISR_Wrapper( void );
\r
192 * Called after the bus reset interrupt - this function readies all the
\r
193 * end points for communication.
\r
195 static void prvResetEndPoints( void );
\r
198 * Setup the USB hardware, install the interrupt service routine and
\r
199 * initialise all the state variables.
\r
201 static void vInitUSBInterface( void );
\r
204 * Decode and act upon an interrupt generated by the control end point.
\r
206 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );
\r
209 * For simplicity requests are separated into device, interface, class
\r
210 * interface and end point requests.
\r
212 * Decode and handle standard device requests originating on the control
\r
215 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );
\r
218 * For simplicity requests are separated into device, interface, class
\r
219 * interface and end point requests.
\r
221 * Decode and handle standard interface requests originating on the control
\r
224 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
227 * For simplicity requests are separated into device, interface, class
\r
228 * interface and end point requests.
\r
230 * Decode and handle standard end point requests originating on the control
\r
233 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );
\r
236 * For simplicity requests are separated into device, interface, class
\r
237 * interface and end point requests.
\r
239 * Decode and handle the class interface requests.
\r
241 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );
\r
244 * Setup the Tx buffer to send data in response to a control request.
\r
246 * The data to be transmitted is buffered, the state variables are updated,
\r
247 * then prvSendNextSegment() is called to start the transmission off. Once
\r
248 * the first segment has been sent the remaining segments are transmitted
\r
249 * in response to TXCOMP interrupts until the entire buffer has been
\r
252 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthLeftToSend, portLONG lSendingDescriptor );
\r
255 * Examine the Tx buffer to see if there is any more data to be transmitted.
\r
257 * If there is data to be transmitted then send the next segment. A segment
\r
258 * can have a maximum of 8 bytes (this is defined as the maximum for the end
\r
259 * point by the descriptor). The final segment may be less than 8 bytes if
\r
260 * the total data length was not an exact multiple of 8.
\r
262 static void prvSendNextSegment( void );
\r
265 * A stall condition is forced each time the host makes a request that is not
\r
266 * supported by this minimal implementation.
\r
268 * A stall is forced by setting the appropriate bit in the end points control
\r
269 * and status register.
\r
271 static void prvSendStall( void );
\r
274 * A NULL (or zero length packet) is transmitted in acknowledge the reception
\r
275 * of certain events from the host.
\r
277 static void prvUSBTransmitNull( void );
\r
280 * When the host requests a descriptor this function is called to determine
\r
281 * which descriptor is being requested and start its transmission.
\r
283 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );
\r
286 * Transmit movement and clicks on the EK joystick as mouse inputs.
\r
288 static void prvTransmitSampleValues( void );
\r
291 * The created task to handle the USB demo functionality.
\r
293 static void vUSBDemoTask( void *pvParameters );
\r
296 * Simple algorithm to ramp up the mouse cursor speed to make it easier to
\r
299 static void prvControlCursorSpeed( signed portCHAR *cVal, unsigned portLONG ulInput, unsigned portLONG ulSwitch1, unsigned portLONG ulSwitch2 );
\r
300 /*-----------------------------------------------------------*/
\r
303 - DESCRIPTOR DEFINITIONS -
\r
306 /* String descriptors used during the enumeration process.
\r
307 These take the form:
\r
310 Length of descriptor,
\r
315 const portCHAR pxLanguageStringDescriptor[] =
\r
318 usbDESCRIPTOR_TYPE_STRING,
\r
322 const portCHAR pxManufacturerStringDescriptor[] =
\r
325 usbDESCRIPTOR_TYPE_STRING,
\r
337 const portCHAR pxProductStringDescriptor[] =
\r
340 usbDESCRIPTOR_TYPE_STRING,
\r
362 const portCHAR pxConfigurationStringDescriptor[] =
\r
365 usbDESCRIPTOR_TYPE_STRING,
\r
387 const portCHAR pxInterfaceStringDescriptor[] =
\r
390 usbDESCRIPTOR_TYPE_STRING,
\r
408 /* Enumeration descriptors. */
\r
409 const portCHAR pxReportDescriptor[] =
\r
411 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
412 0x09, 0x02, /* USAGE (Mouse) */
\r
413 0xa1, 0x01, /* COLLECTION (Application) */
\r
414 0x09, 0x01, /* USAGE (Pointer) */
\r
415 0xa1, 0x00, /* COLLECTION (Physical) */
\r
416 0x95, 0x03, /* REPORT_COUNT (3) */
\r
417 0x75, 0x01, /* REPORT_SIZE (1) */
\r
418 0x05, 0x09, /* USAGE_PAGE (Button) */
\r
419 0x19, 0x01, /* USAGE_MINIMUM (Button 1) */
\r
420 0x29, 0x03, /* USAGE_MAXIMUM (Button 3) */
\r
421 0x15, 0x00, /* LOGICAL_MINIMUM (0) */
\r
422 0x25, 0x01, /* LOGICAL_MAXIMUM (1) */
\r
423 0x81, 0x02, /* INPUT (Data,Var,Abs) */
\r
424 0x95, 0x01, /* REPORT_COUNT (1) */
\r
425 0x75, 0x05, /* REPORT_SIZE (5) */
\r
426 0x81, 0x01, /* INPUT (Cnst,Ary,Abs) */
\r
427 0x75, 0x08, /* REPORT_SIZE (8) */
\r
428 0x95, 0x02, /* REPORT_COUNT (2) */
\r
429 0x05, 0x01, /* USAGE_PAGE (Generic Desktop) */
\r
430 0x09, 0x30, /* USAGE (X) */
\r
431 0x09, 0x31, /* USAGE (Y) */
\r
432 0x15, 0x81, /* LOGICAL_MINIMUM (-127) */
\r
433 0x25, 0x7f, /* LOGICAL_MAXIMUM (127) */
\r
434 0x81, 0x06, /* INPUT (Data,Var,Rel) */
\r
435 0xc0, /* END_COLLECTION */
\r
436 0xc0 /* END_COLLECTION */
\r
441 const char pxDeviceDescriptor[] =
\r
443 /* Device descriptor */
\r
444 0x12, /* bLength */
\r
445 0x01, /* bDescriptorType */
\r
446 0x10, 0x01, /* bcdUSBL */
\r
447 usbDEVICE_CLASS_VENDOR_SPECIFIC, /* bDeviceClass: */
\r
448 0x00, /* bDeviceSubclass: */
\r
449 0x00, /* bDeviceProtocol: */
\r
450 0x08, /* bMaxPacketSize0 */
\r
451 0xFF, 0xFF, /* idVendorL */
\r
452 0x02, 0x00, /* idProductL */
\r
453 0x00, 0x01, /* bcdDeviceL */
\r
454 usbMANUFACTURER_STRING, /* iManufacturer */
\r
455 usbPRODUCT_STRING, /* iProduct */
\r
456 0x00, /* SerialNumber */
\r
457 0x01 /* bNumConfigs */
\r
461 const char pxConfigDescriptor[] = {
\r
462 /* Configuration 1 descriptor */
\r
463 0x09, /* CbLength */
\r
464 0x02, /* CbDescriptorType */
\r
465 0x22, 0x00, /* CwTotalLength 2 EP + Control */
\r
466 0x01, /* CbNumInterfaces */
\r
467 0x01, /* CbConfigurationValue */
\r
468 usbCONFIGURATION_STRING,/* CiConfiguration */
\r
469 usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/
\r
470 0x32, /* CMaxPower: 100mA */
\r
472 /* Mouse Interface Descriptor Requirement */
\r
473 0x09, /* bLength */
\r
474 0x04, /* bDescriptorType */
\r
475 0x00, /* bInterfaceNumber */
\r
476 0x00, /* bAlternateSetting */
\r
477 0x01, /* bNumEndpoints */
\r
478 0x03, /* bInterfaceClass: HID code */
\r
479 0x01, /* bInterfaceSubclass boot */
\r
480 0x02, /* bInterfaceProtocol mouse boot */
\r
481 usbINTERFACE_STRING,/* iInterface */
\r
483 /* HID Descriptor */
\r
484 0x09, /* bLength */
\r
485 0x21, /* bDescriptor type: HID Descriptor Type */
\r
486 0x00, 0x01, /* bcdHID */
\r
487 0x00, /* bCountryCode */
\r
488 0x01, /* bNumDescriptors */
\r
489 usbHID_REPORT_DESCRIPTOR, /* bDescriptorType */
\r
490 sizeof( pxReportDescriptor ), 0x00, /* wItemLength */
\r
492 /* Endpoint 1 descriptor */
\r
493 0x07, /* bLength */
\r
494 0x05, /* bDescriptorType */
\r
495 0x81, /* bEndpointAddress, Endpoint 01 - IN */
\r
496 0x03, /* bmAttributes INT */
\r
497 0x08, 0x00, /* wMaxPacketSize: 8? */
\r
498 0x0A /* bInterval */
\r
501 /*-----------------------------------------------------------*/
\r
503 /* File scope state variables. */
\r
504 static unsigned portCHAR ucUSBConfig = ( unsigned portCHAR ) 0;
\r
505 static unsigned portLONG ulReceivedAddress = ( unsigned portLONG ) 0;
\r
506 static eDRIVER_STATE eDriverState = eNOTHING;
\r
508 /* Structure used to control the characters being sent to the host. */
\r
509 static xTX_MESSAGE pxCharsForTx;
\r
511 /* Queue used to pass messages between the ISR and the task. */
\r
512 xQueueHandle xUSBInterruptQueue;
\r
514 /*-----------------------------------------------------------*/
\r
516 void vStartUSBTask( unsigned portBASE_TYPE uxPriority )
\r
518 /* Create the queue used to communicate between the USB ISR and task. */
\r
519 xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );
\r
521 /* Create the task itself. */
\r
522 xTaskCreate( vUSBDemoTask, "USB", configMINIMAL_STACK_SIZE, NULL, uxPriority, NULL );
\r
524 /*-----------------------------------------------------------*/
\r
526 static void vUSBDemoTask( void *pvParameters )
\r
528 xISRStatus *pxMessage;
\r
530 /* The parameters are not used in this task. */
\r
531 ( void ) pvParameters;
\r
533 /* Init USB device */
\r
534 portENTER_CRITICAL();
\r
535 vInitUSBInterface();
\r
536 portEXIT_CRITICAL();
\r
538 /* Process interrupts as they arrive. The ISR takes a snapshot of the
\r
539 interrupt status then posts the information on this queue for processing
\r
540 at the task level. This simple demo implementation only processes
\r
541 a few interrupt sources. */
\r
544 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )
\r
546 if( pxMessage->ulISR & AT91C_UDP_EPINT0 )
\r
548 /* Process end point 0 interrupt. */
\r
549 prvProcessEndPoint0Interrupt( pxMessage );
\r
552 if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )
\r
554 /* Process an end of bus reset interrupt. */
\r
555 prvResetEndPoints();
\r
560 /* The ISR did not post any data for us to process on the queue, so
\r
561 just generate and send some sample data. */
\r
562 if( eDriverState == eREADY_TO_SEND )
\r
564 prvTransmitSampleValues();
\r
569 /*-----------------------------------------------------------*/
\r
571 static void prvControlCursorSpeed( signed portCHAR *cVal, unsigned portLONG ulInput, unsigned portLONG ulSwitch1, unsigned portLONG ulSwitch2 )
\r
573 const portCHAR cSpeed = 20;
\r
575 if( !( ulInput & ulSwitch1 ) )
\r
577 /* We are going in the decreasing y direction. */
\r
580 /* We have changed direction since last time so start from
\r
585 if( *cVal > -cSpeed )
\r
587 /* Ramp y down to the max speed. */
\r
591 else if( !( ulInput & ulSwitch2 ) )
\r
593 /* We are going in the increasing y direction. */
\r
596 /* We have changed direction since last time, so start from
\r
601 if( *cVal < cSpeed )
\r
603 /* Ramp y up to the max speed again. */
\r
612 /*-----------------------------------------------------------*/
\r
614 static void prvTransmitSampleValues( void )
\r
616 /* Variables to hold dummy x, y and z joystick axis data. */
\r
617 static signed portCHAR x = 0, y = 0, z = 0;
\r
618 unsigned portLONG ulStatus;
\r
620 ulStatus = AT91C_BASE_PIOA->PIO_PDSR;
\r
622 prvControlCursorSpeed( &y, ulStatus, ( unsigned long ) usbSW1, ( unsigned long ) usbSW2 );
\r
623 prvControlCursorSpeed( &x, ulStatus, ( unsigned long ) usbSW3, ( unsigned long ) usbSW4 );
\r
625 /* Just make the z axis go up and down. */
\r
626 z = ( ( ulStatus & usbSW_CLICK ) == 0 );
\r
628 /* Can we place data in the fifo? */
\r
629 if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )
\r
631 /* Write our sample data to the fifo. */
\r
632 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;
\r
633 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;
\r
634 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;
\r
636 /* Send the data. */
\r
637 portENTER_CRITICAL();
\r
639 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
640 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
641 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;
\r
643 portEXIT_CRITICAL();
\r
646 /*-----------------------------------------------------------*/
\r
648 static void prvUSBTransmitNull( void )
\r
650 unsigned portLONG ulStatus;
\r
652 /* Wait until the FIFO is free - even though we are not going to use it.
\r
653 THERE IS NO TIMEOUT HERE! */
\r
654 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
656 vTaskDelay( usbSHORTEST_DELAY );
\r
659 portENTER_CRITICAL();
\r
661 /* Set the length of data to send to equal the index of the next byte
\r
662 to send. This will prevent the ACK to this NULL packet causing any
\r
663 further data transmissions. */
\r
664 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;
\r
666 /* Set the TXPKTRDY bit to cause a transmission with no data. */
\r
667 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
668 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );
\r
669 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
671 portEXIT_CRITICAL();
\r
673 /*-----------------------------------------------------------*/
\r
675 static void prvSendStall( void )
\r
677 unsigned portLONG ulStatus;
\r
679 portENTER_CRITICAL();
\r
681 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */
\r
682 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
683 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );
\r
684 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
686 portEXIT_CRITICAL();
\r
688 /*-----------------------------------------------------------*/
\r
690 static void prvResetEndPoints( void )
\r
692 unsigned portLONG ulTemp;
\r
694 eDriverState = eJUST_RESET;
\r
696 /* Reset all the end points. */
\r
697 AT91C_BASE_UDP->UDP_RSTEP = usbEND_POINT_RESET_MASK;
\r
698 AT91C_BASE_UDP->UDP_RSTEP = ( unsigned portLONG ) 0x00;
\r
700 /* Enable data to be sent and received. */
\r
701 AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;
\r
703 /* Repair the configuration end point. */
\r
704 portENTER_CRITICAL();
\r
706 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
707 usbCSR_SET_BIT( &ulTemp, ( ( unsigned portLONG ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );
\r
708 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
709 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT0;
\r
711 portEXIT_CRITICAL();
\r
713 /*-----------------------------------------------------------*/
\r
715 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )
\r
717 if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )
\r
719 /* We only expect to receive zero length data here as ACK's.
\r
720 Set the data pointer to the end of the current Tx packet to
\r
721 ensure we don't send out any more data. */
\r
722 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;
\r
725 if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )
\r
727 /* We received a TX complete interrupt. What we do depends on
\r
728 what we sent to get this interrupt. */
\r
730 if( eDriverState == eJUST_GOT_CONFIG )
\r
732 /* We sent an acknowledgement of a SET_CONFIG request. We
\r
733 are now at the end of the enumeration. */
\r
734 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;
\r
736 /* Read the end point for data transfer. */
\r
737 portENTER_CRITICAL();
\r
739 unsigned portLONG ulTemp;
\r
741 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
742 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );
\r
743 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
744 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT1;
\r
746 portEXIT_CRITICAL();
\r
748 eDriverState = eREADY_TO_SEND;
\r
750 else if( eDriverState == eJUST_GOT_ADDRESS )
\r
752 /* We sent an acknowledgement of a SET_ADDRESS request. Move
\r
753 to the addressed state. */
\r
754 if( ulReceivedAddress != ( unsigned portLONG ) 0 )
\r
756 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;
\r
760 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
763 AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );
\r
764 eDriverState = eNOTHING;
\r
768 /* The TXCOMP was not for any special type of transmission. See
\r
769 if there is any more data to send. */
\r
770 prvSendNextSegment();
\r
774 if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )
\r
776 xUSB_REQUEST xRequest;
\r
777 unsigned portCHAR ucRequest;
\r
778 unsigned portLONG ulRxBytes;
\r
780 /* A data packet is available. */
\r
781 ulRxBytes = pxMessage->ulCSR0 >> 16;
\r
782 ulRxBytes &= usbRX_COUNT_MASK;
\r
784 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )
\r
786 /* Create an xUSB_REQUEST variable from the raw bytes array. */
\r
788 xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];
\r
789 xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];
\r
791 /* NOT PORTABLE CODE! */
\r
792 xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];
\r
793 xRequest.usValue <<= 8;
\r
794 xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];
\r
796 xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];
\r
797 xRequest.usIndex <<= 8;
\r
798 xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];
\r
800 xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];
\r
801 xRequest.usLength <<= 8;
\r
802 xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];
\r
804 /* Manipulate the ucRequestType and the ucRequest parameters to
\r
805 generate a zero based request selection. This is just done to
\r
806 break up the requests into subsections for clarity. The
\r
807 alternative would be to have more huge switch statement that would
\r
808 be difficult to optimise. */
\r
809 ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );
\r
810 ucRequest |= ( xRequest.ucReqType & 0x03 );
\r
812 switch( ucRequest )
\r
814 case usbSTANDARD_DEVICE_REQUEST:
\r
815 /* Standard Device request */
\r
816 prvHandleStandardDeviceRequest( &xRequest );
\r
819 case usbSTANDARD_INTERFACE_REQUEST:
\r
820 /* Standard Interface request */
\r
821 prvHandleStandardInterfaceRequest( &xRequest );
\r
824 case usbSTANDARD_END_POINT_REQUEST:
\r
825 /* Standard Endpoint request */
\r
826 prvHandleStandardEndPointRequest( &xRequest );
\r
829 case usbCLASS_INTERFACE_REQUEST:
\r
830 /* Class Interface request */
\r
831 prvHandleClassInterfaceRequest( &xRequest );
\r
834 default: /* This is not something we want to respond to. */
\r
840 /*-----------------------------------------------------------*/
\r
842 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )
\r
844 /* The type is in the high byte. Return whatever has been requested. */
\r
845 switch( ( pxRequest->usValue & 0xff00 ) >> 8 )
\r
847 case usbDESCRIPTOR_TYPE_DEVICE:
\r
848 prvSendControlData( ( unsigned portCHAR * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );
\r
851 case usbDESCRIPTOR_TYPE_CONFIGURATION:
\r
852 prvSendControlData( ( unsigned portCHAR * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );
\r
855 case usbDESCRIPTOR_TYPE_STRING:
\r
857 /* The index to the string descriptor is the lower byte. */
\r
858 switch( pxRequest->usValue & 0xff )
\r
860 case usbLANGUAGE_STRING:
\r
861 prvSendControlData( ( unsigned portCHAR * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );
\r
864 case usbMANUFACTURER_STRING:
\r
865 prvSendControlData( ( unsigned portCHAR * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );
\r
868 case usbPRODUCT_STRING:
\r
869 prvSendControlData( ( unsigned portCHAR * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );
\r
872 case usbCONFIGURATION_STRING:
\r
873 prvSendControlData( ( unsigned portCHAR * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );
\r
876 case usbINTERFACE_STRING:
\r
877 prvSendControlData( ( unsigned portCHAR * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );
\r
881 /* Don't know what this string is. */
\r
889 /* We are not responding to anything else. */
\r
894 /*-----------------------------------------------------------*/
\r
896 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )
\r
898 unsigned portSHORT usStatus = 0;
\r
900 switch( pxRequest->ucRequest )
\r
902 case usbGET_STATUS_REQUEST:
\r
903 /* Just send two byte dummy status. */
\r
904 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
907 case usbGET_DESCRIPTOR_REQUEST:
\r
908 /* Send device descriptor */
\r
909 prvGetStandardDeviceDescriptor( pxRequest );
\r
912 case usbGET_CONFIGURATION_REQUEST:
\r
913 /* Send selected device configuration */
\r
914 prvSendControlData( ( unsigned portCHAR * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );
\r
917 case usbSET_FEATURE_REQUEST:
\r
918 prvUSBTransmitNull();
\r
921 case usbSET_ADDRESS_REQUEST:
\r
923 /* Acknowledge the SET_ADDRESS, but (according to the manual) we
\r
924 cannot actually move to the addressed state until we get a TXCOMP
\r
925 interrupt from this NULL packet. Therefore we just remember the
\r
926 address and set our state so we know we have received the address. */
\r
927 prvUSBTransmitNull();
\r
928 eDriverState = eJUST_GOT_ADDRESS;
\r
929 ulReceivedAddress = ( unsigned portLONG ) pxRequest->usValue;
\r
932 case usbSET_CONFIGURATION_REQUEST:
\r
934 /* Acknowledge the SET_CONFIGURATION, but (according to the manual)
\r
935 we cannot actually move to the configured state until we get a
\r
936 TXCOMP interrupt from this NULL packet. Therefore we just remember the
\r
937 config and set our state so we know we have received the go ahead. */
\r
938 ucUSBConfig = ( unsigned portCHAR ) ( pxRequest->usValue & 0xff );
\r
939 eDriverState = eJUST_GOT_CONFIG;
\r
940 prvUSBTransmitNull();
\r
945 /* We don't answer to anything else. */
\r
950 /*-----------------------------------------------------------*/
\r
952 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
954 switch( pxRequest->ucRequest )
\r
956 case usbSET_IDLE_REQUEST:
\r
957 prvUSBTransmitNull();
\r
960 /* This minimal implementation ignores these. */
\r
961 case usbGET_REPORT_REQUEST:
\r
962 case usbGET_IDLE_REQUEST:
\r
963 case usbGET_PROTOCOL_REQUEST:
\r
964 case usbSET_REPORT_REQUEST:
\r
965 case usbSET_PROTOCOL_REQUEST:
\r
972 /*-----------------------------------------------------------*/
\r
974 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )
\r
976 switch( ( pxRequest->usValue & ( unsigned portSHORT ) 0xff00 ) >> 8 )
\r
978 case usbHID_REPORT_DESCRIPTOR:
\r
979 prvSendControlData( ( unsigned portCHAR * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );
\r
984 /* Don't expect to send any others. */
\r
989 /*-----------------------------------------------------------*/
\r
991 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )
\r
993 unsigned portSHORT usStatus = 0;
\r
995 switch( pxRequest->ucRequest )
\r
997 case usbGET_STATUS_REQUEST:
\r
998 /* Send dummy 2 bytes. */
\r
999 prvSendControlData( ( unsigned portCHAR * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );
\r
1002 case usbGET_DESCRIPTOR_REQUEST:
\r
1003 prvGetStandardInterfaceDescriptor( pxRequest );
\r
1006 /* This minimal implementation does not respond to these. */
\r
1007 case usbGET_INTERFACE_REQUEST:
\r
1008 case usbSET_FEATURE_REQUEST:
\r
1009 case usbSET_INTERFACE_REQUEST:
\r
1016 /*-----------------------------------------------------------*/
\r
1018 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )
\r
1020 switch( pxRequest->ucRequest )
\r
1022 /* This minimal implementation does not expect to respond to these. */
\r
1023 case usbGET_STATUS_REQUEST:
\r
1024 case usbCLEAR_FEATURE_REQUEST:
\r
1025 case usbSET_FEATURE_REQUEST:
\r
1032 /*-----------------------------------------------------------*/
\r
1034 static void vInitUSBInterface( void )
\r
1036 volatile unsigned portLONG ulTemp;
\r
1038 /* Initialise a few state variables. */
\r
1039 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1040 ucUSBConfig = ( unsigned portCHAR ) 0;
\r
1041 eDriverState = eNOTHING;
\r
1043 /* HARDWARE SETUP */
\r
1045 /* Set the PLL USB Divider */
\r
1046 AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;
\r
1048 /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */
\r
1049 AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;
\r
1050 AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);
\r
1052 /* Setup the PIO for the USB pull up resistor. */
\r
1053 AT91C_BASE_PIOA->PIO_PER = AT91C_PIO_PA16;
\r
1054 AT91C_BASE_PIOA->PIO_OER = AT91C_PIO_PA16;
\r
1057 /* Start without the pullup - this will get set at the end of this
\r
1059 AT91C_BASE_PIOA->PIO_SODR = AT91C_PIO_PA16;
\r
1061 /* When using the USB debugger the peripheral registers do not always get
\r
1062 set to the correct default values. To make sure set the relevant registers
\r
1064 AT91C_BASE_UDP->UDP_IDR = ( unsigned portLONG ) 0xffffffff;
\r
1065 AT91C_BASE_UDP->UDP_ICR = ( unsigned portLONG ) 0xffffffff;
\r
1066 AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned portLONG ) 0x00;
\r
1067 AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned portLONG ) 0x00;
\r
1068 AT91C_BASE_UDP->UDP_GLBSTATE = 0;
\r
1069 AT91C_BASE_UDP->UDP_FADDR = 0;
\r
1071 /* Enable the transceiver. */
\r
1072 AT91C_UDP_TRANSCEIVER_ENABLE = 0;
\r
1074 /* Enable the USB interrupts - other interrupts get enabled as the
\r
1075 enumeration process progresses. */
\r
1076 AT91F_AIC_ConfigureIt( AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vUSB_ISR_Wrapper );
\r
1077 AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_UDP;
\r
1079 /* Wait a short while before making our presence known. */
\r
1080 vTaskDelay( usbINIT_DELAY );
\r
1081 AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA16;
\r
1083 /*-----------------------------------------------------------*/
\r
1085 static void prvSendControlData( unsigned portCHAR *pucData, unsigned portSHORT usRequestedLength, unsigned portLONG ulLengthToSend, portLONG lSendingDescriptor )
\r
1087 if( ( ( unsigned portLONG ) usRequestedLength < ulLengthToSend ) )
\r
1089 /* Cap the data length to that requested. */
\r
1090 ulLengthToSend = ( unsigned portSHORT ) usRequestedLength;
\r
1092 else if( ( ulLengthToSend < ( unsigned portLONG ) usRequestedLength ) && lSendingDescriptor )
\r
1094 /* We are sending a descriptor. If the descriptor is an exact
\r
1095 multiple of the FIFO length then it will have to be terminated
\r
1096 with a NULL packet. Set the state to indicate this if
\r
1098 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )
\r
1100 eDriverState = eSENDING_EVEN_DESCRIPTOR;
\r
1104 /* Here we assume that the previous message has been sent. THERE IS NO
\r
1105 BUFFER OVERFLOW PROTECTION HERE.
\r
1107 Copy the data to send into the buffer as we cannot send it all at once
\r
1108 (if it is greater than 8 bytes in length). */
\r
1109 memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );
\r
1111 /* Reinitialise the buffer index so we start sending from the start of
\r
1113 pxCharsForTx.ulTotalDataLength = ulLengthToSend;
\r
1114 pxCharsForTx.ulNextCharIndex = ( unsigned portLONG ) 0;
\r
1116 /* Send the first 8 bytes now. The rest will get sent in response to
\r
1117 TXCOMP interrupts. */
\r
1118 prvSendNextSegment();
\r
1120 /*-----------------------------------------------------------*/
\r
1122 static void prvSendNextSegment( void )
\r
1124 volatile unsigned portLONG ulNextLength, ulStatus, ulLengthLeftToSend;
\r
1126 /* Is there any data to send? */
\r
1127 if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )
\r
1129 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;
\r
1131 /* We can only send 8 bytes to the fifo at a time. */
\r
1132 if( ulLengthLeftToSend > usbFIFO_LENGTH )
\r
1134 ulNextLength = usbFIFO_LENGTH;
\r
1138 ulNextLength = ulLengthLeftToSend;
\r
1141 /* Wait until we can place data in the fifo. THERE IS NO TIMEOUT
\r
1143 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )
\r
1145 vTaskDelay( usbSHORTEST_DELAY );
\r
1148 /* Write the data to the FIFO. */
\r
1149 while( ulNextLength > ( unsigned portLONG ) 0 )
\r
1151 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];
\r
1154 pxCharsForTx.ulNextCharIndex++;
\r
1157 /* Start the transmission. */
\r
1158 portENTER_CRITICAL();
\r
1160 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
1161 usbCSR_SET_BIT( &ulStatus, ( ( unsigned portLONG ) 0x10 ) );
\r
1162 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;
\r
1164 portEXIT_CRITICAL();
\r
1168 /* There is no data to send. If we were sending a descriptor and the
\r
1169 descriptor was an exact multiple of the max packet size then we need
\r
1170 to send a null to terminate the transmission. */
\r
1171 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )
\r
1173 prvUSBTransmitNull();
\r
1174 eDriverState = eNOTHING;
\r