2 FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS 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 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; 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, 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
30 ***************************************************************************
\r
35 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
37 This file contains all the serial port components that must be compiled
\r
38 to ARM mode. The components that can be compiled to either ARM or THUMB
\r
39 mode are contained in serial.c.
\r
42 /* This file must always be compiled to ARM mode as it contains ISR
\r
46 /* Standard includes. */
\r
49 /* Scheduler includes. */
\r
50 #include "FreeRTOS.h"
\r
54 /* Demo application includes. */
\r
57 /*-----------------------------------------------------------*/
\r
59 /* Constant to access the VIC. */
\r
60 #define serCLEAR_VIC_INTERRUPT ( ( unsigned portLONG ) 0 )
\r
62 /* Constants to determine the ISR source. */
\r
63 #define serSOURCE_THRE ( ( unsigned portCHAR ) 0x02 )
\r
64 #define serSOURCE_RX_TIMEOUT ( ( unsigned portCHAR ) 0x0c )
\r
65 #define serSOURCE_ERROR ( ( unsigned portCHAR ) 0x06 )
\r
66 #define serSOURCE_RX ( ( unsigned portCHAR ) 0x04 )
\r
67 #define serINTERRUPT_SOURCE_MASK ( ( unsigned portCHAR ) 0x0f )
\r
69 /* Queues used to hold received characters, and characters waiting to be
\r
71 static xQueueHandle xRxedChars;
\r
72 static xQueueHandle xCharsForTx;
\r
73 static volatile portLONG lTHREEmpty;
\r
75 /*-----------------------------------------------------------*/
\r
77 /* UART0 interrupt service routine. This can cause a context switch so MUST
\r
78 be declared "naked". */
\r
79 void vUART_ISR( void );
\r
81 /*-----------------------------------------------------------*/
\r
82 void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars,
\r
83 xQueueHandle *pxCharsForTx, portLONG volatile **pplTHREEmptyFlag )
\r
85 /* Create the queues used to hold Rx and Tx characters. */
\r
86 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
87 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
89 /* Pass back a reference to the queues so the serial API file can
\r
90 post/receive characters. */
\r
91 *pxRxedChars = xRxedChars;
\r
92 *pxCharsForTx = xCharsForTx;
\r
94 /* Initialise the THRE empty flag - and pass back a reference. */
\r
95 lTHREEmpty = pdTRUE;
\r
96 *pplTHREEmptyFlag = &lTHREEmpty;
\r
98 /*-----------------------------------------------------------*/
\r
100 void vUART_ISR( void ) __task
\r
102 portENTER_SWITCHING_ISR()
\r
104 /* Now we can declare the local variables. */
\r
105 static signed portCHAR cChar;
\r
106 static portBASE_TYPE xTaskWokenByRx, xTaskWokenByTx;
\r
108 xTaskWokenByTx = pdFALSE;
\r
109 xTaskWokenByRx = pdFALSE;
\r
111 /* What caused the interrupt? */
\r
112 switch( U0IIR & serINTERRUPT_SOURCE_MASK )
\r
114 case serSOURCE_ERROR : /* Not handling this, but clear the interrupt. */
\r
118 case serSOURCE_THRE : /* The THRE is empty. If there is another
\r
119 character in the Tx queue, send it now. */
\r
120 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWokenByTx ) == pdTRUE )
\r
126 /* There are no further characters
\r
127 queued to send so we can indicate
\r
128 that the THRE is available. */
\r
129 lTHREEmpty = pdTRUE;
\r
133 case serSOURCE_RX_TIMEOUT :
\r
134 case serSOURCE_RX : /* A character was received. Place it in
\r
135 the queue of received characters. */
\r
137 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )
\r
139 xTaskWokenByRx = pdTRUE;
\r
143 default : /* There is nothing to do, leave the ISR. */
\r
147 /* Clear the ISR in the VIC. */
\r
148 VICVectAddr = serCLEAR_VIC_INTERRUPT;
\r
150 /* Exit the ISR. If a task was woken by either a character being received
\r
151 or transmitted then a context switch will occur. */
\r
152 portEXIT_SWITCHING_ISR( ( xTaskWokenByTx || xTaskWokenByRx ) );
\r
154 /*-----------------------------------------------------------*/
\r