2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
69 * This file only supports UART 1
\r
72 /* Standard includes. */
\r
75 /* Scheduler includes. */
\r
76 #include "FreeRTOS.h"
\r
80 /* Demo application includes. */
\r
83 /* Constants required to setup the hardware. */
\r
84 #define serTX_AND_RX ( ( unsigned char ) 0x03 )
\r
86 /* Misc. constants. */
\r
87 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
89 /* Enable the UART Tx interrupt. */
\r
90 #define vInterruptOn() IFG2 |= UTXIFG1
\r
92 /* The queue used to hold received characters. */
\r
93 static QueueHandle_t xRxedChars;
\r
95 /* The queue used to hold characters waiting transmission. */
\r
96 static QueueHandle_t xCharsForTx;
\r
98 static volatile short sTHREEmpty;
\r
100 /*-----------------------------------------------------------*/
\r
102 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
104 unsigned long ulBaudRateCount;
\r
106 /* Initialise the hardware. */
\r
108 /* Generate the baud rate constants for the wanted baud rate. */
\r
109 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
111 portENTER_CRITICAL();
\r
113 /* Create the queues used by the com test task. */
\r
114 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
115 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
120 /* Set pin function. */
\r
121 P4SEL |= serTX_AND_RX;
\r
123 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
125 U1CTL |= CHAR + LISTEN;
\r
128 /* Setup baud rate low byte. */
\r
129 U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
131 /* Setup baud rate high byte. */
\r
132 ulBaudRateCount >>= 8UL;
\r
133 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
135 /* Enable ports. */
\r
136 ME2 |= UTXE1 + URXE1;
\r
141 /* Nothing in the buffer yet. */
\r
142 sTHREEmpty = pdTRUE;
\r
144 /* Enable interrupts. */
\r
145 IE2 |= URXIE1 + UTXIE1;
\r
147 portEXIT_CRITICAL();
\r
149 /* Unlike other ports, this serial code does not allow for more than one
\r
150 com port. We therefore don't return a pointer to a port structure and can
\r
151 instead just return NULL. */
\r
154 /*-----------------------------------------------------------*/
\r
156 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
158 /* Get the next character from the buffer. Return false if no characters
\r
159 are available, or arrive before xBlockTime expires. */
\r
160 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
169 /*-----------------------------------------------------------*/
\r
171 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
173 signed portBASE_TYPE xReturn;
\r
175 /* Transmit a character. */
\r
177 portENTER_CRITICAL();
\r
179 if( sTHREEmpty == pdTRUE )
\r
181 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
182 there are no characters queued to be transmitted - so we can
\r
183 write the character directly to the shift Tx register. */
\r
184 sTHREEmpty = pdFALSE;
\r
185 U1TXBUF = cOutChar;
\r
190 /* sTHREEmpty is false, so there are still characters waiting to be
\r
191 transmitted. We have to queue this character so it gets
\r
192 transmitted in turn. */
\r
194 /* Return false if after the block time there is no room on the Tx
\r
195 queue. It is ok to block inside a critical section as each task
\r
196 maintains it's own critical section status. */
\r
197 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
199 /* Depending on queue sizing and task prioritisation: While we
\r
200 were blocked waiting to post on the queue interrupts were not
\r
201 disabled. It is possible that the serial ISR has emptied the
\r
202 Tx queue, in which case we need to start the Tx off again
\r
203 writing directly to the Tx register. */
\r
204 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
206 /* Get back the character we just posted. */
\r
207 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
208 sTHREEmpty = pdFALSE;
\r
209 U1TXBUF = cOutChar;
\r
213 portEXIT_CRITICAL();
\r
217 /*-----------------------------------------------------------*/
\r
219 #if configINTERRUPT_EXAMPLE_METHOD == 1
\r
222 * UART RX interrupt service routine.
\r
224 #pragma vector=UART1RX_VECTOR
\r
225 __interrupt void vRxISR( void )
\r
228 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
230 /* Get the character from the UART and post it on the queue of Rxed
\r
234 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
236 if( xHigherPriorityTaskWoken )
\r
238 /*If the post causes a task to wake force a context switch
\r
239 as the woken task may have a higher priority than the task we have
\r
244 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
245 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
247 /*-----------------------------------------------------------*/
\r
250 * UART Tx interrupt service routine.
\r
252 #pragma vector=UART1TX_VECTOR
\r
253 __interrupt void vTxISR( void )
\r
256 portBASE_TYPE xTaskWoken = pdFALSE;
\r
258 /* The previous character has been transmitted. See if there are any
\r
259 further characters waiting transmission. */
\r
261 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
263 /* There was another character queued - transmit it now. */
\r
268 /* There were no other characters to transmit. */
\r
269 sTHREEmpty = pdTRUE;
\r
272 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
273 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
275 /*-----------------------------------------------------------*/
\r
277 #elif configINTERRUPT_EXAMPLE_METHOD == 2
\r
279 /* This is a standard C function as an assembly file wrapper is used as an
\r
280 interrupt entry point. */
\r
281 void vRxISR( void )
\r
284 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
286 /* Get the character from the UART and post it on the queue of Rxed
\r
290 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
292 /*If the post causes a task to wake force a context switch
\r
293 as the woken task may have a higher priority than the task we have
\r
295 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
297 /*-----------------------------------------------------------*/
\r
299 /* This is a standard C function as an assembly file wrapper is used as an
\r
300 interrupt entry point. */
\r
301 void vTxISR( void )
\r
304 portBASE_TYPE xTaskWoken = pdFALSE;
\r
306 /* The previous character has been transmitted. See if there are any
\r
307 further characters waiting transmission. */
\r
309 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
311 /* There was another character queued - transmit it now. */
\r
316 /* There were no other characters to transmit. */
\r
317 sTHREEmpty = pdTRUE;
\r
321 #endif /* configINTERRUPT_EXAMPLE_METHOD */
\r
322 /*-----------------------------------------------------------*/
\r