1 |
614 |
jeremybenn |
/*
|
2 |
|
|
FreeRTOS V6.1.1 - Copyright (C) 2011 Real Time Engineers Ltd.
|
3 |
|
|
|
4 |
|
|
***************************************************************************
|
5 |
|
|
* *
|
6 |
|
|
* If you are: *
|
7 |
|
|
* *
|
8 |
|
|
* + New to FreeRTOS, *
|
9 |
|
|
* + Wanting to learn FreeRTOS or multitasking in general quickly *
|
10 |
|
|
* + Looking for basic training, *
|
11 |
|
|
* + Wanting to improve your FreeRTOS skills and productivity *
|
12 |
|
|
* *
|
13 |
|
|
* then take a look at the FreeRTOS books - available as PDF or paperback *
|
14 |
|
|
* *
|
15 |
|
|
* "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
|
16 |
|
|
* http://www.FreeRTOS.org/Documentation *
|
17 |
|
|
* *
|
18 |
|
|
* A pdf reference manual is also available. Both are usually delivered *
|
19 |
|
|
* to your inbox within 20 minutes to two hours when purchased between 8am *
|
20 |
|
|
* and 8pm GMT (although please allow up to 24 hours in case of *
|
21 |
|
|
* exceptional circumstances). Thank you for your support! *
|
22 |
|
|
* *
|
23 |
|
|
***************************************************************************
|
24 |
|
|
|
25 |
|
|
This file is part of the FreeRTOS distribution.
|
26 |
|
|
|
27 |
|
|
FreeRTOS is free software; you can redistribute it and/or modify it under
|
28 |
|
|
the terms of the GNU General Public License (version 2) as published by the
|
29 |
|
|
Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
|
30 |
|
|
***NOTE*** The exception to the GPL is included to allow you to distribute
|
31 |
|
|
a combined work that includes FreeRTOS without being obliged to provide the
|
32 |
|
|
source code for proprietary components outside of the FreeRTOS kernel.
|
33 |
|
|
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
|
34 |
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
35 |
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
36 |
|
|
more details. You should have received a copy of the GNU General Public
|
37 |
|
|
License and the FreeRTOS license exception along with FreeRTOS; if not it
|
38 |
|
|
can be viewed here: http://www.freertos.org/a00114.html and also obtained
|
39 |
|
|
by writing to Richard Barry, contact details for whom are available on the
|
40 |
|
|
FreeRTOS WEB site.
|
41 |
|
|
|
42 |
|
|
1 tab == 4 spaces!
|
43 |
|
|
|
44 |
|
|
http://www.FreeRTOS.org - Documentation, latest information, license and
|
45 |
|
|
contact details.
|
46 |
|
|
|
47 |
|
|
http://www.SafeRTOS.com - A version that is certified for use in safety
|
48 |
|
|
critical systems.
|
49 |
|
|
|
50 |
|
|
http://www.OpenRTOS.com - Commercial support, development, porting,
|
51 |
|
|
licensing and training services.
|
52 |
|
|
*/
|
53 |
|
|
|
54 |
|
|
|
55 |
|
|
/* BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER.
|
56 |
|
|
|
57 |
|
|
NOTE: This driver is primarily to test the scheduler functionality. It does
|
58 |
|
|
not effectively use the buffers or DMA and is therefore not intended to be
|
59 |
|
|
an example of an efficient driver. */
|
60 |
|
|
|
61 |
|
|
/* Standard include file. */
|
62 |
|
|
#include <stdlib.h>
|
63 |
|
|
#include <plib.h>
|
64 |
|
|
|
65 |
|
|
/* Scheduler include files. */
|
66 |
|
|
#include "FreeRTOS.h"
|
67 |
|
|
#include "queue.h"
|
68 |
|
|
#include "task.h"
|
69 |
|
|
|
70 |
|
|
/* Demo app include files. */
|
71 |
|
|
#include "serial.h"
|
72 |
|
|
|
73 |
|
|
/* Hardware setup. */
|
74 |
|
|
#define serSET_FLAG ( 1 )
|
75 |
|
|
|
76 |
|
|
/* The queues used to communicate between tasks and ISR's. */
|
77 |
|
|
static xQueueHandle xRxedChars;
|
78 |
|
|
static xQueueHandle xCharsForTx;
|
79 |
|
|
|
80 |
|
|
/* Flag used to indicate the tx status. */
|
81 |
|
|
static portBASE_TYPE xTxHasEnded;
|
82 |
|
|
|
83 |
|
|
/*-----------------------------------------------------------*/
|
84 |
|
|
|
85 |
|
|
/* The UART interrupt handler. */
|
86 |
|
|
void __attribute__( (interrupt(ipl1), vector(_UART2_VECTOR))) vU2InterruptWrapper( void );
|
87 |
|
|
|
88 |
|
|
/*-----------------------------------------------------------*/
|
89 |
|
|
|
90 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
91 |
|
|
{
|
92 |
|
|
unsigned portSHORT usBRG;
|
93 |
|
|
|
94 |
|
|
/* Create the queues used by the com test task. */
|
95 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
96 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
97 |
|
|
|
98 |
|
|
/* Configure the UART and interrupts. */
|
99 |
|
|
usBRG = (unsigned portSHORT)(( (float)configPERIPHERAL_CLOCK_HZ / ( (float)16 * (float)ulWantedBaud ) ) - (float)0.5);
|
100 |
|
|
OpenUART2( UART_EN, UART_RX_ENABLE | UART_TX_ENABLE | UART_INT_TX | UART_INT_RX_CHAR, usBRG );
|
101 |
|
|
ConfigIntUART2( ( configKERNEL_INTERRUPT_PRIORITY + 1 ) | UART_INT_SUB_PR0 | UART_TX_INT_EN | UART_RX_INT_EN );
|
102 |
|
|
|
103 |
|
|
xTxHasEnded = pdTRUE;
|
104 |
|
|
|
105 |
|
|
/* Only a single port is implemented so we don't need to return anything. */
|
106 |
|
|
return NULL;
|
107 |
|
|
}
|
108 |
|
|
/*-----------------------------------------------------------*/
|
109 |
|
|
|
110 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
|
111 |
|
|
{
|
112 |
|
|
/* Only one port is supported. */
|
113 |
|
|
( void ) pxPort;
|
114 |
|
|
|
115 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
116 |
|
|
are available or arrive before xBlockTime expires. */
|
117 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
118 |
|
|
{
|
119 |
|
|
return pdTRUE;
|
120 |
|
|
}
|
121 |
|
|
else
|
122 |
|
|
{
|
123 |
|
|
return pdFALSE;
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
/*-----------------------------------------------------------*/
|
127 |
|
|
|
128 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
|
129 |
|
|
{
|
130 |
|
|
/* Only one port is supported. */
|
131 |
|
|
( void ) pxPort;
|
132 |
|
|
|
133 |
|
|
/* Return false if after the block time there is no room on the Tx queue. */
|
134 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
135 |
|
|
{
|
136 |
|
|
return pdFAIL;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/* A critical section should not be required as xTxHasEnded will not be
|
140 |
|
|
written to by the ISR if it is already 0 (is this correct?). */
|
141 |
|
|
if( xTxHasEnded )
|
142 |
|
|
{
|
143 |
|
|
xTxHasEnded = pdFALSE;
|
144 |
|
|
IFS1bits.U2TXIF = serSET_FLAG;
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
return pdPASS;
|
148 |
|
|
}
|
149 |
|
|
/*-----------------------------------------------------------*/
|
150 |
|
|
|
151 |
|
|
void vSerialClose( xComPortHandle xPort )
|
152 |
|
|
{
|
153 |
|
|
}
|
154 |
|
|
/*-----------------------------------------------------------*/
|
155 |
|
|
|
156 |
|
|
void vU2InterruptHandler( void )
|
157 |
|
|
{
|
158 |
|
|
/* Declared static to minimise stack use. */
|
159 |
|
|
static portCHAR cChar;
|
160 |
|
|
static portBASE_TYPE xHigherPriorityTaskWoken;
|
161 |
|
|
|
162 |
|
|
xHigherPriorityTaskWoken = pdFALSE;
|
163 |
|
|
|
164 |
|
|
/* Are any Rx interrupts pending? */
|
165 |
|
|
if( mU2RXGetIntFlag() )
|
166 |
|
|
{
|
167 |
|
|
while( U2STAbits.URXDA )
|
168 |
|
|
{
|
169 |
|
|
/* Retrieve the received character and place it in the queue of
|
170 |
|
|
received characters. */
|
171 |
|
|
cChar = U2RXREG;
|
172 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
173 |
|
|
}
|
174 |
|
|
mU2RXClearIntFlag();
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
/* Are any Tx interrupts pending? */
|
178 |
|
|
if( mU2TXGetIntFlag() )
|
179 |
|
|
{
|
180 |
|
|
while( !( U2STAbits.UTXBF ) )
|
181 |
|
|
{
|
182 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
183 |
|
|
{
|
184 |
|
|
/* Send the next character queued for Tx. */
|
185 |
|
|
U2TXREG = cChar;
|
186 |
|
|
}
|
187 |
|
|
else
|
188 |
|
|
{
|
189 |
|
|
/* Queue empty, nothing to send. */
|
190 |
|
|
xTxHasEnded = pdTRUE;
|
191 |
|
|
break;
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
mU2TXClearIntFlag();
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
/* If sending or receiving necessitates a context switch, then switch now. */
|
199 |
|
|
portEND_SWITCHING_ISR( xHigherPriorityTaskWoken );
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
|
203 |
|
|
|
204 |
|
|
|
205 |
|
|
|
206 |
|
|
|
207 |
|
|
|