2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
67 BASIC INTERRUPT DRIVEN DRIVER FOR USB.
\r
69 This file contains all the usb components that must be compiled
\r
70 to ARM mode. The components that can be compiled to either ARM or THUMB
\r
71 mode are contained in USB-CDC.c.
\r
75 /* Scheduler includes. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo application includes. */
\r
83 #include "USB-CDC.h"
\r
85 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
86 /*-----------------------------------------------------------*/
\r
88 /* Messages and queue used to communicate between the ISR and the USB task. */
\r
89 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
90 extern xQueueHandle xUSBInterruptQueue;
\r
91 /*-----------------------------------------------------------*/
\r
93 /* The ISR can cause a context switch so is declared naked. */
\r
94 void vUSB_ISR_Wrapper( void ) __attribute__ ((naked));
\r
96 /* The function that actually performs the ISR work. This must be separate
\r
97 from the wrapper function to ensure the correct stack frame gets set up. */
\r
98 void vUSB_ISR_Handler( void );
\r
99 /*-----------------------------------------------------------*/
\r
101 void vUSB_ISR_Handler( void )
\r
103 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
104 static volatile unsigned long ulNextMessage = 0;
\r
105 xISRStatus *pxMessage;
\r
106 unsigned long ulRxBytes;
\r
107 unsigned char ucFifoIndex;
\r
109 /* Use the next message from the array. */
\r
110 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
113 /* Save UDP ISR state for task-level processing. */
\r
114 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
115 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
117 /* Clear interrupts from ICR. */
\r
118 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
121 /* Process incoming FIFO data. Must set DIR (if needed) and clear RXSETUP
\r
124 /* Read CSR and get incoming byte count. */
\r
125 ulRxBytes = ( pxMessage->ulCSR0 >> 16 ) & usbRX_COUNT_MASK;
\r
127 /* Receive control transfers on endpoint 0. */
\r
128 if( pxMessage->ulCSR0 & ( AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 ) )
\r
130 /* Save FIFO data buffer for either a SETUP or DATA stage */
\r
131 for( ucFifoIndex = 0; ucFifoIndex < ulRxBytes; ucFifoIndex++ )
\r
133 pxMessage->ucFifoData[ ucFifoIndex ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
136 /* Set direction for data stage. Must be done before RXSETUP is
\r
138 if( ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP ) )
\r
140 if( ulRxBytes && ( pxMessage->ucFifoData[ usbREQUEST_TYPE_INDEX ] & 0x80 ) )
\r
142 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] |= AT91C_UDP_DIR;
\r
144 /* Might not be wise in an ISR! */
\r
145 while( !(AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_DIR) );
\r
148 /* Clear RXSETUP */
\r
149 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RXSETUP;
\r
151 /* Might not be wise in an ISR! */
\r
152 while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RXSETUP );
\r
156 /* Clear RX_DATA_BK0 */
\r
157 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~AT91C_UDP_RX_DATA_BK0;
\r
159 /* Might not be wise in an ISR! */
\r
160 while ( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] & AT91C_UDP_RX_DATA_BK0 );
\r
164 /* If we received data on endpoint 1, disable its interrupts until it is
\r
165 processed in the main loop */
\r
166 if( AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] & ( AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 ) )
\r
168 AT91C_BASE_UDP->UDP_IDR = AT91C_UDP_EPINT1;
\r
171 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT );
\r
173 /* Clear interrupts for the other endpoints, retain data flags for endpoint
\r
175 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] &= ~( AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP );
\r
176 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_2 ] &= ~usbINT_CLEAR_MASK;
\r
177 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_3 ] &= ~usbINT_CLEAR_MASK;
\r
179 /* Post ISR data to queue for task-level processing */
\r
180 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
182 /* Clear AIC to complete ISR processing */
\r
183 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
185 /* Do a task switch if needed */
\r
186 if( xHigherPriorityTaskWoken )
\r
188 /* This call will ensure that the unblocked task will be executed
\r
189 immediately upon completion of the ISR if it has a priority higher
\r
190 than the interrupted task. */
\r
191 portYIELD_FROM_ISR();
\r
194 /*-----------------------------------------------------------*/
\r
196 void vUSB_ISR_Wrapper( void )
\r
198 /* Save the context of the interrupted task. */
\r
199 portSAVE_CONTEXT();
\r
201 /* Call the handler to do the work. This must be a separate
\r
202 function to ensure the stack frame is set up correctly. */
\r
203 vUSB_ISR_Handler();
\r
205 /* Restore the context of whichever task will execute next. */
\r
206 portRESTORE_CONTEXT();
\r