2 FreeRTOS.org V4.6.1 - Copyright (C) 2003-2007 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com a version that has been certified for use
\r
32 in safety critical systems, plus commercial licensing, development and
\r
34 ***************************************************************************
\r
37 /* Scheduler includes. */
\r
38 #include "FreeRTOS.h"
\r
42 /* Demo app includes. */
\r
43 #include "USBSample.h"
\r
45 #define usbINT_CLEAR_MASK (AT91C_UDP_TXCOMP | AT91C_UDP_STALLSENT | AT91C_UDP_RXSETUP | AT91C_UDP_RX_DATA_BK0 | AT91C_UDP_RX_DATA_BK1 )
\r
47 #define usbCSR_CLEAR_BIT( pulValueNow, ulBit ) \
\r
49 /* Set TXCOMP, RX_DATA_BK0, RXSETUP, */ \
\r
50 /* STALLSENT and RX_DATA_BK1 to 1 so the */ \
\r
51 /* write has no effect. */ \
\r
52 ( * ( ( unsigned portLONG * ) pulValueNow ) ) |= ( unsigned portLONG ) 0x4f; \
\r
54 /* Clear the FORCE_STALL and TXPKTRDY bits */ \
\r
55 /* so the write has no effect. */ \
\r
56 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( unsigned portLONG ) 0xffffffcf; \
\r
58 /* Clear whichever bit we want clear. */ \
\r
59 ( * ( ( unsigned portLONG * ) pulValueNow ) ) &= ( ~ulBit ); \
\r
63 /*-----------------------------------------------------------*/
\r
69 void vUSB_ISR_Wrapper( void ) __attribute__((naked));
\r
72 * Actual ISR handler. This must be separate from the entry point as the stack
\r
75 void vUSB_ISR_Handler( void );
\r
77 /*-----------------------------------------------------------*/
\r
79 /* Array in which the USB interrupt status is passed between the ISR and task. */
\r
80 static xISRStatus xISRMessages[ usbQUEUE_LENGTH + 1 ];
\r
82 /* Queue used to pass messages between the ISR and the task. */
\r
83 extern xQueueHandle xUSBInterruptQueue;
\r
85 /*-----------------------------------------------------------*/
\r
87 void vUSB_ISR_Handler( void )
\r
89 portBASE_TYPE xTaskWokenByPost = pdFALSE;
\r
90 static volatile unsigned portLONG ulNextMessage = 0;
\r
91 xISRStatus *pxMessage;
\r
92 unsigned portLONG ulTemp, ulRxBytes;
\r
94 /* To reduce the amount of time spent in this interrupt it would be
\r
95 possible to defer the majority of this processing to an 'interrupt task',
\r
96 that is a task that runs at a higher priority than any of the application
\r
99 /* Take the next message from the queue. Note that usbQUEUE_LENGTH *must*
\r
100 be all 1's, as in 0x01, 0x03, 0x07, etc. */
\r
101 pxMessage = &( xISRMessages[ ( ulNextMessage & usbQUEUE_LENGTH ) ] );
\r
104 /* Take a snapshot of the current USB state for processing at the task
\r
106 pxMessage->ulISR = AT91C_BASE_UDP->UDP_ISR;
\r
107 pxMessage->ulCSR0 = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ];
\r
109 /* Clear the interrupts from the ICR register. The bus end interrupt is
\r
110 cleared separately as it does not appear in the mask register. */
\r
111 AT91C_BASE_UDP->UDP_ICR = AT91C_BASE_UDP->UDP_IMR | AT91C_UDP_ENDBUSRES;
\r
113 /* If there are bytes in the FIFO then we have to retrieve them here.
\r
114 Ideally this would be done at the task level. However we need to clear the
\r
115 RXSETUP interrupt before leaving the ISR, and this may cause the data in
\r
116 the FIFO to be overwritten. Also the DIR bit has to be changed before the
\r
117 RXSETUP bit is cleared (as per the SAM7 manual). */
\r
118 ulTemp = pxMessage->ulCSR0;
\r
120 /* Are there any bytes in the FIFO? */
\r
121 ulRxBytes = ulTemp >> 16;
\r
122 ulRxBytes &= usbRX_COUNT_MASK;
\r
124 /* With this minimal implementation we are only interested in receiving
\r
125 setup bytes on the control end point. */
\r
126 if( ( ulRxBytes > 0 ) && ( ulTemp & AT91C_UDP_RXSETUP ) )
\r
128 /* Take off 1 for a zero based index. */
\r
129 while( ulRxBytes > 0 )
\r
132 pxMessage->ucFifoData[ ulRxBytes ] = AT91C_BASE_UDP->UDP_FDR[ usbEND_POINT_0 ];
\r
135 /* The direction must be changed first. */
\r
136 usbCSR_SET_BIT( &ulTemp, ( AT91C_UDP_DIR ) );
\r
137 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
140 /* Must write zero's to TXCOMP, STALLSENT, RXSETUP, and the RX DATA
\r
141 registers to clear the interrupts in the CSR register. */
\r
142 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
143 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_0 ] = ulTemp;
\r
145 /* Also clear the interrupts in the CSR1 register. */
\r
146 ulTemp = AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ];
\r
147 usbCSR_CLEAR_BIT( &ulTemp, usbINT_CLEAR_MASK );
\r
148 AT91C_BASE_UDP->UDP_CSR[ usbEND_POINT_1 ] = ulTemp;
\r
150 /* The message now contains the entire state and optional data from
\r
151 the USB interrupt. This can now be posted on the Rx queue ready for
\r
152 processing at the task level. */
\r
153 xTaskWokenByPost = xQueueSendFromISR( xUSBInterruptQueue, &pxMessage, xTaskWokenByPost );
\r
155 /* We may want to switch to the USB task, if this message has made
\r
156 it the highest priority task that is ready to execute. */
\r
157 if( xTaskWokenByPost )
\r
159 portYIELD_FROM_ISR();
\r
162 /* Clear the AIC ready for the next interrupt. */
\r
163 AT91C_BASE_AIC->AIC_EOICR = 0;
\r
165 /*-----------------------------------------------------------*/
\r
167 void vUSB_ISR_Wrapper( void )
\r
169 /* Save the context of the interrupted task. */
\r
170 portSAVE_CONTEXT();
\r
172 /* Call the handler itself. This must be a separate function as it uses
\r
174 vUSB_ISR_Handler();
\r
176 /* Restore the context of the task that is going to
\r
177 execute next. This might not be the same as the originally
\r
178 interrupted task.*/
\r
179 portRESTORE_CONTEXT();
\r