2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
54 /* Scheduler includes. */
\r
55 #include "FreeRTOS.h"
\r
59 /* Demo app includes. */
\r
60 #include "USBSample.h"
\r
62 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
64 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit ) \
\r
66 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
67 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
68 /* write has no effect. */ \
\r
69 ( * ( ( unsigned long * ) pulValueNow ) ) |= ( unsigned long ) 0x4f; \
\r
71 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
72 /* so the write has no effect. */ \
\r
73 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( unsigned long ) 0xffffffcf; \
\r
75 /* Clear whichever bit we want clear. */ \
\r
76 ( * ( ( unsigned long * ) pulValueNow ) ) &= ( ~ulBit ); \
\r
80 /*-----------------------------------------------------------*/
\r
86 void vUSB_ISR_Wrapper( void ) __attribute__((naked));
\r
89 * Actual ISR handler. This must be separate from the entry point as the stack
\r
92 void vUSB_ISR_Handler( void ) __attribute__((noinline));
\r
94 /*-----------------------------------------------------------*/
\r
96 /* Array in which the USB interrupt status is passed between the ISR and task. */
\r
97 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
99 /* Queue used to pass messages between the ISR and the task. */
\r
100 extern xQueueHandle xUSBInterruptQueue;
\r
102 /*-----------------------------------------------------------*/
\r
104 void vUSB_ISR_Handler( void )
\r
106 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
107 static volatile unsigned long ulNextMessage = 0;
\r
108 xISRStatus *pxMessage;
\r
109 unsigned long ulTemp, ulRxBytes;
\r
111 /* To reduce the amount of time spent in this interrupt it would be
\r
112 possible to defer the majority of this processing to an 'interrupt task',
\r
113 that is a task that runs at a higher priority than any of the application
\r
116 /* Take the next message from the queue. Note that usbQUEUE_LENGTH *must*
\r
117 be all 1's, as in 0x01, 0x03, 0x07, etc. */
\r
118 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
121 /* Take a snapshot of the current USB state for processing at the task
\r
123 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
124 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
126 /* Clear the interrupts from the ICR register. The bus end interrupt is
\r
127 cleared separately as it does not appear in the mask register. */
\r
128 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
130 /* If there are bytes in the FIFO then we have to retrieve them here.
\r
131 Ideally this would be done at the task level. However we need to clear the
\r
132 RXSETUP interrupt before leaving the ISR, and this may cause the data in
\r
133 the FIFO to be overwritten. Also the DIR bit has to be changed before the
\r
134 RXSETUP bit is cleared (as per the SAM7 manual). */
\r
135 ulTemp = pxMessage->ulCSR0;
\r
137 /* Are there any bytes in the FIFO? */
\r
138 ulRxBytes = ulTemp >> 16;
\r
139 ulRxBytes &= usbRX_COUNT_MASK;
\r
141 /* With this minimal implementation we are only interested in receiving
\r
142 setup bytes on the control end point. */
\r
143 if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )
\r
145 /* Take off 1 for a zero based index. */
\r
146 while( ulRxBytes > 0 )
\r
149 pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
152 /* The direction must be changed first. */
\r
153 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );
\r
154 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
157 /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA
\r
158 registers to clear the interrupts in the CSR register. */
\r
159 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
160 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
162 /* Also clear the interrupts in the CSR1 register. */
\r
163 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
164 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
165 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
167 /* The message now contains the entire state and optional data from
\r
168 the USB interrupt. This can now be posted on the Rx queue ready for
\r
169 processing at the task level. */
\r
170 xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, &xHigherPriorityTaskWoken );
\r
172 /* We may want to switch to the USB task, if this message has made
\r
173 it the highest priority task that is ready to execute. */
\r
174 if( xHigherPriorityTaskWoken )
\r
176 portYIELD_FROM_ISR();
\r
179 /* Clear the AIC ready for the next interrupt. */
\r
180 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
182 /*-----------------------------------------------------------*/
\r
184 void vUSB_ISR_Wrapper( void )
\r
186 /* Save the context of the interrupted task. */
\r
187 portSAVE_CONTEXT();
\r
189 /* Call the handler itself. This must be a separate function as it uses
\r
191 __asm volatile ("bl vUSB_ISR_Handler");
\r
193 /* Restore the context of the task that is going to
\r
194 execute next. This might not be the same as the originally
\r
195 interrupted task.*/
\r
196 portRESTORE_CONTEXT();
\r