1 |
584 |
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 FOR UART0.
|
56 |
|
|
|
57 |
|
|
*NOTE* - This file is designed to test some of the RTOS features - it is
|
58 |
|
|
not intended to represent an efficient implementation!
|
59 |
|
|
*/
|
60 |
|
|
|
61 |
|
|
/* Standard includes. */
|
62 |
|
|
#include <stdlib.h>
|
63 |
|
|
|
64 |
|
|
/* Scheduler includes. */
|
65 |
|
|
#include "FreeRTOS.h"
|
66 |
|
|
#include "queue.h"
|
67 |
|
|
|
68 |
|
|
/* Demo application includes. */
|
69 |
|
|
#include "serial.h"
|
70 |
|
|
|
71 |
|
|
|
72 |
|
|
/* Hardware specifics. */
|
73 |
|
|
#define serRX_DATA_PIN ( 0x01 )
|
74 |
|
|
#define serTX_DATA_PIN ( 0x02 )
|
75 |
|
|
#define serCLOCK_Fxx_DIV_8 0x03
|
76 |
|
|
#define serUPWR ( 0x80 )
|
77 |
|
|
#define serUTXE ( 0x40 )
|
78 |
|
|
#define serURXE ( 0x20 )
|
79 |
|
|
#define serUCL ( 0x02 )
|
80 |
|
|
#define serLSB ( 0x10 )
|
81 |
|
|
|
82 |
|
|
/* Misc. */
|
83 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
84 |
|
|
#define serHANDLE ( ( xComPortHandle ) 1 )
|
85 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
86 |
|
|
|
87 |
|
|
/*-----------------------------------------------------------*/
|
88 |
|
|
|
89 |
|
|
/* Queues used to hold received characters, and characters waiting to be
|
90 |
|
|
transmitted. */
|
91 |
|
|
static xQueueHandle xRxedChars;
|
92 |
|
|
static xQueueHandle xCharsForTx;
|
93 |
|
|
|
94 |
|
|
/*-----------------------------------------------------------*/
|
95 |
|
|
|
96 |
|
|
/* Interrupt entry point written in the assembler file serialISR.s85. */
|
97 |
|
|
extern void vSerialISREntry( void );
|
98 |
|
|
|
99 |
|
|
/* Flag to indicate whether or not there are characters already queued to send. */
|
100 |
|
|
static volatile unsigned long ulTxInProgress = pdFALSE;
|
101 |
|
|
|
102 |
|
|
/*-----------------------------------------------------------*/
|
103 |
|
|
|
104 |
|
|
/*
|
105 |
|
|
* See the serial2.h header file.
|
106 |
|
|
*/
|
107 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned portLONG ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
108 |
|
|
{
|
109 |
|
|
xComPortHandle xReturn = serHANDLE;
|
110 |
|
|
const unsigned portLONG ulFuclk = ( configCPU_CLOCK_HZ / 2 ) / 8UL;
|
111 |
|
|
|
112 |
|
|
/* Create the queues used to hold Rx and Tx characters. */
|
113 |
|
|
xRxedChars = xQueueCreate( uxQueueLength, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
114 |
|
|
xCharsForTx = xQueueCreate( uxQueueLength + 1, ( unsigned portBASE_TYPE ) sizeof( signed portCHAR ) );
|
115 |
|
|
|
116 |
|
|
/* If the queues were created correctly then setup the serial port
|
117 |
|
|
hardware. */
|
118 |
|
|
if( ( xRxedChars != serINVALID_QUEUE ) && ( xCharsForTx != serINVALID_QUEUE ) )
|
119 |
|
|
{
|
120 |
|
|
portENTER_CRITICAL();
|
121 |
|
|
{
|
122 |
|
|
/* Set the UART0 Rx and Tx pins to their alternative function. */
|
123 |
|
|
PMC3 |= ( serRX_DATA_PIN | serTX_DATA_PIN );
|
124 |
|
|
PM3 &= ~( serTX_DATA_PIN );
|
125 |
|
|
|
126 |
|
|
/* Setup clock for required baud. */
|
127 |
|
|
UD0CTL1 = serCLOCK_Fxx_DIV_8;
|
128 |
|
|
UD0CTL2 = ulFuclk / ( 2 * ulWantedBaud );
|
129 |
|
|
|
130 |
|
|
/* Enable, n81. */
|
131 |
|
|
UD0CTL0 = ( serUPWR | serUTXE | serURXE | serUCL | serLSB );
|
132 |
|
|
|
133 |
|
|
/* Enable interrupts for both Rx and Tx. */
|
134 |
|
|
UD0TIC = 0x07;
|
135 |
|
|
UD0RIC = 0x07;
|
136 |
|
|
|
137 |
|
|
ulTxInProgress = pdFALSE;
|
138 |
|
|
}
|
139 |
|
|
portEXIT_CRITICAL();
|
140 |
|
|
}
|
141 |
|
|
else
|
142 |
|
|
{
|
143 |
|
|
xReturn = ( xComPortHandle ) 0;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
/* This demo file only supports a single port but we have to return
|
147 |
|
|
something to comply with the standard demo header file. */
|
148 |
|
|
return xReturn;
|
149 |
|
|
}
|
150 |
|
|
/*-----------------------------------------------------------*/
|
151 |
|
|
|
152 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed portCHAR *pcRxedChar, portTickType xBlockTime )
|
153 |
|
|
{
|
154 |
|
|
/* The port handle is not required as this driver only supports one port. */
|
155 |
|
|
( void ) pxPort;
|
156 |
|
|
|
157 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
158 |
|
|
are available, or arrive before xBlockTime expires. */
|
159 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
160 |
|
|
{
|
161 |
|
|
return pdTRUE;
|
162 |
|
|
}
|
163 |
|
|
else
|
164 |
|
|
{
|
165 |
|
|
return pdFALSE;
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
/*-----------------------------------------------------------*/
|
169 |
|
|
|
170 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed portCHAR * const pcString, unsigned portSHORT usStringLength )
|
171 |
|
|
{
|
172 |
|
|
signed portCHAR *pxNext;
|
173 |
|
|
|
174 |
|
|
/* A couple of parameters that this port does not use. */
|
175 |
|
|
( void ) usStringLength;
|
176 |
|
|
( void ) pxPort;
|
177 |
|
|
|
178 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
179 |
|
|
block time is used! */
|
180 |
|
|
|
181 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
182 |
|
|
( void ) pxPort;
|
183 |
|
|
|
184 |
|
|
/* Send each character in the string, one at a time. */
|
185 |
|
|
pxNext = ( signed portCHAR * ) pcString;
|
186 |
|
|
while( *pxNext )
|
187 |
|
|
{
|
188 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
189 |
|
|
pxNext++;
|
190 |
|
|
}
|
191 |
|
|
}
|
192 |
|
|
/*-----------------------------------------------------------*/
|
193 |
|
|
|
194 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed portCHAR cOutChar, portTickType xBlockTime )
|
195 |
|
|
{
|
196 |
|
|
portBASE_TYPE xReturn = pdPASS;
|
197 |
|
|
|
198 |
|
|
portENTER_CRITICAL();
|
199 |
|
|
{
|
200 |
|
|
/* There are currently no characters queued up to send so write the
|
201 |
|
|
character directly to the UART. */
|
202 |
|
|
if( ulTxInProgress == pdFALSE )
|
203 |
|
|
{
|
204 |
|
|
UD0TX = cOutChar;
|
205 |
|
|
ulTxInProgress = pdTRUE;
|
206 |
|
|
}
|
207 |
|
|
else
|
208 |
|
|
{
|
209 |
|
|
/* The UART is already busy so write the character to the Tx queue.
|
210 |
|
|
The queue is drained from within the Tx interrupt. */
|
211 |
|
|
if( xQueueSend( xCharsForTx, &cOutChar, xBlockTime ) != pdPASS )
|
212 |
|
|
{
|
213 |
|
|
xReturn = pdFAIL;
|
214 |
|
|
}
|
215 |
|
|
}
|
216 |
|
|
}
|
217 |
|
|
portEXIT_CRITICAL();
|
218 |
|
|
|
219 |
|
|
return xReturn;
|
220 |
|
|
}
|
221 |
|
|
/*-----------------------------------------------------------*/
|
222 |
|
|
|
223 |
|
|
void vSerialClose( xComPortHandle xPort )
|
224 |
|
|
{
|
225 |
|
|
/* Not supported as not required by the demo application. */
|
226 |
|
|
}
|
227 |
|
|
/*-----------------------------------------------------------*/
|
228 |
|
|
|
229 |
|
|
/* Tx interrupt handler. This is called from the asm file wrapper. */
|
230 |
|
|
void vUARTTxISRHandler( void )
|
231 |
|
|
{
|
232 |
|
|
char cChar;
|
233 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
234 |
|
|
|
235 |
|
|
/* Are there any more characters queue to transmit? */
|
236 |
|
|
if( xQueueReceiveFromISR( xCharsForTx, &cChar, &xHigherPriorityTaskWoken ) == pdTRUE )
|
237 |
|
|
{
|
238 |
|
|
/* Send the next character. */
|
239 |
|
|
UD0TX = cChar;
|
240 |
|
|
}
|
241 |
|
|
else
|
242 |
|
|
{
|
243 |
|
|
/* The UART is no longer active. */
|
244 |
|
|
ulTxInProgress = pdFALSE;
|
245 |
|
|
}
|
246 |
|
|
|
247 |
|
|
/* If reading a character from the Rx queue caused a task to unblock, and
|
248 |
|
|
the unblocked task has a priority higher than the currently running task,
|
249 |
|
|
then xHigherPriorityTaskWoken will have been set to true and a context
|
250 |
|
|
switch should occur now. */
|
251 |
|
|
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
252 |
|
|
}
|
253 |
|
|
/*-----------------------------------------------------------*/
|
254 |
|
|
|
255 |
|
|
/* Rx interrupt handler. This is called from the asm file wrapper. */
|
256 |
|
|
void vUARTRxISRHandler( void )
|
257 |
|
|
{
|
258 |
|
|
portCHAR cChar;
|
259 |
|
|
portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;
|
260 |
|
|
|
261 |
|
|
/* Send the received character to the Rx queue. */
|
262 |
|
|
cChar = UD0RX;
|
263 |
|
|
xQueueSendFromISR( xRxedChars, &cChar, &xHigherPriorityTaskWoken );
|
264 |
|
|
|
265 |
|
|
/* If sending a character to the Tx queue caused a task to unblock, and
|
266 |
|
|
the unblocked task has a priority higher than the currently running task,
|
267 |
|
|
then xHigherPriorityTaskWoken will have been set to true and a context
|
268 |
|
|
switch should occur now. */
|
269 |
|
|
portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
|
275 |
|
|
|