2 FreeRTOS.org V4.3.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
142 /*-----------------------------------------------------------*/
\r
144 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
146 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
148 /* Initialise the com port then spawn both tasks. */
\r
149 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
150 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
151 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
152 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
154 /*-----------------------------------------------------------*/
\r
156 static void vComTxTask( void *pvParameters )
\r
158 const portCHAR * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
159 portTickType xTimeToWait;
\r
161 /* Stop warnings. */
\r
162 ( void ) pvParameters;
\r
164 /* Queue a message for printing to say the task has started. */
\r
165 vPrintDisplayMessage( &pcTaskStartMsg );
\r
169 /* Send the string to the serial port. */
\r
170 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
172 /* We have posted all the characters in the string - increment the variable
\r
173 used to check that this task is still running, then wait before re-sending
\r
177 xTimeToWait = xTaskGetTickCount();
\r
179 /* Make sure we don't wait too long... */
\r
180 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
182 /* ...but we do want to wait. */
\r
183 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
185 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
188 vTaskDelay( xTimeToWait );
\r
190 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
191 /*-----------------------------------------------------------*/
\r
193 static void vComRxTask( void *pvParameters )
\r
195 const portCHAR * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
196 const portCHAR * const pcTaskErrorMsg = "COM read error\r\n";
\r
197 const portCHAR * const pcTaskRestartMsg = "COM resynced\r\n";
\r
198 const portCHAR * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
199 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
200 const portCHAR *pcExpectedChar;
\r
201 portBASE_TYPE xGotChar;
\r
202 portCHAR cRxedChar;
\r
203 portSHORT sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
205 /* Stop warnings. */
\r
206 ( void ) pvParameters;
\r
208 /* Queue a message for printing to say the task has started. */
\r
209 vPrintDisplayMessage( &pcTaskStartMsg );
\r
211 /* The first expected character is the first character in the string. */
\r
212 pcExpectedChar = pcMessageToExchange;
\r
213 sResyncRequired = pdFALSE;
\r
214 sConsecutiveErrors = 0;
\r
215 sLatchedError = pdFALSE;
\r
219 /* Receive a message from the com port interrupt routine. If a message is
\r
220 not yet available the call will block the task. */
\r
221 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
222 if( xGotChar == pdTRUE )
\r
224 if( sResyncRequired == pdTRUE )
\r
226 /* We got out of sequence and are waiting for the start of the next
\r
227 transmission of the string. */
\r
228 if( cRxedChar == '\n' )
\r
230 /* This is the end of the message so we can start again - with
\r
231 the first character in the string being the next thing we expect
\r
233 pcExpectedChar = pcMessageToExchange;
\r
234 sResyncRequired = pdFALSE;
\r
236 /* Queue a message for printing to say that we are going to try
\r
238 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
240 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
241 sConsecutiveErrors++;
\r
242 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
244 sLatchedError = pdTRUE;
\r
250 /* We have received a character, but is it the expected character? */
\r
251 if( cRxedChar != *pcExpectedChar )
\r
253 /* This was not the expected character so post a message for
\r
254 printing to say that an error has occurred. We will then wait
\r
255 to resynchronise. */
\r
256 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
257 sResyncRequired = pdTRUE;
\r
261 /* This was the expected character so next time we will expect
\r
262 the next character in the string. Wrap back to the beginning
\r
263 of the string when the null terminator has been reached. */
\r
265 if( *pcExpectedChar == '\0' )
\r
267 pcExpectedChar = pcMessageToExchange;
\r
269 /* We have got through the entire string without error. */
\r
270 sConsecutiveErrors = 0;
\r
275 /* Increment the count that is used to check that this task is still
\r
276 running. This is only done if an error has never occurred. */
\r
277 if( sLatchedError == pdFALSE )
\r
284 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
287 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
288 /*-----------------------------------------------------------*/
\r
290 static void vSemTestTask( void * pvParameters )
\r
292 const portCHAR * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
294 /* Stop warnings. */
\r
295 ( void ) pvParameters;
\r
297 /* Queue a message for printing to say the task has started. */
\r
298 vPrintDisplayMessage( &pcTaskStartMsg );
\r
302 if( xSerialWaitForSemaphore( xPort ) )
\r
307 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
308 /*-----------------------------------------------------------*/
\r
310 /* This is called to check that all the created tasks are still running. */
\r
311 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
313 static portSHORT sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
314 portBASE_TYPE xReturn;
\r
316 /* Not too worried about mutual exclusion on these variables as they are 16
\r
317 bits and we are only reading them. We also only care to see if they have
\r
320 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
329 sLastTxCount = sTxCount;
\r
330 sLastRxCount = sRxCount;
\r
331 sLastSemCount = sSemCount;
\r