2 FreeRTOS.org V5.4.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
56 This file contains all the serial port components that must be compiled
\r
57 to ARM mode. The components that can be compiled to either ARM or THUMB
\r
58 mode are contained in serial.c.
\r
62 /* Standard includes. */
\r
65 /* Scheduler includes. */
\r
66 #include "FreeRTOS.h"
\r
70 /* Demo application includes. */
\r
72 #include "AT91R40008.h"
\r
75 /*-----------------------------------------------------------*/
\r
77 /* Constant to access the AIC. */
\r
78 #define serCLEAR_AIC_INTERRUPT ( ( unsigned portLONG ) 0 )
\r
80 /* Constants to determine the ISR source. */
\r
81 #define serSOURCE_THRE ( ( unsigned portCHAR ) 0x02 )
\r
82 #define serSOURCE_RX_TIMEOUT ( ( unsigned portCHAR ) 0x0c )
\r
83 #define serSOURCE_ERROR ( ( unsigned portCHAR ) 0x06 )
\r
84 #define serSOURCE_RX ( ( unsigned portCHAR ) 0x04 )
\r
85 #define serINTERRUPT_SOURCE_MASK ( ( unsigned portLONG ) (US_RXRDY | US_TXRDY | US_RXBRK | US_OVRE | US_FRAME | US_PARE) )
\r
87 /* Queues used to hold received characters, and characters waiting to be
\r
89 static xQueueHandle xRxedChars;
\r
90 static xQueueHandle xCharsForTx;
\r
92 /*-----------------------------------------------------------*/
\r
94 /* UART0 interrupt service routine. This can cause a context switch so MUST
\r
95 be declared "naked". */
\r
96 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));
\r
98 /* The ISR function that actually performs the work. This must be separate
\r
99 from the wrapper to ensure the correct stack frame is set up. */
\r
100 void vUART_ISR_Handler( void );
\r
102 /*-----------------------------------------------------------*/
\r
103 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx )
\r
105 /* Create the queues used to hold Rx and Tx characters. */
\r
106 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
107 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
109 /* Pass back a reference to the queues so the serial API file can
\r
110 post/receive characters. */
\r
111 *pxRxedChars = xRxedChars;
\r
112 *pxCharsForTx = xCharsForTx;
\r
114 /*-----------------------------------------------------------*/
\r
116 void vUART_ISR_Wrapper( void )
\r
118 /* Save the context of the interrupted task. */
\r
119 portSAVE_CONTEXT();
\r
121 /* Call the handler. This must be a separate function to ensure the
\r
122 stack frame is correctly set up. */
\r
123 vUART_ISR_Handler();
\r
125 /* Restore the context of whichever task will run next. */
\r
126 portRESTORE_CONTEXT();
\r
128 /*-----------------------------------------------------------*/
\r
130 void vUART_ISR_Handler( void )
\r
132 /* Now we can declare the local variables. These must be static. */
\r
133 signed portCHAR cChar;
\r
134 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
135 unsigned portLONG ulStatus;
\r
137 /* What caused the interrupt? */
\r
138 ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;
\r
140 if (ulStatus & US_TXRDY)
\r
142 /* The interrupt was caused by the THR becoming empty. Are there any
\r
143 more characters to transmit? */
\r
144 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
146 /* A character was retrieved from the queue so can be sent to the
\r
148 AT91C_BASE_US0->US_THR = cChar;
\r
152 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
153 AT91C_BASE_US0->US_IDR = US_TXRDY;
\r
157 if (ulStatus & US_RXRDY)
\r
159 /* The interrupt was caused by the receiver getting data. */
\r
160 cChar = AT91C_BASE_US0->US_RHR;
\r
162 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);
\r
165 /* Acknowledge the interrupt at AIC level... */
\r
166 AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;
\r
168 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
169 ensure that the unblocked task is the task that executes when the interrupt
\r
170 completes if the unblocked task has a priority higher than the interrupted
\r
172 if( xHigherPriorityTaskWoken )
\r
174 portYIELD_FROM_ISR();
\r
177 /*-----------------------------------------------------------*/
\r