2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
69 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
70 not effectively use the buffers or DMA and is therefore not intended to be
\r
71 an example of an efficient driver. */
\r
73 /* Standard include file. */
\r
77 /* Scheduler include files. */
\r
78 #include "FreeRTOS.h"
\r
82 /* Demo app include files. */
\r
85 /* Hardware setup. */
\r
86 #define serSET_FLAG ( 1 )
\r
88 /* The queues used to communicate between tasks and ISR's. */
\r
89 static xQueueHandle xRxedChars;
\r
90 static xQueueHandle xCharsForTx;
\r
92 /* Flag used to indicate the tx status. */
\r
93 static volatile portBASE_TYPE xTxHasEnded;
\r
95 /*-----------------------------------------------------------*/
\r
97 /* The UART interrupt handler. As this uses the FreeRTOS assembly interrupt
\r
98 entry point the IPL setting in the following prototype has no effect. The
\r
99 interrupt priority is set by the call to ConfigIntUART2() in
\r
100 xSerialPortInitMinimal(). */
\r
101 void __attribute__( (interrupt(ipl0), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );
\r
103 /*-----------------------------------------------------------*/
\r
105 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
107 unsigned short usBRG;
\r
109 /* Create the queues used by the com test task. */
\r
110 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
111 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
113 /* Configure the UART and interrupts. */
\r
114 usBRG = (unsigned short)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
115 OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
\r
116 ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );
\r
118 xTxHasEnded = pdTRUE;
\r
120 /* Only a single port is implemented so we don't need to return anything. */
\r
123 /*-----------------------------------------------------------*/
\r
125 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
127 /* Only one port is supported. */
\r
130 /* Get the next character from the buffer. Return false if no characters
\r
131 are available or arrive before xBlockTime expires. */
\r
132 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
141 /*-----------------------------------------------------------*/
\r
143 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
145 signed portBASE_TYPE xReturn;
\r
147 /* Only one port is supported. */
\r
150 /* Return false if after the block time there is no room on the Tx queue. */
\r
151 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
160 if( xReturn != pdFAIL )
\r
162 /* A critical section should not be required as xTxHasEnded will not be
\r
163 written to by the ISR if it is already 0. */
\r
164 if( xTxHasEnded == pdTRUE )
\r
166 xTxHasEnded = pdFALSE;
\r
167 IFS1SET = _IFS1_U2TXIF_MASK;
\r
173 /*-----------------------------------------------------------*/
\r
175 void vSerialClose( xComPortHandle xPort )
\r
178 /*-----------------------------------------------------------*/
\r
180 void vU2InterruptHandler( void )
\r
182 /* Declared static to minimise stack use. */
\r
184 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
186 xHigherPriorityTaskWoken = pdFALSE;
\r
188 /* Are any Rx interrupts pending? */
\r
189 if( IFS1bits.U2RXIF == 1)
\r
191 while( U2STAbits.URXDA )
\r
193 /* Retrieve the received character and place it in the queue of
\r
194 received characters. */
\r
196 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
198 IFS1CLR = _IFS1_U2RXIF_MASK;
\r
201 /* Are any Tx interrupts pending? */
\r
202 if( IFS1bits.U2TXIF == 1 )
\r
204 while( ( U2STAbits.UTXBF ) == 0 )
\r
206 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
208 /* Send the next character queued for Tx. */
\r
213 /* Queue empty, nothing to send. */
\r
214 xTxHasEnded = pdTRUE;
\r
219 IFS1CLR = _IFS1_U2TXIF_MASK;
\r
222 /* If sending or receiving necessitates a context switch, then switch now. */
\r
223 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r