2 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 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 without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * The FreeRTOS eBook and reference manual are available to purchase for a *
\r
29 * small fee. Help yourself get started quickly while also helping the *
\r
30 * FreeRTOS project! See http://www.FreeRTOS.org/Documentation for details *
\r
32 ***************************************************************************
\r
36 Please ensure to read the configuration and relevant port sections of the
\r
37 online documentation.
\r
39 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
42 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
45 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
46 licensing and training services.
\r
51 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR USART0.
\r
53 This file contains all the serial port components that must be compiled
\r
54 to ARM mode. The components that can be compiled to either ARM or THUMB
\r
55 mode are contained in serial.c.
\r
59 /* Standard includes. */
\r
62 /* Scheduler includes. */
\r
63 #include "FreeRTOS.h"
\r
67 /* Demo application includes. */
\r
69 #include "AT91R40008.h"
\r
72 /*-----------------------------------------------------------*/
\r
74 /* Constant to access the AIC. */
\r
75 #define serCLEAR_AIC_INTERRUPT ( ( unsigned long ) 0 )
\r
77 /* Constants to determine the ISR source. */
\r
78 #define serSOURCE_THRE ( ( unsigned char ) 0x02 )
\r
79 #define serSOURCE_RX_TIMEOUT ( ( unsigned char ) 0x0c )
\r
80 #define serSOURCE_ERROR ( ( unsigned char ) 0x06 )
\r
81 #define serSOURCE_RX ( ( unsigned char ) 0x04 )
\r
82 #define serINTERRUPT_SOURCE_MASK ( ( unsigned long ) (US_RXRDY | US_TXRDY | US_RXBRK | US_OVRE | US_FRAME | US_PARE) )
\r
84 /* Queues used to hold received characters, and characters waiting to be
\r
86 static xQueueHandle xRxedChars;
\r
87 static xQueueHandle xCharsForTx;
\r
89 /*-----------------------------------------------------------*/
\r
91 /* UART0 interrupt service routine. This can cause a context switch so MUST
\r
92 be declared "naked". */
\r
93 void vUART_ISR_Wrapper( void ) __attribute__ ((naked));
\r
95 /* The ISR function that actually performs the work. This must be separate
\r
96 from the wrapper to ensure the correct stack frame is set up. */
\r
97 void vUART_ISR_Handler( void ) __attribute__ ((noinline));
\r
99 /*-----------------------------------------------------------*/
\r
100 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx )
\r
102 /* Create the queues used to hold Rx and Tx characters. */
\r
103 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
104 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
106 /* Pass back a reference to the queues so the serial API file can
\r
107 post/receive characters. */
\r
108 *pxRxedChars = xRxedChars;
\r
109 *pxCharsForTx = xCharsForTx;
\r
111 /*-----------------------------------------------------------*/
\r
113 void vUART_ISR_Wrapper( void )
\r
115 /* Save the context of the interrupted task. */
\r
116 portSAVE_CONTEXT();
\r
118 /* Call the handler. This must be a separate function to ensure the
\r
119 stack frame is correctly set up. */
\r
120 __asm volatile( "bl vUART_ISR_Handler" );
\r
122 /* Restore the context of whichever task will run next. */
\r
123 portRESTORE_CONTEXT();
\r
125 /*-----------------------------------------------------------*/
\r
127 void vUART_ISR_Handler( void )
\r
129 /* Now we can declare the local variables. These must be static. */
\r
131 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
132 unsigned long ulStatus;
\r
134 /* What caused the interrupt? */
\r
135 ulStatus = AT91C_BASE_US0->US_CSR & AT91C_BASE_US0->US_IMR;
\r
137 if (ulStatus & US_TXRDY)
\r
139 /* The interrupt was caused by the THR becoming empty. Are there any
\r
140 more characters to transmit? */
\r
141 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
143 /* A character was retrieved from the queue so can be sent to the
\r
145 AT91C_BASE_US0->US_THR = cChar;
\r
149 /* Queue empty, nothing to send so turn off the Tx interrupt. */
\r
150 AT91C_BASE_US0->US_IDR = US_TXRDY;
\r
154 if (ulStatus & US_RXRDY)
\r
156 /* The interrupt was caused by the receiver getting data. */
\r
157 cChar = AT91C_BASE_US0->US_RHR;
\r
159 xQueueSendFromISR(xRxedChars, &cChar, &xHigherPriorityTaskWoken);
\r
162 /* Acknowledge the interrupt at AIC level... */
\r
163 AT91C_BASE_AIC->AIC_EOICR = serCLEAR_AIC_INTERRUPT;
\r
165 /* If an event caused a task to unblock then we call "Yield from ISR" to
\r
166 ensure that the unblocked task is the task that executes when the interrupt
\r
167 completes if the unblocked task has a priority higher than the interrupted
\r
169 if( xHigherPriorityTaskWoken )
\r
171 portYIELD_FROM_ISR();
\r
174 /*-----------------------------------------------------------*/
\r