2 FreeRTOS.org V4.6.0 - Copyright (C) 2003-2007 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 See http://www.FreeRTOS.org for documentation, latest information, license
\r
28 and contact details. Please ensure to read the configuration and relevant
\r
29 port sections of the online documentation.
\r
31 Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along
\r
32 with commercial development and support options.
\r
33 ***************************************************************************
\r
37 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
38 * connector should be used so that everything that is transmitted is also received.
\r
39 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
40 * pins two and three should be connected together.
\r
42 * The first task repeatedly sends a string to a queue, character at a time. The
\r
43 * serial port interrupt will empty the queue and transmit the characters. The
\r
44 * task blocks for a pseudo random period before resending the string.
\r
46 * The second task blocks on a queue waiting for a character to be received.
\r
47 * Characters received by the serial port interrupt routine are posted onto the
\r
48 * queue - unblocking the task making it ready to execute. If this is then the
\r
49 * highest priority task ready to run it will run immediately - with a context
\r
50 * switch occurring at the end of the interrupt service routine. The task
\r
51 * receiving characters is spawned with a higher priority than the task
\r
52 * transmitting the characters.
\r
54 * With the loop back connector in place, one task will transmit a string and the
\r
55 * other will immediately receive it. The receiving task knows the string it
\r
56 * expects to receive so can detect an error.
\r
58 * This also creates a third task. This is used to test semaphore usage from an
\r
59 * ISR and does nothing interesting.
\r
61 * \page ComTestC comtest.c
\r
62 * \ingroup DemoFiles
\r
69 + The priority of the Rx task has been lowered. Received characters are
\r
70 now processed (read from the queue) at the idle priority, allowing low
\r
71 priority tasks to run evenly at times of a high communications overhead.
\r
75 + The Tx task now waits a pseudo random time between transissions.
\r
76 Previously a fixed period was used but this was not such a good test as
\r
77 interrupts fired at regular intervals.
\r
79 Changes From V1.2.0:
\r
81 + Use vSerialPutString() instead of single character puts.
\r
82 + Only stop the check variable incrementing after two consecutive errors.
\r
86 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
87 only 1. This is done to tie in better with the other demo application
\r
92 + Delay periods are now specified using variables and constants of
\r
93 portTickType rather than unsigned portLONG.
\r
94 + Slight modification to task priorities.
\r
99 /* Scheduler include files. */
\r
100 #include <stdlib.h>
\r
101 #include <string.h>
\r
102 #include "FreeRTOS.h"
\r
105 /* Demo program include files. */
\r
106 #include "serial.h"
\r
107 #include "comtest.h"
\r
110 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
111 interval. This is the maximum and minimum block time between sends. */
\r
112 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
113 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
115 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
117 #define comSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
119 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
121 /* Handle to the com port used by both tasks. */
\r
122 static xComPortHandle xPort;
\r
124 /* The transmit function as described at the top of the file. */
\r
125 static void vComTxTask( void *pvParameters );
\r
127 /* The receive function as described at the top of the file. */
\r
128 static void vComRxTask( void *pvParameters );
\r
130 /* The semaphore test function as described at the top of the file. */
\r
131 static void vSemTestTask( void * pvParameters );
\r
133 /* The string that is repeatedly transmitted. */
\r
134 const portCHAR * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
135 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
137 /* Variables that are incremented on each cycle of each task. These are used to
\r
138 check that both tasks are still executing. */
\r
139 volatile portSHORT sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
141 /* The handle to the semaphore test task. */
\r
142 static xTaskHandle xSemTestTaskHandle = NULL;
\r
144 /*-----------------------------------------------------------*/
\r
146 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
148 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
150 /* Initialise the com port then spawn both tasks. */
\r
151 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
152 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
153 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
154 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
156 /*-----------------------------------------------------------*/
\r
158 static void vComTxTask( void *pvParameters )
\r
160 const portCHAR * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
161 portTickType xTimeToWait;
\r
163 /* Stop warnings. */
\r
164 ( void ) pvParameters;
\r
166 /* Queue a message for printing to say the task has started. */
\r
167 vPrintDisplayMessage( &pcTaskStartMsg );
\r
171 /* Send the string to the serial port. */
\r
172 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
174 /* We have posted all the characters in the string - increment the variable
\r
175 used to check that this task is still running, then wait before re-sending
\r
179 xTimeToWait = xTaskGetTickCount();
\r
181 /* Make sure we don't wait too long... */
\r
182 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
184 /* ...but we do want to wait. */
\r
185 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
187 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
190 vTaskDelay( xTimeToWait );
\r
192 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
193 /*-----------------------------------------------------------*/
\r
195 static void vComRxTask( void *pvParameters )
\r
197 const portCHAR * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
198 const portCHAR * const pcTaskErrorMsg = "COM read error\r\n";
\r
199 const portCHAR * const pcTaskRestartMsg = "COM resynced\r\n";
\r
200 const portCHAR * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
201 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
202 const portCHAR *pcExpectedChar;
\r
203 portBASE_TYPE xGotChar;
\r
204 portCHAR cRxedChar;
\r
205 portSHORT sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
207 /* Stop warnings. */
\r
208 ( void ) pvParameters;
\r
210 /* Queue a message for printing to say the task has started. */
\r
211 vPrintDisplayMessage( &pcTaskStartMsg );
\r
213 /* The first expected character is the first character in the string. */
\r
214 pcExpectedChar = pcMessageToExchange;
\r
215 sResyncRequired = pdFALSE;
\r
216 sConsecutiveErrors = 0;
\r
217 sLatchedError = pdFALSE;
\r
221 /* Receive a message from the com port interrupt routine. If a message is
\r
222 not yet available the call will block the task. */
\r
223 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
224 if( xGotChar == pdTRUE )
\r
226 if( sResyncRequired == pdTRUE )
\r
228 /* We got out of sequence and are waiting for the start of the next
\r
229 transmission of the string. */
\r
230 if( cRxedChar == '\n' )
\r
232 /* This is the end of the message so we can start again - with
\r
233 the first character in the string being the next thing we expect
\r
235 pcExpectedChar = pcMessageToExchange;
\r
236 sResyncRequired = pdFALSE;
\r
238 /* Queue a message for printing to say that we are going to try
\r
240 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
242 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
243 sConsecutiveErrors++;
\r
244 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
246 sLatchedError = pdTRUE;
\r
252 /* We have received a character, but is it the expected character? */
\r
253 if( cRxedChar != *pcExpectedChar )
\r
255 /* This was not the expected character so post a message for
\r
256 printing to say that an error has occurred. We will then wait
\r
257 to resynchronise. */
\r
258 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
259 sResyncRequired = pdTRUE;
\r
263 /* This was the expected character so next time we will expect
\r
264 the next character in the string. Wrap back to the beginning
\r
265 of the string when the null terminator has been reached. */
\r
267 if( *pcExpectedChar == '\0' )
\r
269 pcExpectedChar = pcMessageToExchange;
\r
271 /* We have got through the entire string without error. */
\r
272 sConsecutiveErrors = 0;
\r
277 /* Increment the count that is used to check that this task is still
\r
278 running. This is only done if an error has never occurred. */
\r
279 if( sLatchedError == pdFALSE )
\r
286 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
289 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
290 /*-----------------------------------------------------------*/
\r
292 static void vSemTestTask( void * pvParameters )
\r
294 const portCHAR * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
295 portBASE_TYPE xError = pdFALSE;
\r
297 /* Stop warnings. */
\r
298 ( void ) pvParameters;
\r
300 /* Queue a message for printing to say the task has started. */
\r
301 vPrintDisplayMessage( &pcTaskStartMsg );
\r
305 if( xSerialWaitForSemaphore( xPort ) )
\r
307 if( xError == pdFALSE )
\r
317 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
318 /*-----------------------------------------------------------*/
\r
320 /* This is called to check that all the created tasks are still running. */
\r
321 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
323 static portSHORT sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
324 portBASE_TYPE xReturn;
\r
326 /* Not too worried about mutual exclusion on these variables as they are 16
\r
327 bits and we are only reading them. We also only care to see if they have
\r
330 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
339 sLastTxCount = sTxCount;
\r
340 sLastRxCount = sRxCount;
\r
341 sLastSemCount = sSemCount;
\r
345 /*-----------------------------------------------------------*/
\r
347 void vComTestUnsuspendTask( void )
\r
349 /* The task that is suspended on the semaphore will be referenced from the
\r
350 Suspended list as it is blocking indefinitely. This call just checks that
\r
351 the kernel correctly detects this and does not attempt to unsuspend the
\r
353 xTaskResumeFromISR( xSemTestTaskHandle );
\r