2 FreeRTOS.org V5.2.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
10 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
15 You should have received a copy of the GNU General Public License along
\r
16 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
17 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
19 A special exception to the GPL is included to allow you to distribute a
\r
20 combined work that includes FreeRTOS.org without being obliged to provide
\r
21 the source code for any proprietary components. See the licensing section
\r
22 of http://www.FreeRTOS.org for full details.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
53 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
55 * This file only supports UART 1
\r
58 /* Standard includes. */
\r
61 /* Scheduler includes. */
\r
62 #include "FreeRTOS.h"
\r
66 /* Demo application includes. */
\r
69 /* Constants required to setup the hardware. */
\r
70 #define serTX_AND_RX ( ( unsigned portCHAR ) 0x03 )
\r
72 /* Misc. constants. */
\r
73 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
75 /* Enable the UART Tx interrupt. */
\r
76 #define vInterruptOn() IFG2 |= UTXIFG1
\r
78 /* The queue used to hold received characters. */
\r
79 static xQueueHandle xRxedChars;
\r
81 /* The queue used to hold characters waiting transmission. */
\r
82 static xQueueHandle xCharsForTx;
\r
84 static volatile portSHORT sTHREEmpty;
\r
86 /*-----------------------------------------------------------*/
\r
88 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
90 unsigned portLONG ulBaudRateCount;
\r
92 /* Initialise the hardware. */
\r
94 /* Generate the baud rate constants for the wanted baud rate. */
\r
95 ulBaudRateCount = configCPU_CLOCK_HZ / ulWantedBaud;
\r
97 portENTER_CRITICAL();
\r
99 /* Create the queues used by the com test task. */
\r
100 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
101 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
106 /* Set pin function. */
\r
107 P4SEL |= serTX_AND_RX;
\r
109 /* All other bits remain at zero for n, 8, 1 interrupt driven operation.
\r
111 U1CTL |= CHAR + LISTEN;
\r
114 /* Setup baud rate low byte. */
\r
115 U1BR0 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
117 /* Setup baud rate high byte. */
\r
118 ulBaudRateCount >>= 8UL;
\r
119 U1BR1 = ( unsigned portCHAR ) ( ulBaudRateCount & ( unsigned portLONG ) 0xff );
\r
121 /* Enable ports. */
\r
122 ME2 |= UTXE1 + URXE1;
\r
127 /* Nothing in the buffer yet. */
\r
128 sTHREEmpty = pdTRUE;
\r
130 /* Enable interrupts. */
\r
131 IE2 |= URXIE1 + UTXIE1;
\r
133 portEXIT_CRITICAL();
\r
135 /* Unlike other ports, this serial code does not allow for more than one
\r
136 com port. We therefore don't return a pointer to a port structure and can
\r
137 instead just return NULL. */
\r
140 /*-----------------------------------------------------------*/
\r
142 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
144 /* Get the next character from the buffer. Return false if no characters
\r
145 are available, or arrive before xBlockTime expires. */
\r
146 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
155 /*-----------------------------------------------------------*/
\r
157 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
159 signed portBASE_TYPE xReturn;
\r
161 /* Transmit a character. */
\r
163 portENTER_CRITICAL();
\r
165 if( sTHREEmpty == pdTRUE )
\r
167 /* If sTHREEmpty is true then the UART Tx ISR has indicated that
\r
168 there are no characters queued to be transmitted - so we can
\r
169 write the character directly to the shift Tx register. */
\r
170 sTHREEmpty = pdFALSE;
\r
171 U1TXBUF = cOutChar;
\r
176 /* sTHREEmpty is false, so there are still characters waiting to be
\r
177 transmitted. We have to queue this character so it gets
\r
178 transmitted in turn. */
\r
180 /* Return false if after the block time there is no room on the Tx
\r
181 queue. It is ok to block inside a critical section as each task
\r
182 maintains it's own critical section status. */
\r
183 xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
\r
185 /* Depending on queue sizing and task prioritisation: While we
\r
186 were blocked waiting to post on the queue interrupts were not
\r
187 disabled. It is possible that the serial ISR has emptied the
\r
188 Tx queue, in which case we need to start the Tx off again
\r
189 writing directly to the Tx register. */
\r
190 if( ( sTHREEmpty == pdTRUE ) && ( xReturn == pdPASS ) )
\r
192 /* Get back the character we just posted. */
\r
193 xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
\r
194 sTHREEmpty = pdFALSE;
\r
195 U1TXBUF = cOutChar;
\r
199 portEXIT_CRITICAL();
\r
203 /*-----------------------------------------------------------*/
\r
205 #if configINTERRUPT_EXAMPLE_METHOD == 1
\r
208 * UART RX interrupt service routine.
\r
210 #pragma vector=UART1RX_VECTOR
\r
211 __interrupt void vRxISR( void )
\r
213 signed portCHAR cChar;
\r
214 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
216 /* Get the character from the UART and post it on the queue of Rxed
\r
220 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
222 if( xHigherPriorityTaskWoken )
\r
224 /*If the post causes a task to wake force a context switch
\r
225 as the woken task may have a higher priority than the task we have
\r
230 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
231 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
233 /*-----------------------------------------------------------*/
\r
236 * UART Tx interrupt service routine.
\r
238 #pragma vector=UART1TX_VECTOR
\r
239 __interrupt void vTxISR( void )
\r
241 signed portCHAR cChar;
\r
242 portBASE_TYPE xTaskWoken = pdFALSE;
\r
244 /* The previous character has been transmitted. See if there are any
\r
245 further characters waiting transmission. */
\r
247 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
249 /* There was another character queued - transmit it now. */
\r
254 /* There were no other characters to transmit. */
\r
255 sTHREEmpty = pdTRUE;
\r
258 /* Make sure any low power mode bits are clear before leaving the ISR. */
\r
259 __bic_SR_register_on_exit( SCG1 + SCG0 + OSCOFF + CPUOFF );
\r
261 /*-----------------------------------------------------------*/
\r
263 #elif configINTERRUPT_EXAMPLE_METHOD == 2
\r
265 /* This is a standard C function as an assembly file wrapper is used as an
\r
266 interrupt entry point. */
\r
267 void vRxISR( void )
\r
269 signed portCHAR cChar;
\r
270 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
272 /* Get the character from the UART and post it on the queue of Rxed
\r
276 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
278 /*If the post causes a task to wake force a context switch
\r
279 as the woken task may have a higher priority than the task we have
\r
281 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
283 /*-----------------------------------------------------------*/
\r
285 /* This is a standard C function as an assembly file wrapper is used as an
\r
286 interrupt entry point. */
\r
287 void vTxISR( void )
\r
289 signed portCHAR cChar;
\r
290 portBASE_TYPE xTaskWoken = pdFALSE;
\r
292 /* The previous character has been transmitted. See if there are any
\r
293 further characters waiting transmission. */
\r
295 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
297 /* There was another character queued - transmit it now. */
\r
302 /* There were no other characters to transmit. */
\r
303 sTHREEmpty = pdTRUE;
\r
307 #endif /* configINTERRUPT_EXAMPLE_METHOD */
\r
308 /*-----------------------------------------------------------*/
\r