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