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