1 |
577 |
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 |
|
|
Changes from V2.4.0
|
56 |
|
|
|
57 |
|
|
+ Made serial ISR handling more complete and robust.
|
58 |
|
|
|
59 |
|
|
Changes from V2.4.1
|
60 |
|
|
|
61 |
|
|
+ Split serial.c into serial.c and serialISR.c. serial.c can be
|
62 |
|
|
compiled using ARM or THUMB modes. serialISR.c must always be
|
63 |
|
|
compiled in ARM mode.
|
64 |
|
|
+ Another small change to cSerialPutChar().
|
65 |
|
|
|
66 |
|
|
Changed from V2.5.1
|
67 |
|
|
|
68 |
|
|
+ In cSerialPutChar() an extra check is made to ensure the post to
|
69 |
|
|
the queue was successful if then attempting to retrieve the posted
|
70 |
|
|
character.
|
71 |
|
|
|
72 |
|
|
*/
|
73 |
|
|
|
74 |
|
|
/*
|
75 |
|
|
BASIC INTERRUPT DRIVEN SERIAL PORT DRIVER FOR UART0.
|
76 |
|
|
|
77 |
|
|
This file contains all the serial port components that can be compiled to
|
78 |
|
|
either ARM or THUMB mode. Components that must be compiled to ARM mode are
|
79 |
|
|
contained in serialISR.c.
|
80 |
|
|
*/
|
81 |
|
|
|
82 |
|
|
/* Standard includes. */
|
83 |
|
|
#include <stdlib.h>
|
84 |
|
|
|
85 |
|
|
/* Scheduler includes. */
|
86 |
|
|
#include "FreeRTOS.h"
|
87 |
|
|
#include "queue.h"
|
88 |
|
|
#include "task.h"
|
89 |
|
|
|
90 |
|
|
/* Demo application includes. */
|
91 |
|
|
#include "serial.h"
|
92 |
|
|
|
93 |
|
|
/*-----------------------------------------------------------*/
|
94 |
|
|
|
95 |
|
|
/* Constants to setup and access the UART. */
|
96 |
|
|
#define serDLAB ( ( unsigned char ) 0x80 )
|
97 |
|
|
#define serENABLE_INTERRUPTS ( ( unsigned char ) 0x03 )
|
98 |
|
|
#define serNO_PARITY ( ( unsigned char ) 0x00 )
|
99 |
|
|
#define ser1_STOP_BIT ( ( unsigned char ) 0x00 )
|
100 |
|
|
#define ser8_BIT_CHARS ( ( unsigned char ) 0x03 )
|
101 |
|
|
#define serFIFO_ON ( ( unsigned char ) 0x01 )
|
102 |
|
|
#define serCLEAR_FIFO ( ( unsigned char ) 0x06 )
|
103 |
|
|
#define serWANTED_CLOCK_SCALING ( ( unsigned long ) 16 )
|
104 |
|
|
|
105 |
|
|
/* Constants to setup and access the VIC. */
|
106 |
|
|
#define serUART0_VIC_CHANNEL ( ( unsigned long ) 0x0006 )
|
107 |
|
|
#define serUART0_VIC_CHANNEL_BIT ( ( unsigned long ) 0x0040 )
|
108 |
|
|
#define serUART0_VIC_ENABLE ( ( unsigned long ) 0x0020 )
|
109 |
|
|
#define serCLEAR_VIC_INTERRUPT ( ( unsigned long ) 0 )
|
110 |
|
|
|
111 |
|
|
#define serINVALID_QUEUE ( ( xQueueHandle ) 0 )
|
112 |
|
|
#define serHANDLE ( ( xComPortHandle ) 1 )
|
113 |
|
|
#define serNO_BLOCK ( ( portTickType ) 0 )
|
114 |
|
|
|
115 |
|
|
/*-----------------------------------------------------------*/
|
116 |
|
|
|
117 |
|
|
/* Queues used to hold received characters, and characters waiting to be
|
118 |
|
|
transmitted. */
|
119 |
|
|
static xQueueHandle xRxedChars;
|
120 |
|
|
static xQueueHandle xCharsForTx;
|
121 |
|
|
|
122 |
|
|
/*-----------------------------------------------------------*/
|
123 |
|
|
|
124 |
|
|
/* Communication flag between the interrupt service routine and serial API. */
|
125 |
|
|
static volatile long *plTHREEmpty;
|
126 |
|
|
|
127 |
|
|
/*
|
128 |
|
|
* The queues are created in serialISR.c as they are used from the ISR.
|
129 |
|
|
* Obtain references to the queues and THRE Empty flag.
|
130 |
|
|
*/
|
131 |
|
|
extern void vSerialISRCreateQueues( unsigned portBASE_TYPE uxQueueLength, xQueueHandle *pxRxedChars, xQueueHandle *pxCharsForTx, long volatile **pplTHREEmptyFlag );
|
132 |
|
|
|
133 |
|
|
/*-----------------------------------------------------------*/
|
134 |
|
|
|
135 |
|
|
xComPortHandle xSerialPortInitMinimal( unsigned long ulWantedBaud, unsigned portBASE_TYPE uxQueueLength )
|
136 |
|
|
{
|
137 |
|
|
unsigned long ulDivisor, ulWantedClock;
|
138 |
|
|
xComPortHandle xReturn = serHANDLE;
|
139 |
|
|
extern void ( vUART_ISR_Wrapper )( void );
|
140 |
|
|
|
141 |
|
|
/* The queues are used in the serial ISR routine, so are created from
|
142 |
|
|
serialISR.c (which is always compiled to ARM mode. */
|
143 |
|
|
vSerialISRCreateQueues( uxQueueLength, &xRxedChars, &xCharsForTx, &plTHREEmpty );
|
144 |
|
|
|
145 |
|
|
if(
|
146 |
|
|
( xRxedChars != serINVALID_QUEUE ) &&
|
147 |
|
|
( xCharsForTx != serINVALID_QUEUE ) &&
|
148 |
|
|
( ulWantedBaud != ( unsigned long ) 0 )
|
149 |
|
|
)
|
150 |
|
|
{
|
151 |
|
|
portENTER_CRITICAL();
|
152 |
|
|
{
|
153 |
|
|
/* Setup the baud rate: Calculate the divisor value. */
|
154 |
|
|
ulWantedClock = ulWantedBaud * serWANTED_CLOCK_SCALING;
|
155 |
|
|
ulDivisor = configCPU_CLOCK_HZ / ulWantedClock;
|
156 |
|
|
|
157 |
|
|
/* Set the DLAB bit so we can access the divisor. */
|
158 |
|
|
UART0_LCR |= serDLAB;
|
159 |
|
|
|
160 |
|
|
/* Setup the divisor. */
|
161 |
|
|
UART0_DLL = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
|
162 |
|
|
ulDivisor >>= 8;
|
163 |
|
|
UART0_DLM = ( unsigned char ) ( ulDivisor & ( unsigned long ) 0xff );
|
164 |
|
|
|
165 |
|
|
/* Turn on the FIFO's and clear the buffers. */
|
166 |
|
|
UART0_FCR = ( serFIFO_ON | serCLEAR_FIFO );
|
167 |
|
|
|
168 |
|
|
/* Setup transmission format. */
|
169 |
|
|
UART0_LCR = serNO_PARITY | ser1_STOP_BIT | ser8_BIT_CHARS;
|
170 |
|
|
|
171 |
|
|
/* Setup the VIC for the UART. */
|
172 |
|
|
VICIntSelect &= ~( serUART0_VIC_CHANNEL_BIT );
|
173 |
|
|
VICIntEnable |= serUART0_VIC_CHANNEL_BIT;
|
174 |
|
|
VICVectAddr1 = ( long ) vUART_ISR_Wrapper;
|
175 |
|
|
VICVectCntl1 = serUART0_VIC_CHANNEL | serUART0_VIC_ENABLE;
|
176 |
|
|
|
177 |
|
|
/* Enable UART0 interrupts. */
|
178 |
|
|
UART0_IER |= serENABLE_INTERRUPTS;
|
179 |
|
|
}
|
180 |
|
|
portEXIT_CRITICAL();
|
181 |
|
|
}
|
182 |
|
|
else
|
183 |
|
|
{
|
184 |
|
|
xReturn = ( xComPortHandle ) 0;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
return xReturn;
|
188 |
|
|
}
|
189 |
|
|
/*-----------------------------------------------------------*/
|
190 |
|
|
|
191 |
|
|
signed portBASE_TYPE xSerialGetChar( xComPortHandle pxPort, signed char *pcRxedChar, portTickType xBlockTime )
|
192 |
|
|
{
|
193 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
194 |
|
|
( void ) pxPort;
|
195 |
|
|
|
196 |
|
|
/* Get the next character from the buffer. Return false if no characters
|
197 |
|
|
are available, or arrive before xBlockTime expires. */
|
198 |
|
|
if( xQueueReceive( xRxedChars, pcRxedChar, xBlockTime ) )
|
199 |
|
|
{
|
200 |
|
|
return pdTRUE;
|
201 |
|
|
}
|
202 |
|
|
else
|
203 |
|
|
{
|
204 |
|
|
return pdFALSE;
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
/*-----------------------------------------------------------*/
|
208 |
|
|
|
209 |
|
|
void vSerialPutString( xComPortHandle pxPort, const signed char * const pcString, unsigned short usStringLength )
|
210 |
|
|
{
|
211 |
|
|
signed char *pxNext;
|
212 |
|
|
|
213 |
|
|
/* NOTE: This implementation does not handle the queue being full as no
|
214 |
|
|
block time is used! */
|
215 |
|
|
|
216 |
|
|
/* The port handle is not required as this driver only supports UART0. */
|
217 |
|
|
( void ) pxPort;
|
218 |
|
|
( void ) usStringLength;
|
219 |
|
|
|
220 |
|
|
/* Send each character in the string, one at a time. */
|
221 |
|
|
pxNext = ( signed char * ) pcString;
|
222 |
|
|
while( *pxNext )
|
223 |
|
|
{
|
224 |
|
|
xSerialPutChar( pxPort, *pxNext, serNO_BLOCK );
|
225 |
|
|
pxNext++;
|
226 |
|
|
}
|
227 |
|
|
}
|
228 |
|
|
/*-----------------------------------------------------------*/
|
229 |
|
|
|
230 |
|
|
signed portBASE_TYPE xSerialPutChar( xComPortHandle pxPort, signed char cOutChar, portTickType xBlockTime )
|
231 |
|
|
{
|
232 |
|
|
signed portBASE_TYPE xReturn;
|
233 |
|
|
|
234 |
|
|
/* This demo driver only supports one port so the parameter is not used. */
|
235 |
|
|
( void ) pxPort;
|
236 |
|
|
|
237 |
|
|
portENTER_CRITICAL();
|
238 |
|
|
{
|
239 |
|
|
/* Is there space to write directly to the UART? */
|
240 |
|
|
if( *plTHREEmpty == ( long ) pdTRUE )
|
241 |
|
|
{
|
242 |
|
|
/* We wrote the character directly to the UART, so was
|
243 |
|
|
successful. */
|
244 |
|
|
*plTHREEmpty = pdFALSE;
|
245 |
|
|
UART0_THR = cOutChar;
|
246 |
|
|
xReturn = pdPASS;
|
247 |
|
|
}
|
248 |
|
|
else
|
249 |
|
|
{
|
250 |
|
|
/* We cannot write directly to the UART, so queue the character.
|
251 |
|
|
Block for a maximum of xBlockTime if there is no space in the
|
252 |
|
|
queue. */
|
253 |
|
|
xReturn = xQueueSend( xCharsForTx, &cOutChar, xBlockTime );
|
254 |
|
|
|
255 |
|
|
/* Depending on queue sizing and task prioritisation: While we
|
256 |
|
|
were blocked waiting to post interrupts were not disabled. It is
|
257 |
|
|
possible that the serial ISR has emptied the Tx queue, in which
|
258 |
|
|
case we need to start the Tx off again. */
|
259 |
|
|
if( ( *plTHREEmpty == ( long ) pdTRUE ) && ( xReturn == pdPASS ) )
|
260 |
|
|
{
|
261 |
|
|
xQueueReceive( xCharsForTx, &cOutChar, serNO_BLOCK );
|
262 |
|
|
*plTHREEmpty = pdFALSE;
|
263 |
|
|
UART0_THR = cOutChar;
|
264 |
|
|
}
|
265 |
|
|
}
|
266 |
|
|
}
|
267 |
|
|
portEXIT_CRITICAL();
|
268 |
|
|
|
269 |
|
|
return xReturn;
|
270 |
|
|
}
|
271 |
|
|
/*-----------------------------------------------------------*/
|
272 |
|
|
|
273 |
|
|
void vSerialClose( xComPortHandle xPort )
|
274 |
|
|
{
|
275 |
|
|
/* Not supported as not required by the demo application. */
|
276 |
|
|
( void ) xPort;
|
277 |
|
|
}
|
278 |
|
|
/*-----------------------------------------------------------*/
|
279 |
|
|
|
280 |
|
|
|
281 |
|
|
|
282 |
|
|
|
283 |
|
|
|
284 |
|
|
|