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