]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/ARM7_AT91SAM7X256_Eclipse/RTOSDemo/USB/USB_ISR.c
b2e7bd9e2cd3e52075bcb0e8a062b67221ca55c7
[freertos] / FreeRTOS / Demo / ARM7_AT91SAM7X256_Eclipse / RTOSDemo / USB / USB_ISR.c
1 /*\r
2     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
3 \r
4     FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
5     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\r
13      *    Purchasing FreeRTOS documentation will not only help you, by       *\r
14      *    ensuring you get running as quickly as possible and with an        *\r
15      *    in-depth knowledge of how to use FreeRTOS, it will also help       *\r
16      *    the FreeRTOS project to continue with its mission of providing     *\r
17      *    professional grade, cross platform, de facto standard solutions    *\r
18      *    for microcontrollers - completely free of charge!                  *\r
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \r
29     FreeRTOS is free software; you can redistribute it and/or modify it under\r
30     the terms of the GNU General Public License (version 2) as published by the\r
31     Free Software Foundation AND MODIFIED BY the FreeRTOS exception.\r
32     >>>NOTE<<< The modification to the GPL is included to allow you to\r
33     distribute a combined work that includes FreeRTOS without being obliged to\r
34     provide the source code for proprietary components outside of the FreeRTOS\r
35     kernel.  FreeRTOS is distributed in the hope that it will be useful, but\r
36     WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
37     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
38     more details. You should have received a copy of the GNU General Public\r
39     License and the FreeRTOS license exception along with FreeRTOS; if not it\r
40     can be viewed here: http://www.freertos.org/a00114.html and also obtained\r
41     by writing to Richard Barry, contact details for whom are available on the\r
42     FreeRTOS WEB site.\r
43 \r
44     1 tab == 4 spaces!\r
45     \r
46     ***************************************************************************\r
47      *                                                                       *\r
48      *    Having a problem?  Start by reading the FAQ "My application does   *\r
49      *    not run, what could be wrong?"                                     *\r
50      *                                                                       *\r
51      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
52      *                                                                       *\r
53     ***************************************************************************\r
54 \r
55     \r
56     http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
57     and contact details.  \r
58     \r
59     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
60     including FreeRTOS+Trace - an indispensable productivity tool.\r
61 \r
62     Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
63     the code with commercial support, indemnification, and middleware, under \r
64     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
65     provide a safety engineered and independently SIL3 certified version under \r
66     the SafeRTOS brand: http://www.SafeRTOS.com.\r
67 */\r
68 \r
69 /* Scheduler includes. */\r
70 #include "FreeRTOS.h"\r
71 #include "task.h"\r
72 #include "queue.h"\r
73 \r
74 /* Demo app includes. */\r
75 #include "USBSample.h"\r
76 \r
77 #define usbINT_CLEAR_MASK       (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )\r
78 \r
79 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit )                                                                                  \\r
80 {                                                                                                                                                                               \\r
81         /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */                                                                                         \\r
82         /* STALLSENT and RX_DATA_BK1 to 1 so the */                                                                                     \\r
83         /* write has no effect. */                                                                                                                      \\r
84         ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f;            \\r
85                                                                                                                                                                                 \\r
86         /* Clear the FORCE_STALL and TXPKTRDY bits */                                                                           \\r
87         /* so the write has no effect. */                                                                                                       \\r
88         ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf;      \\r
89                                                                                                                                                                                 \\r
90         /* Clear whichever bit we want clear. */                                                                                        \\r
91         ( * ( ( unsigned long * ) pulValueNow ) ) &= ( ~ulBit );                                                \\r
92 }\r
93 \r
94 \r
95 /*-----------------------------------------------------------*/\r
96 \r
97 /*\r
98  * ISR entry point.\r
99  */\r
100 \r
101 void vUSB_ISR_Wrapper( void ) __attribute__((naked));\r
102 \r
103 /*\r
104  * Actual ISR handler.  This must be separate from the entry point as the stack\r
105  * is used.\r
106  */\r
107 void vUSB_ISR_Handler( void ) __attribute__((noinline));\r
108 \r
109 /*-----------------------------------------------------------*/\r
110 \r
111 /* Array in which the USB interrupt status is passed between the ISR and task. */\r
112 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];\r
113 \r
114 /* Queue used to pass messages between the ISR and the task. */\r
115 extern xQueueHandle xUSBInterruptQueue; \r
116 \r
117 /*-----------------------------------------------------------*/\r
118 \r
119 void vUSB_ISR_Handler( void )\r
120 {\r
121 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE; \r
122 static volatile unsigned long ulNextMessage = 0;\r
123 xISRStatus *pxMessage;\r
124 unsigned long ulTemp, ulRxBytes;\r
125 \r
126         /* To reduce the amount of time spent in this interrupt it would be \r
127         possible to defer the majority of this processing to an 'interrupt task',\r
128         that is a task that runs at a higher priority than any of the application\r
129         tasks. */\r
130 \r
131         /* Take the next message from the queue.  Note that usbQUEUE_LENGTH *must*\r
132         be all 1's, as in 0x01, 0x03, 0x07, etc. */\r
133         pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );\r
134         ulNextMessage++;\r
135 \r
136         /* Take a snapshot of the current USB state for processing at the task\r
137         level. */\r
138         pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;\r
139         pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
140 \r
141         /* Clear the interrupts from the ICR register.  The bus end interrupt is\r
142         cleared separately as it does not appear in the mask register. */\r
143         AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;\r
144         \r
145         /* If there are bytes in the FIFO then we have to retrieve them here.  \r
146         Ideally this would be done at the task level.  However we need to clear the\r
147         RXSETUP interrupt before leaving the ISR, and this may cause the data in\r
148         the FIFO to be overwritten.  Also the DIR bit has to be changed before the\r
149         RXSETUP bit is cleared (as per the SAM7 manual). */\r
150         ulTemp = pxMessage->ulCSR0;\r
151         \r
152         /* Are there any bytes in the FIFO? */\r
153         ulRxBytes = ulTemp >> 16;\r
154         ulRxBytes &= usbRX_COUNT_MASK;\r
155         \r
156         /* With this minimal implementation we are only interested in receiving \r
157         setup bytes on the control end point. */\r
158         if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )\r
159         {\r
160                 /* Take off 1 for a zero based index. */\r
161                 while( ulRxBytes > 0 )\r
162                 {\r
163                         ulRxBytes--;\r
164                         pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];                 \r
165                 }\r
166                 \r
167                 /* The direction must be changed first. */\r
168                 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );\r
169                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
170         }\r
171         \r
172         /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA\r
173         registers to clear the interrupts in the CSR register. */\r
174         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );\r
175         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;\r
176 \r
177         /* Also clear the interrupts in the CSR1 register. */\r
178         ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];\r
179         usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK ); \r
180         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;\r
181 \r
182         /* The message now contains the entire state and optional data from\r
183         the USB interrupt.  This can now be posted on the Rx queue ready for\r
184         processing at the task level. */\r
185         xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );\r
186 \r
187         /* We may want to switch to the USB task, if this message has made\r
188         it the highest priority task that is ready to execute. */\r
189         if( xHigherPriorityTaskWoken )\r
190         {\r
191                 portYIELD_FROM_ISR();\r
192         }\r
193 \r
194         /* Clear the AIC ready for the next interrupt. */               \r
195         AT91C_BASE_AIC->AIC_EOICR = 0;\r
196 }\r
197 /*-----------------------------------------------------------*/\r
198 \r
199 void vUSB_ISR_Wrapper( void )\r
200 {\r
201         /* Save the context of the interrupted task. */\r
202         portSAVE_CONTEXT();\r
203 \r
204         /* Call the handler itself.  This must be a separate function as it uses\r
205         the stack. */\r
206         __asm volatile ("bl vUSB_ISR_Handler");\r
207 \r
208         /* Restore the context of the task that is going to \r
209         execute next. This might not be the same as the originally \r
210         interrupted task.*/\r
211         portRESTORE_CONTEXT();\r
212 }\r