2 FreeRTOS V5.4.0 - Copyright (C) 2003-2009 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
7 the terms of the GNU General Public License (version 2) as published by the
\r
8 Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS without being obliged to provide the
\r
11 source code for proprietary components outside of the FreeRTOS kernel.
\r
12 Alternative commercial license and support terms are also available upon
\r
13 request. See the licensing section of http://www.FreeRTOS.org for full
\r
16 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
\r
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
21 You should have received a copy of the GNU General Public License along
\r
22 with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59
\r
23 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
26 ***************************************************************************
\r
28 * Looking for a quick start? Then check out the FreeRTOS eBook! *
\r
29 * See http://www.FreeRTOS.org/Documentation for details *
\r
31 ***************************************************************************
\r
35 Please ensure to read the configuration and relevant port sections of the
\r
36 online documentation.
\r
38 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
41 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
44 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
45 licensing and training services.
\r
49 BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
\r
51 *NOTE* - This file is designed to test some of the RTOS features - it is
\r
52 not intended to represent an efficient implementation!
\r
55 /* Standard includes. */
\r
58 /* Scheduler includes. */
\r
59 #include "FreeRTOS.h"
\r
62 /* Demo application includes. */
\r
66 /* Hardware specifics. */
\r
67 #define serRX_DATA_PIN ( 0x01 )
\r
68 #define serTX_DATA_PIN ( 0x02 )
\r
69 #define serCLOCK_Fxx_DIV_8 0x03
\r
70 #define serUPWR ( 0x80 )
\r
71 #define serUTXE ( 0x40 )
\r
72 #define serURXE ( 0x20 )
\r
73 #define serUCL ( 0x02 )
\r
74 #define serLSB ( 0x10 )
\r
77 #define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
\r
78 #define serHANDLE ( ( xComPortHandle ) 1 )
\r
79 #define serNO_BLOCK ( ( portTickType ) 0 )
\r
81 /*-----------------------------------------------------------*/
\r
83 /* Queues used to hold received characters, and characters waiting to be
\r
85 static xQueueHandle xRxedChars;
\r
86 static xQueueHandle xCharsForTx;
\r
88 /*-----------------------------------------------------------*/
\r
90 /* Interrupt entry point written in the assembler file serialISR.s85. */
\r
91 extern void vSerialISREntry( void );
\r
93 /* Flag to indicate whether or not there are characters already queued to send. */
\r
94 static volatile unsigned long ulTxInProgress = pdFALSE;
\r
96 /*-----------------------------------------------------------*/
\r
99 * See the serial2.h header file.
\r
101 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
103 xComPortHandle xReturn = serHANDLE;
\r
104 const unsigned portLONG ulFuclk = ( configCPU_CLOCK_HZ / 2 ) / 8UL;
\r
106 /* Create the queues used to hold Rx and Tx characters. */
\r
107 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
108 xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
110 /* If the queues were created correctly then setup the serial port
\r
112 if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
\r
114 portENTER_CRITICAL();
\r
116 /* Set the UART0 Rx and Tx pins to their alternative function. */
\r
117 PMC3 |= ( serRX_DATA_PIN | serTX_DATA_PIN );
\r
118 PM3 &= ~( serTX_DATA_PIN );
\r
120 /* Setup clock for required baud. */
\r
121 UD0CTL1 = serCLOCK_Fxx_DIV_8;
\r
122 UD0CTL2 = ulFuclk / ( 2 * ulWantedBaud );
\r
124 /* Enable, n81. */
\r
125 UD0CTL0 = ( serUPWR | serUTXE | serURXE | serUCL | serLSB );
\r
127 /* Enable interrupts for both Rx and Tx. */
\r
131 ulTxInProgress = pdFALSE;
\r
133 portEXIT_CRITICAL();
\r
137 xReturn = ( xComPortHandle ) 0;
\r
140 /* This demo file only supports a single port but we have to return
\r
141 something to comply with the standard demo header file. */
\r
144 /*-----------------------------------------------------------*/
\r
146 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
148 /* The port handle is not required as this driver only supports one port. */
\r
151 /* Get the next character from the buffer. Return false if no characters
\r
152 are available, or arrive before xBlockTime expires. */
\r
153 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
162 /*-----------------------------------------------------------*/
\r
164 void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
\r
166 signed portCHAR *pxNext;
\r
168 /* A couple of parameters that this port does not use. */
\r
169 ( void ) usStringLength;
\r
172 /* NOTE: This implementation does not handle the queue being full as no
\r
173 block time is used! */
\r
175 /* The port handle is not required as this driver only supports UART0. */
\r
178 /* Send each character in the string, one at a time. */
\r
179 pxNext = ( signed portCHAR * ) pcString;
\r
182 xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
\r
186 /*-----------------------------------------------------------*/
\r
188 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
190 portBASE_TYPE xReturn = pdPASS;
\r
192 portENTER_CRITICAL();
\r
194 /* There are currently no characters queued up to send so write the
\r
195 character directly to the UART. */
\r
196 if( ulTxInProgress == pdFALSE )
\r
199 ulTxInProgress = pdTRUE;
\r
203 /* The UART is already busy so write the character to the Tx queue.
\r
204 The queue is drained from within the Tx interrupt. */
\r
205 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
211 portEXIT_CRITICAL();
\r
215 /*-----------------------------------------------------------*/
\r
217 void vSerialClose( xComPortHandle xPort )
\r
219 /* Not supported as not required by the demo application. */
\r
221 /*-----------------------------------------------------------*/
\r
223 /* Tx interrupt handler. This is called from the asm file wrapper. */
\r
224 void vUARTTxISRHandler( void )
\r
227 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
229 /* Are there any more characters queue to transmit? */
\r
230 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
232 /* Send the next character. */
\r
237 /* The UART is no longer active. */
\r
238 ulTxInProgress = pdFALSE;
\r
241 /* If reading a character from the Rx queue caused a task to unblock, and
\r
242 the unblocked task has a priority higher than the currently running task,
\r
243 then xHigherPriorityTaskWoken will have been set to true and a context
\r
244 switch should occur now. */
\r
245 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r
247 /*-----------------------------------------------------------*/
\r
249 /* Rx interrupt handler. This is called from the asm file wrapper. */
\r
250 void vUARTRxISRHandler( void )
\r
253 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
255 /* Send the received character to the Rx queue. */
\r
257 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
259 /* If sending a character to the Tx queue caused a task to unblock, and
\r
260 the unblocked task has a priority higher than the currently running task,
\r
261 then xHigherPriorityTaskWoken will have been set to true and a context
\r
262 switch should occur now. */
\r
263 portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
\r