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