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