2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 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 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 /* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
\r
99 NOTE: This driver is primarily to test the scheduler functionality. It does
\r
100 not effectively use the buffers or DMA and is therefore not intended to be
\r
101 an example of an efficient driver. */
\r
103 /* Standard include file. */
\r
104 #include <stdlib.h>
\r
106 /* Scheduler include files. */
\r
107 #include "FreeRTOS.h"
\r
111 /* Demo app include files. */
\r
112 #include "serial.h"
\r
114 /* Hardware setup. */
\r
115 #define serOUTPUT 0
\r
117 #define serLOW_SPEED 0
\r
118 #define serONE_STOP_BIT 0
\r
119 #define serEIGHT_DATA_BITS_NO_PARITY 0
\r
120 #define serNORMAL_IDLE_STATE 0
\r
121 #define serAUTO_BAUD_OFF 0
\r
122 #define serLOOPBACK_OFF 0
\r
123 #define serWAKE_UP_DISABLE 0
\r
124 #define serNO_HARDWARE_FLOW_CONTROL 0
\r
125 #define serSTANDARD_IO 0
\r
126 #define serNO_IRDA 0
\r
127 #define serCONTINUE_IN_IDLE_MODE 0
\r
128 #define serUART_ENABLED 1
\r
129 #define serINTERRUPT_ON_SINGLE_CHAR 0
\r
130 #define serTX_ENABLE 1
\r
131 #define serINTERRUPT_ENABLE 1
\r
132 #define serINTERRUPT_DISABLE 0
\r
133 #define serCLEAR_FLAG 0
\r
134 #define serSET_FLAG 1
\r
137 /* The queues used to communicate between tasks and ISR's. */
\r
138 static QueueHandle_t xRxedChars;
\r
139 static QueueHandle_t xCharsForTx;
\r
141 static portBASE_TYPE xTxHasEnded;
\r
142 /*-----------------------------------------------------------*/
\r
144 xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
\r
148 /* Create the queues used by the com test task. */
\r
149 xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
150 xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed char ) );
\r
152 /* Setup the UART. */
\r
153 U2MODEbits.BRGH = serLOW_SPEED;
\r
154 U2MODEbits.STSEL = serONE_STOP_BIT;
\r
155 U2MODEbits.PDSEL = serEIGHT_DATA_BITS_NO_PARITY;
\r
156 U2MODEbits.RXINV = serNORMAL_IDLE_STATE;
\r
157 U2MODEbits.ABAUD = serAUTO_BAUD_OFF;
\r
158 U2MODEbits.LPBACK = serLOOPBACK_OFF;
\r
159 U2MODEbits.WAKE = serWAKE_UP_DISABLE;
\r
160 U2MODEbits.UEN = serNO_HARDWARE_FLOW_CONTROL;
\r
161 U2MODEbits.IREN = serNO_IRDA;
\r
162 U2MODEbits.USIDL = serCONTINUE_IN_IDLE_MODE;
\r
163 U2MODEbits.UARTEN = serUART_ENABLED;
\r
165 U2BRG = (unsigned short)(( (float)configCPU_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
\r
167 U2STAbits.URXISEL = serINTERRUPT_ON_SINGLE_CHAR;
\r
168 U2STAbits.UTXEN = serTX_ENABLE;
\r
169 U2STAbits.UTXINV = serNORMAL_IDLE_STATE;
\r
170 U2STAbits.UTXISEL0 = serINTERRUPT_ON_SINGLE_CHAR;
\r
171 U2STAbits.UTXISEL1 = serINTERRUPT_ON_SINGLE_CHAR;
\r
173 /* It is assumed that this function is called prior to the scheduler being
\r
174 started. Therefore interrupts must not be allowed to occur yet as they
\r
175 may attempt to perform a context switch. */
\r
176 portDISABLE_INTERRUPTS();
\r
178 IFS1bits.U2RXIF = serCLEAR_FLAG;
\r
179 IFS1bits.U2TXIF = serCLEAR_FLAG;
\r
180 IPC7bits.U2RXIP = configKERNEL_INTERRUPT_PRIORITY;
\r
181 IPC7bits.U2TXIP = configKERNEL_INTERRUPT_PRIORITY;
\r
182 IEC1bits.U2TXIE = serINTERRUPT_ENABLE;
\r
183 IEC1bits.U2RXIE = serINTERRUPT_ENABLE;
\r
185 /* Clear the Rx buffer. */
\r
186 while( U2STAbits.URXDA == serSET_FLAG )
\r
191 xTxHasEnded = pdTRUE;
\r
195 /*-----------------------------------------------------------*/
\r
197 signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, TickType_t xBlockTime )
\r
199 /* Only one port is supported. */
\r
202 /* Get the next character from the buffer. Return false if no characters
\r
203 are available or arrive before xBlockTime expires. */
\r
204 if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
\r
213 /*-----------------------------------------------------------*/
\r
215 signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, TickType_t xBlockTime )
\r
217 /* Only one port is supported. */
\r
220 /* Return false if after the block time there is no room on the Tx queue. */
\r
221 if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
\r
226 /* A critical section should not be required as xTxHasEnded will not be
\r
227 written to by the ISR if it is already 0 (is this correct?). */
\r
230 xTxHasEnded = pdFALSE;
\r
231 IFS1bits.U2TXIF = serSET_FLAG;
\r
236 /*-----------------------------------------------------------*/
\r
238 void vSerialClose( xComPortHandle xPort )
\r
240 /* Note implemented. */
\r
243 /*-----------------------------------------------------------*/
\r
245 void __attribute__((__interrupt__, auto_psv)) _U2RXInterrupt( void )
\r
248 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
250 /* Get the character and post it on the queue of Rxed characters.
\r
251 If the post causes a task to wake force a context switch as the woken task
\r
252 may have a higher priority than the task we have interrupted. */
\r
253 IFS1bits.U2RXIF = serCLEAR_FLAG;
\r
254 while( U2STAbits.URXDA )
\r
257 xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
\r
260 if( xHigherPriorityTaskWoken != pdFALSE )
\r
265 /*-----------------------------------------------------------*/
\r
267 void __attribute__((__interrupt__, auto_psv)) _U2TXInterrupt( void )
\r
270 portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
\r
272 /* If the transmit buffer is full we cannot get the next character.
\r
273 Another interrupt will occur the next time there is space so this does
\r
275 IFS1bits.U2TXIF = serCLEAR_FLAG;
\r
276 while( !( U2STAbits.UTXBF ) )
\r
278 if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
\r
280 /* Send the next character queued for Tx. */
\r
285 /* Queue empty, nothing to send. */
\r
286 xTxHasEnded = pdTRUE;
\r
291 if( xHigherPriorityTaskWoken != pdFALSE )
\r