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