2 FreeRTOS V7.5.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
68 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
69 not effectively use the buffers or DMA and is therefore not intended to be
\r
70 an example of an efficient driver. */
\r
72 /* Standard include file. */
\r
76 /* Scheduler include files. */
\r
77 #include "FreeRTOS.h"
\r
81 /* Demo app include files. */
\r
84 /* Hardware setup. */
\r
85 #define serSET_FLAG ( 1 )
\r
87 /* The queues used to communicate between tasks and ISR's. */
\r
88 static xQueueHandle xRxedChars;
\r
89 static xQueueHandle xCharsForTx;
\r
91 /* Flag used to indicate the tx status. */
\r
92 static volatile portBASE_TYPE xTxHasEnded;
\r
94 /*-----------------------------------------------------------*/
\r
96 /* The UART interrupt handler. As this uses the FreeRTOS assembly interrupt
\r
97 entry point the IPL setting in the following prototype has no effect. The
\r
98 interrupt priority is set by the call to ConfigIntUART2() in
\r
99 xSerialPortInitMinimal(). */
\r
100 void __attribute__( (interrupt(ipl0), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );
\r
102 /*-----------------------------------------------------------*/
\r
104 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
106 unsigned short usBRG;
\r
108 /* Create the queues used by the com test task. */
\r
109 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
110 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
112 /* Configure the UART and interrupts. */
\r
113 usBRG = (unsigned short)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
114 OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
\r
115 ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );
\r
117 xTxHasEnded = pdTRUE;
\r
119 /* Only a single port is implemented so we don't need to return anything. */
\r
122 /*-----------------------------------------------------------*/
\r
124 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
\r
126 /* Only one port is supported. */
\r
129 /* Get the next character from the buffer. Return false if no characters
\r
130 are available or arrive before xBlockTime expires. */
\r
131 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
140 /*-----------------------------------------------------------*/
\r
142 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
\r
144 signed portBASE_TYPE xReturn;
\r
146 /* Only one port is supported. */
\r
149 /* Return false if after the block time there is no room on the Tx queue. */
\r
150 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
159 if( xReturn != pdFAIL )
\r
161 /* A critical section should not be required as xTxHasEnded will not be
\r
162 written to by the ISR if it is already 0. */
\r
163 if( xTxHasEnded == pdTRUE )
\r
165 xTxHasEnded = pdFALSE;
\r
166 IFS1SET = _IFS1_U2TXIF_MASK;
\r
172 /*-----------------------------------------------------------*/
\r
174 void vSerialClose( xComPortHandle xPort )
\r
177 /*-----------------------------------------------------------*/
\r
179 void vU2InterruptHandler( void )
\r
181 /* Declared static to minimise stack use. */
\r
183 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
185 xHigherPriorityTaskWoken = pdFALSE;
\r
187 /* Are any Rx interrupts pending? */
\r
188 if( IFS1bits.U2RXIF == 1)
\r
190 while( U2STAbits.URXDA )
\r
192 /* Retrieve the received character and place it in the queue of
\r
193 received characters. */
\r
195 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
197 IFS1CLR = _IFS1_U2RXIF_MASK;
\r
200 /* Are any Tx interrupts pending? */
\r
201 if( IFS1bits.U2TXIF == 1 )
\r
203 while( ( U2STAbits.UTXBF ) == 0 )
\r
205 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
207 /* Send the next character queued for Tx. */
\r
212 /* Queue empty, nothing to send. */
\r
213 xTxHasEnded = pdTRUE;
\r
218 IFS1CLR = _IFS1_U2TXIF_MASK;
\r
221 /* If sending or receiving necessitates a context switch, then switch now. */
\r
222 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r