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