1 |
/* |
---|
2 |
FreeRTOS V5.4.1 - Copyright (C) 2009 Real Time Engineers Ltd. |
---|
3 |
|
---|
4 |
This file is part of the FreeRTOS distribution. |
---|
5 |
|
---|
6 |
FreeRTOS is free software; you can redistribute it and/or modify it under |
---|
7 |
the terms of the GNU General Public License (version 2) as published by the |
---|
8 |
Free Software Foundation and modified by the FreeRTOS exception. |
---|
9 |
**NOTE** The exception to the GPL is included to allow you to distribute a |
---|
10 |
combined work that includes FreeRTOS without being obliged to provide the |
---|
11 |
source code for proprietary components outside of the FreeRTOS kernel. |
---|
12 |
Alternative commercial license and support terms are also available upon |
---|
13 |
request. See the licensing section of http://www.FreeRTOS.org for full |
---|
14 |
license details. |
---|
15 |
|
---|
16 |
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT |
---|
17 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
18 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
---|
19 |
more details. |
---|
20 |
|
---|
21 |
You should have received a copy of the GNU General Public License along |
---|
22 |
with FreeRTOS; if not, write to the Free Software Foundation, Inc., 59 |
---|
23 |
Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
---|
24 |
|
---|
25 |
|
---|
26 |
*************************************************************************** |
---|
27 |
* * |
---|
28 |
* Looking for a quick start? Then check out the FreeRTOS eBook! * |
---|
29 |
* See http://www.FreeRTOS.org/Documentation for details * |
---|
30 |
* * |
---|
31 |
*************************************************************************** |
---|
32 |
|
---|
33 |
1 tab == 4 spaces! |
---|
34 |
|
---|
35 |
Please ensure to read the configuration and relevant port sections of the |
---|
36 |
online documentation. |
---|
37 |
|
---|
38 |
http://www.FreeRTOS.org - Documentation, latest information, license and |
---|
39 |
contact details. |
---|
40 |
|
---|
41 |
http://www.SafeRTOS.com - A version that is certified for use in safety |
---|
42 |
critical systems. |
---|
43 |
|
---|
44 |
http://www.OpenRTOS.com - Commercial support, development, porting, |
---|
45 |
licensing and training services. |
---|
46 |
*/ |
---|
47 |
|
---|
48 |
|
---|
49 |
/* |
---|
50 |
* This version of comtest. c is for use on systems that have limited stack |
---|
51 |
* space and no display facilities. The complete version can be found in |
---|
52 |
* the Demo/Common/Full directory. |
---|
53 |
* |
---|
54 |
* Creates two tasks that operate on an interrupt driven serial port. A |
---|
55 |
* loopback connector should be used so that everything that is transmitted is |
---|
56 |
* also received. The serial port does not use any flow control. On a |
---|
57 |
* standard 9way 'D' connector pins two and three should be connected together. |
---|
58 |
* |
---|
59 |
* The first task posts a sequence of characters to the Tx queue, toggling an |
---|
60 |
* LED on each successful post. At the end of the sequence it sleeps for a |
---|
61 |
* pseudo-random period before resending the same sequence. |
---|
62 |
* |
---|
63 |
* The UART Tx end interrupt is enabled whenever data is available in the Tx |
---|
64 |
* queue. The Tx end ISR removes a single character from the Tx queue and |
---|
65 |
* passes it to the UART for transmission. |
---|
66 |
* |
---|
67 |
* The second task blocks on the Rx queue waiting for a character to become |
---|
68 |
* available. When the UART Rx end interrupt receives a character it places |
---|
69 |
* it in the Rx queue, waking the second task. The second task checks that the |
---|
70 |
* characters removed from the Rx queue form the same sequence as those posted |
---|
71 |
* to the Tx queue, and toggles an LED for each correct character. |
---|
72 |
* |
---|
73 |
* The receiving task is spawned with a higher priority than the transmitting |
---|
74 |
* task. The receiver will therefore wake every time a character is |
---|
75 |
* transmitted so neither the Tx or Rx queue should ever hold more than a few |
---|
76 |
* characters. |
---|
77 |
* |
---|
78 |
*/ |
---|
79 |
|
---|
80 |
/* Scheduler include files. */ |
---|
81 |
#include <stdlib.h> |
---|
82 |
#include "FreeRTOS.h" |
---|
83 |
#include "task.h" |
---|
84 |
|
---|
85 |
/* Demo program include files. */ |
---|
86 |
#include "serial.h" |
---|
87 |
#include "comtest.h" |
---|
88 |
#include "partest.h" |
---|
89 |
|
---|
90 |
#define comSTACK_SIZE configMINIMAL_STACK_SIZE |
---|
91 |
#define comTX_LED_OFFSET ( 0 ) |
---|
92 |
#define comRX_LED_OFFSET ( 1 ) |
---|
93 |
#define comTOTAL_PERMISSIBLE_ERRORS ( 2 ) |
---|
94 |
|
---|
95 |
/* The Tx task will transmit the sequence of characters at a pseudo random |
---|
96 |
interval. This is the maximum and minimum block time between sends. */ |
---|
97 |
#define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 ) |
---|
98 |
#define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 ) |
---|
99 |
#define comOFFSET_TIME ( ( portTickType ) 3 ) |
---|
100 |
|
---|
101 |
/* We should find that each character can be queued for Tx immediately and we |
---|
102 |
don't have to block to send. */ |
---|
103 |
#define comNO_BLOCK ( ( portTickType ) 0 ) |
---|
104 |
|
---|
105 |
/* The Rx task will block on the Rx queue for a long period. */ |
---|
106 |
#define comRX_BLOCK_TIME ( ( portTickType ) 0xffff ) |
---|
107 |
|
---|
108 |
/* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */ |
---|
109 |
#define comFIRST_BYTE ( 'A' ) |
---|
110 |
#define comLAST_BYTE ( 'X' ) |
---|
111 |
|
---|
112 |
#define comBUFFER_LEN ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 ) |
---|
113 |
#define comINITIAL_RX_COUNT_VALUE ( 0 ) |
---|
114 |
|
---|
115 |
/* Handle to the com port used by both tasks. */ |
---|
116 |
static xComPortHandle xPort = NULL; |
---|
117 |
|
---|
118 |
/* The transmit task as described at the top of the file. */ |
---|
119 |
static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters ); |
---|
120 |
|
---|
121 |
/* The receive task as described at the top of the file. */ |
---|
122 |
static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters ); |
---|
123 |
|
---|
124 |
/* The LED that should be toggled by the Rx and Tx tasks. The Rx task will |
---|
125 |
toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED |
---|
126 |
( uxBaseLED + comTX_LED_OFFSET ). */ |
---|
127 |
static unsigned portBASE_TYPE uxBaseLED = 0; |
---|
128 |
|
---|
129 |
/* Check variable used to ensure no error have occurred. The Rx task will |
---|
130 |
increment this variable after every successfully received sequence. If at any |
---|
131 |
time the sequence is incorrect the the variable will stop being incremented. */ |
---|
132 |
static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE; |
---|
133 |
|
---|
134 |
/*-----------------------------------------------------------*/ |
---|
135 |
|
---|
136 |
void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned portLONG ulBaudRate, unsigned portBASE_TYPE uxLED ) |
---|
137 |
{ |
---|
138 |
/* Initialise the com port then spawn the Rx and Tx tasks. */ |
---|
139 |
uxBaseLED = uxLED; |
---|
140 |
xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN ); |
---|
141 |
|
---|
142 |
/* The Tx task is spawned with a lower priority than the Rx task. */ |
---|
143 |
xTaskCreate( vComTxTask, ( signed portCHAR * ) "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL ); |
---|
144 |
xTaskCreate( vComRxTask, ( signed portCHAR * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL ); |
---|
145 |
} |
---|
146 |
/*-----------------------------------------------------------*/ |
---|
147 |
|
---|
148 |
static portTASK_FUNCTION( vComTxTask, pvParameters ) |
---|
149 |
{ |
---|
150 |
signed portCHAR cByteToSend; |
---|
151 |
portTickType xTimeToWait; |
---|
152 |
|
---|
153 |
/* Just to stop compiler warnings. */ |
---|
154 |
( void ) pvParameters; |
---|
155 |
|
---|
156 |
for( ;; ) |
---|
157 |
{ |
---|
158 |
/* Simply transmit a sequence of characters from comFIRST_BYTE to |
---|
159 |
comLAST_BYTE. */ |
---|
160 |
for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ ) |
---|
161 |
{ |
---|
162 |
if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS ) |
---|
163 |
{ |
---|
164 |
vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET ); |
---|
165 |
} |
---|
166 |
} |
---|
167 |
|
---|
168 |
/* Turn the LED off while we are not doing anything. */ |
---|
169 |
vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE ); |
---|
170 |
|
---|
171 |
/* We have posted all the characters in the string - wait before |
---|
172 |
re-sending. Wait a pseudo-random time as this will provide a better |
---|
173 |
test. */ |
---|
174 |
xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME; |
---|
175 |
|
---|
176 |
/* Make sure we don't wait too long... */ |
---|
177 |
xTimeToWait %= comTX_MAX_BLOCK_TIME; |
---|
178 |
|
---|
179 |
/* ...but we do want to wait. */ |
---|
180 |
if( xTimeToWait < comTX_MIN_BLOCK_TIME ) |
---|
181 |
{ |
---|
182 |
xTimeToWait = comTX_MIN_BLOCK_TIME; |
---|
183 |
} |
---|
184 |
|
---|
185 |
vTaskDelay( xTimeToWait ); |
---|
186 |
} |
---|
187 |
} /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */ |
---|
188 |
/*-----------------------------------------------------------*/ |
---|
189 |
|
---|
190 |
static portTASK_FUNCTION( vComRxTask, pvParameters ) |
---|
191 |
{ |
---|
192 |
signed portCHAR cExpectedByte, cByteRxed; |
---|
193 |
portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE; |
---|
194 |
|
---|
195 |
/* Just to stop compiler warnings. */ |
---|
196 |
( void ) pvParameters; |
---|
197 |
|
---|
198 |
for( ;; ) |
---|
199 |
{ |
---|
200 |
/* We expect to receive the characters from comFIRST_BYTE to |
---|
201 |
comLAST_BYTE in an incrementing order. Loop to receive each byte. */ |
---|
202 |
for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ ) |
---|
203 |
{ |
---|
204 |
/* Block on the queue that contains received bytes until a byte is |
---|
205 |
available. */ |
---|
206 |
if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) ) |
---|
207 |
{ |
---|
208 |
/* Was this the byte we were expecting? If so, toggle the LED, |
---|
209 |
otherwise we are out on sync and should break out of the loop |
---|
210 |
until the expected character sequence is about to restart. */ |
---|
211 |
if( cByteRxed == cExpectedByte ) |
---|
212 |
{ |
---|
213 |
vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET ); |
---|
214 |
} |
---|
215 |
else |
---|
216 |
{ |
---|
217 |
xResyncRequired = pdTRUE; |
---|
218 |
break; /*lint !e960 Non-switch break allowed. */ |
---|
219 |
} |
---|
220 |
} |
---|
221 |
} |
---|
222 |
|
---|
223 |
/* Turn the LED off while we are not doing anything. */ |
---|
224 |
vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE ); |
---|
225 |
|
---|
226 |
/* Did we break out of the loop because the characters were received in |
---|
227 |
an unexpected order? If so wait here until the character sequence is |
---|
228 |
about to restart. */ |
---|
229 |
if( xResyncRequired == pdTRUE ) |
---|
230 |
{ |
---|
231 |
while( cByteRxed != comLAST_BYTE ) |
---|
232 |
{ |
---|
233 |
/* Block until the next char is available. */ |
---|
234 |
xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ); |
---|
235 |
} |
---|
236 |
|
---|
237 |
/* Note that an error occurred which caused us to have to resync. |
---|
238 |
We use this to stop incrementing the loop counter so |
---|
239 |
sAreComTestTasksStillRunning() will return false - indicating an |
---|
240 |
error. */ |
---|
241 |
xErrorOccurred++; |
---|
242 |
|
---|
243 |
/* We have now resynced with the Tx task and can continue. */ |
---|
244 |
xResyncRequired = pdFALSE; |
---|
245 |
} |
---|
246 |
else |
---|
247 |
{ |
---|
248 |
if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS ) |
---|
249 |
{ |
---|
250 |
/* Increment the count of successful loops. As error |
---|
251 |
occurring (i.e. an unexpected character being received) will |
---|
252 |
prevent this counter being incremented for the rest of the |
---|
253 |
execution. Don't worry about mutual exclusion on this |
---|
254 |
variable - it doesn't really matter as we just want it |
---|
255 |
to change. */ |
---|
256 |
uxRxLoops++; |
---|
257 |
} |
---|
258 |
} |
---|
259 |
} |
---|
260 |
} /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */ |
---|
261 |
/*-----------------------------------------------------------*/ |
---|
262 |
|
---|
263 |
portBASE_TYPE xAreComTestTasksStillRunning( void ) |
---|
264 |
{ |
---|
265 |
portBASE_TYPE xReturn; |
---|
266 |
|
---|
267 |
/* If the count of successful reception loops has not changed than at |
---|
268 |
some time an error occurred (i.e. a character was received out of sequence) |
---|
269 |
and we will return false. */ |
---|
270 |
if( uxRxLoops == comINITIAL_RX_COUNT_VALUE ) |
---|
271 |
{ |
---|
272 |
xReturn = pdFALSE; |
---|
273 |
} |
---|
274 |
else |
---|
275 |
{ |
---|
276 |
xReturn = pdTRUE; |
---|
277 |
} |
---|
278 |
|
---|
279 |
/* Reset the count of successful Rx loops. When this function is called |
---|
280 |
again we expect this to have been incremented. */ |
---|
281 |
uxRxLoops = comINITIAL_RX_COUNT_VALUE; |
---|
282 |
|
---|
283 |
return xReturn; |
---|
284 |
} |
---|
285 |
|
---|