2 FreeRTOS.org V4.0.5 - Copyright (C) 2003-2006 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
30 ***************************************************************************
\r
34 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
36 * This file only supports UART 1
\r
39 /* Standard includes. */
\r
43 /* Scheduler includes. */
\r
44 #include "FreeRTOS.h"
\r
48 /* Demo application includes. */
\r
51 /* Constants required to setup the hardware. */
\r
52 #define serTX_AND_RX ( ( unsigned portCHAR ) 0x03 )
\r
54 /* Misc. constants. */
\r
55 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
57 /* Enable the UART Tx interrupt. */
\r
58 #define vInterruptOn() IFG2 |= UTXIFG1
\r
60 /* The queue used to hold received characters. */
\r
61 static xQueueHandle xRxedChars;
\r
63 /* The queue used to hold characters waiting transmission. */
\r
64 static xQueueHandle xCharsForTx;
\r
66 static volatile portSHORT sTHREEmpty;
\r
68 /* Interrupt service routines. */
\r
69 interrupt (UART1RX_VECTOR) vRxISR( void );
\r
70 interrupt (UART1TX_VECTOR) vTxISR( void );
\r
72 /*-----------------------------------------------------------*/
\r
74 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
76 unsigned portLONG ulBaudRateCount;
\r
78 /* Initialise the hardware. */
\r
80 /* Generate the baud rate constants for the wanted baud rate. */
\r
81 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
83 portENTER_CRITICAL();
\r
85 /* Create the queues used by the com test task. */
\r
86 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
87 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
92 /* Set pin function. */
\r
93 P4SEL |= serTX_AND_RX;
\r
95 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
97 U1CTL |= CHAR + LISTEN;
\r
100 /* Setup baud rate low byte. */
\r
101 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
103 /* Setup baud rate high byte. */
\r
104 ulBaudRateCount >>= 8UL;
\r
105 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
107 /* Enable ports. */
\r
108 ME2 |= UTXE1 + URXE1;
\r
113 /* Nothing in the buffer yet. */
\r
114 sTHREEmpty = pdTRUE;
\r
116 /* Enable interrupts. */
\r
117 IE2 |= URXIE1 + UTXIE1;
\r
119 portEXIT_CRITICAL();
\r
121 /* Unlike other ports, this serial code does not allow for more than one
\r
122 com port. We therefore don't return a pointer to a port structure and can
\r
123 instead just return NULL. */
\r
126 /*-----------------------------------------------------------*/
\r
128 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
130 /* Get the next character from the buffer. Return false if no characters
\r
131 are available, or arrive before xBlockTime expires. */
\r
132 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
141 /*-----------------------------------------------------------*/
\r
143 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
145 signed portBASE_TYPE xReturn;
\r
147 /* Transmit a character. */
\r
149 portENTER_CRITICAL();
\r
151 if( sTHREEmpty == pdTRUE )
\r
153 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
154 there are no characters queued to be transmitted - so we can
\r
155 write the character directly to the shift Tx register. */
\r
156 sTHREEmpty = pdFALSE;
\r
157 U1TXBUF = cOutChar;
\r
162 /* sTHREEmpty is false, so there are still characters waiting to be
\r
163 transmitted. We have to queue this character so it gets
\r
164 transmitted in turn. */
\r
166 /* Return false if after the block time there is no room on the Tx
\r
167 queue. It is ok to block inside a critical section as each task
\r
168 maintains it's own critical section status. */
\r
169 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
171 /* Depending on queue sizing and task prioritisation: While we
\r
172 were blocked waiting to post on the queue interrupts were not
\r
173 disabled. It is possible that the serial ISR has emptied the
\r
174 Tx queue, in which case we need to start the Tx off again
\r
175 writing directly to the Tx register. */
\r
176 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
178 /* Get back the character we just posted. */
\r
179 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
180 sTHREEmpty = pdFALSE;
\r
181 U1TXBUF = cOutChar;
\r
185 portEXIT_CRITICAL();
\r
189 /*-----------------------------------------------------------*/
\r
192 * UART RX interrupt service routine.
\r
194 interrupt (UART1RX_VECTOR) vRxISR( void )
\r
196 signed portCHAR cChar;
\r
198 /* Get the character from the UART and post it on the queue of Rxed
\r
202 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )
\r
204 /*If the post causes a task to wake force a context switch
\r
205 as the woken task may have a higher priority than the task we have
\r
210 /*-----------------------------------------------------------*/
\r
213 * UART Tx interrupt service routine.
\r
215 interrupt (UART1TX_VECTOR) vTxISR( void )
\r
217 signed portCHAR cChar;
\r
218 portBASE_TYPE xTaskWoken;
\r
220 /* The previous character has been transmitted. See if there are any
\r
221 further characters waiting transmission. */
\r
223 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
225 /* There was another character queued - transmit it now. */
\r
230 /* There were no other characters to transmit. */
\r
231 sTHREEmpty = pdTRUE;
\r