]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/lwIP_Demo_Rowley_ARM7/USB/USBIsr.c
Update version numbers in preparation for V8.2.0 release candidate 1.
[freertos] / FreeRTOS / Demo / lwIP_Demo_Rowley_ARM7 / USB / USBIsr.c
1 /*\r
2     FreeRTOS V8.2.0rc1 - 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     This file is part of the FreeRTOS distribution.\r
8 \r
9     FreeRTOS is free software; you can redistribute it and/or modify it under\r
10     the terms of the GNU General Public License (version 2) as published by the\r
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
12 \r
13     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
14     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
15     >>!   obliged to provide the source code for proprietary components     !<<\r
16     >>!   outside of the FreeRTOS kernel.                                   !<<\r
17 \r
18     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
19     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
20     FOR A PARTICULAR PURPOSE.  Full license text is available on the following\r
21     link: http://www.freertos.org/a00114.html\r
22 \r
23     1 tab == 4 spaces!\r
24 \r
25     ***************************************************************************\r
26      *                                                                       *\r
27      *    Having a problem?  Start by reading the FAQ "My application does   *\r
28      *    not run, what could be wrong?".  Have you defined configASSERT()?  *\r
29      *                                                                       *\r
30      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
31      *                                                                       *\r
32     ***************************************************************************\r
33 \r
34     ***************************************************************************\r
35      *                                                                       *\r
36      *    FreeRTOS provides completely free yet professionally developed,    *\r
37      *    robust, strictly quality controlled, supported, and cross          *\r
38      *    platform software that is more than just the market leader, it     *\r
39      *    is the industry's de facto standard.                               *\r
40      *                                                                       *\r
41      *    Help yourself get started quickly while simultaneously helping     *\r
42      *    to support the FreeRTOS project by purchasing a FreeRTOS           *\r
43      *    tutorial book, reference manual, or both:                          *\r
44      *    http://www.FreeRTOS.org/Documentation                              *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     ***************************************************************************\r
49      *                                                                       *\r
50      *   Investing in training allows your team to be as productive as       *\r
51      *   possible as early as possible, lowering your overall development    *\r
52      *   cost, and enabling you to bring a more robust product to market     *\r
53      *   earlier than would otherwise be possible.  Richard Barry is both    *\r
54      *   the architect and key author of FreeRTOS, and so also the world's   *\r
55      *   leading authority on what is the world's most popular real time     *\r
56      *   kernel for deeply embedded MCU designs.  Obtaining your training    *\r
57      *   from Richard ensures your team will gain directly from his in-depth *\r
58      *   product knowledge and years of usage experience.  Contact Real Time *\r
59      *   Engineers Ltd to enquire about the FreeRTOS Masterclass, presented  *\r
60      *   by Richard Barry:  http://www.FreeRTOS.org/contact\r
61      *                                                                       *\r
62     ***************************************************************************\r
63 \r
64     ***************************************************************************\r
65      *                                                                       *\r
66      *    You are receiving this top quality software for free.  Please play *\r
67      *    fair and reciprocate by reporting any suspected issues and         *\r
68      *    participating in the community forum:                              *\r
69      *    http://www.FreeRTOS.org/support                                    *\r
70      *                                                                       *\r
71      *    Thank you!                                                         *\r
72      *                                                                       *\r
73     ***************************************************************************\r
74 \r
75     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
76     license and Real Time Engineers Ltd. contact details.\r
77 \r
78     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
79     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
80     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
81 \r
82     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.\r
83     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.\r
84 \r
85     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
86     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
87     licenses offer ticketed support, indemnification and commercial middleware.\r
88 \r
89     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
90     engineered and independently SIL3 certified version for use in safety and\r
91     mission critical applications that require provable dependability.\r
92 \r
93     1 tab == 4 spaces!\r
94 */\r
95 \r
96 \r
97 /* \r
98   BASIC INTERRUPT DRIVEN DRIVER FOR USB. \r
99 \r
100   This file contains all the usb components that must be compiled\r
101   to ARM mode.  The components that can be compiled to either ARM or THUMB\r
102   mode are contained in USB-CDC.c.\r
103 \r
104 */\r
105 \r
106 /* Scheduler includes. */\r
107 #include "FreeRTOS.h"\r
108 #include "task.h"\r
109 #include "queue.h"\r
110 \r
111 /* Demo application includes. */\r
112 #include "Board.h"\r
113 #include "usb.h"\r
114 #include "USB-CDC.h"\r
115 \r
116 #define usbINT_CLEAR_MASK       (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )\r
117 /*-----------------------------------------------------------*/\r
118 \r
119 /* Messages and queue used to communicate between the ISR and the USB task. */\r
120 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];\r
121 extern QueueHandle_t xUSBInterruptQueue;\r
122 /*-----------------------------------------------------------*/\r
123 \r
124 /* The ISR can cause a context switch so is declared naked. */\r
125 void vUSB_ISR_Wrapper( void ) __attribute__ ((naked));\r
126 \r
127 /* The function that actually performs the ISR work.  This must be separate\r
128 from the wrapper function to ensure the correct stack frame gets set up. */\r
129 void vUSB_ISR_Handler( void );\r
130 /*-----------------------------------------------------------*/\r
131 \r
132 void vUSB_ISR_Handler( void )\r
133 {\r
134 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;\r
135 static volatile unsigned long ulNextMessage = 0;\r
136 xISRStatus *pxMessage;\r
137 unsigned long ulRxBytes;\r
138 unsigned char ucFifoIndex;\r
139 \r
140     /* Use the next message from the array. */\r
141         pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );\r
142         ulNextMessage++;\r
143 \r
144     /* Save UDP ISR state for task-level processing. */\r
145         pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;\r
146         pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];\r
147 \r
148     /* Clear interrupts from ICR. */\r
149         AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;\r
150         \r
151     \r
152         /* Process incoming FIFO data.  Must set DIR (if needed) and clear RXSETUP \r
153         before exit. */\r
154 \r
155     /* Read CSR and get incoming byte count. */\r
156         ulRxBytes = ( pxMessage->ulCSR0 >> 16 ) & usbRX_COUNT_MASK;\r
157         \r
158         /* Receive control transfers on endpoint 0. */\r
159         if( pxMessage->ulCSR0 & ( AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 ) )\r
160         {\r
161                 /* Save FIFO data buffer for either a SETUP or DATA stage */\r
162                 for( ucFifoIndex = 0; ucFifoIndex < ulRxBytes; ucFifoIndex++ )\r
163                 {\r
164                         pxMessage->ucFifoData[ ucFifoIndex ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];\r
165                 }\r
166 \r
167                 /* Set direction for data stage.  Must be done before RXSETUP is \r
168                 cleared. */\r
169                 if( ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP ) )\r
170                 {\r
171                         if( ulRxBytes && ( pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ] & 0x80 ) )\r
172                         {\r
173                                 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] |= AT91C_UDP_DIR;\r
174 \r
175                                 /* Might not be wise in an ISR! */\r
176                                 while( !(AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_DIR) );\r
177                         }\r
178 \r
179                         /* Clear RXSETUP */\r
180                         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RXSETUP;\r
181 \r
182                         /* Might not be wise in an ISR! */\r
183                         while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP );\r
184                 }\r
185                 else\r
186                 {\r
187                    /* Clear RX_DATA_BK0 */\r
188                    AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RX_DATA_BK0;\r
189 \r
190                    /* Might not be wise in an ISR! */\r
191                    while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RX_DATA_BK0 );\r
192                 }\r
193         }\r
194         \r
195         /* If we received data on endpoint 1, disable its interrupts until it is \r
196         processed in the main loop */\r
197         if( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & ( AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 ) )\r
198         {\r
199                 AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_EPINT1;\r
200         }\r
201         \r
202         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT );\r
203      \r
204         /* Clear interrupts for the other endpoints, retain data flags for endpoint \r
205         1. */\r
206         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP );\r
207         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_2 ] &= ~usbINT_CLEAR_MASK;\r
208         AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_3 ] &= ~usbINT_CLEAR_MASK;\r
209 \r
210         /* Post ISR data to queue for task-level processing */\r
211         xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );\r
212 \r
213         /* Clear AIC to complete ISR processing */\r
214         AT91C_BASE_AIC->AIC_EOICR = 0;\r
215 \r
216         /* Do a task switch if needed */\r
217         if( xHigherPriorityTaskWoken )\r
218         {\r
219                 /* This call will ensure that the unblocked task will be executed\r
220                 immediately upon completion of the ISR if it has a priority higher\r
221                 than the interrupted task. */\r
222                 portYIELD_FROM_ISR();\r
223         }\r
224 }\r
225 /*-----------------------------------------------------------*/\r
226 \r
227 void vUSB_ISR_Wrapper( void )\r
228 {\r
229         /* Save the context of the interrupted task. */\r
230         portSAVE_CONTEXT();\r
231 \r
232         /* Call the handler to do the work.  This must be a separate\r
233         function to ensure the stack frame is set up correctly. */\r
234         vUSB_ISR_Handler();\r
235 \r
236         /* Restore the context of whichever task will execute next. */\r
237         portRESTORE_CONTEXT();\r
238 }\r
239 \r
240 \r