2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
67 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
68 * connector should be used so that everything that is transmitted is also received.
\r
69 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
70 * pins two and three should be connected together.
\r
72 * The first task repeatedly sends a string to a queue, character at a time. The
\r
73 * serial port interrupt will empty the queue and transmit the characters. The
\r
74 * task blocks for a pseudo random period before resending the string.
\r
76 * The second task blocks on a queue waiting for a character to be received.
\r
77 * Characters received by the serial port interrupt routine are posted onto the
\r
78 * queue - unblocking the task making it ready to execute. If this is then the
\r
79 * highest priority task ready to run it will run immediately - with a context
\r
80 * switch occurring at the end of the interrupt service routine. The task
\r
81 * receiving characters is spawned with a higher priority than the task
\r
82 * transmitting the characters.
\r
84 * With the loop back connector in place, one task will transmit a string and the
\r
85 * other will immediately receive it. The receiving task knows the string it
\r
86 * expects to receive so can detect an error.
\r
88 * This also creates a third task. This is used to test semaphore usage from an
\r
89 * ISR and does nothing interesting.
\r
91 * \page ComTestC comtest.c
\r
92 * \ingroup DemoFiles
\r
99 + The priority of the Rx task has been lowered. Received characters are
\r
100 now processed (read from the queue) at the idle priority, allowing low
\r
101 priority tasks to run evenly at times of a high communications overhead.
\r
103 Changes from V1.01:
\r
105 + The Tx task now waits a pseudo random time between transissions.
\r
106 Previously a fixed period was used but this was not such a good test as
\r
107 interrupts fired at regular intervals.
\r
109 Changes From V1.2.0:
\r
111 + Use vSerialPutString() instead of single character puts.
\r
112 + Only stop the check variable incrementing after two consecutive errors.
\r
114 Changed from V1.2.5
\r
116 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
117 only 1. This is done to tie in better with the other demo application
\r
120 Changes from V2.0.0
\r
122 + Delay periods are now specified using variables and constants of
\r
123 portTickType rather than unsigned long.
\r
124 + Slight modification to task priorities.
\r
129 /* Scheduler include files. */
\r
130 #include <stdlib.h>
\r
131 #include <string.h>
\r
132 #include "FreeRTOS.h"
\r
135 /* Demo program include files. */
\r
136 #include "serial.h"
\r
137 #include "comtest.h"
\r
140 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
141 interval. This is the maximum and minimum block time between sends. */
\r
142 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
143 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
145 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
147 #define comSTACK_SIZE ( ( unsigned short ) 256 )
\r
149 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
151 /* Handle to the com port used by both tasks. */
\r
152 static xComPortHandle xPort;
\r
154 /* The transmit function as described at the top of the file. */
\r
155 static void vComTxTask( void *pvParameters );
\r
157 /* The receive function as described at the top of the file. */
\r
158 static void vComRxTask( void *pvParameters );
\r
160 /* The semaphore test function as described at the top of the file. */
\r
161 static void vSemTestTask( void * pvParameters );
\r
163 /* The string that is repeatedly transmitted. */
\r
164 const char * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
165 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
167 /* Variables that are incremented on each cycle of each task. These are used to
\r
168 check that both tasks are still executing. */
\r
169 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
171 /* The handle to the semaphore test task. */
\r
172 static xTaskHandle xSemTestTaskHandle = NULL;
\r
174 /*-----------------------------------------------------------*/
\r
176 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
178 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
180 /* Initialise the com port then spawn both tasks. */
\r
181 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
182 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
183 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
184 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
186 /*-----------------------------------------------------------*/
\r
188 static void vComTxTask( void *pvParameters )
\r
190 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
191 portTickType xTimeToWait;
\r
193 /* Stop warnings. */
\r
194 ( void ) pvParameters;
\r
196 /* Queue a message for printing to say the task has started. */
\r
197 vPrintDisplayMessage( &pcTaskStartMsg );
\r
201 /* Send the string to the serial port. */
\r
202 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
204 /* We have posted all the characters in the string - increment the variable
\r
205 used to check that this task is still running, then wait before re-sending
\r
209 xTimeToWait = xTaskGetTickCount();
\r
211 /* Make sure we don't wait too long... */
\r
212 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
214 /* ...but we do want to wait. */
\r
215 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
217 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
220 vTaskDelay( xTimeToWait );
\r
222 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
223 /*-----------------------------------------------------------*/
\r
225 static void vComRxTask( void *pvParameters )
\r
227 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
228 const char * const pcTaskErrorMsg = "COM read error\r\n";
\r
229 const char * const pcTaskRestartMsg = "COM resynced\r\n";
\r
230 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
231 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
232 const char *pcExpectedChar;
\r
233 portBASE_TYPE xGotChar;
\r
235 short sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
237 /* Stop warnings. */
\r
238 ( void ) pvParameters;
\r
240 /* Queue a message for printing to say the task has started. */
\r
241 vPrintDisplayMessage( &pcTaskStartMsg );
\r
243 /* The first expected character is the first character in the string. */
\r
244 pcExpectedChar = pcMessageToExchange;
\r
245 sResyncRequired = pdFALSE;
\r
246 sConsecutiveErrors = 0;
\r
247 sLatchedError = pdFALSE;
\r
251 /* Receive a message from the com port interrupt routine. If a message is
\r
252 not yet available the call will block the task. */
\r
253 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
254 if( xGotChar == pdTRUE )
\r
256 if( sResyncRequired == pdTRUE )
\r
258 /* We got out of sequence and are waiting for the start of the next
\r
259 transmission of the string. */
\r
260 if( cRxedChar == '\n' )
\r
262 /* This is the end of the message so we can start again - with
\r
263 the first character in the string being the next thing we expect
\r
265 pcExpectedChar = pcMessageToExchange;
\r
266 sResyncRequired = pdFALSE;
\r
268 /* Queue a message for printing to say that we are going to try
\r
270 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
272 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
273 sConsecutiveErrors++;
\r
274 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
276 sLatchedError = pdTRUE;
\r
282 /* We have received a character, but is it the expected character? */
\r
283 if( cRxedChar != *pcExpectedChar )
\r
285 /* This was not the expected character so post a message for
\r
286 printing to say that an error has occurred. We will then wait
\r
287 to resynchronise. */
\r
288 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
289 sResyncRequired = pdTRUE;
\r
293 /* This was the expected character so next time we will expect
\r
294 the next character in the string. Wrap back to the beginning
\r
295 of the string when the null terminator has been reached. */
\r
297 if( *pcExpectedChar == '\0' )
\r
299 pcExpectedChar = pcMessageToExchange;
\r
301 /* We have got through the entire string without error. */
\r
302 sConsecutiveErrors = 0;
\r
307 /* Increment the count that is used to check that this task is still
\r
308 running. This is only done if an error has never occurred. */
\r
309 if( sLatchedError == pdFALSE )
\r
316 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
319 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
320 /*-----------------------------------------------------------*/
\r
322 static void vSemTestTask( void * pvParameters )
\r
324 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
325 portBASE_TYPE xError = pdFALSE;
\r
327 /* Stop warnings. */
\r
328 ( void ) pvParameters;
\r
330 /* Queue a message for printing to say the task has started. */
\r
331 vPrintDisplayMessage( &pcTaskStartMsg );
\r
335 if( xSerialWaitForSemaphore( xPort ) )
\r
337 if( xError == pdFALSE )
\r
347 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
348 /*-----------------------------------------------------------*/
\r
350 /* This is called to check that all the created tasks are still running. */
\r
351 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
353 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
354 portBASE_TYPE xReturn;
\r
356 /* Not too worried about mutual exclusion on these variables as they are 16
\r
357 bits and we are only reading them. We also only care to see if they have
\r
360 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
369 sLastTxCount = sTxCount;
\r
370 sLastRxCount = sRxCount;
\r
371 sLastSemCount = sSemCount;
\r
375 /*-----------------------------------------------------------*/
\r
377 void vComTestUnsuspendTask( void )
\r
379 /* The task that is suspended on the semaphore will be referenced from the
\r
380 Suspended list as it is blocking indefinitely. This call just checks that
\r
381 the kernel correctly detects this and does not attempt to unsuspend the
\r
383 xTaskResumeFromISR( xSemTestTaskHandle );
\r