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