2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
71 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
73 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
74 not effectively use the buffers or DMA and is therefore not intended to be
\r
75 an example of an efficient driver. */
\r
77 /* Standard include file. */
\r
81 /* Scheduler include files. */
\r
82 #include "FreeRTOS.h"
\r
86 /* Demo app include files. */
\r
89 /* Hardware setup. */
\r
90 #define serSET_FLAG ( 1 )
\r
92 /* The queues used to communicate between tasks and ISR's. */
\r
93 static QueueHandle_t xRxedChars;
\r
94 static QueueHandle_t xCharsForTx;
\r
96 /* Flag used to indicate the tx status. */
\r
97 static volatile portBASE_TYPE xTxHasEnded;
\r
99 /*-----------------------------------------------------------*/
\r
101 /* The UART interrupt handler. As this uses the FreeRTOS assembly interrupt
\r
102 entry point the IPL setting in the following prototype has no effect. The
\r
103 interrupt priority is set by the call to ConfigIntUART2() in
\r
104 xSerialPortInitMinimal(). */
\r
105 void __attribute__( (interrupt(IPL0AUTO), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );
\r
107 /*-----------------------------------------------------------*/
\r
109 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
111 unsigned short usBRG;
\r
113 /* Create the queues used by the com test task. */
\r
114 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
115 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
117 /* Configure the UART and interrupts. */
\r
118 usBRG = (unsigned short)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
119 OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
\r
120 ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );
\r
122 xTxHasEnded = pdTRUE;
\r
124 /* Only a single port is implemented so we don't need to return anything. */
\r
127 /*-----------------------------------------------------------*/
\r
129 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
131 /* Only one port is supported. */
\r
134 /* Get the next character from the buffer. Return false if no characters
\r
135 are available or arrive before xBlockTime expires. */
\r
136 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
145 /*-----------------------------------------------------------*/
\r
147 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
149 signed portBASE_TYPE xReturn;
\r
151 /* Only one port is supported. */
\r
154 /* Return false if after the block time there is no room on the Tx queue. */
\r
155 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
164 if( xReturn != pdFAIL )
\r
166 /* A critical section should not be required as xTxHasEnded will not be
\r
167 written to by the ISR if it is already 0. */
\r
168 if( xTxHasEnded == pdTRUE )
\r
170 xTxHasEnded = pdFALSE;
\r
171 IFS1SET = _IFS1_U2TXIF_MASK;
\r
177 /*-----------------------------------------------------------*/
\r
179 void vSerialClose( xComPortHandle xPort )
\r
182 /*-----------------------------------------------------------*/
\r
184 void vU2InterruptHandler( void )
\r
186 /* Declared static to minimise stack use. */
\r
188 static portBASE_TYPE xHigherPriorityTaskWoken;
\r
190 xHigherPriorityTaskWoken = pdFALSE;
\r
192 /* Are any Rx interrupts pending? */
\r
193 if( IFS1bits.U2RXIF == 1)
\r
195 while( U2STAbits.URXDA )
\r
197 /* Retrieve the received character and place it in the queue of
\r
198 received characters. */
\r
200 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
202 IFS1CLR = _IFS1_U2RXIF_MASK;
\r
205 /* Are any Tx interrupts pending? */
\r
206 if( IFS1bits.U2TXIF == 1 )
\r
208 while( ( U2STAbits.UTXBF ) == 0 )
\r
210 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
212 /* Send the next character queued for Tx. */
\r
217 /* Queue empty, nothing to send. */
\r
218 xTxHasEnded = pdTRUE;
\r
223 IFS1CLR = _IFS1_U2TXIF_MASK;
\r
226 /* If sending or receiving necessitates a context switch, then switch now. */
\r
227 portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
\r