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