2 FreeRTOS V7.4.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not itcan be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
76 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
78 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
79 not effectively use the buffers or DMA and is therefore not intended to be
\r
80 an example of an efficient driver. */
\r
82 /* Standard include file. */
\r
85 /* Scheduler include files. */
\r
86 #include "FreeRTOS.h"
\r
90 /* Demo app include files. */
\r
93 /* Hardware setup. */
\r
96 #define serLOW_SPEED 0
\r
97 #define serONE_STOP_BIT 0
\r
98 #define serEIGHT_DATA_BITS_NO_PARITY 0
\r
99 #define serNORMAL_IDLE_STATE 0
\r
100 #define serAUTO_BAUD_OFF 0
\r
101 #define serLOOPBACK_OFF 0
\r
102 #define serWAKE_UP_DISABLE 0
\r
103 #define serNO_HARDWARE_FLOW_CONTROL 0
\r
104 #define serSTANDARD_IO 0
\r
105 #define serNO_IRDA 0
\r
106 #define serCONTINUE_IN_IDLE_MODE 0
\r
107 #define serUART_ENABLED 1
\r
108 #define serINTERRUPT_ON_SINGLE_CHAR 0
\r
109 #define serTX_ENABLE 1
\r
110 #define serINTERRUPT_ENABLE 1
\r
111 #define serINTERRUPT_DISABLE 0
\r
112 #define serCLEAR_FLAG 0
\r
113 #define serSET_FLAG 1
\r
116 /* The queues used to communicate between tasks and ISR's. */
\r
117 static xQueueHandle xRxedChars;
\r
118 static xQueueHandle xCharsForTx;
\r
120 static portBASE_TYPE xTxHasEnded;
\r
121 /*-----------------------------------------------------------*/
\r
123 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
127 /* Create the queues used by the com test task. */
\r
128 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
129 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
131 /* Setup the UART. */
\r
132 U2MODEbits.BRGH = serLOW_SPEED;
\r
133 U2MODEbits.STSEL = serONE_STOP_BIT;
\r
134 U2MODEbits.PDSEL = serEIGHT_DATA_BITS_NO_PARITY;
\r
135 U2MODEbits.ABAUD = serAUTO_BAUD_OFF;
\r
136 U2MODEbits.LPBACK = serLOOPBACK_OFF;
\r
137 U2MODEbits.WAKE = serWAKE_UP_DISABLE;
\r
138 U2MODEbits.UEN = serNO_HARDWARE_FLOW_CONTROL;
\r
139 U2MODEbits.IREN = serNO_IRDA;
\r
140 U2MODEbits.USIDL = serCONTINUE_IN_IDLE_MODE;
\r
141 U2MODEbits.UARTEN = serUART_ENABLED;
\r
143 U2BRG = (unsigned short)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
145 U2STAbits.URXISEL = serINTERRUPT_ON_SINGLE_CHAR;
\r
146 U2STAbits.UTXEN = serTX_ENABLE;
\r
147 U2STAbits.UTXINV = serNORMAL_IDLE_STATE;
\r
148 U2STAbits.UTXISEL0 = serINTERRUPT_ON_SINGLE_CHAR;
\r
149 U2STAbits.UTXISEL1 = serINTERRUPT_ON_SINGLE_CHAR;
\r
151 /* It is assumed that this function is called prior to the scheduler being
\r
152 started. Therefore interrupts must not be allowed to occur yet as they
\r
153 may attempt to perform a context switch. */
\r
154 portDISABLE_INTERRUPTS();
\r
156 IFS1bits.U2RXIF = serCLEAR_FLAG;
\r
157 IFS1bits.U2TXIF = serCLEAR_FLAG;
\r
158 IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;
\r
159 IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;
\r
160 IEC1bits.U2TXIE = serINTERRUPT_ENABLE;
\r
161 IEC1bits.U2RXIE = serINTERRUPT_ENABLE;
\r
163 /* Clear the Rx buffer. */
\r
164 while( U2STAbits.URXDA == serSET_FLAG )
\r
169 xTxHasEnded = pdTRUE;
\r
173 /*-----------------------------------------------------------*/
\r
175 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
177 /* Only one port is supported. */
\r
180 /* Get the next character from the buffer. Return false if no characters
\r
181 are available or arrive before xBlockTime expires. */
\r
182 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
191 /*-----------------------------------------------------------*/
\r
193 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
195 /* Only one port is supported. */
\r
198 /* Return false if after the block time there is no room on the Tx queue. */
\r
199 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
204 /* A critical section should not be required as xTxHasEnded will not be
\r
205 written to by the ISR if it is already 0 (is this correct?). */
\r
208 xTxHasEnded = pdFALSE;
\r
209 IFS1bits.U2TXIF = serSET_FLAG;
\r
214 /*-----------------------------------------------------------*/
\r
216 void vSerialClose( xComPortHandle xPort )
\r
219 /*-----------------------------------------------------------*/
\r
221 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )
\r
224 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
226 /* Get the character and post it on the queue of Rxed characters.
\r
227 If the post causes a task to wake force a context switch as the woken task
\r
228 may have a higher priority than the task we have interrupted. */
\r
229 IFS1bits.U2RXIF = serCLEAR_FLAG;
\r
230 while( U2STAbits.URXDA )
\r
233 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
236 if( xHigherPriorityTaskWoken != pdFALSE )
\r
241 /*-----------------------------------------------------------*/
\r
243 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )
\r
246 portBASE_TYPE xTaskWoken = pdFALSE;
\r
248 /* If the transmit buffer is full we cannot get the next character.
\r
249 Another interrupt will occur the next time there is space so this does
\r
251 IFS1bits.U2TXIF = serCLEAR_FLAG;
\r
252 while( !( U2STAbits.UTXBF ) )
\r
254 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xTaskWoken ) == pdTRUE )
\r
256 /* Send the next character queued for Tx. */
\r
261 /* Queue empty, nothing to send. */
\r
262 xTxHasEnded = pdTRUE;
\r
267 if( xTaskWoken != pdFALSE )
\r