2 FreeRTOS V7.5.3 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
68 BASIC INTERRUPT DRIVEN DRIVER FOR USB.
\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
76 /* Scheduler includes. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Demo application includes. */
\r
84 #include "USB-CDC.h"
\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
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 xQueueHandle xUSBInterruptQueue;
\r
92 /*-----------------------------------------------------------*/
\r
94 /* The ISR can cause a context switch so is declared naked. */
\r
95 void vUSB_ISR_Wrapper( void ) __attribute__ ((naked));
\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
102 void vUSB_ISR_Handler( void )
\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
110 /* Use the next message from the array. */
\r
111 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\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
118 /* Clear interrupts from ICR. */
\r
119 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
122 /* Process incoming FIFO data. Must set DIR (if needed) and clear RXSETUP
\r
125 /* Read CSR and get incoming byte count. */
\r
126 ulRxBytes = ( pxMessage->ulCSR0 >> 16 ) & usbRX_COUNT_MASK;
\r
128 /* Receive control transfers on endpoint 0. */
\r
129 if( pxMessage->ulCSR0 & ( AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 ) )
\r
131 /* Save FIFO data buffer for either a SETUP or DATA stage */
\r
132 for( ucFifoIndex = 0; ucFifoIndex < ulRxBytes; ucFifoIndex++ )
\r
134 pxMessage->ucFifoData[ ucFifoIndex ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
137 /* Set direction for data stage. Must be done before RXSETUP is
\r
139 if( ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP ) )
\r
141 if( ulRxBytes && ( pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ] & 0x80 ) )
\r
143 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] |= AT91C_UDP_DIR;
\r
145 /* Might not be wise in an ISR! */
\r
146 while( !(AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_DIR) );
\r
149 /* Clear RXSETUP */
\r
150 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RXSETUP;
\r
152 /* Might not be wise in an ISR! */
\r
153 while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP );
\r
157 /* Clear RX_DATA_BK0 */
\r
158 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RX_DATA_BK0;
\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
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
169 AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_EPINT1;
\r
172 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT );
\r
174 /* Clear interrupts for the other endpoints, retain data flags for endpoint
\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
180 /* Post ISR data to queue for task-level processing */
\r
181 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
183 /* Clear AIC to complete ISR processing */
\r
184 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
186 /* Do a task switch if needed */
\r
187 if( xHigherPriorityTaskWoken )
\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
195 /*-----------------------------------------------------------*/
\r
197 void vUSB_ISR_Wrapper( void )
\r
199 /* Save the context of the interrupted task. */
\r
200 portSAVE_CONTEXT();
\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
206 /* Restore the context of whichever task will execute next. */
\r
207 portRESTORE_CONTEXT();
\r