2 FreeRTOS.org V5.3.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
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\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 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
56 not effectively use the buffers or DMA and is therefore not intended to be
\r
57 an example of an efficient driver. */
\r
59 /* Standard include file. */
\r
62 /* Scheduler include files. */
\r
63 #include "FreeRTOS.h"
\r
67 /* Demo app include files. */
\r
70 /* Hardware setup. */
\r
73 #define serLOW_SPEED 0
\r
74 #define serONE_STOP_BIT 0
\r
75 #define serEIGHT_DATA_BITS_NO_PARITY 0
\r
76 #define serNORMAL_IDLE_STATE 0
\r
77 #define serAUTO_BAUD_OFF 0
\r
78 #define serLOOPBACK_OFF 0
\r
79 #define serWAKE_UP_DISABLE 0
\r
80 #define serNO_HARDWARE_FLOW_CONTROL 0
\r
81 #define serSTANDARD_IO 0
\r
82 #define serNO_IRDA 0
\r
83 #define serCONTINUE_IN_IDLE_MODE 0
\r
84 #define serUART_ENABLED 1
\r
85 #define serINTERRUPT_ON_SINGLE_CHAR 0
\r
86 #define serTX_ENABLE 1
\r
87 #define serINTERRUPT_ENABLE 1
\r
88 #define serINTERRUPT_DISABLE 0
\r
89 #define serCLEAR_FLAG 0
\r
90 #define serSET_FLAG 1
\r
93 /* The queues used to communicate between tasks and ISR's. */
\r
94 static xQueueHandle xRxedChars;
\r
95 static xQueueHandle xCharsForTx;
\r
97 static portBASE_TYPE xTxHasEnded;
\r
98 /*-----------------------------------------------------------*/
\r
100 xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
104 /* Create the queues used by the com test task. */
\r
105 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
106 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
\r
108 /* Setup the UART. */
\r
109 U2MODEbits.BRGH = serLOW_SPEED;
\r
110 U2MODEbits.STSEL = serONE_STOP_BIT;
\r
111 U2MODEbits.PDSEL = serEIGHT_DATA_BITS_NO_PARITY;
\r
112 U2MODEbits.ABAUD = serAUTO_BAUD_OFF;
\r
113 U2MODEbits.LPBACK = serLOOPBACK_OFF;
\r
114 U2MODEbits.WAKE = serWAKE_UP_DISABLE;
\r
115 U2MODEbits.UEN = serNO_HARDWARE_FLOW_CONTROL;
\r
116 U2MODEbits.IREN = serNO_IRDA;
\r
117 U2MODEbits.USIDL = serCONTINUE_IN_IDLE_MODE;
\r
118 U2MODEbits.UARTEN = serUART_ENABLED;
\r
120 U2BRG = (unsigned portSHORT)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
122 U2STAbits.URXISEL = serINTERRUPT_ON_SINGLE_CHAR;
\r
123 U2STAbits.UTXEN = serTX_ENABLE;
\r
124 U2STAbits.UTXINV = serNORMAL_IDLE_STATE;
\r
125 U2STAbits.UTXISEL0 = serINTERRUPT_ON_SINGLE_CHAR;
\r
126 U2STAbits.UTXISEL1 = serINTERRUPT_ON_SINGLE_CHAR;
\r
128 /* It is assumed that this function is called prior to the scheduler being
\r
129 started. Therefore interrupts must not be allowed to occur yet as they
\r
130 may attempt to perform a context switch. */
\r
131 portDISABLE_INTERRUPTS();
\r
133 IFS1bits.U2RXIF = serCLEAR_FLAG;
\r
134 IFS1bits.U2TXIF = serCLEAR_FLAG;
\r
135 IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;
\r
136 IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;
\r
137 IEC1bits.U2TXIE = serINTERRUPT_ENABLE;
\r
138 IEC1bits.U2RXIE = serINTERRUPT_ENABLE;
\r
140 /* Clear the Rx buffer. */
\r
141 while( U2STAbits.URXDA == serSET_FLAG )
\r
146 xTxHasEnded = pdTRUE;
\r
150 /*-----------------------------------------------------------*/
\r
152 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
\r
154 /* Only one port is supported. */
\r
157 /* Get the next character from the buffer. Return false if no characters
\r
158 are available or arrive before xBlockTime expires. */
\r
159 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
168 /*-----------------------------------------------------------*/
\r
170 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
\r
172 /* Only one port is supported. */
\r
175 /* Return false if after the block time there is no room on the Tx queue. */
\r
176 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
181 /* A critical section should not be required as xTxHasEnded will not be
\r
182 written to by the ISR if it is already 0 (is this correct?). */
\r
185 xTxHasEnded = pdFALSE;
\r
186 IFS1bits.U2TXIF = serSET_FLAG;
\r
191 /*-----------------------------------------------------------*/
\r
193 void vSerialClose( xComPortHandle xPort )
\r
196 /*-----------------------------------------------------------*/
\r
198 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )
\r
201 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
203 /* Get the character and post it on the queue of Rxed characters.
\r
204 If the post causes a task to wake force a context switch as the woken task
\r
205 may have a higher priority than the task we have interrupted. */
\r
206 IFS1bits.U2RXIF = serCLEAR_FLAG;
\r
207 while( U2STAbits.URXDA )
\r
210 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
213 if( xHigherPriorityTaskWoken != pdFALSE )
\r
218 /*-----------------------------------------------------------*/
\r
220 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )
\r
222 signed portCHAR cChar;
\r
223 portBASE_TYPE xTaskWoken = pdFALSE;
\r
225 /* If the transmit buffer is full we cannot get the next character.
\r
226 Another interrupt will occur the next time there is space so this does
\r
228 IFS1bits.U2TXIF = serCLEAR_FLAG;
\r
229 while( !( U2STAbits.UTXBF ) )
\r
231 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
233 /* Send the next character queued for Tx. */
\r
238 /* Queue empty, nothing to send. */
\r
239 xTxHasEnded = pdTRUE;
\r
244 if( xTaskWoken != pdFALSE )
\r