2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
72 * This file only supports UART 1
\r
75 /* Standard includes. */
\r
78 /* Scheduler includes. */
\r
79 #include "FreeRTOS.h"
\r
83 /* Demo application includes. */
\r
86 /* Constants required to setup the hardware. */
\r
87 #define serTX_AND_RX ( ( unsigned portCHAR ) 0x03 )
\r
89 /* Misc. constants. */
\r
90 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
92 /* Enable the UART Tx interrupt. */
\r
93 #define vInterruptOn() IFG2 |= UTXIFG1
\r
95 /* The queue used to hold received characters. */
\r
96 static xQueueHandle xRxedChars;
\r
98 /* The queue used to hold characters waiting transmission. */
\r
99 static xQueueHandle xCharsForTx;
\r
101 static volatile portSHORT sTHREEmpty;
\r
103 /*-----------------------------------------------------------*/
\r
105 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
107 unsigned portLONG ulBaudRateCount;
\r
109 /* Initialise the hardware. */
\r
111 /* Generate the baud rate constants for the wanted baud rate. */
\r
112 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
114 portENTER_CRITICAL();
\r
116 /* Create the queues used by the com test task. */
\r
117 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
118 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
123 /* Set pin function. */
\r
124 P4SEL |= serTX_AND_RX;
\r
126 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
128 U1CTL |= CHAR + LISTEN;
\r
131 /* Setup baud rate low byte. */
\r
132 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
134 /* Setup baud rate high byte. */
\r
135 ulBaudRateCount >>= 8UL;
\r
136 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
138 /* Enable ports. */
\r
139 ME2 |= UTXE1 + URXE1;
\r
144 /* Nothing in the buffer yet. */
\r
145 sTHREEmpty = pdTRUE;
\r
147 /* Enable interrupts. */
\r
148 IE2 |= URXIE1 + UTXIE1;
\r
150 portEXIT_CRITICAL();
\r
152 /* Unlike other ports, this serial code does not allow for more than one
\r
153 com port. We therefore don't return a pointer to a port structure and can
\r
154 instead just return NULL. */
\r
157 /*-----------------------------------------------------------*/
\r
159 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
161 /* Get the next character from the buffer. Return false if no characters
\r
162 are available, or arrive before xBlockTime expires. */
\r
163 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
172 /*-----------------------------------------------------------*/
\r
174 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
176 signed portBASE_TYPE xReturn;
\r
178 /* Transmit a character. */
\r
180 portENTER_CRITICAL();
\r
182 if( sTHREEmpty == pdTRUE )
\r
184 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
185 there are no characters queued to be transmitted - so we can
\r
186 write the character directly to the shift Tx register. */
\r
187 sTHREEmpty = pdFALSE;
\r
188 U1TXBUF = cOutChar;
\r
193 /* sTHREEmpty is false, so there are still characters waiting to be
\r
194 transmitted. We have to queue this character so it gets
\r
195 transmitted in turn. */
\r
197 /* Return false if after the block time there is no room on the Tx
\r
198 queue. It is ok to block inside a critical section as each task
\r
199 maintains it's own critical section status. */
\r
200 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
202 /* Depending on queue sizing and task prioritisation: While we
\r
203 were blocked waiting to post on the queue interrupts were not
\r
204 disabled. It is possible that the serial ISR has emptied the
\r
205 Tx queue, in which case we need to start the Tx off again
\r
206 writing directly to the Tx register. */
\r
207 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
209 /* Get back the character we just posted. */
\r
210 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
211 sTHREEmpty = pdFALSE;
\r
212 U1TXBUF = cOutChar;
\r
216 portEXIT_CRITICAL();
\r
220 /*-----------------------------------------------------------*/
\r
222 #if configINTERRUPT_EXAMPLE_METHOD == 1
\r
225 * UART RX interrupt service routine.
\r
227 #pragma vector=UART1RX_VECTOR
\r
228 __interrupt void vRxISR( void )
\r
230 signed portCHAR cChar;
\r
231 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
233 /* Get the character from the UART and post it on the queue of Rxed
\r
237 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
239 if( xHigherPriorityTaskWoken )
\r
241 /*If the post causes a task to wake force a context switch
\r
242 as the woken task may have a higher priority than the task we have
\r
247 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
248 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
250 /*-----------------------------------------------------------*/
\r
253 * UART Tx interrupt service routine.
\r
255 #pragma vector=UART1TX_VECTOR
\r
256 __interrupt void vTxISR( void )
\r
258 signed portCHAR cChar;
\r
259 portBASE_TYPE xTaskWoken = pdFALSE;
\r
261 /* The previous character has been transmitted. See if there are any
\r
262 further characters waiting transmission. */
\r
264 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
266 /* There was another character queued - transmit it now. */
\r
271 /* There were no other characters to transmit. */
\r
272 sTHREEmpty = pdTRUE;
\r
275 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
276 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
278 /*-----------------------------------------------------------*/
\r
280 #elif configINTERRUPT_EXAMPLE_METHOD == 2
\r
282 /* This is a standard C function as an assembly file wrapper is used as an
\r
283 interrupt entry point. */
\r
284 void vRxISR( void )
\r
286 signed portCHAR cChar;
\r
287 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
289 /* Get the character from the UART and post it on the queue of Rxed
\r
293 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
295 /*If the post causes a task to wake force a context switch
\r
296 as the woken task may have a higher priority than the task we have
\r
298 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
300 /*-----------------------------------------------------------*/
\r
302 /* This is a standard C function as an assembly file wrapper is used as an
\r
303 interrupt entry point. */
\r
304 void vTxISR( void )
\r
306 signed portCHAR cChar;
\r
307 portBASE_TYPE xTaskWoken = pdFALSE;
\r
309 /* The previous character has been transmitted. See if there are any
\r
310 further characters waiting transmission. */
\r
312 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
314 /* There was another character queued - transmit it now. */
\r
319 /* There were no other characters to transmit. */
\r
320 sTHREEmpty = pdTRUE;
\r
324 #endif /* configINTERRUPT_EXAMPLE_METHOD */
\r
325 /*-----------------------------------------------------------*/
\r