2 FreeRTOS.org V5.1.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
51 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
52 * connector should be used so that everything that is transmitted is also received.
\r
53 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
54 * pins two and three should be connected together.
\r
56 * The first task repeatedly sends a string to a queue, character at a time. The
\r
57 * serial port interrupt will empty the queue and transmit the characters. The
\r
58 * task blocks for a pseudo random period before resending the string.
\r
60 * The second task blocks on a queue waiting for a character to be received.
\r
61 * Characters received by the serial port interrupt routine are posted onto the
\r
62 * queue - unblocking the task making it ready to execute. If this is then the
\r
63 * highest priority task ready to run it will run immediately - with a context
\r
64 * switch occurring at the end of the interrupt service routine. The task
\r
65 * receiving characters is spawned with a higher priority than the task
\r
66 * transmitting the characters.
\r
68 * With the loop back connector in place, one task will transmit a string and the
\r
69 * other will immediately receive it. The receiving task knows the string it
\r
70 * expects to receive so can detect an error.
\r
72 * This also creates a third task. This is used to test semaphore usage from an
\r
73 * ISR and does nothing interesting.
\r
75 * \page ComTestC comtest.c
\r
76 * \ingroup DemoFiles
\r
83 + The priority of the Rx task has been lowered. Received characters are
\r
84 now processed (read from the queue) at the idle priority, allowing low
\r
85 priority tasks to run evenly at times of a high communications overhead.
\r
89 + The Tx task now waits a pseudo random time between transissions.
\r
90 Previously a fixed period was used but this was not such a good test as
\r
91 interrupts fired at regular intervals.
\r
93 Changes From V1.2.0:
\r
95 + Use vSerialPutString() instead of single character puts.
\r
96 + Only stop the check variable incrementing after two consecutive errors.
\r
100 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
101 only 1. This is done to tie in better with the other demo application
\r
104 Changes from V2.0.0
\r
106 + Delay periods are now specified using variables and constants of
\r
107 portTickType rather than unsigned portLONG.
\r
108 + Slight modification to task priorities.
\r
113 /* Scheduler include files. */
\r
114 #include <stdlib.h>
\r
115 #include <string.h>
\r
116 #include "FreeRTOS.h"
\r
119 /* Demo program include files. */
\r
120 #include "serial.h"
\r
121 #include "comtest.h"
\r
124 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
125 interval. This is the maximum and minimum block time between sends. */
\r
126 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
127 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
129 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
131 #define comSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
133 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
135 /* Handle to the com port used by both tasks. */
\r
136 static xComPortHandle xPort;
\r
138 /* The transmit function as described at the top of the file. */
\r
139 static void vComTxTask( void *pvParameters );
\r
141 /* The receive function as described at the top of the file. */
\r
142 static void vComRxTask( void *pvParameters );
\r
144 /* The semaphore test function as described at the top of the file. */
\r
145 static void vSemTestTask( void * pvParameters );
\r
147 /* The string that is repeatedly transmitted. */
\r
148 const portCHAR * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
149 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
151 /* Variables that are incremented on each cycle of each task. These are used to
\r
152 check that both tasks are still executing. */
\r
153 volatile portSHORT sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
155 /* The handle to the semaphore test task. */
\r
156 static xTaskHandle xSemTestTaskHandle = NULL;
\r
158 /*-----------------------------------------------------------*/
\r
160 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
162 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
164 /* Initialise the com port then spawn both tasks. */
\r
165 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
166 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
167 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
168 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
170 /*-----------------------------------------------------------*/
\r
172 static void vComTxTask( void *pvParameters )
\r
174 const portCHAR * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
175 portTickType xTimeToWait;
\r
177 /* Stop warnings. */
\r
178 ( void ) pvParameters;
\r
180 /* Queue a message for printing to say the task has started. */
\r
181 vPrintDisplayMessage( &pcTaskStartMsg );
\r
185 /* Send the string to the serial port. */
\r
186 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
188 /* We have posted all the characters in the string - increment the variable
\r
189 used to check that this task is still running, then wait before re-sending
\r
193 xTimeToWait = xTaskGetTickCount();
\r
195 /* Make sure we don't wait too long... */
\r
196 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
198 /* ...but we do want to wait. */
\r
199 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
201 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
204 vTaskDelay( xTimeToWait );
\r
206 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
207 /*-----------------------------------------------------------*/
\r
209 static void vComRxTask( void *pvParameters )
\r
211 const portCHAR * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
212 const portCHAR * const pcTaskErrorMsg = "COM read error\r\n";
\r
213 const portCHAR * const pcTaskRestartMsg = "COM resynced\r\n";
\r
214 const portCHAR * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
215 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
216 const portCHAR *pcExpectedChar;
\r
217 portBASE_TYPE xGotChar;
\r
218 portCHAR cRxedChar;
\r
219 portSHORT sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
221 /* Stop warnings. */
\r
222 ( void ) pvParameters;
\r
224 /* Queue a message for printing to say the task has started. */
\r
225 vPrintDisplayMessage( &pcTaskStartMsg );
\r
227 /* The first expected character is the first character in the string. */
\r
228 pcExpectedChar = pcMessageToExchange;
\r
229 sResyncRequired = pdFALSE;
\r
230 sConsecutiveErrors = 0;
\r
231 sLatchedError = pdFALSE;
\r
235 /* Receive a message from the com port interrupt routine. If a message is
\r
236 not yet available the call will block the task. */
\r
237 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
238 if( xGotChar == pdTRUE )
\r
240 if( sResyncRequired == pdTRUE )
\r
242 /* We got out of sequence and are waiting for the start of the next
\r
243 transmission of the string. */
\r
244 if( cRxedChar == '\n' )
\r
246 /* This is the end of the message so we can start again - with
\r
247 the first character in the string being the next thing we expect
\r
249 pcExpectedChar = pcMessageToExchange;
\r
250 sResyncRequired = pdFALSE;
\r
252 /* Queue a message for printing to say that we are going to try
\r
254 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
256 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
257 sConsecutiveErrors++;
\r
258 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
260 sLatchedError = pdTRUE;
\r
266 /* We have received a character, but is it the expected character? */
\r
267 if( cRxedChar != *pcExpectedChar )
\r
269 /* This was not the expected character so post a message for
\r
270 printing to say that an error has occurred. We will then wait
\r
271 to resynchronise. */
\r
272 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
273 sResyncRequired = pdTRUE;
\r
277 /* This was the expected character so next time we will expect
\r
278 the next character in the string. Wrap back to the beginning
\r
279 of the string when the null terminator has been reached. */
\r
281 if( *pcExpectedChar == '\0' )
\r
283 pcExpectedChar = pcMessageToExchange;
\r
285 /* We have got through the entire string without error. */
\r
286 sConsecutiveErrors = 0;
\r
291 /* Increment the count that is used to check that this task is still
\r
292 running. This is only done if an error has never occurred. */
\r
293 if( sLatchedError == pdFALSE )
\r
300 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
303 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
304 /*-----------------------------------------------------------*/
\r
306 static void vSemTestTask( void * pvParameters )
\r
308 const portCHAR * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
309 portBASE_TYPE xError = pdFALSE;
\r
311 /* Stop warnings. */
\r
312 ( void ) pvParameters;
\r
314 /* Queue a message for printing to say the task has started. */
\r
315 vPrintDisplayMessage( &pcTaskStartMsg );
\r
319 if( xSerialWaitForSemaphore( xPort ) )
\r
321 if( xError == pdFALSE )
\r
331 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
332 /*-----------------------------------------------------------*/
\r
334 /* This is called to check that all the created tasks are still running. */
\r
335 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
337 static portSHORT sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
338 portBASE_TYPE xReturn;
\r
340 /* Not too worried about mutual exclusion on these variables as they are 16
\r
341 bits and we are only reading them. We also only care to see if they have
\r
344 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
353 sLastTxCount = sTxCount;
\r
354 sLastRxCount = sRxCount;
\r
355 sLastSemCount = sSemCount;
\r
359 /*-----------------------------------------------------------*/
\r
361 void vComTestUnsuspendTask( void )
\r
363 /* The task that is suspended on the semaphore will be referenced from the
\r
364 Suspended list as it is blocking indefinitely. This call just checks that
\r
365 the kernel correctly detects this and does not attempt to unsuspend the
\r
367 xTaskResumeFromISR( xSemTestTaskHandle );
\r