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