]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/USB/USBIsr.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / USB / USBIsr.c
1 /*\r
2     FreeRTOS V8.1.2 - 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 \r
67 /* \r
68   BASIC INTERRUPT DRIVEN DRIVER FOR USB. \r
69 \r
70   This file contains all the usb components that must be compiled\r
71   to ARM mode.  The components that can be compiled to either ARM or THUMB\r
72   mode are contained in USB-CDC.c.\r
73 \r
74 */\r
75 \r
76 /* Scheduler includes. */\r
77 #include "FreeRTOS.h"\r
78 #include "task.h"\r
79 #include "queue.h"\r
80 \r
81 /* Demo application includes. */\r
82 #include "Board.h"\r
83 #include "usb.h"\r
84 #include "USB-CDC.h"\r
85 \r
86 #define usbINT_CLEAR_MASK       (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )\r
87 /*-----------------------------------------------------------*/\r
88 \r
89 /* Messages and queue used to communicate between the ISR and the USB task. */\r
90 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];\r
91 extern QueueHandle_t xUSBInterruptQueue;\r
92 /*-----------------------------------------------------------*/\r
93 \r
94 /* The ISR can cause a context switch so is declared naked. */\r
95 void vUSB_ISR_Wrapper( void ) __attribute__ ((naked));\r
96 \r
97 /* The function that actually performs the ISR work.  This must be separate\r
98 from the wrapper function to ensure the correct stack frame gets set up. */\r
99 void vUSB_ISR_Handler( void );\r
100 /*-----------------------------------------------------------*/\r
101 \r
102 void vUSB_ISR_Handler( void )\r
103 {\r
104 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
105 static volatile unsigned long ulNextMessage = 0;\r
106 xISRStatus *pxMessage;\r
107 unsigned long ulRxBytes;\r
108 unsigned char ucFifoIndex;\r
109 \r
110     /* Use the next message from the array. */\r
111         pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );\r
112         ulNextMessage++;\r
113 \r
114     /* Save UDP ISR state for task-level processing. */\r
115         pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;\r
116         pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
117 \r
118     /* Clear interrupts from ICR. */\r
119         AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;\r
120         \r
121     \r
122         /* Process incoming FIFO data.  Must set DIR (if needed) and clear RXSETUP \r
123         before exit. */\r
124 \r
125     /* Read CSR and get incoming byte count. */\r
126         ulRxBytes = ( pxMessage->ulCSR0 >> 16 ) & usbRX_COUNT_MASK;\r
127         \r
128         /* Receive control transfers on endpoint 0. */\r
129         if( pxMessage->ulCSR0 & ( AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 ) )\r
130         {\r
131                 /* Save FIFO data buffer for either a SETUP or DATA stage */\r
132                 for( ucFifoIndex = 0; ucFifoIndex < ulRxBytes; ucFifoIndex++ )\r
133                 {\r
134                         pxMessage->ucFifoData[ ucFifoIndex ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];\r
135                 }\r
136 \r
137                 /* Set direction for data stage.  Must be done before RXSETUP is \r
138                 cleared. */\r
139                 if( ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP ) )\r
140                 {\r
141                         if( ulRxBytes && ( pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ] & 0x80 ) )\r
142                         {\r
143                                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] |= AT91C_UDP_DIR;\r
144 \r
145                                 /* Might not be wise in an ISR! */\r
146                                 while( !(AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_DIR) );\r
147                         }\r
148 \r
149                         /* Clear RXSETUP */\r
150                         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RXSETUP;\r
151 \r
152                         /* Might not be wise in an ISR! */\r
153                         while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP );\r
154                 }\r
155                 else\r
156                 {\r
157                    /* Clear RX_DATA_BK0 */\r
158                    AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RX_DATA_BK0;\r
159 \r
160                    /* Might not be wise in an ISR! */\r
161                    while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RX_DATA_BK0 );\r
162                 }\r
163         }\r
164         \r
165         /* If we received data on endpoint 1, disable its interrupts until it is \r
166         processed in the main loop */\r
167         if( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & ( AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 ) )\r
168         {\r
169                 AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_EPINT1;\r
170         }\r
171         \r
172         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT );\r
173      \r
174         /* Clear interrupts for the other endpoints, retain data flags for endpoint \r
175         1. */\r
176         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP );\r
177         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_2 ] &= ~usbINT_CLEAR_MASK;\r
178         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_3 ] &= ~usbINT_CLEAR_MASK;\r
179 \r
180         /* Post ISR data to queue for task-level processing */\r
181         xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );\r
182 \r
183         /* Clear AIC to complete ISR processing */\r
184         AT91C_BASE_AIC->AIC_EOICR = 0;\r
185 \r
186         /* Do a task switch if needed */\r
187         if( xHigherPriorityTaskWoken )\r
188         {\r
189                 /* This call will ensure that the unblocked task will be executed\r
190                 immediately upon completion of the ISR if it has a priority higher\r
191                 than the interrupted task. */\r
192                 portYIELD_FROM_ISR();\r
193         }\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 void vUSB_ISR_Wrapper( void )\r
198 {\r
199         /* Save the context of the interrupted task. */\r
200         portSAVE_CONTEXT();\r
201 \r
202         /* Call the handler to do the work.  This must be a separate\r
203         function to ensure the stack frame is set up correctly. */\r
204         vUSB_ISR_Handler();\r
205 \r
206         /* Restore the context of whichever task will execute next. */\r
207         portRESTORE_CONTEXT();\r
208 }\r
209 \r
210 \r