]> git.sur5r.net Git - freertos/blob - Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/USB/USB_ISR.c
Prepare for V5.3.0 release.
[freertos] / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / USB / USB_ISR.c
1 /*\r
2         FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it\r
7         under the terms of the GNU General Public License (version 2) as published\r
8         by the Free Software Foundation and modified by the FreeRTOS exception.\r
9         **NOTE** The exception to the GPL is included to allow you to distribute a\r
10         combined work that includes FreeRTOS.org without being obliged to provide\r
11         the source code for any proprietary components.  Alternative commercial\r
12         license and support terms are also available upon request.  See the \r
13         licensing section of http://www.FreeRTOS.org for full details.\r
14 \r
15         FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
16         ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
17         FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
18         more details.\r
19 \r
20         You should have received a copy of the GNU General Public License along\r
21         with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
22         Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
23 \r
24 \r
25         ***************************************************************************\r
26         *                                                                         *\r
27         * Get the FreeRTOS eBook!  See http://www.FreeRTOS.org/Documentation      *\r
28         *                                                                         *\r
29         * This is a concise, step by step, 'hands on' guide that describes both   *\r
30         * general multitasking concepts and FreeRTOS specifics. It presents and   *\r
31         * explains numerous examples that are written using the FreeRTOS API.     *\r
32         * Full source code for all the examples is provided in an accompanying    *\r
33         * .zip file.                                                              *\r
34         *                                                                         *\r
35         ***************************************************************************\r
36 \r
37         1 tab == 4 spaces!\r
38 \r
39         Please ensure to read the configuration and relevant port sections of the\r
40         online documentation.\r
41 \r
42         http://www.FreeRTOS.org - Documentation, latest information, license and\r
43         contact details.\r
44 \r
45         http://www.SafeRTOS.com - A version that is certified for use in safety\r
46         critical systems.\r
47 \r
48         http://www.OpenRTOS.com - Commercial support, development, porting,\r
49         licensing and training services.\r
50 */\r
51 \r
52 /* Scheduler includes. */\r
53 #include "FreeRTOS.h"\r
54 #include "task.h"\r
55 #include "queue.h"\r
56 \r
57 /* Demo app includes. */\r
58 #include "USBSample.h"\r
59 \r
60 #define usbINT_CLEAR_MASK       (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )\r
61 \r
62 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit )                                                                                  \\r
63 {                                                                                                                                                                               \\r
64         /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */                                                                                         \\r
65         /* STALLSENT and RX_DATA_BK1 to 1 so the */                                                                                     \\r
66         /* write has no effect. */                                                                                                                      \\r
67         ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f;            \\r
68                                                                                                                                                                                 \\r
69         /* Clear the FORCE_STALL and TXPKTRDY bits */                                                                           \\r
70         /* so the write has no effect. */                                                                                                       \\r
71         ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf;      \\r
72                                                                                                                                                                                 \\r
73         /* Clear whichever bit we want clear. */                                                                                        \\r
74         ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( ~ulBit );                                            \\r
75 }\r
76 \r
77 \r
78 /*-----------------------------------------------------------*/\r
79 \r
80 /*\r
81  * ISR entry point.\r
82  */\r
83 \r
84 void vUSB_ISR_Wrapper( void ) __attribute__((naked));\r
85 \r
86 /*\r
87  * Actual ISR handler.  This must be separate from the entry point as the stack\r
88  * is used.\r
89  */\r
90 void vUSB_ISR_Handler( void );\r
91 \r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* Array in which the USB interrupt status is passed between the ISR and task. */\r
95 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];\r
96 \r
97 /* Queue used to pass messages between the ISR and the task. */\r
98 extern xQueueHandle xUSBInterruptQueue; \r
99 \r
100 /*-----------------------------------------------------------*/\r
101 \r
102 void vUSB_ISR_Handler( void )\r
103 {\r
104 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; \r
105 static volatile unsigned portLONG ulNextMessage = 0;\r
106 xISRStatus *pxMessage;\r
107 unsigned portLONG ulTemp, ulRxBytes;\r
108 \r
109         /* To reduce the amount of time spent in this interrupt it would be \r
110         possible to defer the majority of this processing to an 'interrupt task',\r
111         that is a task that runs at a higher priority than any of the application\r
112         tasks. */\r
113 \r
114         /* Take the next message from the queue.  Note that usbQUEUE_LENGTH *must*\r
115         be all 1's, as in 0x01, 0x03, 0x07, etc. */\r
116         pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );\r
117         ulNextMessage++;\r
118 \r
119         /* Take a snapshot of the current USB state for processing at the task\r
120         level. */\r
121         pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;\r
122         pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
123 \r
124         /* Clear the interrupts from the ICR register.  The bus end interrupt is\r
125         cleared separately as it does not appear in the mask register. */\r
126         AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;\r
127         \r
128         /* If there are bytes in the FIFO then we have to retrieve them here.  \r
129         Ideally this would be done at the task level.  However we need to clear the\r
130         RXSETUP interrupt before leaving the ISR, and this may cause the data in\r
131         the FIFO to be overwritten.  Also the DIR bit has to be changed before the\r
132         RXSETUP bit is cleared (as per the SAM7 manual). */\r
133         ulTemp = pxMessage->ulCSR0;\r
134         \r
135         /* Are there any bytes in the FIFO? */\r
136         ulRxBytes = ulTemp >> 16;\r
137         ulRxBytes &= usbRX_COUNT_MASK;\r
138         \r
139         /* With this minimal implementation we are only interested in receiving \r
140         setup bytes on the control end point. */\r
141         if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )\r
142         {\r
143                 /* Take off 1 for a zero based index. */\r
144                 while( ulRxBytes > 0 )\r
145                 {\r
146                         ulRxBytes--;\r
147                         pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];                 \r
148                 }\r
149                 \r
150                 /* The direction must be changed first. */\r
151                 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );\r
152                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
153         }\r
154         \r
155         /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA\r
156         registers to clear the interrupts in the CSR register. */\r
157         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );\r
158         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
159 \r
160         /* Also clear the interrupts in the CSR1 register. */\r
161         ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];\r
162         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK ); \r
163         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;\r
164 \r
165         /* The message now contains the entire state and optional data from\r
166         the USB interrupt.  This can now be posted on the Rx queue ready for\r
167         processing at the task level. */\r
168         xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );\r
169 \r
170         /* We may want to switch to the USB task, if this message has made\r
171         it the highest priority task that is ready to execute. */\r
172         if( xHigherPriorityTaskWoken )\r
173         {\r
174                 portYIELD_FROM_ISR();\r
175         }\r
176 \r
177         /* Clear the AIC ready for the next interrupt. */               \r
178         AT91C_BASE_AIC->AIC_EOICR = 0;\r
179 }\r
180 /*-----------------------------------------------------------*/\r
181 \r
182 void vUSB_ISR_Wrapper( void )\r
183 {\r
184         /* Save the context of the interrupted task. */\r
185         portSAVE_CONTEXT();\r
186 \r
187         /* Call the handler itself.  This must be a separate function as it uses\r
188         the stack. */\r
189         vUSB_ISR_Handler();\r
190 \r
191         /* Restore the context of the task that is going to \r
192         execute next. This might not be the same as the originally \r
193         interrupted task.*/\r
194         portRESTORE_CONTEXT();\r
195 }\r