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