2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
68 * This file only supports UART 1
\r
71 /* Standard includes. */
\r
74 /* Scheduler includes. */
\r
75 #include "FreeRTOS.h"
\r
79 /* Demo application includes. */
\r
82 /* Constants required to setup the hardware. */
\r
83 #define serTX_AND_RX ( ( unsigned portCHAR ) 0x03 )
\r
85 /* Misc. constants. */
\r
86 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
88 /* Enable the UART Tx interrupt. */
\r
89 #define vInterruptOn() IFG2 |= UTXIFG1
\r
91 /* The queue used to hold received characters. */
\r
92 static xQueueHandle xRxedChars;
\r
94 /* The queue used to hold characters waiting transmission. */
\r
95 static xQueueHandle xCharsForTx;
\r
97 static volatile portSHORT sTHREEmpty;
\r
99 /*-----------------------------------------------------------*/
\r
101 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
103 unsigned portLONG ulBaudRateCount;
\r
105 /* Initialise the hardware. */
\r
107 /* Generate the baud rate constants for the wanted baud rate. */
\r
108 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
110 portENTER_CRITICAL();
\r
112 /* Create the queues used by the com test task. */
\r
113 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
114 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
119 /* Set pin function. */
\r
120 P4SEL |= serTX_AND_RX;
\r
122 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
124 U1CTL |= CHAR + LISTEN;
\r
127 /* Setup baud rate low byte. */
\r
128 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
130 /* Setup baud rate high byte. */
\r
131 ulBaudRateCount >>= 8UL;
\r
132 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
134 /* Enable ports. */
\r
135 ME2 |= UTXE1 + URXE1;
\r
140 /* Nothing in the buffer yet. */
\r
141 sTHREEmpty = pdTRUE;
\r
143 /* Enable interrupts. */
\r
144 IE2 |= URXIE1 + UTXIE1;
\r
146 portEXIT_CRITICAL();
\r
148 /* Unlike other ports, this serial code does not allow for more than one
\r
149 com port. We therefore don't return a pointer to a port structure and can
\r
150 instead just return NULL. */
\r
153 /*-----------------------------------------------------------*/
\r
155 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
157 /* Get the next character from the buffer. Return false if no characters
\r
158 are available, or arrive before xBlockTime expires. */
\r
159 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
168 /*-----------------------------------------------------------*/
\r
170 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
172 signed portBASE_TYPE xReturn;
\r
174 /* Transmit a character. */
\r
176 portENTER_CRITICAL();
\r
178 if( sTHREEmpty == pdTRUE )
\r
180 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
181 there are no characters queued to be transmitted - so we can
\r
182 write the character directly to the shift Tx register. */
\r
183 sTHREEmpty = pdFALSE;
\r
184 U1TXBUF = cOutChar;
\r
189 /* sTHREEmpty is false, so there are still characters waiting to be
\r
190 transmitted. We have to queue this character so it gets
\r
191 transmitted in turn. */
\r
193 /* Return false if after the block time there is no room on the Tx
\r
194 queue. It is ok to block inside a critical section as each task
\r
195 maintains it's own critical section status. */
\r
196 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
198 /* Depending on queue sizing and task prioritisation: While we
\r
199 were blocked waiting to post on the queue interrupts were not
\r
200 disabled. It is possible that the serial ISR has emptied the
\r
201 Tx queue, in which case we need to start the Tx off again
\r
202 writing directly to the Tx register. */
\r
203 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
205 /* Get back the character we just posted. */
\r
206 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
207 sTHREEmpty = pdFALSE;
\r
208 U1TXBUF = cOutChar;
\r
212 portEXIT_CRITICAL();
\r
216 /*-----------------------------------------------------------*/
\r
218 #if configINTERRUPT_EXAMPLE_METHOD == 1
\r
221 * UART RX interrupt service routine.
\r
223 #pragma vector=UART1RX_VECTOR
\r
224 __interrupt void vRxISR( void )
\r
226 signed portCHAR cChar;
\r
227 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
229 /* Get the character from the UART and post it on the queue of Rxed
\r
233 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
235 if( xHigherPriorityTaskWoken )
\r
237 /*If the post causes a task to wake force a context switch
\r
238 as the woken task may have a higher priority than the task we have
\r
243 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
244 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
246 /*-----------------------------------------------------------*/
\r
249 * UART Tx interrupt service routine.
\r
251 #pragma vector=UART1TX_VECTOR
\r
252 __interrupt void vTxISR( void )
\r
254 signed portCHAR cChar;
\r
255 portBASE_TYPE xTaskWoken = pdFALSE;
\r
257 /* The previous character has been transmitted. See if there are any
\r
258 further characters waiting transmission. */
\r
260 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
262 /* There was another character queued - transmit it now. */
\r
267 /* There were no other characters to transmit. */
\r
268 sTHREEmpty = pdTRUE;
\r
271 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
272 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
274 /*-----------------------------------------------------------*/
\r
276 #elif configINTERRUPT_EXAMPLE_METHOD == 2
\r
278 /* This is a standard C function as an assembly file wrapper is used as an
\r
279 interrupt entry point. */
\r
280 void vRxISR( void )
\r
282 signed portCHAR cChar;
\r
283 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
285 /* Get the character from the UART and post it on the queue of Rxed
\r
289 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
291 /*If the post causes a task to wake force a context switch
\r
292 as the woken task may have a higher priority than the task we have
\r
294 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
296 /*-----------------------------------------------------------*/
\r
298 /* This is a standard C function as an assembly file wrapper is used as an
\r
299 interrupt entry point. */
\r
300 void vTxISR( void )
\r
302 signed portCHAR cChar;
\r
303 portBASE_TYPE xTaskWoken = pdFALSE;
\r
305 /* The previous character has been transmitted. See if there are any
\r
306 further characters waiting transmission. */
\r
308 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
310 /* There was another character queued - transmit it now. */
\r
315 /* There were no other characters to transmit. */
\r
316 sTHREEmpty = pdTRUE;
\r
320 #endif /* configINTERRUPT_EXAMPLE_METHOD */
\r
321 /*-----------------------------------------------------------*/
\r