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