2 FreeRTOS.org V5.0.0 - 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.
\r
53 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
54 not effectively use the buffers or DMA and is therefore not intended to be
\r
55 an example of an efficient driver. */
\r
57 /* Standard include file. */
\r
61 /* Scheduler include files. */
\r
62 #include "FreeRTOS.h"
\r
66 /* Demo app include files. */
\r
69 /* Hardware setup. */
\r
70 #define serSET_FLAG ( 1 )
\r
72 /* The queues used to communicate between tasks and ISR's. */
\r
73 static xQueueHandle xRxedChars;
\r
74 static xQueueHandle xCharsForTx;
\r
76 /* Flag used to indicate the tx status. */
\r
77 static portBASE_TYPE xTxHasEnded;
\r
79 /*-----------------------------------------------------------*/
\r
81 /* The UART interrupt handler. */
\r
82 void __attribute__( (interrupt(ipl1), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );
\r
84 /*-----------------------------------------------------------*/
\r
86 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
88 unsigned portSHORT usBRG;
\r
90 /* Create the queues used by the com test task. */
\r
91 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
92 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
94 /* Configure the UART and interrupts. */
\r
95 usBRG = (unsigned portSHORT)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
96 OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
\r
97 ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );
\r
99 xTxHasEnded = pdTRUE;
\r
101 /* Only a single port is implemented so we don't need to return anything. */
\r
104 /*-----------------------------------------------------------*/
\r
106 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
108 /* Only one port is supported. */
\r
111 /* Get the next character from the buffer. Return false if no characters
\r
112 are available or arrive before xBlockTime expires. */
\r
113 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
122 /*-----------------------------------------------------------*/
\r
124 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
126 /* Only one port is supported. */
\r
129 /* Return false if after the block time there is no room on the Tx queue. */
\r
130 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
135 /* A critical section should not be required as xTxHasEnded will not be
\r
136 written to by the ISR if it is already 0 (is this correct?). */
\r
139 xTxHasEnded = pdFALSE;
\r
140 IFS1bits.U2TXIF = serSET_FLAG;
\r
145 /*-----------------------------------------------------------*/
\r
147 void vSerialClose( xComPortHandle xPort )
\r
150 /*-----------------------------------------------------------*/
\r
152 void vU2InterruptHandler( void )
\r
154 /* Declared static to minimise stack use. */
\r
155 static portCHAR cChar;
\r
156 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
158 xHigherPriorityTaskWoken = pdFALSE;
\r
160 /* Are any Rx interrupts pending? */
\r
161 if( mU2RXGetIntFlag() )
\r
163 while( U2STAbits.URXDA )
\r
165 /* Retrieve the received character and place it in the queue of
\r
166 received characters. */
\r
168 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
170 mU2RXClearIntFlag();
\r
173 /* Are any Tx interrupts pending? */
\r
174 if( mU2TXGetIntFlag() )
\r
176 while( !( U2STAbits.UTXBF ) )
\r
178 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
180 /* Send the next character queued for Tx. */
\r
185 /* Queue empty, nothing to send. */
\r
186 xTxHasEnded = pdTRUE;
\r
191 mU2TXClearIntFlag();
\r
194 /* If sending or receiving necessitates a context switch, then switch now. */
\r
195 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r