2 FreeRTOS.org V5.1.1 - Copyright (C) 2003-2008 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
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
51 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
53 *NOTE* - This file is designed to test some of the RTOS features - it is
\r
54 not intended to represent an efficient implementation!
\r
57 /* Standard includes. */
\r
60 /* Scheduler includes. */
\r
61 #include "FreeRTOS.h"
\r
64 /* Demo application includes. */
\r
68 /* Hardware specifics. */
\r
69 #define serRX_DATA_PIN ( 0x01 )
\r
70 #define serTX_DATA_PIN ( 0x02 )
\r
71 #define serCLOCK_Fxx_DIV_8 0x03
\r
72 #define serUPWR ( 0x80 )
\r
73 #define serUTXE ( 0x40 )
\r
74 #define serURXE ( 0x20 )
\r
75 #define serUCL ( 0x02 )
\r
76 #define serLSB ( 0x10 )
\r
79 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
80 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
81 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
83 /*-----------------------------------------------------------*/
\r
85 /* Queues used to hold received characters, and characters waiting to be
\r
87 static xQueueHandle xRxedChars;
\r
88 static xQueueHandle xCharsForTx;
\r
90 /*-----------------------------------------------------------*/
\r
92 /* Interrupt entry point written in the assembler file serialISR.s85. */
\r
93 extern void vSerialISREntry( void );
\r
95 /* Flag to indicate whether or not there are characters already queued to send. */
\r
96 static volatile unsigned long ulTxInProgress = pdFALSE;
\r
98 /*-----------------------------------------------------------*/
\r
101 * See the serial2.h header file.
\r
103 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
105 xComPortHandle xReturn = serHANDLE;
\r
106 const unsigned portLONG ulFuclk = ( configCPU_CLOCK_HZ / 2 ) / 8UL;
\r
108 /* Create the queues used to hold Rx and Tx characters. */
\r
109 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
110 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
112 /* If the queues were created correctly then setup the serial port
\r
114 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
116 portENTER_CRITICAL();
\r
118 /* Set the UART0 Rx and Tx pins to their alternative function. */
\r
119 PMC3 |= ( serRX_DATA_PIN | serTX_DATA_PIN );
\r
120 PM3 &= ~( serTX_DATA_PIN );
\r
122 /* Setup clock for required baud. */
\r
123 UD0CTL1 = serCLOCK_Fxx_DIV_8;
\r
124 UD0CTL2 = ulFuclk / ( 2 * ulWantedBaud );
\r
126 /* Enable, n81. */
\r
127 UD0CTL0 = ( serUPWR | serUTXE | serURXE | serUCL | serLSB );
\r
129 /* Enable interrupts for both Rx and Tx. */
\r
133 ulTxInProgress = pdFALSE;
\r
135 portEXIT_CRITICAL();
\r
139 xReturn = ( xComPortHandle ) 0;
\r
142 /* This demo file only supports a single port but we have to return
\r
143 something to comply with the standard demo header file. */
\r
146 /*-----------------------------------------------------------*/
\r
148 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
150 /* The port handle is not required as this driver only supports one port. */
\r
153 /* Get the next character from the buffer. Return false if no characters
\r
154 are available, or arrive before xBlockTime expires. */
\r
155 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
164 /*-----------------------------------------------------------*/
\r
166 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
168 signed portCHAR *pxNext;
\r
170 /* A couple of parameters that this port does not use. */
\r
171 ( void ) usStringLength;
\r
174 /* NOTE: This implementation does not handle the queue being full as no
\r
175 block time is used! */
\r
177 /* The port handle is not required as this driver only supports UART0. */
\r
180 /* Send each character in the string, one at a time. */
\r
181 pxNext = ( signed portCHAR * ) pcString;
\r
184 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
188 /*-----------------------------------------------------------*/
\r
190 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
192 portBASE_TYPE xReturn = pdPASS;
\r
194 portENTER_CRITICAL();
\r
196 /* There are currently no characters queued up to send so write the
\r
197 character directly to the UART. */
\r
198 if( ulTxInProgress == pdFALSE )
\r
201 ulTxInProgress = pdTRUE;
\r
205 /* The UART is already busy so write the character to the Tx queue.
\r
206 The queue is drained from within the Tx interrupt. */
\r
207 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
213 portEXIT_CRITICAL();
\r
217 /*-----------------------------------------------------------*/
\r
219 void vSerialClose( xComPortHandle xPort )
\r
221 /* Not supported as not required by the demo application. */
\r
223 /*-----------------------------------------------------------*/
\r
225 /* Tx interrupt handler. This is called from the asm file wrapper. */
\r
226 void vUARTTxISRHandler( void )
\r
229 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
231 /* Are there any more characters queue to transmit? */
\r
232 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
234 /* Send the next character. */
\r
239 /* The UART is no longer active. */
\r
240 ulTxInProgress = pdFALSE;
\r
243 /* If reading a character from the Rx queue caused a task to unblock, and
\r
244 the unblocked task has a priority higher than the currently running task,
\r
245 then xHigherPriorityTaskWoken will have been set to true and a context
\r
246 switch should occur now. */
\r
247 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
249 /*-----------------------------------------------------------*/
\r
251 /* Rx interrupt handler. This is called from the asm file wrapper. */
\r
252 void vUARTRxISRHandler( void )
\r
255 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
257 /* Send the received character to the Rx queue. */
\r
259 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
261 /* If sending a character to the Tx queue caused a task to unblock, and
\r
262 the unblocked task has a priority higher than the currently running task,
\r
263 then xHigherPriorityTaskWoken will have been set to true and a context
\r
264 switch should occur now. */
\r
265 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r