]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91SAM7S64_IAR/USB/USBSample.c
Update version numbers to V7.4.1.
[freertos] / FreeRTOS / Demo / ARM7_AT91SAM7S64_IAR / 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 USB device driver.  This is a minimal implementation\r
77         for demonstration only.  Although functional, it is not a full and compliant\r
78         implementation.\r
79         \r
80         The USB device enumerates as a simple 3 axis joystick, and once configured\r
81         transmits 3 axis of data which can be viewed from the USB host machine.\r
82 \r
83         This file implements the USB interrupt service routine, and a demo FreeRTOS\r
84         task.  The interrupt service routine handles the USB hardware - taking a\r
85         snapshot of the USB status at the point of the interrupt.  The task receives\r
86         the status information from the interrupt for processing at the task level.\r
87         \r
88         See the FreeRTOS.org WEB documentation for more information.\r
89 */\r
90 \r
91 /*\r
92         Changes from V2.5.5\r
93         \r
94         + Descriptors that have a length that is an exact multiple of usbFIFO_LENGTH\r
95           can now be transmitted.  To this end an extra parameter has been\r
96           added to the prvSendControlData() function, and the state\r
97           eSENDING_EVEN_DESCRIPTOR has been introduced.  Thanks to Scott Miller for\r
98           assisting with this contribution.\r
99 \r
100         Changes from V2.6.0\r
101 \r
102         + Replaced the duplicated RX_DATA_BK0 in the interrupt mask with the\r
103           RX_DATA_BK1.\r
104 */\r
105 \r
106 /* Standard includes. */\r
107 #include <string.h>\r
108 \r
109 /* Demo board includes. */\r
110 #include "board.h"\r
111 \r
112 /* Scheduler includes. */\r
113 #include "FreeRTOS.h"\r
114 #include "task.h"\r
115 #include "queue.h"\r
116 \r
117 \r
118 /* Descriptor type definitions. */\r
119 #define usbDESCRIPTOR_TYPE_DEVICE                       ( 0x01 )\r
120 #define usbDESCRIPTOR_TYPE_CONFIGURATION        ( 0x02 )\r
121 #define usbDESCRIPTOR_TYPE_STRING                       ( 0x03 )\r
122 \r
123 /* USB request type definitions. */\r
124 #define usbGET_REPORT_REQUEST                           ( 0x01 )\r
125 #define usbGET_IDLE_REQUEST                                     ( 0x02 )\r
126 #define usbGET_PROTOCOL_REQUEST                         ( 0x03 )\r
127 #define usbSET_REPORT_REQUEST                           ( 0x09 )\r
128 #define usbSET_IDLE_REQUEST                                     ( 0x0A )\r
129 #define usbSET_PROTOCOL_REQUEST                         ( 0x0B )\r
130 #define usbGET_CONFIGURATION_REQUEST            ( 0x08 )\r
131 #define usbGET_STATUS_REQUEST                           ( 0x00 )\r
132 #define usbCLEAR_FEATURE_REQUEST                        ( 0x01 )\r
133 #define usbSET_FEATURE_REQUEST                          ( 0x03 )\r
134 #define usbSET_ADDRESS_REQUEST                          ( 0x05 )\r
135 #define usbGET_DESCRIPTOR_REQUEST                       ( 0x06 )\r
136 #define usbSET_CONFIGURATION_REQUEST            ( 0x09 )\r
137 #define usbGET_INTERFACE_REQUEST                        ( 0x0A )\r
138 #define usbSET_INTERFACE_REQUEST                        ( 0x0B )\r
139 \r
140 \r
141 /* Misc USB definitions. */\r
142 #define usbDEVICE_CLASS_VENDOR_SPECIFIC         ( 0xFF )\r
143 #define usbBUS_POWERED                                          ( 0x80 )\r
144 #define usbHID_REPORT_DESCRIPTOR                        ( 0x22 )\r
145 #define AT91C_UDP_TRANSCEIVER_ENABLE                    ( *( ( unsigned long * ) 0xfffb0074 ) )\r
146 \r
147 /* Index to the various string. */\r
148 #define usbLANGUAGE_STRING                                      ( 0 )\r
149 #define usbMANUFACTURER_STRING                          ( 1 )\r
150 #define usbPRODUCT_STRING                                       ( 2 )\r
151 #define usbCONFIGURATION_STRING                         ( 3 )\r
152 #define usbINTERFACE_STRING                                     ( 4 )\r
153 \r
154 /* Data indexes for reading the request from the xISRStatus.ucFifoData[]\r
155 into xUSB_REQUEST.  The data order is designed for speed - so looks a\r
156 little odd. */\r
157 #define usbREQUEST_TYPE_INDEX                           ( 7 )\r
158 #define usbREQUEST_INDEX                                        ( 6 )\r
159 #define usbVALUE_HIGH_BYTE                                      ( 4 )\r
160 #define usbVALUE_LOW_BYTE                                       ( 5 )\r
161 #define usbINDEX_HIGH_BYTE                                      ( 2 )\r
162 #define usbINDEX_LOW_BYTE                                       ( 3 )\r
163 #define usbLENGTH_HIGH_BYTE                                     ( 0 )\r
164 #define usbLENGTH_LOW_BYTE                                      ( 1 )\r
165 \r
166 /* Misc application definitions. */\r
167 #define usbINTERRUPT_PRIORITY                           ( 3 )\r
168 #define usbQUEUE_LENGTH                                         ( 0x3 ) /* Must have all bits set! */\r
169 #define usbFIFO_LENGTH                                          ( ( unsigned long ) 8 )\r
170 #define usbEND_POINT_0                                          ( 0 )\r
171 #define usbEND_POINT_1                                          ( 1 )\r
172 #define usbXUP                                                          ( 1 )\r
173 #define usbXDOWN                                                        ( 2 )\r
174 #define usbYUP                                                          ( 3 )\r
175 #define usbYDOWN                                                        ( 4 )\r
176 #define usbMAX_COORD                                            ( 120 )\r
177 #define usbMAX_TX_MESSAGE_SIZE                          ( 128 )\r
178 #define usbRX_COUNT_MASK                                        ( ( unsigned long ) 0x7ff )\r
179 #define AT91C_UDP_STALLSENT                                     AT91C_UDP_ISOERROR\r
180 #define usbSHORTEST_DELAY                                       ( ( portTickType ) 1 )\r
181 #define usbINIT_DELAY                                           ( ( portTickType ) 500 / portTICK_RATE_MS )\r
182 #define usbSHORT_DELAY                                          ( ( portTickType ) 50 / portTICK_RATE_MS )\r
183 #define usbEND_POINT_RESET_MASK                         ( ( unsigned long ) 0x0f )\r
184 #define usbDATA_INC                                                     ( ( char ) 5 )\r
185 #define usbEXPECTED_NUMBER_OF_BYTES                     ( ( unsigned long ) 8 )\r
186 \r
187 /* Control request types. */\r
188 #define usbSTANDARD_DEVICE_REQUEST                      ( 0 )\r
189 #define usbSTANDARD_INTERFACE_REQUEST           ( 1 )\r
190 #define usbSTANDARD_END_POINT_REQUEST           ( 2 )\r
191 #define usbCLASS_INTERFACE_REQUEST                      ( 5 )\r
192 \r
193 /*-----------------------------------------------------------*/\r
194 \r
195 /* Structure used to take a snapshot of the USB status from within the ISR. */\r
196 typedef struct X_ISR_STATUS\r
197 {\r
198         unsigned long ulISR;\r
199         unsigned long ulCSR0;\r
200         unsigned char ucFifoData[ 8 ];\r
201 } xISRStatus;\r
202 \r
203 /* Structure used to hold the received requests. */\r
204 typedef struct\r
205 {\r
206         unsigned char ucReqType;\r
207         unsigned char ucRequest;\r
208         unsigned short usValue;\r
209         unsigned short usIndex;\r
210         unsigned short usLength;\r
211 } xUSB_REQUEST;\r
212 \r
213 typedef enum\r
214 {\r
215         eNOTHING,\r
216         eJUST_RESET,\r
217         eJUST_GOT_CONFIG,\r
218         eJUST_GOT_ADDRESS,\r
219         eSENDING_EVEN_DESCRIPTOR,\r
220         eREADY_TO_SEND\r
221 } eDRIVER_STATE;\r
222 \r
223 /* Structure used to control the data being sent to the host. */\r
224 typedef struct\r
225 {\r
226         unsigned char ucTxBuffer[ usbMAX_TX_MESSAGE_SIZE ];\r
227         unsigned long ulNextCharIndex;\r
228         unsigned long ulTotalDataLength;\r
229 } xTX_MESSAGE;\r
230 \r
231 /*-----------------------------------------------------------*/\r
232 \r
233 /*\r
234  * The USB interrupt service routine.  This takes a snapshot of the USB\r
235  * device at the time of the interrupt, clears the interrupts, and posts\r
236  * the data to the USB processing task.\r
237  */\r
238 __arm void vUSB_ISR( void );\r
239 \r
240 /*\r
241  * Called after the bus reset interrupt - this function readies all the\r
242  * end points for communication.\r
243  */\r
244 static void prvResetEndPoints( void );\r
245 \r
246 /*\r
247  * Setup the USB hardware, install the interrupt service routine and\r
248  * initialise all the state variables.\r
249  */\r
250 static void vInitUSBInterface( void );\r
251 \r
252 /*\r
253  * Decode and act upon an interrupt generated by the control end point.\r
254  */\r
255 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage );\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 standard device requests originating on the control\r
262  * end point.\r
263  */\r
264 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest );\r
265 \r
266 /*\r
267  * For simplicity requests are separated into device, interface, class\r
268  * interface and end point requests.\r
269  *\r
270  * Decode and handle standard interface requests originating on the control\r
271  * end point.\r
272  */\r
273 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest );\r
274 \r
275 /*\r
276  * For simplicity requests are separated into device, interface, class\r
277  * interface and end point requests.\r
278  *\r
279  * Decode and handle standard end point requests originating on the control\r
280  * end point.\r
281  */\r
282 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest );\r
283 \r
284 /*\r
285  * For simplicity requests are separated into device, interface, class\r
286  * interface and end point requests.\r
287  *\r
288  * Decode and handle the class interface requests.\r
289  */\r
290 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest );\r
291 \r
292 /*\r
293  * Setup the Tx buffer to send data in response to a control request.\r
294  *\r
295  * The data to be transmitted is buffered, the state variables are updated,\r
296  * then prvSendNextSegment() is called to start the transmission off.  Once\r
297  * the first segment has been sent the remaining segments are transmitted\r
298  * in response to TXCOMP interrupts until the entire buffer has been\r
299  * sent.\r
300  */\r
301 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthLeftToSend, long lSendingDescriptor );\r
302 \r
303 /*\r
304  * Examine the Tx buffer to see if there is any more data to be transmitted.\r
305  *\r
306  * If there is data to be transmitted then send the next segment.  A segment\r
307  * can have a maximum of 8 bytes (this is defined as the maximum for the end\r
308  * point by the descriptor).  The final segment may be less than 8 bytes if\r
309  * the total data length was not an exact multiple of 8.\r
310  */\r
311 static void prvSendNextSegment( void );\r
312 \r
313 /*\r
314  * A stall condition is forced each time the host makes a request that is not\r
315  * supported by this minimal implementation.\r
316  *\r
317  * A stall is forced by setting the appropriate bit in the end points control\r
318  * and status register.\r
319  */\r
320 static void prvSendStall( void );\r
321 \r
322 /*\r
323  * A NULL (or zero length packet) is transmitted in acknowledge the reception\r
324  * of certain events from the host.\r
325  */\r
326 static void prvUSBTransmitNull( void );\r
327 \r
328 /*\r
329  * When the host requests a descriptor this function is called to determine\r
330  * which descriptor is being requested and start its transmission.\r
331  */\r
332 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest );\r
333 \r
334 /*\r
335  * This demo USB device enumerates as a simple 3 axis joystick.  Once\r
336  * configured this function is periodically called to generate some sample\r
337  * joystick data.\r
338  *\r
339  * The x and y axis are made to move in a square.  The z axis is made to\r
340  * repeatedly increment up to its maximum.\r
341  */\r
342 static void prvTransmitSampleValues( void );\r
343 \r
344 /*\r
345  * The created task to handle the USB demo functionality.\r
346  */\r
347 void vUSBDemoTask( void *pvParameters );\r
348 \r
349 /*-----------------------------------------------------------*/\r
350 \r
351 /*\r
352         - DESCRIPTOR DEFINITIONS -\r
353 */\r
354 \r
355 /* String descriptors used during the enumeration process.\r
356 These take the form:\r
357 \r
358 {\r
359         Length of descriptor,\r
360         Descriptor type,\r
361         Data\r
362 }\r
363 */\r
364 const char pxLanguageStringDescriptor[] =\r
365 {\r
366         4,\r
367         usbDESCRIPTOR_TYPE_STRING,\r
368         0x09, 0x04\r
369 };\r
370 \r
371 const char pxManufacturerStringDescriptor[] =\r
372 {\r
373         18,\r
374         usbDESCRIPTOR_TYPE_STRING,\r
375 \r
376         'F', 0x00,\r
377         'r', 0x00,\r
378         'e', 0x00,\r
379         'e', 0x00,\r
380         'R', 0x00,\r
381         'T', 0x00,\r
382         'O', 0x00,\r
383         'S', 0x00       \r
384 };\r
385 \r
386 const char pxProductStringDescriptor[] =\r
387 {\r
388         44,\r
389         usbDESCRIPTOR_TYPE_STRING,\r
390 \r
391         'F', 0x00,\r
392         'r', 0x00,\r
393         'e', 0x00,\r
394         'e', 0x00,\r
395         'R', 0x00,\r
396         'T', 0x00,\r
397         'O', 0x00,\r
398         'S', 0x00,\r
399         '.', 0x00,\r
400         'o', 0x00,\r
401         'r', 0x00,\r
402         'g', 0x00,\r
403         ' ', 0x00,\r
404         'J', 0x00,\r
405         'o', 0x00,\r
406         'y', 0x00,\r
407         's', 0x00,\r
408         't', 0x00,\r
409         'i', 0x00,\r
410         'c', 0x00,\r
411         'k', 0x00\r
412 };\r
413 \r
414 const char pxConfigurationStringDescriptor[] =\r
415 {\r
416         38,\r
417         usbDESCRIPTOR_TYPE_STRING,\r
418 \r
419         'C', 0x00,\r
420         'o', 0x00,\r
421         'n', 0x00,\r
422         'f', 0x00,\r
423         'i', 0x00,\r
424         'g', 0x00,\r
425         'u', 0x00,\r
426         'r', 0x00,\r
427         'a', 0x00,\r
428         't', 0x00,\r
429         'i', 0x00,\r
430         'o', 0x00,\r
431         'n', 0x00,\r
432         ' ', 0x00,\r
433         'N', 0x00,\r
434         'a', 0x00,\r
435         'm', 0x00,\r
436         'e', 0x00\r
437 };\r
438 \r
439 const char pxInterfaceStringDescriptor[] =\r
440 {\r
441         30,\r
442         usbDESCRIPTOR_TYPE_STRING,\r
443 \r
444         'I', 0x00,\r
445         'n', 0x00,\r
446         't', 0x00,\r
447         'e', 0x00,\r
448         'r', 0x00,\r
449         'f', 0x00,\r
450         'a', 0x00,\r
451         'c', 0x00,\r
452         'e', 0x00,\r
453         ' ', 0x00,\r
454         'N', 0x00,\r
455         'a', 0x00,\r
456         'm', 0x00,\r
457         'e', 0x00\r
458 };\r
459 \r
460 /* Enumeration descriptors. */\r
461 const char pxReportDescriptor[] =\r
462 {\r
463          0x05,  0x01,   /* USAGE_PAGE (Generic Desktop)         */\r
464          0x09,  0x04,   /* USAGE (Joystick)                                     */\r
465          0xa1,  0x01,   /* COLLECTION (Application)                     */\r
466          0x05,  0x01,   /*   USAGE_PAGE (Generic Desktop)       */\r
467          0x09,  0x01,   /*   USAGE (Pointer)                            */\r
468          0xa1,  0x00,   /*   COLLECTION (Physical)                      */\r
469          0x09,  0x30,   /*     USAGE (X)                                        */\r
470          0x09,  0x31,   /*     USAGE (Y)                                        */\r
471          0x09,  0x32,   /*     USAGE (Z)                                        */\r
472          0x15,  0x81,   /*     LOGICAL_MINIMUM (-127)           */\r
473          0x25,  0x7f,   /*     LOGICAL_MAXIMUM (127)            */\r
474          0x75,  0x08,   /*     REPORT_SIZE (8)                          */\r
475          0x95,  0x03,   /*     REPORT_COUNT (3)                         */\r
476          0x81,  0x02,   /*     INPUT (Data,Var,Abs)                     */\r
477          0xc0,                  /*   END_COLLECTION                                     */\r
478          0xc0                   /* END_COLLECTION                                       */\r
479 };\r
480 \r
481 const char pxDeviceDescriptor[] =\r
482 {\r
483         /* Device descriptor */\r
484         0x12,                                                           /* bLength                              */\r
485         0x01,                                                           /* bDescriptorType              */\r
486         0x10, 0x01,                                                     /* bcdUSBL                              */\r
487         usbDEVICE_CLASS_VENDOR_SPECIFIC,        /* bDeviceClass:                */\r
488         0x00,                                                           /* bDeviceSubclass:             */\r
489         0x00,                                                           /* bDeviceProtocol:             */\r
490         0x08,                                                           /* bMaxPacketSize0              */\r
491         0xFF, 0xFF,                                                     /* idVendorL                    */\r
492         0x01, 0x00,                                                     /* idProductL                   */\r
493         0x00, 0x01,                                                     /* bcdDeviceL                   */\r
494         usbMANUFACTURER_STRING,                         /* iManufacturer                */\r
495         usbPRODUCT_STRING,                                      /* iProduct                             */\r
496         0x00,                                                           /* SerialNumber                 */\r
497         0x01                                                            /* bNumConfigs                  */\r
498 };\r
499 \r
500 const char pxConfigDescriptor[] = {\r
501         /* Configuration 1 descriptor */\r
502         0x09,                   /* CbLength                                                                     */\r
503         0x02,                   /* CbDescriptorType                                                     */\r
504         0x22, 0x00,             /* CwTotalLength 2 EP + Control                         */\r
505         0x01,                   /* CbNumInterfaces                                                      */\r
506         0x01,                   /* CbConfigurationValue                                         */\r
507         usbCONFIGURATION_STRING,/* CiConfiguration                                      */\r
508         usbBUS_POWERED, /* CbmAttributes Bus powered + Remote Wakeup*/\r
509         0x32,                   /* CMaxPower: 100mA                                                     */\r
510 \r
511         /* Joystick Interface Descriptor Requirement */\r
512         0x09,                   /* bLength                                                                      */\r
513         0x04,                   /* bDescriptorType                                                      */\r
514         0x00,                   /* bInterfaceNumber                                                     */\r
515         0x00,                   /* bAlternateSetting                                            */\r
516         0x01,                   /* bNumEndpoints                                                        */\r
517         0x03,                   /* bInterfaceClass: HID code                            */\r
518         0x00,                   /* bInterfaceSubclass                                           */\r
519         0x00,                   /* bInterfaceProtocol                                           */\r
520         usbINTERFACE_STRING,/* iInterface                                                       */\r
521 \r
522         /* HID Descriptor */\r
523         0x09,                   /* bLength                                                                      */\r
524         0x21,                   /* bDescriptor type: HID Descriptor Type        */\r
525         0x00, 0x01,             /* bcdHID                                                                       */\r
526         0x00,                   /* bCountryCode                                                         */\r
527         0x01,                   /* bNumDescriptors                                                      */\r
528         usbHID_REPORT_DESCRIPTOR,         /* bDescriptorType                    */\r
529         sizeof( pxReportDescriptor ), 0x00, /* wItemLength                      */\r
530 \r
531         /* Endpoint 1 descriptor */\r
532         0x07,                   /* bLength                                                                      */\r
533         0x05,                   /* bDescriptorType                                                      */\r
534         0x81,                   /* bEndpointAddress, Endpoint 01 - IN           */\r
535         0x03,                   /* bmAttributes      INT                                        */\r
536         0x03, 0x00,             /* wMaxPacketSize: 3 bytes (x, y, z)            */\r
537         0x0A                    /* bInterval                                                            */\r
538 };\r
539 \r
540 /*-----------------------------------------------------------*/\r
541 \r
542 /* File scope state variables. */\r
543 static unsigned char ucUSBConfig = ( unsigned char ) 0;\r
544 static unsigned long ulReceivedAddress = ( unsigned long ) 0;\r
545 static eDRIVER_STATE eDriverState = eNOTHING;\r
546 \r
547 /* Array in which the USB interrupt status is passed between the ISR and task. */\r
548 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];\r
549 \r
550 /* Structure used to control the characters being sent to the host. */\r
551 static xTX_MESSAGE pxCharsForTx;\r
552 \r
553 /* Queue used to pass messages between the ISR and the task. */\r
554 static xQueueHandle xUSBInterruptQueue;\r
555 \r
556 /* ISR entry has to be written in the asm file as we want a context switch\r
557 to occur from within the ISR.  See the port documentation on the FreeRTOS.org\r
558 WEB site for more information. */\r
559 extern void vUSBISREntry( void );\r
560 \r
561 /*-----------------------------------------------------------*/\r
562 \r
563 /* Macros to manipulate the control and status registers.  These registers\r
564 cannot be accessed using a direct read modify write operation outside of the\r
565 ISR as some bits are left unchanged by writing with a 0, and some are left\r
566 unchanged by writing with a 1. */\r
567 \r
568 #define usbINT_CLEAR_MASK       (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )\r
569 \r
570 #define usbCSR_SET_BIT( pulValueNow, ulBit )                                                                                    \\r
571 {                                                                                                                                                                               \\r
572         /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */                                                                                         \\r
573         /* STALLSENT and RX_DATA_BK1 to 1 so the */                                                                                     \\r
574         /* write has no effect. */                                                                                                                      \\r
575         ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f;            \\r
576                                                                                                                                                                                 \\r
577         /* Clear the FORCE_STALL and TXPKTRDY bits */                                                                           \\r
578         /* so the write has no effect. */                                                                                                       \\r
579         ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf;      \\r
580                                                                                                                                                                                 \\r
581         /* Set whichever bit we want set. */                                                                                            \\r
582         ( * ( ( unsigned long * ) pulValueNow ) ) |= ( ulBit );                                                 \\r
583 }\r
584 \r
585 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit )                                                                                  \\r
586 {                                                                                                                                                                               \\r
587         /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */                                                                                         \\r
588         /* STALLSENT and RX_DATA_BK1 to 1 so the */                                                                                     \\r
589         /* write has no effect. */                                                                                                                      \\r
590         ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f;            \\r
591                                                                                                                                                                                 \\r
592         /* Clear the FORCE_STALL and TXPKTRDY bits */                                                                           \\r
593         /* so the write has no effect. */                                                                                                       \\r
594         ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf;      \\r
595                                                                                                                                                                                 \\r
596         /* Clear whichever bit we want clear. */                                                                                        \\r
597         ( * ( ( unsigned long * ) pulValueNow ) ) &= ( ~ulBit );                                                \\r
598 }\r
599 \r
600 /*-----------------------------------------------------------*/\r
601 \r
602 __arm void vUSB_ISR( void )\r
603 {\r
604 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
605 static volatile unsigned long ulNextMessage = 0;\r
606 xISRStatus *pxMessage;\r
607 unsigned long ulTemp, ulRxBytes;\r
608 \r
609         /* Take the next message from the queue.  Note that usbQUEUE_LENGTH *must*\r
610         be all 1's, as in 0x01, 0x03, 0x07, etc. */\r
611         pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );\r
612         ulNextMessage++;\r
613 \r
614         /* Take a snapshot of the current USB state for processing at the task\r
615         level. */\r
616         pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;\r
617         pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
618 \r
619         /* Clear the interrupts from the ICR register.  The bus end interrupt is\r
620         cleared separately as it does not appear in the mask register. */\r
621         AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;\r
622         \r
623         /* If there are bytes in the FIFO then we have to retrieve them here.\r
624         Ideally this would be done at the task level.  However we need to clear the\r
625         RXSETUP interrupt before leaving the ISR, and this may cause the data in\r
626         the FIFO to be overwritten.  Also the DIR bit has to be changed before the\r
627         RXSETUP bit is cleared (as per the SAM7 manual). */\r
628         ulTemp = pxMessage->ulCSR0;\r
629         \r
630         /* Are there any bytes in the FIFO? */\r
631         ulRxBytes = ulTemp >> 16;\r
632         ulRxBytes &= usbRX_COUNT_MASK;\r
633         \r
634         /* With this minimal implementation we are only interested in receiving\r
635         setup bytes on the control end point. */\r
636         if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )\r
637         {\r
638                 /* Take off 1 for a zero based index. */\r
639                 while( ulRxBytes > 0 )\r
640                 {\r
641                         ulRxBytes--;\r
642                         pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];                 \r
643                 }\r
644                 \r
645                 /* The direction must be changed first. */\r
646                 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );\r
647                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
648         }\r
649         \r
650         /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA\r
651         registers to clear the interrupts in the CSR register. */\r
652         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );\r
653         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
654 \r
655         /* Also clear the interrupts in the CSR1 register. */\r
656         ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];\r
657         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK ); \r
658         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;\r
659 \r
660         /* The message now contains the entire state and optional data from\r
661         the USB interrupt.  This can now be posted on the Rx queue ready for\r
662         processing at the task level. */\r
663         xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );\r
664 \r
665         /* We may want to switch to the USB task, if this message has made\r
666         it the highest priority task that is ready to execute. */\r
667         portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );\r
668 \r
669         /* Clear the AIC ready for the next interrupt. */               \r
670         AT91C_BASE_AIC->AIC_EOICR = 0;\r
671 }\r
672 /*-----------------------------------------------------------*/\r
673 \r
674 void vUSBDemoTask( void *pvParameters )\r
675 {\r
676 xISRStatus *pxMessage;\r
677 \r
678         /* The parameters are not used in this task. */\r
679         ( void ) pvParameters;\r
680 \r
681     /* Init USB device */\r
682     portENTER_CRITICAL();\r
683             vInitUSBInterface();\r
684     portEXIT_CRITICAL();\r
685 \r
686         /* Process interrupts as they arrive.   The ISR takes a snapshot of the\r
687         interrupt status then posts the information on this queue for processing\r
688         at the task level.  This simple demo implementation only processes\r
689         a few interrupt sources. */\r
690         for( ;; )\r
691         {\r
692                 if( xQueueReceive( xUSBInterruptQueue, &pxMessage, usbSHORT_DELAY ) )\r
693                 {\r
694                         if( pxMessage->ulISR & AT91C_UDP_EPINT0 )\r
695                         {\r
696                                 /* Process end point 0 interrupt. */\r
697                                 prvProcessEndPoint0Interrupt( pxMessage );\r
698                         }\r
699 \r
700                         if( pxMessage->ulISR & AT91C_UDP_ENDBUSRES )\r
701                         {\r
702                                 /* Process an end of bus reset interrupt. */\r
703                                 prvResetEndPoints();            \r
704                         }\r
705                 }\r
706                 else\r
707                 {\r
708                         /* The ISR did not post any data for us to process on the queue, so\r
709                         just generate and send some sample data. */\r
710                         if( eDriverState == eREADY_TO_SEND )\r
711                         {\r
712                                 prvTransmitSampleValues();\r
713                         }\r
714                 }\r
715         }\r
716 }\r
717 /*-----------------------------------------------------------*/\r
718 \r
719 static void prvTransmitSampleValues( void )\r
720 {\r
721 unsigned long ulStatus;\r
722 static long lState = usbXUP;\r
723 \r
724 /* Variables to hold dummy x, y and z joystick axis data. */\r
725 static signed char x = 0, y = 0, z = 0;\r
726 \r
727         /* Generate some sample data in the x and y axis - draw a square. */\r
728         switch( lState )\r
729         {\r
730                 case usbXUP     :       x += usbDATA_INC;\r
731                                                 if( x >= usbMAX_COORD )\r
732                                                 {\r
733                                                         lState = usbYUP;\r
734                                                 }\r
735                                                 break;\r
736                                                 \r
737                 case usbXDOWN : x -= usbDATA_INC;\r
738                                                 if( x <= -usbMAX_COORD )\r
739                                                 {\r
740                                                         lState = usbYDOWN;\r
741                                                 }\r
742                                                 break;\r
743                                                 \r
744                 case usbYUP :   y += usbDATA_INC;\r
745                                                 if( y >= usbMAX_COORD )\r
746                                                 {\r
747                                                         lState = usbXDOWN;\r
748                                                 }\r
749                                                 break;\r
750                                                 \r
751                 case usbYDOWN : y -= usbDATA_INC;\r
752                                                 if( y <= -usbMAX_COORD )\r
753                                                 {\r
754                                                         lState = usbXUP;\r
755                                                 }\r
756                                                 break;\r
757         }\r
758 \r
759         /* Just make the z axis go up and down. */\r
760         z += usbDATA_INC;\r
761 \r
762         /* Can we place data in the fifo? */\r
763         if( !( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & AT91C_UDP_TXPKTRDY ) )\r
764         {\r
765                 /* Write our sample data to the fifo. */\r
766                 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = x;\r
767                 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = y;\r
768                 AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_1 ] = z;\r
769                 \r
770                 /* Send the data. */\r
771                 portENTER_CRITICAL();\r
772                 {\r
773                         ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];\r
774                         usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );\r
775                         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulStatus;\r
776                 }\r
777                 portEXIT_CRITICAL();\r
778         }\r
779 }\r
780 /*-----------------------------------------------------------*/\r
781 \r
782 static void prvUSBTransmitNull( void )\r
783 {\r
784 unsigned long ulStatus;\r
785 \r
786         /* Wait until the FIFO is free - even though we are not going to use it.\r
787         THERE IS NO TIMEOUT HERE! */\r
788         while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )\r
789         {\r
790                 vTaskDelay( usbSHORTEST_DELAY );\r
791         }\r
792 \r
793         portENTER_CRITICAL();\r
794         {\r
795                 /* Set the length of data to send to equal the index of the next byte\r
796                 to send.  This will prevent the ACK to this NULL packet causing any\r
797                 further data transmissions. */\r
798                 pxCharsForTx.ulTotalDataLength = pxCharsForTx.ulNextCharIndex;\r
799 \r
800                 /* Set the TXPKTRDY bit to cause a transmission with no data. */\r
801                 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
802                 usbCSR_SET_BIT( &ulStatus, ( AT91C_UDP_TXPKTRDY ) );\r
803                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;\r
804         }\r
805         portEXIT_CRITICAL();\r
806 }\r
807 /*-----------------------------------------------------------*/\r
808 \r
809 static void prvSendStall( void )\r
810 {\r
811 unsigned long ulStatus;\r
812 \r
813         portENTER_CRITICAL();\r
814         {\r
815                 /* Force a stall by simply setting the FORCESTALL bit in the CSR. */\r
816                 ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
817                 usbCSR_SET_BIT( &ulStatus, AT91C_UDP_FORCESTALL );\r
818                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;\r
819         }\r
820         portEXIT_CRITICAL();\r
821 }\r
822 /*-----------------------------------------------------------*/\r
823 \r
824 static void prvResetEndPoints( void )\r
825 {\r
826 unsigned long ulTemp;\r
827 \r
828         eDriverState = eJUST_RESET;\r
829 \r
830         /* Reset all the end points. */\r
831         AT91C_BASE_UDP->UDP_RSTEP  = usbEND_POINT_RESET_MASK;\r
832         AT91C_BASE_UDP->UDP_RSTEP  = ( unsigned long ) 0x00;\r
833 \r
834         /* Enable data to be sent and received. */\r
835         AT91C_BASE_UDP->UDP_FADDR = AT91C_UDP_FEN;\r
836 \r
837         /* Repair the configuration end point. */\r
838         portENTER_CRITICAL();\r
839         {\r
840                 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
841                 usbCSR_SET_BIT( &ulTemp, ( ( unsigned long ) ( AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_CTRL ) ) );\r
842                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
843                 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT0 );\r
844         }\r
845         portEXIT_CRITICAL();\r
846 }\r
847 /*-----------------------------------------------------------*/\r
848 \r
849 static void prvProcessEndPoint0Interrupt( xISRStatus *pxMessage )\r
850 {\r
851         if( pxMessage->ulCSR0 & AT91C_UDP_RX_DATA_BK0 )\r
852         {               \r
853                 /* We only expect to receive zero length data here as ACK's.\r
854                 Set the data pointer to the end of the current Tx packet to\r
855                 ensure we don't send out any more data. */      \r
856                 pxCharsForTx.ulNextCharIndex = pxCharsForTx.ulTotalDataLength;\r
857         }\r
858 \r
859         if( pxMessage->ulCSR0 & AT91C_UDP_TXCOMP )\r
860         {\r
861                 /* We received a TX complete interrupt.  What we do depends on\r
862                 what we sent to get this interrupt. */\r
863 \r
864                 if( eDriverState == eJUST_GOT_CONFIG )\r
865                 {\r
866                         /* We sent an acknowledgement of a SET_CONFIG request.  We\r
867                         are now at the end of the enumeration. */\r
868                         AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_CONFG;\r
869 \r
870                         /* Read the end point for data transfer. */\r
871                         portENTER_CRITICAL();\r
872                         {\r
873                                 unsigned long ulTemp;\r
874 \r
875                                 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];                                     \r
876                                 usbCSR_SET_BIT( &ulTemp, AT91C_UDP_EPEDS | AT91C_UDP_EPTYPE_INT_IN );\r
877                                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;             \r
878                                 AT91F_UDP_EnableIt( AT91C_BASE_UDP, AT91C_UDP_EPINT1 );\r
879                         }\r
880                         portEXIT_CRITICAL();\r
881 \r
882                         eDriverState = eREADY_TO_SEND;\r
883                 }               \r
884                 else if( eDriverState == eJUST_GOT_ADDRESS )\r
885                 {\r
886                         /* We sent an acknowledgement of a SET_ADDRESS request.  Move\r
887                         to the addressed state. */\r
888                         if( ulReceivedAddress != ( unsigned long ) 0 )\r
889                         {                       \r
890                                 AT91C_BASE_UDP->UDP_GLBSTATE = AT91C_UDP_FADDEN;\r
891                         }\r
892                         else\r
893                         {\r
894                                 AT91C_BASE_UDP->UDP_GLBSTATE = 0;\r
895                         }                       \r
896 \r
897                         AT91C_BASE_UDP->UDP_FADDR = ( AT91C_UDP_FEN | ulReceivedAddress );              \r
898                         eDriverState = eNOTHING;\r
899                 }\r
900                 else\r
901                 {               \r
902                         /* The TXCOMP was not for any special type of transmission.  See\r
903                         if there is any more data to send. */\r
904                         prvSendNextSegment();\r
905                 }\r
906         }\r
907 \r
908         if( pxMessage->ulCSR0 & AT91C_UDP_RXSETUP )\r
909         {\r
910                 xUSB_REQUEST xRequest;\r
911                 unsigned char ucRequest;\r
912                 unsigned long ulRxBytes;\r
913 \r
914                 /* A data packet is available. */       \r
915                 ulRxBytes = pxMessage->ulCSR0 >> 16;\r
916                 ulRxBytes &= usbRX_COUNT_MASK;\r
917 \r
918                 if( ulRxBytes >= usbEXPECTED_NUMBER_OF_BYTES )\r
919                 {\r
920                         /* Create an xUSB_REQUEST variable from the raw bytes array. */\r
921 \r
922                         xRequest.ucReqType = pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ];\r
923                         xRequest.ucRequest = pxMessage->ucFifoData[ usbREQUEST_INDEX ];\r
924 \r
925                         /* NOT PORTABLE CODE! */\r
926                         xRequest.usValue = pxMessage->ucFifoData[ usbVALUE_HIGH_BYTE ];\r
927                         xRequest.usValue <<= 8;\r
928                         xRequest.usValue |= pxMessage->ucFifoData[ usbVALUE_LOW_BYTE ];\r
929                                                 \r
930                         xRequest.usIndex = pxMessage->ucFifoData[ usbINDEX_HIGH_BYTE ];\r
931                         xRequest.usIndex <<= 8;\r
932                         xRequest.usIndex |= pxMessage->ucFifoData[ usbINDEX_LOW_BYTE ];\r
933                         \r
934                         xRequest.usLength = pxMessage->ucFifoData[ usbLENGTH_HIGH_BYTE ];\r
935                         xRequest.usLength <<= 8;\r
936                         xRequest.usLength |= pxMessage->ucFifoData[ usbLENGTH_LOW_BYTE ];\r
937         \r
938                         /* Manipulate the ucRequestType and the ucRequest parameters to\r
939                         generate a zero based request selection.  This is just done to\r
940                         break up the requests into subsections for clarity.  The\r
941                         alternative would be to have more huge switch statement that would\r
942                         be difficult to optimise. */\r
943                         ucRequest = ( ( xRequest.ucReqType & 0x60 ) >> 3 );\r
944                         ucRequest |= ( xRequest.ucReqType & 0x03 );\r
945 \r
946                         switch( ucRequest )\r
947                         {\r
948                                 case usbSTANDARD_DEVICE_REQUEST:        \r
949                                                         /* Standard Device request */\r
950                                                         prvHandleStandardDeviceRequest( &xRequest );\r
951                                                         break;\r
952         \r
953                                 case usbSTANDARD_INTERFACE_REQUEST:     \r
954                                                         /* Standard Interface request */\r
955                                                         prvHandleStandardInterfaceRequest( &xRequest );\r
956                                                         break;\r
957         \r
958                                 case usbSTANDARD_END_POINT_REQUEST:     \r
959                                                         /* Standard Endpoint request */\r
960                                                         prvHandleStandardEndPointRequest( &xRequest );\r
961                                                         break;\r
962         \r
963                                 case usbCLASS_INTERFACE_REQUEST:        \r
964                                                         /* Class Interface request */\r
965                                                         prvHandleClassInterfaceRequest( &xRequest );\r
966                                                         break;\r
967         \r
968                                 default:        /* This is not something we want to respond to. */\r
969                                                         prvSendStall(); \r
970                         }\r
971                 }\r
972         }\r
973 }\r
974 /*-----------------------------------------------------------*/\r
975 \r
976 static void prvGetStandardDeviceDescriptor( xUSB_REQUEST *pxRequest )\r
977 {\r
978         /* The type is in the high byte.  Return whatever has been requested. */\r
979         switch( ( pxRequest->usValue & 0xff00 ) >> 8 )\r
980         {\r
981             case usbDESCRIPTOR_TYPE_DEVICE:\r
982                         prvSendControlData( ( unsigned char * ) &pxDeviceDescriptor, pxRequest->usLength, sizeof( pxDeviceDescriptor ), pdTRUE );\r
983                     break;\r
984         \r
985             case usbDESCRIPTOR_TYPE_CONFIGURATION:\r
986                         prvSendControlData( ( unsigned char * ) &( pxConfigDescriptor ), pxRequest->usLength, sizeof( pxConfigDescriptor ), pdTRUE );\r
987                     break;\r
988 \r
989             case usbDESCRIPTOR_TYPE_STRING:\r
990 \r
991                         /* The index to the string descriptor is the lower byte. */\r
992                     switch( pxRequest->usValue & 0xff )\r
993                         {                       \r
994                         case usbLANGUAGE_STRING:\r
995                                         prvSendControlData( ( unsigned char * ) &pxLanguageStringDescriptor, pxRequest->usLength, sizeof(pxLanguageStringDescriptor), pdTRUE );\r
996                                 break;\r
997 \r
998                         case usbMANUFACTURER_STRING:\r
999                                         prvSendControlData( ( unsigned char * ) &pxManufacturerStringDescriptor, pxRequest->usLength, sizeof( pxManufacturerStringDescriptor ), pdTRUE );\r
1000                                 break;\r
1001 \r
1002                         case usbPRODUCT_STRING:\r
1003                                         prvSendControlData( ( unsigned char * ) &pxProductStringDescriptor, pxRequest->usLength, sizeof( pxProductStringDescriptor ), pdTRUE );\r
1004                                 break;\r
1005 \r
1006                         case usbCONFIGURATION_STRING:\r
1007                                         prvSendControlData( ( unsigned char * ) &pxConfigurationStringDescriptor, pxRequest->usLength, sizeof( pxConfigurationStringDescriptor ), pdTRUE );\r
1008                                 break;\r
1009 \r
1010                         case usbINTERFACE_STRING:\r
1011                                         prvSendControlData( ( unsigned char * ) &pxInterfaceStringDescriptor, pxRequest->usLength, sizeof( pxInterfaceStringDescriptor ), pdTRUE );\r
1012                                 break;\r
1013 \r
1014                         default:\r
1015                                 /* Don't know what this string is. */\r
1016                                         prvSendStall();\r
1017                                         break;\r
1018                         }\r
1019 \r
1020                         break;\r
1021 \r
1022             default:\r
1023                         /* We are not responding to anything else. */\r
1024                         prvSendStall();\r
1025                     break;\r
1026         }\r
1027 }\r
1028 /*-----------------------------------------------------------*/\r
1029 \r
1030 static void prvHandleStandardDeviceRequest( xUSB_REQUEST *pxRequest )\r
1031 {\r
1032 unsigned short usStatus = 0;\r
1033 \r
1034         switch( pxRequest->ucRequest )\r
1035         {\r
1036             case usbGET_STATUS_REQUEST:\r
1037                         /* Just send two byte dummy status. */\r
1038                         prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );\r
1039                     break;\r
1040 \r
1041             case usbGET_DESCRIPTOR_REQUEST:\r
1042                         /* Send device descriptor */\r
1043                     prvGetStandardDeviceDescriptor( pxRequest );\r
1044                     break;\r
1045 \r
1046             case usbGET_CONFIGURATION_REQUEST:\r
1047                         /* Send selected device configuration */\r
1048                         prvSendControlData( ( unsigned char * ) &ucUSBConfig, sizeof( ucUSBConfig ), sizeof( ucUSBConfig ), pdFALSE );\r
1049                     break;\r
1050 \r
1051                 case usbSET_FEATURE_REQUEST:\r
1052                     prvUSBTransmitNull();\r
1053                     break;\r
1054 \r
1055             case usbSET_ADDRESS_REQUEST:\r
1056                 \r
1057                         /* Acknowledge the SET_ADDRESS, but (according to the manual) we\r
1058                         cannot actually move to the addressed state until we get a TXCOMP\r
1059                         interrupt from this NULL packet.  Therefore we just remember the\r
1060                         address and set our state so we know we have received the address. */\r
1061                 prvUSBTransmitNull();                   \r
1062                         eDriverState = eJUST_GOT_ADDRESS;               \r
1063                         ulReceivedAddress = ( unsigned long ) pxRequest->usValue;\r
1064                     break;\r
1065 \r
1066             case usbSET_CONFIGURATION_REQUEST:\r
1067 \r
1068                         /* Acknowledge the SET_CONFIGURATION, but (according to the manual)\r
1069                         we cannot actually move to the configured state until we get a\r
1070                         TXCOMP interrupt from this NULL packet.  Therefore we just remember the\r
1071                         config and set our state so we know we have received the go ahead. */                   \r
1072                         ucUSBConfig = ( unsigned char ) ( pxRequest->usValue & 0xff );\r
1073                         eDriverState = eJUST_GOT_CONFIG;\r
1074                         prvUSBTransmitNull();\r
1075                     break;\r
1076 \r
1077             default:\r
1078 \r
1079                     /* We don't answer to anything else. */\r
1080                         prvSendStall();\r
1081                     break;\r
1082         }\r
1083 }\r
1084 /*-----------------------------------------------------------*/\r
1085 \r
1086 static void prvHandleClassInterfaceRequest( xUSB_REQUEST *pxRequest )\r
1087 {\r
1088         switch( pxRequest->ucRequest )\r
1089         {\r
1090             case usbSET_IDLE_REQUEST:\r
1091                 prvUSBTransmitNull();\r
1092                         break;\r
1093 \r
1094                 /* This minimal implementation ignores these. */\r
1095             case usbGET_REPORT_REQUEST:\r
1096             case usbGET_IDLE_REQUEST:\r
1097             case usbGET_PROTOCOL_REQUEST:\r
1098             case usbSET_REPORT_REQUEST:\r
1099             case usbSET_PROTOCOL_REQUEST:       \r
1100             default:\r
1101 \r
1102                         prvSendStall();\r
1103                         break;\r
1104         }\r
1105 }\r
1106 /*-----------------------------------------------------------*/\r
1107 \r
1108 static void prvGetStandardInterfaceDescriptor( xUSB_REQUEST *pxRequest )\r
1109 {\r
1110         switch( ( pxRequest->usValue & ( unsigned short ) 0xff00 ) >> 8 )\r
1111         {\r
1112             case usbHID_REPORT_DESCRIPTOR:\r
1113                         prvSendControlData( ( unsigned char * ) pxReportDescriptor, pxRequest->usLength, sizeof( pxReportDescriptor ), pdTRUE );\r
1114                     break;\r
1115 \r
1116             default:\r
1117 \r
1118                         /* Don't expect to send any others. */\r
1119                         prvSendStall();\r
1120                     break;\r
1121         }\r
1122 }\r
1123 /*-----------------------------------------------------------*/\r
1124 \r
1125 static void prvHandleStandardInterfaceRequest( xUSB_REQUEST *pxRequest )\r
1126 {\r
1127 unsigned short usStatus = 0;\r
1128 \r
1129         switch( pxRequest->ucRequest )\r
1130         {\r
1131             case usbGET_STATUS_REQUEST:\r
1132                         /* Send dummy 2 bytes. */\r
1133                         prvSendControlData( ( unsigned char * ) &usStatus, sizeof( usStatus ), sizeof( usStatus ), pdFALSE );\r
1134                         break;\r
1135 \r
1136             case usbGET_DESCRIPTOR_REQUEST:\r
1137                         prvGetStandardInterfaceDescriptor( pxRequest );\r
1138                         break;\r
1139 \r
1140                 /* This minimal implementation does not respond to these. */\r
1141             case usbGET_INTERFACE_REQUEST:\r
1142             case usbSET_FEATURE_REQUEST:\r
1143             case usbSET_INTERFACE_REQUEST:      \r
1144 \r
1145             default:\r
1146                         prvSendStall();\r
1147                         break;\r
1148         }\r
1149 }\r
1150 /*-----------------------------------------------------------*/\r
1151 \r
1152 static void prvHandleStandardEndPointRequest( xUSB_REQUEST *pxRequest )\r
1153 {\r
1154         switch( pxRequest->ucRequest )\r
1155         {\r
1156                 /* This minimal implementation does not expect to respond to these. */\r
1157             case usbGET_STATUS_REQUEST:\r
1158             case usbCLEAR_FEATURE_REQUEST:\r
1159             case usbSET_FEATURE_REQUEST:\r
1160 \r
1161             default:                    \r
1162                         prvSendStall();\r
1163                         break;\r
1164         }\r
1165 }\r
1166 /*-----------------------------------------------------------*/\r
1167 \r
1168 static void vInitUSBInterface( void )\r
1169 {\r
1170 volatile unsigned long ulTemp;\r
1171 \r
1172         /* Create the queue used to communicate between the USB ISR and task. */\r
1173         xUSBInterruptQueue = xQueueCreate( usbQUEUE_LENGTH + 1, sizeof( xISRStatus * ) );\r
1174 \r
1175         /* Initialise a few state variables. */\r
1176         pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;\r
1177         ucUSBConfig = ( unsigned char ) 0;\r
1178         eDriverState = eNOTHING;\r
1179 \r
1180         /* HARDWARE SETUP */\r
1181 \r
1182     /* Set the PLL USB Divider */\r
1183     AT91C_BASE_CKGR->CKGR_PLLR |= AT91C_CKGR_USBDIV_1;\r
1184 \r
1185     /* Enables the 48MHz USB clock UDPCK and System Peripheral USB Clock. */\r
1186     AT91C_BASE_PMC->PMC_SCER = AT91C_PMC_UDP;\r
1187     AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_UDP);\r
1188 \r
1189     /* Setup the PIO for the USB pull up resistor. */\r
1190     AT91F_PIO_CfgOutput(AT91C_BASE_PIOA,AT91C_PIO_PA16);\r
1191 \r
1192     /* Start without the pullup - this will get set at the end of this\r
1193         function. */\r
1194     AT91F_PIO_SetOutput( AT91C_BASE_PIOA, AT91C_PIO_PA16 );\r
1195 \r
1196         /* When using the USB debugger the peripheral registers do not always get\r
1197         set to the correct default values.  To make sure set the relevant registers\r
1198         manually here. */\r
1199         AT91C_BASE_UDP->UDP_IDR = ( unsigned long ) 0xffffffff;\r
1200         AT91C_BASE_UDP->UDP_ICR = ( unsigned long ) 0xffffffff;\r
1201         AT91C_BASE_UDP->UDP_CSR[ 0 ] = ( unsigned long ) 0x00;\r
1202         AT91C_BASE_UDP->UDP_CSR[ 1 ] = ( unsigned long ) 0x00;\r
1203         AT91C_BASE_UDP->UDP_GLBSTATE = 0;\r
1204         AT91C_BASE_UDP->UDP_FADDR = 0;\r
1205 \r
1206         /* Enable the transceiver. */\r
1207         AT91C_UDP_TRANSCEIVER_ENABLE = 0;\r
1208 \r
1209         /* Enable the USB interrupts - other interrupts get enabled as the\r
1210         enumeration process progresses. */\r
1211         AT91F_AIC_ConfigureIt( AT91C_BASE_AIC, AT91C_ID_UDP, usbINTERRUPT_PRIORITY, AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, ( void (*)( void ) ) vUSBISREntry );\r
1212         AT91F_AIC_EnableIt( AT91C_BASE_AIC, AT91C_ID_UDP );\r
1213 \r
1214         /* Wait a short while before making our presence known. */\r
1215         vTaskDelay( usbINIT_DELAY );\r
1216     AT91F_PIO_ClearOutput(AT91C_BASE_PIOA, AT91C_PIO_PA16 );\r
1217 }\r
1218 /*-----------------------------------------------------------*/\r
1219 \r
1220 static void prvSendControlData( unsigned char *pucData, unsigned short usRequestedLength, unsigned long ulLengthToSend, long lSendingDescriptor )\r
1221 {\r
1222         if( ( ( unsigned long ) usRequestedLength < ulLengthToSend ) )\r
1223         {\r
1224                 /* Cap the data length to that requested. */\r
1225                 ulLengthToSend = ( unsigned short ) usRequestedLength;\r
1226         }\r
1227         else if( ( ulLengthToSend < ( unsigned long ) usRequestedLength ) && lSendingDescriptor )\r
1228         {\r
1229                 /* We are sending a descriptor.  If the descriptor is an exact\r
1230                 multiple of the FIFO length then it will have to be terminated\r
1231                 with a NULL packet.  Set the state to indicate this if\r
1232                 necessary. */\r
1233                 if( ( ulLengthToSend % usbFIFO_LENGTH ) == 0 )\r
1234                 {\r
1235                         eDriverState = eSENDING_EVEN_DESCRIPTOR;\r
1236                 }\r
1237         }\r
1238 \r
1239         /* Here we assume that the previous message has been sent.  THERE IS NO\r
1240         BUFFER OVERFLOW PROTECTION HERE.\r
1241 \r
1242         Copy the data to send into the buffer as we cannot send it all at once\r
1243         (if it is greater than 8 bytes in length). */\r
1244         memcpy( pxCharsForTx.ucTxBuffer, pucData, ulLengthToSend );\r
1245 \r
1246         /* Reinitialise the buffer index so we start sending from the start of\r
1247         the data. */\r
1248         pxCharsForTx.ulTotalDataLength = ulLengthToSend;\r
1249         pxCharsForTx.ulNextCharIndex = ( unsigned long ) 0;\r
1250 \r
1251         /* Send the first 8 bytes now.  The rest will get sent in response to\r
1252         TXCOMP interrupts. */\r
1253         prvSendNextSegment();\r
1254 }\r
1255 /*-----------------------------------------------------------*/\r
1256 \r
1257 static void prvSendNextSegment( void )\r
1258 {\r
1259 volatile unsigned long ulNextLength, ulStatus, ulLengthLeftToSend;\r
1260 \r
1261         /* Is there any data to send? */\r
1262         if( pxCharsForTx.ulTotalDataLength > pxCharsForTx.ulNextCharIndex )\r
1263         {\r
1264                 ulLengthLeftToSend = pxCharsForTx.ulTotalDataLength - pxCharsForTx.ulNextCharIndex;\r
1265         \r
1266                 /* We can only send 8 bytes to the fifo at a time. */\r
1267                 if( ulLengthLeftToSend > usbFIFO_LENGTH )\r
1268                 {\r
1269                         ulNextLength = usbFIFO_LENGTH;\r
1270                 }\r
1271                 else\r
1272                 {\r
1273                         ulNextLength = ulLengthLeftToSend;\r
1274                 }\r
1275 \r
1276                 /* Wait until we can place data in the fifo.  THERE IS NO TIMEOUT\r
1277                 HERE! */\r
1278                 while( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_TXPKTRDY )\r
1279                 {\r
1280                         vTaskDelay( usbSHORTEST_DELAY );\r
1281                 }\r
1282 \r
1283                 /* Write the data to the FIFO. */\r
1284                 while( ulNextLength > ( unsigned long ) 0 )\r
1285                 {\r
1286                         AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ] = pxCharsForTx.ucTxBuffer[ pxCharsForTx.ulNextCharIndex ];\r
1287         \r
1288                         ulNextLength--;\r
1289                         pxCharsForTx.ulNextCharIndex++;\r
1290                 }\r
1291         \r
1292                 /* Start the transmission. */\r
1293                 portENTER_CRITICAL();\r
1294                 {\r
1295                         ulStatus = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
1296                         usbCSR_SET_BIT( &ulStatus, ( ( unsigned long ) 0x10 ) );\r
1297                         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulStatus;\r
1298                 }\r
1299                 portEXIT_CRITICAL();\r
1300         }\r
1301         else\r
1302         {\r
1303                 /* There is no data to send.  If we were sending a descriptor and the\r
1304                 descriptor was an exact multiple of the max packet size then we need\r
1305                 to send a null to terminate the transmission. */\r
1306                 if( eDriverState == eSENDING_EVEN_DESCRIPTOR )\r
1307                 {\r
1308                         prvUSBTransmitNull();\r
1309                         eDriverState = eNOTHING;\r
1310                 }\r
1311         }\r
1312 }\r
1313 \r
1314 \r
1315 \r