]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/USB/USB_ISR.c
Update to V4.7.2.
[freertos] / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / USB / USB_ISR.c
1 /*\r
2         FreeRTOS.org V4.7.2 - 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 /* Scheduler includes. */\r
44 #include "FreeRTOS.h"\r
45 #include "task.h"\r
46 #include "queue.h"\r
47 \r
48 /* Demo app includes. */\r
49 #include "USBSample.h"\r
50 \r
51 #define usbINT_CLEAR_MASK       (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )\r
52 \r
53 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit )                                                                                  \\r
54 {                                                                                                                                                                               \\r
55         /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */                                                                                         \\r
56         /* STALLSENT and RX_DATA_BK1 to 1 so the */                                                                                     \\r
57         /* write has no effect. */                                                                                                                      \\r
58         ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f;            \\r
59                                                                                                                                                                                 \\r
60         /* Clear the FORCE_STALL and TXPKTRDY bits */                                                                           \\r
61         /* so the write has no effect. */                                                                                                       \\r
62         ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf;      \\r
63                                                                                                                                                                                 \\r
64         /* Clear whichever bit we want clear. */                                                                                        \\r
65         ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( ~ulBit );                                            \\r
66 }\r
67 \r
68 \r
69 /*-----------------------------------------------------------*/\r
70 \r
71 /*\r
72  * ISR entry point.\r
73  */\r
74 \r
75 void vUSB_ISR_Wrapper( void ) __attribute__((naked));\r
76 \r
77 /*\r
78  * Actual ISR handler.  This must be separate from the entry point as the stack\r
79  * is used.\r
80  */\r
81 void vUSB_ISR_Handler( void );\r
82 \r
83 /*-----------------------------------------------------------*/\r
84 \r
85 /* Array in which the USB interrupt status is passed between the ISR and task. */\r
86 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];\r
87 \r
88 /* Queue used to pass messages between the ISR and the task. */\r
89 extern xQueueHandle xUSBInterruptQueue; \r
90 \r
91 /*-----------------------------------------------------------*/\r
92 \r
93 void vUSB_ISR_Handler( void )\r
94 {\r
95 portBASE_TYPE xTaskWokenByPost = pdFALSE; \r
96 static volatile unsigned portLONG ulNextMessage = 0;\r
97 xISRStatus *pxMessage;\r
98 unsigned portLONG ulTemp, ulRxBytes;\r
99 \r
100         /* To reduce the amount of time spent in this interrupt it would be \r
101         possible to defer the majority of this processing to an 'interrupt task',\r
102         that is a task that runs at a higher priority than any of the application\r
103         tasks. */\r
104 \r
105         /* Take the next message from the queue.  Note that usbQUEUE_LENGTH *must*\r
106         be all 1's, as in 0x01, 0x03, 0x07, etc. */\r
107         pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );\r
108         ulNextMessage++;\r
109 \r
110         /* Take a snapshot of the current USB state for processing at the task\r
111         level. */\r
112         pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;\r
113         pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
114 \r
115         /* Clear the interrupts from the ICR register.  The bus end interrupt is\r
116         cleared separately as it does not appear in the mask register. */\r
117         AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;\r
118         \r
119         /* If there are bytes in the FIFO then we have to retrieve them here.  \r
120         Ideally this would be done at the task level.  However we need to clear the\r
121         RXSETUP interrupt before leaving the ISR, and this may cause the data in\r
122         the FIFO to be overwritten.  Also the DIR bit has to be changed before the\r
123         RXSETUP bit is cleared (as per the SAM7 manual). */\r
124         ulTemp = pxMessage->ulCSR0;\r
125         \r
126         /* Are there any bytes in the FIFO? */\r
127         ulRxBytes = ulTemp >> 16;\r
128         ulRxBytes &= usbRX_COUNT_MASK;\r
129         \r
130         /* With this minimal implementation we are only interested in receiving \r
131         setup bytes on the control end point. */\r
132         if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )\r
133         {\r
134                 /* Take off 1 for a zero based index. */\r
135                 while( ulRxBytes > 0 )\r
136                 {\r
137                         ulRxBytes--;\r
138                         pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];                 \r
139                 }\r
140                 \r
141                 /* The direction must be changed first. */\r
142                 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );\r
143                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
144         }\r
145         \r
146         /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA\r
147         registers to clear the interrupts in the CSR register. */\r
148         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );\r
149         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
150 \r
151         /* Also clear the interrupts in the CSR1 register. */\r
152         ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];\r
153         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK ); \r
154         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;\r
155 \r
156         /* The message now contains the entire state and optional data from\r
157         the USB interrupt.  This can now be posted on the Rx queue ready for\r
158         processing at the task level. */\r
159         xTaskWokenByPost = xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, xTaskWokenByPost );\r
160 \r
161         /* We may want to switch to the USB task, if this message has made\r
162         it the highest priority task that is ready to execute. */\r
163         if( xTaskWokenByPost )\r
164         {\r
165                 portYIELD_FROM_ISR();\r
166         }\r
167 \r
168         /* Clear the AIC ready for the next interrupt. */               \r
169         AT91C_BASE_AIC->AIC_EOICR = 0;\r
170 }\r
171 /*-----------------------------------------------------------*/\r
172 \r
173 void vUSB_ISR_Wrapper( void )\r
174 {\r
175         /* Save the context of the interrupted task. */\r
176         portSAVE_CONTEXT();\r
177 \r
178         /* Call the handler itself.  This must be a separate function as it uses\r
179         the stack. */\r
180         vUSB_ISR_Handler();\r
181 \r
182         /* Restore the context of the task that is going to \r
183         execute next. This might not be the same as the originally \r
184         interrupted task.*/\r
185         portRESTORE_CONTEXT();\r
186 }\r