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
76 /* Scheduler includes. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Demo application includes. */
\r
84 /* Constants required to setup the hardware. */
\r
85 #define serTX_AND_RX ( ( unsigned char ) 0x03 )
\r
87 /* Misc. constants. */
\r
88 #define serNO_BLOCK ( ( TickType_t ) 0 )
\r
90 /* Enable the UART Tx interrupt. */
\r
91 #define vInterruptOn() IFG2 |= UTXIFG1
\r
93 /* The queue used to hold received characters. */
\r
94 static QueueHandle_t xRxedChars;
\r
96 /* The queue used to hold characters waiting transmission. */
\r
97 static QueueHandle_t xCharsForTx;
\r
99 static volatile short sTHREEmpty;
\r
101 /* Interrupt service routines. */
\r
102 interrupt (UART1RX_VECTOR) wakeup vRxISR( void );
\r
103 interrupt (UART1TX_VECTOR) wakeup vTxISR( void );
\r
105 /*-----------------------------------------------------------*/
\r
107 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
109 unsigned long ulBaudRateCount;
\r
111 /* Initialise the hardware. */
\r
113 /* Generate the baud rate constants for the wanted baud rate. */
\r
114 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
116 portENTER_CRITICAL();
\r
118 /* Create the queues used by the com test task. */
\r
119 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
120 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
125 /* Set pin function. */
\r
126 P4SEL |= serTX_AND_RX;
\r
128 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
130 U1CTL |= CHAR + LISTEN;
\r
133 /* Setup baud rate low byte. */
\r
134 U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
136 /* Setup baud rate high byte. */
\r
137 ulBaudRateCount >>= 8UL;
\r
138 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
\r
140 /* Enable ports. */
\r
141 ME2 |= UTXE1 + URXE1;
\r
146 /* Nothing in the buffer yet. */
\r
147 sTHREEmpty = pdTRUE;
\r
149 /* Enable interrupts. */
\r
150 IE2 |= URXIE1 + UTXIE1;
\r
152 portEXIT_CRITICAL();
\r
154 /* Unlike other ports, this serial code does not allow for more than one
\r
155 com port. We therefore don't return a pointer to a port structure and can
\r
156 instead just return NULL. */
\r
159 /*-----------------------------------------------------------*/
\r
161 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
163 /* Get the next character from the buffer. Return false if no characters
\r
164 are available, or arrive before xBlockTime expires. */
\r
165 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
174 /*-----------------------------------------------------------*/
\r
176 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
178 signed portBASE_TYPE xReturn;
\r
180 /* Transmit a character. */
\r
182 portENTER_CRITICAL();
\r
184 if( sTHREEmpty == pdTRUE )
\r
186 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
187 there are no characters queued to be transmitted - so we can
\r
188 write the character directly to the shift Tx register. */
\r
189 sTHREEmpty = pdFALSE;
\r
190 U1TXBUF = cOutChar;
\r
195 /* sTHREEmpty is false, so there are still characters waiting to be
\r
196 transmitted. We have to queue this character so it gets
\r
197 transmitted in turn. */
\r
199 /* Return false if after the block time there is no room on the Tx
\r
200 queue. It is ok to block inside a critical section as each task
\r
201 maintains it's own critical section status. */
\r
202 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
204 /* Depending on queue sizing and task prioritisation: While we
\r
205 were blocked waiting to post on the queue interrupts were not
\r
206 disabled. It is possible that the serial ISR has emptied the
\r
207 Tx queue, in which case we need to start the Tx off again
\r
208 writing directly to the Tx register. */
\r
209 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
211 /* Get back the character we just posted. */
\r
212 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
213 sTHREEmpty = pdFALSE;
\r
214 U1TXBUF = cOutChar;
\r
218 portEXIT_CRITICAL();
\r
222 /*-----------------------------------------------------------*/
\r
225 * UART RX interrupt service routine.
\r
227 interrupt (UART1RX_VECTOR) wakeup vRxISR( void )
\r
230 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
232 /* Get the character from the UART and post it on the queue of Rxed
\r
236 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
238 if( xHigherPriorityTaskWoken )
\r
240 /*If the post causes a task to wake force a context switch
\r
241 as the woken task may have a higher priority than the task we have
\r
246 /*-----------------------------------------------------------*/
\r
249 * UART Tx interrupt service routine.
\r
251 interrupt (UART1TX_VECTOR) wakeup vTxISR( void )
\r
254 portBASE_TYPE xTaskWoken = pdFALSE;
\r
256 /* The previous character has been transmitted. See if there are any
\r
257 further characters waiting transmission. */
\r
259 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
261 /* There was another character queued - transmit it now. */
\r
266 /* There were no other characters to transmit. */
\r
267 sTHREEmpty = pdTRUE;
\r