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