2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
71 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
73 * This file only supports UART 1
76 /* Standard includes. */
79 /* Scheduler includes. */
84 /* Demo application includes. */
87 /* Constants required to setup the hardware. */
88 #define serTX_AND_RX ( ( unsigned char ) 0x03 )
90 /* Misc. constants. */
91 #define serNO_BLOCK ( ( TickType_t ) 0 )
93 /* Enable the UART Tx interrupt. */
94 #define vInterruptOn() IFG2 |= UTXIFG1
96 /* The queue used to hold received characters. */
97 static QueueHandle_t xRxedChars;
99 /* The queue used to hold characters waiting transmission. */
100 static QueueHandle_t xCharsForTx;
102 static volatile short sTHREEmpty;
104 /*-----------------------------------------------------------*/
106 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
108 unsigned long ulBaudRateCount;
110 /* Initialise the hardware. */
112 /* Generate the baud rate constants for the wanted baud rate. */
113 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
115 portENTER_CRITICAL();
117 /* Create the queues used by the com test task. */
118 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
119 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
124 /* Set pin function. */
125 P4SEL |= serTX_AND_RX;
127 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
129 U1CTL |= CHAR + LISTEN;
132 /* Setup baud rate low byte. */
133 U1BR0 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
135 /* Setup baud rate high byte. */
136 ulBaudRateCount >>= 8UL;
137 U1BR1 = ( unsigned char ) ( ulBaudRateCount & ( unsigned long ) 0xff );
140 ME2 |= UTXE1 + URXE1;
145 /* Nothing in the buffer yet. */
148 /* Enable interrupts. */
149 IE2 |= URXIE1 + UTXIE1;
153 /* Unlike other ports, this serial code does not allow for more than one
154 com port. We therefore don't return a pointer to a port structure and can
155 instead just return NULL. */
158 /*-----------------------------------------------------------*/
160 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
162 /* Get the next character from the buffer. Return false if no characters
163 are available, or arrive before xBlockTime expires. */
164 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
173 /*-----------------------------------------------------------*/
175 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
177 signed portBASE_TYPE xReturn;
179 /* Transmit a character. */
181 portENTER_CRITICAL();
183 if( sTHREEmpty == pdTRUE )
185 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
186 there are no characters queued to be transmitted - so we can
187 write the character directly to the shift Tx register. */
188 sTHREEmpty = pdFALSE;
194 /* sTHREEmpty is false, so there are still characters waiting to be
195 transmitted. We have to queue this character so it gets
196 transmitted in turn. */
198 /* Return false if after the block time there is no room on the Tx
199 queue. It is ok to block inside a critical section as each task
200 maintains it's own critical section status. */
201 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
203 /* Depending on queue sizing and task prioritisation: While we
204 were blocked waiting to post on the queue interrupts were not
205 disabled. It is possible that the serial ISR has emptied the
206 Tx queue, in which case we need to start the Tx off again
207 writing directly to the Tx register. */
208 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
210 /* Get back the character we just posted. */
211 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
212 sTHREEmpty = pdFALSE;
221 /*-----------------------------------------------------------*/
223 #if configINTERRUPT_EXAMPLE_METHOD == 1
226 * UART RX interrupt service routine.
228 void vRxISR( void ) __interrupt[ UART1RX_VECTOR ]
231 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
233 /* Get the character from the UART and post it on the queue of Rxed
237 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
239 if( xHigherPriorityTaskWoken )
241 /*If the post causes a task to wake force a context switch
242 as the woken task may have a higher priority than the task we have
247 /* Make sure any low power mode bits are clear before leaving the ISR. */
248 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
250 /*-----------------------------------------------------------*/
253 * UART Tx interrupt service routine.
255 void vTxISR( void ) __interrupt[ UART1TX_VECTOR ]
258 portBASE_TYPE xTaskWoken = pdFALSE;
260 /* The previous character has been transmitted. See if there are any
261 further characters waiting transmission. */
263 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
265 /* There was another character queued - transmit it now. */
270 /* There were no other characters to transmit. */
274 /* Make sure any low power mode bits are clear before leaving the ISR. */
275 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
277 /*-----------------------------------------------------------*/
279 #elif configINTERRUPT_EXAMPLE_METHOD == 2
281 /* This is a standard C function as an assembly file wrapper is used as an
282 interrupt entry point. */
286 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
288 /* Get the character from the UART and post it on the queue of Rxed
292 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
294 /*If the post causes a task to wake force a context switch
295 as the woken task may have a higher priority than the task we have
297 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
299 /*-----------------------------------------------------------*/
301 /* This is a standard C function as an assembly file wrapper is used as an
302 interrupt entry point. */
306 portBASE_TYPE xTaskWoken = pdFALSE;
308 /* The previous character has been transmitted. See if there are any
309 further characters waiting transmission. */
311 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
313 /* There was another character queued - transmit it now. */
318 /* There were no other characters to transmit. */
323 #endif /* configINTERRUPT_EXAMPLE_METHOD */
324 /*-----------------------------------------------------------*/