2 FreeRTOS.org V4.7.1 - Copyright (C) 2003-2008 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
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
44 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
46 * This file only supports UART 1
\r
49 /* Standard includes. */
\r
52 /* Scheduler includes. */
\r
53 #include "FreeRTOS.h"
\r
57 /* Demo application includes. */
\r
60 /* Constants required to setup the hardware. */
\r
61 #define serTX_AND_RX ( ( unsigned portCHAR ) 0x03 )
\r
63 /* Misc. constants. */
\r
64 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
66 /* Enable the UART Tx interrupt. */
\r
67 #define vInterruptOn() IFG2 |= UTXIFG1
\r
69 /* The queue used to hold received characters. */
\r
70 static xQueueHandle xRxedChars;
\r
72 /* The queue used to hold characters waiting transmission. */
\r
73 static xQueueHandle xCharsForTx;
\r
75 static volatile portSHORT sTHREEmpty;
\r
77 /*-----------------------------------------------------------*/
\r
79 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
81 unsigned portLONG ulBaudRateCount;
\r
83 /* Initialise the hardware. */
\r
85 /* Generate the baud rate constants for the wanted baud rate. */
\r
86 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
88 portENTER_CRITICAL();
\r
90 /* Create the queues used by the com test task. */
\r
91 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
92 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
97 /* Set pin function. */
\r
98 P4SEL |= serTX_AND_RX;
\r
100 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
102 U1CTL |= CHAR + LISTEN;
\r
105 /* Setup baud rate low byte. */
\r
106 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
108 /* Setup baud rate high byte. */
\r
109 ulBaudRateCount >>= 8UL;
\r
110 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
112 /* Enable ports. */
\r
113 ME2 |= UTXE1 + URXE1;
\r
118 /* Nothing in the buffer yet. */
\r
119 sTHREEmpty = pdTRUE;
\r
121 /* Enable interrupts. */
\r
122 IE2 |= URXIE1 + UTXIE1;
\r
124 portEXIT_CRITICAL();
\r
126 /* Unlike other ports, this serial code does not allow for more than one
\r
127 com port. We therefore don't return a pointer to a port structure and can
\r
128 instead just return NULL. */
\r
131 /*-----------------------------------------------------------*/
\r
133 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
135 /* Get the next character from the buffer. Return false if no characters
\r
136 are available, or arrive before xBlockTime expires. */
\r
137 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
146 /*-----------------------------------------------------------*/
\r
148 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
150 signed portBASE_TYPE xReturn;
\r
152 /* Transmit a character. */
\r
154 portENTER_CRITICAL();
\r
156 if( sTHREEmpty == pdTRUE )
\r
158 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
159 there are no characters queued to be transmitted - so we can
\r
160 write the character directly to the shift Tx register. */
\r
161 sTHREEmpty = pdFALSE;
\r
162 U1TXBUF = cOutChar;
\r
167 /* sTHREEmpty is false, so there are still characters waiting to be
\r
168 transmitted. We have to queue this character so it gets
\r
169 transmitted in turn. */
\r
171 /* Return false if after the block time there is no room on the Tx
\r
172 queue. It is ok to block inside a critical section as each task
\r
173 maintains it's own critical section status. */
\r
174 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
176 /* Depending on queue sizing and task prioritisation: While we
\r
177 were blocked waiting to post on the queue interrupts were not
\r
178 disabled. It is possible that the serial ISR has emptied the
\r
179 Tx queue, in which case we need to start the Tx off again
\r
180 writing directly to the Tx register. */
\r
181 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
183 /* Get back the character we just posted. */
\r
184 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
185 sTHREEmpty = pdFALSE;
\r
186 U1TXBUF = cOutChar;
\r
190 portEXIT_CRITICAL();
\r
194 /*-----------------------------------------------------------*/
\r
196 #ifdef MSP_ROWLEY_RB_PORT
\r
198 /* Serial interrupt service routines for the RB port. */
\r
201 * UART RX interrupt service routine.
\r
203 void vRxISR( void ) __interrupt[ UART1RX_VECTOR ]
\r
205 signed portCHAR cChar;
\r
207 /* Get the character from the UART and post it on the queue of Rxed
\r
211 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )
\r
213 /*If the post causes a task to wake force a context switch
\r
214 as the woken task may have a higher priority than the task we have
\r
219 /*-----------------------------------------------------------*/
\r
222 * UART Tx interrupt service routine.
\r
224 void vTxISR( void ) __interrupt[ UART1TX_VECTOR ]
\r
226 signed portCHAR cChar;
\r
227 portBASE_TYPE xTaskWoken;
\r
229 /* The previous character has been transmitted. See if there are any
\r
230 further characters waiting transmission. */
\r
232 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
234 /* There was another character queued - transmit it now. */
\r
239 /* There were no other characters to transmit. */
\r
240 sTHREEmpty = pdTRUE;
\r
245 /*-----------------------------------------------------------*/
\r
247 #ifdef MSP_ROWLEY_MP_PORT
\r
249 /* Serial port interrupts for the alternative port code. */
\r
251 void ISRCom1Rx( void )
\r
253 signed portCHAR cChar;
\r
255 /* Get the character from the UART and post it on the queue of Rxed
\r
259 if( xQueueSendFromISR( xRxedChars, &cChar, pdFALSE ) )
\r
261 /*If the post causes a task to wake force a context switch
\r
262 as the woken task may have a higher priority than the task we have
\r
264 portEXIT_SWITCHING_ISR( pdTRUE );
\r
267 /*-----------------------------------------------------------*/
\r
269 void ISRCom1Tx( void )
\r
271 signed portCHAR cChar;
\r
272 portBASE_TYPE xTaskWoken;
\r
274 /* The previous character has been transmitted. See if there are any
\r
275 further characters waiting transmission. */
\r
277 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
279 /* There was another character queued - transmit it now. */
\r
284 /* There were no other characters to transmit. */
\r
285 sTHREEmpty = pdTRUE;
\r
290 /*-----------------------------------------------------------*/
\r