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