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