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