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