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