]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/USB/USBSample.c
2d7bcec8bf553da86e07eb4d178ccb248265dc30
[freertos] / FreeRTOS / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / USB / USBSample.c
1 /*\r
2     FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /*\r
76         Sample interrupt driven mouse device driver.  This is a minimal implementation \r
77         for demonstration only.  Although functional, it may not be a fully and \r
78         compliant implementation.  The small joystick on the SAM7X EK can be used to\r
79         move the mouse cursor, pressing the joystick transmits mouse clicks.  Note\r
80         that it might be necessary to run the demo stand along (without the \r
81         debugger) in order for the USB device to be recognised by the host computer.\r
82 \r
83         The interrupt handler itself is contained within USB_ISR.c.\r
84         \r
85         See the FreeRTOS.org online documentation for more information.\r
86 */\r
87 \r
88 /* Standard includes. */\r
89 #include <string.h>\r
90 \r
91 /* Scheduler includes. */\r
92 #include "FreeRTOS.h"\r
93 #include "task.h"\r
94 #include "queue.h"\r
95 \r
96 /* Demo application includes. */\r
97 #include "USBSample.h"\r
98 \r
99 /* Joystick inputs used to move the 'mouse' cursor. */\r
100 #define usbSW1  ( 1 << 21 )     /* PA21 */\r
101 #define usbSW2  ( 1 << 22 )     /* PA22 */\r
102 #define usbSW3  ( 1 << 23 )     /* PA23 */\r
103 #define usbSW4  ( 1 << 24 )     /* PA24 */\r
104 #define usbSW_CLICK     ( 1 << 25 ) /* PA25 */\r
105 \r
106 /* Descriptor type definitions. */\r
107 #define usbDESCRIPTOR_TYPE_DEVICE                       ( 0x01 )\r
108 #define usbDESCRIPTOR_TYPE_CONFIGURATION        ( 0x02 )\r
109 #define usbDESCRIPTOR_TYPE_STRING                       ( 0x03 )\r
110 \r
111 /* USB request type definitions. */\r
112 #define usbGET_REPORT_REQUEST                           ( 0x01 )\r
113 #define usbGET_IDLE_REQUEST                                     ( 0x02 )\r
114 #define usbGET_PROTOCOL_REQUEST                         ( 0x03 )\r
115 #define usbSET_REPORT_REQUEST                           ( 0x09 )\r
116 #define usbSET_IDLE_REQUEST                                     ( 0x0A )\r
117 #define usbSET_PROTOCOL_REQUEST                         ( 0x0B )\r
118 #define usbGET_CONFIGURATION_REQUEST            ( 0x08 )\r
119 #define usbGET_STATUS_REQUEST                           ( 0x00 )\r
120 #define usbCLEAR_FEATURE_REQUEST                        ( 0x01 )\r
121 #define usbSET_FEATURE_REQUEST                          ( 0x03 )\r
122 #define usbSET_ADDRESS_REQUEST                          ( 0x05 )\r
123 #define usbGET_DESCRIPTOR_REQUEST                       ( 0x06 )\r
124 #define usbSET_CONFIGURATION_REQUEST            ( 0x09 )\r
125 #define usbGET_INTERFACE_REQUEST                        ( 0x0A )\r
126 #define usbSET_INTERFACE_REQUEST                        ( 0x0B )\r
127 \r
128 \r
129 /* Misc USB definitions. */\r
130 #define usbDEVICE_CLASS_VENDOR_SPECIFIC         ( 0xFF )\r
131 #define usbBUS_POWERED                                          ( 0x80 )\r
132 #define usbHID_REPORT_DESCRIPTOR                        ( 0x22 )\r
133 #define AT91C_UDP_TRANSCEIVER_ENABLE                    ( *( ( unsigned long * ) 0xfffb0074 ) )\r
134 \r
135 /* Index to the various string. */\r
136 #define usbLANGUAGE_STRING                                      ( 0 )\r
137 #define usbMANUFACTURER_STRING                          ( 1 )\r
138 #define usbPRODUCT_STRING                                       ( 2 )\r
139 #define usbCONFIGURATION_STRING                         ( 3 )\r
140 #define usbINTERFACE_STRING                                     ( 4 )\r
141 \r
142 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]\r
143 into xUSB_REQUEST.  The data order is designed for speed - so looks a \r
144 little odd. */\r
145 #define usbREQUEST_TYPE_INDEX                           ( 7 )\r
146 #define usbREQUEST_INDEX                                        ( 6 )\r
147 #define usbVALUE_HIGH_BYTE                                      ( 4 )\r
148 #define usbVALUE_LOW_BYTE                                       ( 5 )\r
149 #define usbINDEX_HIGH_BYTE                                      ( 2 )\r
150 #define usbINDEX_LOW_BYTE                                       ( 3 )\r
151 #define usbLENGTH_HIGH_BYTE                                     ( 0 )\r
152 #define usbLENGTH_LOW_BYTE                                      ( 1 )\r
153 \r
154 /* Misc application definitions. */\r
155 #define usbINTERRUPT_PRIORITY                           ( 3 )\r
156 #define usbFIFO_LENGTH                                          ( ( unsigned long ) 8 )\r
157 #define usbXUP                                                          ( 1 )\r
158 #define usbXDOWN                                                        ( 2 )\r
159 #define usbYUP                                                          ( 3 )\r
160 #define usbYDOWN                                                        ( 4 )\r
161 #define usbMAX_COORD                                            ( 120 )\r
162 #define usbMAX_TX_MESSAGE_SIZE                          ( 128 )\r
163 #define usbSHORTEST_DELAY                                       ( ( portTickType ) 1 )\r
164 #define usbINIT_DELAY                                           ( ( portTickType ) 1000 / portTICK_RATE_MS )\r
165 #define usbSHORT_DELAY                                          ( ( portTickType ) 50 / portTICK_RATE_MS )\r
166 #define usbEND_POINT_RESET_MASK                         ( ( unsigned long ) 0x0f )\r
167 #define usbDATA_INC                                                     ( ( char ) 5 )\r
168 #define usbEXPECTED_NUMBER_OF_BYTES                     ( ( unsigned long ) 8 )\r
169 \r
170 /* Control request types. */\r
171 #define usbSTANDARD_DEVICE_REQUEST                      ( 0 )\r
172 #define usbSTANDARD_INTERFACE_REQUEST           ( 1 )\r
173 #define usbSTANDARD_END_POINT_REQUEST           ( 2 )\r
174 #define usbCLASS_INTERFACE_REQUEST                      ( 5 )\r
175 \r
176 /* Structure used to hold the received requests. */\r
177 typedef struct \r
178 {\r
179         unsigned char ucReqType;\r
180         unsigned char ucRequest;\r
181         unsigned short usValue;\r
182         unsigned short usIndex;\r
183         unsigned short usLength;\r
184 } xUSB_REQUEST;\r
185 \r
186 typedef enum\r
187 {\r
188         eNOTHING,\r
189         eJUST_RESET,\r
190         eJUST_GOT_CONFIG,\r
191         eJUST_GOT_ADDRESS,\r
192         eSENDING_EVEN_DESCRIPTOR,\r
193         eREADY_TO_SEND\r
194 } eDRIVER_STATE;\r
195 \r
196 /* Structure used to control the data being sent to the host. */\r
197 typedef struct\r
198 {\r
199         unsigned char ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];\r
200         unsigned long ulNextCharIndex;\r
201         unsigned long ulTotalDataLength;\r
202 } xTX_MESSAGE;\r
203 \r
204 /*-----------------------------------------------------------*/\r
205 \r
206 /* \r
207  * The USB interrupt service routine.  This takes a snapshot of the USB\r
208  * device at the time of the interrupt, clears the interrupts, and posts\r
209  * the data to the USB processing task.  This is implemented in USB_ISR.c.\r
210  */\r
211 extern void vUSB_ISR_Wrapper( void );\r
212 \r
213 /*\r
214  * Called after the bus reset interrupt - this function readies all the\r
215  * end points for communication.\r
216  */\r
217 static void prvResetEndPoints( void );\r
218 \r
219 /*\r
220  * Setup the USB hardware, install the interrupt service routine and \r
221  * initialise all the state variables.\r
222  */\r
223 static void vInitUSBInterface( void );\r
224 \r
225 /*\r
226  * Decode and act upon an interrupt generated by the control end point.\r
227  */\r
228 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );\r
229 \r
230 /* \r
231  * For simplicity requests are separated into device, interface, class \r
232  * interface and end point requests.\r
233  *\r
234  * Decode and handle standard device requests originating on the control\r
235  * end point. \r
236  */\r
237 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );\r
238 \r
239 /*\r
240  * For simplicity requests are separated into device, interface, class \r
241  * interface and end point requests.\r
242  *\r
243  * Decode and handle standard interface requests originating on the control\r
244  * end point.\r
245  */\r
246 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );\r
247 \r
248 /*\r
249  * For simplicity requests are separated into device, interface, class \r
250  * interface and end point requests.\r
251  *\r
252  * Decode and handle standard end point requests originating on the control\r
253  * end point.\r
254  */\r
255 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );\r
256 \r
257 /*\r
258  * For simplicity requests are separated into device, interface, class \r
259  * interface and end point requests.\r
260  *\r
261  * Decode and handle the class interface requests.\r
262  */\r
263 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );\r
264 \r
265 /*\r
266  * Setup the Tx buffer to send data in response to a control request.\r
267  *\r
268  * The data to be transmitted is buffered, the state variables are updated,\r
269  * then prvSendNextSegment() is called to start the transmission off.  Once\r
270  * the first segment has been sent the remaining segments are transmitted\r
271  * in response to TXCOMP interrupts until the entire buffer has been\r
272  * sent.\r
273  */\r
274 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthLeftToSend, long lSendingDescriptor );\r
275 \r
276 /*\r
277  * Examine the Tx buffer to see if there is any more data to be transmitted.\r
278  * \r
279  * If there is data to be transmitted then send the next segment.  A segment\r
280  * can have a maximum of 8 bytes (this is defined as the maximum for the end\r
281  * point by the descriptor).  The final segment may be less than 8 bytes if\r
282  * the total data length was not an exact multiple of 8.\r
283  */\r
284 static void prvSendNextSegment( void );\r
285 \r
286 /*\r
287  * A stall condition is forced each time the host makes a request that is not\r
288  * supported by this minimal implementation.\r
289  * \r
290  * A stall is forced by setting the appropriate bit in the end points control\r
291  * and status register. \r
292  */\r
293 static void prvSendStall( void );\r
294 \r
295 /*\r
296  * A NULL (or zero length packet) is transmitted in acknowledge the reception \r
297  * of certain events from the host.\r
298  */\r
299 static void prvUSBTransmitNull( void );\r
300 \r
301 /* \r
302  * When the host requests a descriptor this function is called to determine \r
303  * which descriptor is being requested and start its transmission.\r
304  */\r
305 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );\r
306 \r
307 /*\r
308  * Transmit movement and clicks on the EK joystick as mouse inputs.\r
309  */\r
310 static void prvTransmitSampleValues( void );\r
311 \r
312 /*\r
313  * The created task to handle the USB demo functionality. \r
314  */\r
315 static void vUSBDemoTask( void *pvParameters );\r
316 \r
317 /*\r
318  * Simple algorithm to ramp up the mouse cursor speed to make it easier to\r
319  * use.\r
320  */\r
321 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 );\r
322 /*-----------------------------------------------------------*/\r
323 \r
324 /*\r
325         - DESCRIPTOR DEFINITIONS -\r
326 */\r
327 \r
328 /* String descriptors used during the enumeration process.\r
329 These take the form:\r
330 \r
331 {\r
332         Length of descriptor,\r
333         Descriptor type,\r
334         Data\r
335 }\r
336 */\r
337 const char pxLanguageStringDescriptor[] =\r
338 {\r
339         4,\r
340         usbDESCRIPTOR_TYPE_STRING,\r
341         0x09, 0x04\r
342 };\r
343 \r
344 const char pxManufacturerStringDescriptor[] = \r
345 {\r
346         18,\r
347         usbDESCRIPTOR_TYPE_STRING,\r
348 \r
349         'F', 0x00,\r
350         'r', 0x00,\r
351         'e', 0x00,\r
352         'e', 0x00,\r
353         'R', 0x00,\r
354         'T', 0x00,\r
355         'O', 0x00,\r
356         'S', 0x00       \r
357 };\r
358 \r
359 const char pxProductStringDescriptor[] = \r
360 {\r
361         38,\r
362         usbDESCRIPTOR_TYPE_STRING,\r
363 \r
364         'F', 0x00,\r
365         'r', 0x00,\r
366         'e', 0x00,\r
367         'e', 0x00,\r
368         'R', 0x00,\r
369         'T', 0x00,\r
370         'O', 0x00,\r
371         'S', 0x00,\r
372         '.', 0x00,\r
373         'o', 0x00,\r
374         'r', 0x00,\r
375         'g', 0x00,\r
376         ' ', 0x00,\r
377         'M', 0x00,\r
378         'o', 0x00,\r
379         'u', 0x00,\r
380         's', 0x00,\r
381         'e', 0x00\r
382 };\r
383 \r
384 const char pxConfigurationStringDescriptor[] = \r
385 {\r
386         38,\r
387         usbDESCRIPTOR_TYPE_STRING,\r
388 \r
389         'C', 0x00,\r
390         'o', 0x00,\r
391         'n', 0x00,\r
392         'f', 0x00,\r
393         'i', 0x00,\r
394         'g', 0x00,\r
395         'u', 0x00,\r
396         'r', 0x00,\r
397         'a', 0x00,\r
398         't', 0x00,\r
399         'i', 0x00,\r
400         'o', 0x00,\r
401         'n', 0x00,\r
402         ' ', 0x00,\r
403         'N', 0x00,\r
404         'a', 0x00,\r
405         'm', 0x00,\r
406         'e', 0x00\r
407 };\r
408 \r
409 const char pxInterfaceStringDescriptor[] = \r
410 {\r
411         30,\r
412         usbDESCRIPTOR_TYPE_STRING,\r
413 \r
414         'I', 0x00,\r
415         'n', 0x00,\r
416         't', 0x00,\r
417         'e', 0x00,\r
418         'r', 0x00,\r
419         'f', 0x00,\r
420         'a', 0x00,\r
421         'c', 0x00,\r
422         'e', 0x00,\r
423         ' ', 0x00,\r
424         'N', 0x00,\r
425         'a', 0x00,\r
426         'm', 0x00,\r
427         'e', 0x00\r
428 };\r
429 \r
430 /* Enumeration descriptors. */\r
431 const char pxReportDescriptor[] =\r
432 {\r
433         0x05, 0x01,                                             /* USAGE_PAGE (Generic Desktop)                 */\r
434         0x09, 0x02,                                             /* USAGE (Mouse)                                                */\r
435         0xa1, 0x01,                                             /* COLLECTION (Application)                             */\r
436         0x09, 0x01,                                             /*   USAGE (Pointer)                                    */\r
437         0xa1, 0x00,                                             /*   COLLECTION (Physical)                              */\r
438         0x95, 0x03,                                             /*     REPORT_COUNT (3)                                 */\r
439         0x75, 0x01,                                             /*     REPORT_SIZE (1)                                  */\r
440         0x05, 0x09,                                             /*     USAGE_PAGE (Button)                              */\r
441         0x19, 0x01,                                             /*     USAGE_MINIMUM (Button 1)                 */\r
442         0x29, 0x03,                                             /*     USAGE_MAXIMUM (Button 3)                 */\r
443         0x15, 0x00,                                             /*     LOGICAL_MINIMUM (0)                              */\r
444         0x25, 0x01,                                             /*     LOGICAL_MAXIMUM (1)                              */\r
445         0x81, 0x02,                                             /*     INPUT (Data,Var,Abs)                             */\r
446         0x95, 0x01,                                             /*     REPORT_COUNT (1)                                 */\r
447         0x75, 0x05,                                             /*     REPORT_SIZE (5)                                  */\r
448         0x81, 0x01,                                             /*     INPUT (Cnst,Ary,Abs)                             */\r
449         0x75, 0x08,                                             /*     REPORT_SIZE (8)                                  */\r
450         0x95, 0x02,                                             /*     REPORT_COUNT (2)                                 */\r
451         0x05, 0x01,                                             /*     USAGE_PAGE (Generic Desktop)             */\r
452         0x09, 0x30,                                             /*     USAGE (X)                                                */\r
453         0x09, 0x31,                                             /*     USAGE (Y)                                                */\r
454         0x15, 0x81,                                             /*     LOGICAL_MINIMUM (-127)                   */\r
455         0x25, 0x7f,                                             /*     LOGICAL_MAXIMUM (127)                    */\r
456         0x81, 0x06,                                             /*     INPUT (Data,Var,Rel)                             */\r
457         0xc0,                                                   /*   END_COLLECTION                                             */\r
458         0xc0                                                    /* END_COLLECTION                                               */\r
459 };\r
460 \r
461 \r
462 \r
463 const char pxDeviceDescriptor[] = \r
464 {\r
465         /* Device descriptor */\r
466         0x12,                                                           /* bLength                              */\r
467         0x01,                                                           /* bDescriptorType              */\r
468         0x10, 0x01,                                                     /* bcdUSBL                              */\r
469         usbDEVICE_CLASS_VENDOR_SPECIFIC,        /* bDeviceClass:                */\r
470         0x00,                                                           /* bDeviceSubclass:             */\r
471         0x00,                                                           /* bDeviceProtocol:             */\r
472         0x08,                                                           /* bMaxPacketSize0              */\r
473         0xFF, 0xFF,                                                     /* idVendorL                    */\r
474         0x02, 0x00,                                                     /* idProductL                   */\r
475         0x00, 0x01,                                                     /* bcdDeviceL                   */\r
476         usbMANUFACTURER_STRING,                         /* iManufacturer                */\r
477         usbPRODUCT_STRING,                                      /* iProduct                             */\r
478         0x00,                                                           /* SerialNumber                 */\r
479         0x01                                                            /* bNumConfigs                  */\r
480 };\r
481 \r
482 \r
483 const char pxConfigDescriptor[] = {\r
484         /* Configuration 1 descriptor */\r
485         0x09,                   /* CbLength                                                                     */\r
486         0x02,                   /* CbDescriptorType                                                     */\r
487         0x22, 0x00,             /* CwTotalLength 2 EP + Control                         */\r
488         0x01,                   /* CbNumInterfaces                                                      */\r
489         0x01,                   /* CbConfigurationValue                                         */\r
490         usbCONFIGURATION_STRING,/* CiConfiguration                                      */\r
491         usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/\r
492         0x32,                   /* CMaxPower: 100mA                                                     */\r
493 \r
494         /* Mouse Interface Descriptor Requirement */\r
495         0x09,                   /* bLength                                                                      */\r
496         0x04,                   /* bDescriptorType                                                      */\r
497         0x00,                   /* bInterfaceNumber                                                     */\r
498         0x00,                   /* bAlternateSetting                                            */\r
499         0x01,                   /* bNumEndpoints                                                        */\r
500         0x03,                   /* bInterfaceClass: HID code                            */\r
501         0x01,                   /* bInterfaceSubclass boot                                      */\r
502         0x02,                   /* bInterfaceProtocol mouse boot                        */\r
503         usbINTERFACE_STRING,/* iInterface                                                       */\r
504 \r
505         /* HID Descriptor */\r
506         0x09,                   /* bLength                                                                      */\r
507         0x21,                   /* bDescriptor type: HID Descriptor Type        */\r
508         0x00, 0x01,             /* bcdHID                                                                       */\r
509         0x00,                   /* bCountryCode                                                         */\r
510         0x01,                   /* bNumDescriptors                                                      */\r
511         usbHID_REPORT_DESCRIPTOR,         /* bDescriptorType                    */\r
512         sizeof( pxReportDescriptor ), 0x00, /* wItemLength                      */\r
513 \r
514         /* Endpoint 1 descriptor */\r
515         0x07,                   /* bLength                                                                      */\r
516         0x05,                   /* bDescriptorType                                                      */\r
517         0x81,                   /* bEndpointAddress, Endpoint 01 - IN           */\r
518         0x03,                   /* bmAttributes      INT                                        */\r
519         0x08, 0x00,             /* wMaxPacketSize: 8?                                           */\r
520         0x0A                    /* bInterval                                                            */\r
521 };\r
522 \r
523 /*-----------------------------------------------------------*/\r
524 \r
525 /* File scope state variables. */\r
526 static unsigned char ucUSBConfig = ( unsigned char ) 0;\r
527 static unsigned long ulReceivedAddress = ( unsigned long ) 0;\r
528 static eDRIVER_STATE eDriverState = eNOTHING;\r
529 \r
530 /* Structure used to control the characters being sent to the host. */\r
531 static xTX_MESSAGE pxCharsForTx;\r
532 \r
533 /* Queue used to pass messages between the ISR and the task. */\r
534 xQueueHandle xUSBInterruptQueue; \r
535 \r
536 /*-----------------------------------------------------------*/\r
537 \r
538 void vStartUSBTask( unsigned portBASE_TYPE uxPriority )\r
539 {\r
540         /* Create the queue used to communicate between the USB ISR and task. */\r
541         xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );\r
542 \r
543         /* Create the task itself. */\r
544         xTaskCreate( vUSBDemoTask, "USB", configMINIMAL_STACK_SIZE, NULL, uxPriority, NULL );\r
545 }\r
546 /*-----------------------------------------------------------*/\r
547 \r
548 static void vUSBDemoTask( void *pvParameters )\r
549 {\r
550 xISRStatus *pxMessage;\r
551 \r
552         /* The parameters are not used in this task. */\r
553         ( void ) pvParameters;\r
554 \r
555     /* Init USB device */\r
556     portENTER_CRITICAL();\r
557             vInitUSBInterface();\r
558     portEXIT_CRITICAL();\r
559 \r
560         /* Process interrupts as they arrive.   The ISR takes a snapshot of the \r
561         interrupt status then posts the information on this queue for processing\r
562         at the task level.  This simple demo implementation only processes\r
563         a few interrupt sources. */\r
564         for( ;; )\r
565         {\r
566                 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )\r
567                 {\r
568                         if( pxMessage->ulISR & AT91C_UDP_EPINT0 )\r
569                         {\r
570                                 /* Process end point 0 interrupt. */\r
571                                 prvProcessEndPoint0Interrupt( pxMessage );\r
572                         }\r
573 \r
574                         if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )\r
575                         {\r
576                                 /* Process an end of bus reset interrupt. */\r
577                                 prvResetEndPoints();            \r
578                         }\r
579                 }\r
580                 else\r
581                 {\r
582                         /* The ISR did not post any data for us to process on the queue, so\r
583                         just generate and send some sample data. */\r
584                         if( eDriverState == eREADY_TO_SEND )\r
585                         {\r
586                                 prvTransmitSampleValues();\r
587                         }\r
588                 }\r
589         }\r
590 }\r
591 /*-----------------------------------------------------------*/\r
592 \r
593 static void prvControlCursorSpeed( signed char *cVal, unsigned long ulInput, unsigned long ulSwitch1, unsigned long ulSwitch2 )\r
594 {\r
595 const char cSpeed = 20;\r
596 \r
597         if( !( ulInput & ulSwitch1 ) )\r
598         {\r
599                 /* We are going in the decreasing y direction. */\r
600                 if( *cVal > 0 )\r
601                 {\r
602                         /* We have changed direction since last time so start from\r
603                         0 again. */\r
604                         *cVal = 0;\r
605                 }\r
606                 \r
607                 if( *cVal > -cSpeed )\r
608                 {\r
609                         /* Ramp y down to the max speed. */\r
610                         (*cVal)--;\r
611                 }\r
612         }\r
613         else if( !( ulInput & ulSwitch2 ) )\r
614         {\r
615                 /* We are going in the increasing y direction. */\r
616                 if( *cVal < 0 )\r
617                 {\r
618                         /* We have changed direction since last time, so start from\r
619                         0 again. */\r
620                         *cVal = 0;\r
621                 }\r
622                 \r
623                 if( *cVal < cSpeed )\r
624                 {\r
625                         /* Ramp y up to the max speed again. */\r
626                         (*cVal)++;\r
627                 }\r
628         }\r
629         else\r
630         {\r
631                 *cVal = 0;\r
632         }\r
633 }\r
634 /*-----------------------------------------------------------*/\r
635 \r
636 static void prvTransmitSampleValues( void )\r
637 {\r
638 /* Variables to hold dummy x, y and z joystick axis data. */\r
639 static signed char x = 0, y = 0, z = 0;\r
640 unsigned long ulStatus;\r
641 \r
642         ulStatus =      AT91C_BASE_PIOA->PIO_PDSR;\r
643 \r
644         prvControlCursorSpeed( &y, ulStatus, ( unsigned long ) usbSW1, ( unsigned long ) usbSW2 );\r
645         prvControlCursorSpeed( &x, ulStatus, ( unsigned long ) usbSW3, ( unsigned long ) usbSW4 );\r
646         \r
647         /* Just make the z axis go up and down. */\r
648         z = ( ( ulStatus & usbSW_CLICK ) == 0 );\r
649 \r
650         /* Can we place data in the fifo? */\r
651         if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )\r
652         {\r
653                 /* Write our sample data to the fifo. */\r
654                 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;\r
655                 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;\r
656                 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;\r
657                 \r
658                 /* Send the data. */\r
659                 portENTER_CRITICAL();\r
660                 {\r
661                         ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];\r
662                         usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );\r
663                         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;\r
664                 }\r
665                 portEXIT_CRITICAL();\r
666         }\r
667 }\r
668 /*-----------------------------------------------------------*/\r
669 \r
670 static void prvUSBTransmitNull( void )\r
671 {\r
672 unsigned long ulStatus;\r
673 \r
674         /* Wait until the FIFO is free - even though we are not going to use it.\r
675         THERE IS NO TIMEOUT HERE! */\r
676         while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )\r
677         {\r
678                 vTaskDelay( usbSHORTEST_DELAY );\r
679         }\r
680 \r
681         portENTER_CRITICAL();\r
682         {\r
683                 /* Set the length of data to send to equal the index of the next byte\r
684                 to send.  This will prevent the ACK to this NULL packet causing any\r
685                 further data transmissions. */\r
686                 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;\r
687 \r
688                 /* Set the TXPKTRDY bit to cause a transmission with no data. */\r
689                 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
690                 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );\r
691                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;\r
692         }\r
693         portEXIT_CRITICAL();\r
694 }\r
695 /*-----------------------------------------------------------*/\r
696 \r
697 static void prvSendStall( void )\r
698 {\r
699 unsigned long ulStatus;\r
700 \r
701         portENTER_CRITICAL();\r
702         {\r
703                 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */\r
704                 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
705                 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );\r
706                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;\r
707         }\r
708         portEXIT_CRITICAL();\r
709 }\r
710 /*-----------------------------------------------------------*/\r
711 \r
712 static void prvResetEndPoints( void )\r
713 {\r
714 unsigned long ulTemp;\r
715 \r
716         eDriverState = eJUST_RESET;\r
717 \r
718         /* Reset all the end points. */\r
719         AT91C_BASE_UDP->UDP_RSTEP  = usbEND_POINT_RESET_MASK;\r
720         AT91C_BASE_UDP->UDP_RSTEP  = ( unsigned long ) 0x00;\r
721 \r
722         /* Enable data to be sent and received. */\r
723         AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;\r
724 \r
725         /* Repair the configuration end point. */\r
726         portENTER_CRITICAL();\r
727         {\r
728                 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
729                 usbCSR_SET_BIT( &ulTemp, ( ( unsigned long ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );\r
730                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
731                 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT0;\r
732         }\r
733         portEXIT_CRITICAL();\r
734 }\r
735 /*-----------------------------------------------------------*/\r
736 \r
737 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )\r
738 {\r
739         if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )\r
740         {               \r
741                 /* We only expect to receive zero length data here as ACK's. \r
742                 Set the data pointer to the end of the current Tx packet to\r
743                 ensure we don't send out any more data. */      \r
744                 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;\r
745         }\r
746 \r
747         if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )\r
748         {\r
749                 /* We received a TX complete interrupt.  What we do depends on\r
750                 what we sent to get this interrupt. */\r
751 \r
752                 if( eDriverState == eJUST_GOT_CONFIG )\r
753                 {\r
754                         /* We sent an acknowledgement of a SET_CONFIG request.  We\r
755                         are now at the end of the enumeration. */\r
756                         AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;\r
757 \r
758                         /* Read the end point for data transfer. */\r
759                         portENTER_CRITICAL();\r
760                         {\r
761                                 unsigned long ulTemp;\r
762 \r
763                                 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];                                     \r
764                                 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );\r
765                                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;             \r
766                                 AT91C_BASE_UDP->UDP_IER = AT91C_UDP_EPINT1;\r
767                         }\r
768                         portEXIT_CRITICAL();\r
769 \r
770                         eDriverState = eREADY_TO_SEND;\r
771                 }               \r
772                 else if( eDriverState == eJUST_GOT_ADDRESS )\r
773                 {\r
774                         /* We sent an acknowledgement of a SET_ADDRESS request.  Move\r
775                         to the addressed state. */\r
776                         if( ulReceivedAddress != ( unsigned long ) 0 )\r
777                         {                       \r
778                                 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;\r
779                         }\r
780                         else\r
781                         {\r
782                                 AT91C_BASE_UDP->UDP_GLBSTATE = 0;\r
783                         }                       \r
784 \r
785                         AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );              \r
786                         eDriverState = eNOTHING;\r
787                 }\r
788                 else\r
789                 {               \r
790                         /* The TXCOMP was not for any special type of transmission.  See\r
791                         if there is any more data to send. */\r
792                         prvSendNextSegment();\r
793                 }\r
794         }\r
795 \r
796         if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )\r
797         {\r
798                 xUSB_REQUEST xRequest;\r
799                 unsigned char ucRequest;\r
800                 unsigned long ulRxBytes;\r
801 \r
802                 /* A data packet is available. */       \r
803                 ulRxBytes = pxMessage->ulCSR0 >> 16;\r
804                 ulRxBytes &= usbRX_COUNT_MASK;\r
805 \r
806                 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )\r
807                 {\r
808                         /* Create an xUSB_REQUEST variable from the raw bytes array. */\r
809 \r
810                         xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];\r
811                         xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];\r
812 \r
813                         /* NOT PORTABLE CODE! */\r
814                         xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];\r
815                         xRequest.usValue <<= 8;\r
816                         xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];\r
817                                                 \r
818                         xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];\r
819                         xRequest.usIndex <<= 8;\r
820                         xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];\r
821                         \r
822                         xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];\r
823                         xRequest.usLength <<= 8;\r
824                         xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];\r
825         \r
826                         /* Manipulate the ucRequestType and the ucRequest parameters to \r
827                         generate a zero based request selection.  This is just done to \r
828                         break up the requests into subsections for clarity.  The \r
829                         alternative would be to have more huge switch statement that would\r
830                         be difficult to optimise. */\r
831                         ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );\r
832                         ucRequest |= ( xRequest.ucReqType & 0x03 );\r
833 \r
834                         switch( ucRequest )\r
835                         {\r
836                                 case usbSTANDARD_DEVICE_REQUEST:        \r
837                                                         /* Standard Device request */\r
838                                                         prvHandleStandardDeviceRequest( &xRequest );\r
839                                                         break;\r
840         \r
841                                 case usbSTANDARD_INTERFACE_REQUEST:     \r
842                                                         /* Standard Interface request */\r
843                                                         prvHandleStandardInterfaceRequest( &xRequest );\r
844                                                         break;\r
845         \r
846                                 case usbSTANDARD_END_POINT_REQUEST:     \r
847                                                         /* Standard Endpoint request */\r
848                                                         prvHandleStandardEndPointRequest( &xRequest );\r
849                                                         break;\r
850         \r
851                                 case usbCLASS_INTERFACE_REQUEST:        \r
852                                                         /* Class Interface request */\r
853                                                         prvHandleClassInterfaceRequest( &xRequest );\r
854                                                         break;\r
855         \r
856                                 default:        /* This is not something we want to respond to. */\r
857                                                         prvSendStall(); \r
858                         }\r
859                 }\r
860         }\r
861 }\r
862 /*-----------------------------------------------------------*/\r
863 \r
864 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )\r
865 {\r
866         /* The type is in the high byte.  Return whatever has been requested. */\r
867         switch( ( pxRequest->usValue & 0xff00 ) >> 8 )\r
868         {\r
869             case usbDESCRIPTOR_TYPE_DEVICE:\r
870                         prvSendControlData( ( unsigned char * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );\r
871                     break;\r
872         \r
873             case usbDESCRIPTOR_TYPE_CONFIGURATION:\r
874                         prvSendControlData( ( unsigned char * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );\r
875                     break;\r
876 \r
877             case usbDESCRIPTOR_TYPE_STRING:\r
878 \r
879                         /* The index to the string descriptor is the lower byte. */\r
880                     switch( pxRequest->usValue & 0xff )\r
881                         {                       \r
882                         case usbLANGUAGE_STRING:\r
883                                         prvSendControlData( ( unsigned char * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );\r
884                                 break;\r
885 \r
886                         case usbMANUFACTURER_STRING:\r
887                                         prvSendControlData( ( unsigned char * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );\r
888                                 break;\r
889 \r
890                         case usbPRODUCT_STRING:\r
891                                         prvSendControlData( ( unsigned char * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );\r
892                                 break;\r
893 \r
894                         case usbCONFIGURATION_STRING:\r
895                                         prvSendControlData( ( unsigned char * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );\r
896                                 break;\r
897 \r
898                         case usbINTERFACE_STRING:\r
899                                         prvSendControlData( ( unsigned char * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );\r
900                                 break;\r
901 \r
902                         default:\r
903                                 /* Don't know what this string is. */\r
904                                         prvSendStall();\r
905                                         break;\r
906                         }\r
907 \r
908                         break;\r
909 \r
910             default:\r
911                         /* We are not responding to anything else. */\r
912                         prvSendStall();\r
913                     break;\r
914         }\r
915 }\r
916 /*-----------------------------------------------------------*/\r
917 \r
918 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )\r
919 {\r
920 unsigned short usStatus = 0;\r
921 \r
922         switch( pxRequest->ucRequest )\r
923         {\r
924             case usbGET_STATUS_REQUEST:\r
925                         /* Just send two byte dummy status. */\r
926                         prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );\r
927                     break;\r
928 \r
929             case usbGET_DESCRIPTOR_REQUEST:\r
930                         /* Send device descriptor */\r
931                     prvGetStandardDeviceDescriptor( pxRequest );\r
932                     break;\r
933 \r
934             case usbGET_CONFIGURATION_REQUEST:\r
935                         /* Send selected device configuration */\r
936                         prvSendControlData( ( unsigned char * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );\r
937                     break;\r
938 \r
939                 case usbSET_FEATURE_REQUEST:\r
940                     prvUSBTransmitNull();\r
941                     break;\r
942 \r
943             case usbSET_ADDRESS_REQUEST:\r
944                 \r
945                         /* Acknowledge the SET_ADDRESS, but (according to the manual) we\r
946                         cannot actually move to the addressed state until we get a TXCOMP\r
947                         interrupt from this NULL packet.  Therefore we just remember the\r
948                         address and set our state so we know we have received the address. */\r
949                 prvUSBTransmitNull();                   \r
950                         eDriverState = eJUST_GOT_ADDRESS;               \r
951                         ulReceivedAddress = ( unsigned long ) pxRequest->usValue;\r
952                     break;\r
953 \r
954             case usbSET_CONFIGURATION_REQUEST:\r
955 \r
956                         /* Acknowledge the SET_CONFIGURATION, but (according to the manual) \r
957                         we cannot actually move to the configured state until we get a \r
958                         TXCOMP interrupt from this NULL packet.  Therefore we just remember the\r
959                         config and set our state so we know we have received the go ahead. */                   \r
960                         ucUSBConfig = ( unsigned char ) ( pxRequest->usValue & 0xff );\r
961                         eDriverState = eJUST_GOT_CONFIG;\r
962                         prvUSBTransmitNull();\r
963                     break;\r
964 \r
965             default:\r
966 \r
967                     /* We don't answer to anything else. */\r
968                         prvSendStall();\r
969                     break;\r
970         }\r
971 }\r
972 /*-----------------------------------------------------------*/\r
973 \r
974 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )\r
975 {\r
976         switch( pxRequest->ucRequest )\r
977         {\r
978             case usbSET_IDLE_REQUEST:\r
979                 prvUSBTransmitNull();\r
980                         break;\r
981 \r
982                 /* This minimal implementation ignores these. */\r
983             case usbGET_REPORT_REQUEST:\r
984             case usbGET_IDLE_REQUEST:\r
985             case usbGET_PROTOCOL_REQUEST:\r
986             case usbSET_REPORT_REQUEST:\r
987             case usbSET_PROTOCOL_REQUEST:       \r
988             default:\r
989 \r
990                         prvSendStall();\r
991                         break;\r
992         }\r
993 }\r
994 /*-----------------------------------------------------------*/\r
995 \r
996 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )\r
997 {\r
998         switch( ( pxRequest->usValue & ( unsigned short ) 0xff00 ) >> 8 )\r
999         {\r
1000             case usbHID_REPORT_DESCRIPTOR:\r
1001                         prvSendControlData( ( unsigned char * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );\r
1002                     break;\r
1003 \r
1004             default:\r
1005 \r
1006                         /* Don't expect to send any others. */\r
1007                         prvSendStall();\r
1008                     break;\r
1009         }\r
1010 }\r
1011 /*-----------------------------------------------------------*/\r
1012 \r
1013 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )\r
1014 {\r
1015 unsigned short usStatus = 0;\r
1016 \r
1017         switch( pxRequest->ucRequest )\r
1018         {\r
1019             case usbGET_STATUS_REQUEST:\r
1020                         /* Send dummy 2 bytes. */\r
1021                         prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );\r
1022                         break;\r
1023 \r
1024             case usbGET_DESCRIPTOR_REQUEST:\r
1025                         prvGetStandardInterfaceDescriptor( pxRequest ); \r
1026                         break;\r
1027 \r
1028                 /* This minimal implementation does not respond to these. */\r
1029             case usbGET_INTERFACE_REQUEST:\r
1030             case usbSET_FEATURE_REQUEST:\r
1031             case usbSET_INTERFACE_REQUEST:      \r
1032 \r
1033             default:\r
1034                         prvSendStall();\r
1035                         break;\r
1036         }\r
1037 }\r
1038 /*-----------------------------------------------------------*/\r
1039 \r
1040 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )\r
1041 {\r
1042         switch( pxRequest->ucRequest )\r
1043         {\r
1044                 /* This minimal implementation does not expect to respond to these. */\r
1045             case usbGET_STATUS_REQUEST:\r
1046             case usbCLEAR_FEATURE_REQUEST: \r
1047             case usbSET_FEATURE_REQUEST:\r
1048 \r
1049             default:                    \r
1050                         prvSendStall();\r
1051                         break;\r
1052         }\r
1053 }\r
1054 /*-----------------------------------------------------------*/\r
1055 \r
1056 static void vInitUSBInterface( void )\r
1057 {\r
1058 volatile unsigned long ulTemp;\r
1059 \r
1060         /* Initialise a few state variables. */\r
1061         pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;\r
1062         ucUSBConfig = ( unsigned char ) 0;\r
1063         eDriverState = eNOTHING;\r
1064 \r
1065         /* HARDWARE SETUP */\r
1066 \r
1067     /* Set the PLL USB Divider */\r
1068     AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;\r
1069 \r
1070     /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */\r
1071     AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;\r
1072     AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);\r
1073 \r
1074     /* Setup the PIO for the USB pull up resistor. */\r
1075     AT91C_BASE_PIOA->PIO_PER = AT91C_PIO_PA16;\r
1076     AT91C_BASE_PIOA->PIO_OER = AT91C_PIO_PA16;\r
1077     \r
1078 \r
1079     /* Start without the pullup - this will get set at the end of this \r
1080         function. */\r
1081     AT91C_BASE_PIOA->PIO_SODR = AT91C_PIO_PA16;\r
1082 \r
1083         /* When using the USB debugger the peripheral registers do not always get\r
1084         set to the correct default values.  To make sure set the relevant registers\r
1085         manually here. */\r
1086         AT91C_BASE_UDP->UDP_IDR = ( unsigned long ) 0xffffffff;\r
1087         AT91C_BASE_UDP->UDP_ICR = ( unsigned long ) 0xffffffff;\r
1088         AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned long ) 0x00;\r
1089         AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned long ) 0x00;\r
1090         AT91C_BASE_UDP->UDP_GLBSTATE = 0;\r
1091         AT91C_BASE_UDP->UDP_FADDR = 0;\r
1092 \r
1093         /* Enable the transceiver. */\r
1094         AT91C_UDP_TRANSCEIVER_ENABLE = 0;\r
1095 \r
1096         /* Enable the USB interrupts - other interrupts get enabled as the \r
1097         enumeration process progresses. */\r
1098         AT91F_AIC_ConfigureIt( AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, ( void (*)( void ) ) vUSB_ISR_Wrapper );\r
1099         AT91C_BASE_AIC->AIC_IECR = 0x1 << AT91C_ID_UDP;\r
1100 \r
1101         /* Wait a short while before making our presence known. */\r
1102         vTaskDelay( usbINIT_DELAY );\r
1103         AT91C_BASE_PIOA->PIO_CODR = AT91C_PIO_PA16;\r
1104 }\r
1105 /*-----------------------------------------------------------*/\r
1106 \r
1107 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthToSend, long lSendingDescriptor )\r
1108 {\r
1109         if( ( ( unsigned long ) usRequestedLength < ulLengthToSend ) )\r
1110         {\r
1111                 /* Cap the data length to that requested. */\r
1112                 ulLengthToSend = ( unsigned short ) usRequestedLength;\r
1113         }\r
1114         else if( ( ulLengthToSend < ( unsigned long ) usRequestedLength ) && lSendingDescriptor )\r
1115         {\r
1116                 /* We are sending a descriptor.  If the descriptor is an exact \r
1117                 multiple of the FIFO length then it will have to be terminated\r
1118                 with a NULL packet.  Set the state to indicate this if\r
1119                 necessary. */\r
1120                 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )\r
1121                 {\r
1122                         eDriverState = eSENDING_EVEN_DESCRIPTOR;\r
1123                 }\r
1124         }\r
1125 \r
1126         /* Here we assume that the previous message has been sent.  THERE IS NO\r
1127         BUFFER OVERFLOW PROTECTION HERE.\r
1128 \r
1129         Copy the data to send into the buffer as we cannot send it all at once\r
1130         (if it is greater than 8 bytes in length). */\r
1131         memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );\r
1132 \r
1133         /* Reinitialise the buffer index so we start sending from the start of \r
1134         the data. */\r
1135         pxCharsForTx.ulTotalDataLength = ulLengthToSend;\r
1136         pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;\r
1137 \r
1138         /* Send the first 8 bytes now.  The rest will get sent in response to \r
1139         TXCOMP interrupts. */\r
1140         prvSendNextSegment();\r
1141 }\r
1142 /*-----------------------------------------------------------*/\r
1143 \r
1144 static void prvSendNextSegment( void )\r
1145 {\r
1146 volatile unsigned long ulNextLength, ulStatus, ulLengthLeftToSend;\r
1147 \r
1148         /* Is there any data to send? */\r
1149         if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )\r
1150         {\r
1151                 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;\r
1152         \r
1153                 /* We can only send 8 bytes to the fifo at a time. */\r
1154                 if( ulLengthLeftToSend > usbFIFO_LENGTH )\r
1155                 {\r
1156                         ulNextLength = usbFIFO_LENGTH;\r
1157                 }\r
1158                 else\r
1159                 {\r
1160                         ulNextLength = ulLengthLeftToSend;\r
1161                 }\r
1162 \r
1163                 /* Wait until we can place data in the fifo.  THERE IS NO TIMEOUT\r
1164                 HERE! */\r
1165                 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )\r
1166                 {\r
1167                         vTaskDelay( usbSHORTEST_DELAY );\r
1168                 }\r
1169 \r
1170                 /* Write the data to the FIFO. */\r
1171                 while( ulNextLength > ( unsigned long ) 0 )\r
1172                 {\r
1173                         AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];\r
1174         \r
1175                         ulNextLength--;\r
1176                         pxCharsForTx.ulNextCharIndex++;\r
1177                 }\r
1178         \r
1179                 /* Start the transmission. */\r
1180                 portENTER_CRITICAL();\r
1181                 {\r
1182                         ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
1183                         usbCSR_SET_BIT( &ulStatus, ( ( unsigned long ) 0x10 ) );\r
1184                         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;\r
1185                 }\r
1186                 portEXIT_CRITICAL();\r
1187         }\r
1188         else\r
1189         {\r
1190                 /* There is no data to send.  If we were sending a descriptor and the \r
1191                 descriptor was an exact multiple of the max packet size then we need\r
1192                 to send a null to terminate the transmission. */\r
1193                 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )\r
1194                 {\r
1195                         prvUSBTransmitNull();\r
1196                         eDriverState = eNOTHING;\r
1197                 }\r
1198         }\r
1199 }\r
1200 \r
1201 \r
1202 \r