2 FreeRTOS.org V4.7.2 - 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
28 Please ensure to read the configuration and relevant port sections of the
\r
29 online documentation.
\r
31 +++ http://www.FreeRTOS.org +++
\r
32 Documentation, latest information, license and contact details.
\r
34 +++ http://www.SafeRTOS.com +++
\r
35 A version that is certified for use in safety critical systems.
\r
37 +++ http://www.OpenRTOS.com +++
\r
38 Commercial support, development, porting, licensing and training services.
\r
40 ***************************************************************************
\r
44 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
45 * connector should be used so that everything that is transmitted is also received.
\r
46 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
47 * pins two and three should be connected together.
\r
49 * The first task repeatedly sends a string to a queue, character at a time. The
\r
50 * serial port interrupt will empty the queue and transmit the characters. The
\r
51 * task blocks for a pseudo random period before resending the string.
\r
53 * The second task blocks on a queue waiting for a character to be received.
\r
54 * Characters received by the serial port interrupt routine are posted onto the
\r
55 * queue - unblocking the task making it ready to execute. If this is then the
\r
56 * highest priority task ready to run it will run immediately - with a context
\r
57 * switch occurring at the end of the interrupt service routine. The task
\r
58 * receiving characters is spawned with a higher priority than the task
\r
59 * transmitting the characters.
\r
61 * With the loop back connector in place, one task will transmit a string and the
\r
62 * other will immediately receive it. The receiving task knows the string it
\r
63 * expects to receive so can detect an error.
\r
65 * This also creates a third task. This is used to test semaphore usage from an
\r
66 * ISR and does nothing interesting.
\r
68 * \page ComTestC comtest.c
\r
69 * \ingroup DemoFiles
\r
76 + The priority of the Rx task has been lowered. Received characters are
\r
77 now processed (read from the queue) at the idle priority, allowing low
\r
78 priority tasks to run evenly at times of a high communications overhead.
\r
82 + The Tx task now waits a pseudo random time between transissions.
\r
83 Previously a fixed period was used but this was not such a good test as
\r
84 interrupts fired at regular intervals.
\r
86 Changes From V1.2.0:
\r
88 + Use vSerialPutString() instead of single character puts.
\r
89 + Only stop the check variable incrementing after two consecutive errors.
\r
93 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
94 only 1. This is done to tie in better with the other demo application
\r
99 + Delay periods are now specified using variables and constants of
\r
100 portTickType rather than unsigned portLONG.
\r
101 + Slight modification to task priorities.
\r
106 /* Scheduler include files. */
\r
107 #include <stdlib.h>
\r
108 #include <string.h>
\r
109 #include "FreeRTOS.h"
\r
112 /* Demo program include files. */
\r
113 #include "serial.h"
\r
114 #include "comtest.h"
\r
117 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
118 interval. This is the maximum and minimum block time between sends. */
\r
119 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
120 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
122 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
124 #define comSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
126 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
128 /* Handle to the com port used by both tasks. */
\r
129 static xComPortHandle xPort;
\r
131 /* The transmit function as described at the top of the file. */
\r
132 static void vComTxTask( void *pvParameters );
\r
134 /* The receive function as described at the top of the file. */
\r
135 static void vComRxTask( void *pvParameters );
\r
137 /* The semaphore test function as described at the top of the file. */
\r
138 static void vSemTestTask( void * pvParameters );
\r
140 /* The string that is repeatedly transmitted. */
\r
141 const portCHAR * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
142 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
144 /* Variables that are incremented on each cycle of each task. These are used to
\r
145 check that both tasks are still executing. */
\r
146 volatile portSHORT sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
148 /* The handle to the semaphore test task. */
\r
149 static xTaskHandle xSemTestTaskHandle = NULL;
\r
151 /*-----------------------------------------------------------*/
\r
153 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
155 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
157 /* Initialise the com port then spawn both tasks. */
\r
158 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
159 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
160 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
161 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
163 /*-----------------------------------------------------------*/
\r
165 static void vComTxTask( void *pvParameters )
\r
167 const portCHAR * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
168 portTickType xTimeToWait;
\r
170 /* Stop warnings. */
\r
171 ( void ) pvParameters;
\r
173 /* Queue a message for printing to say the task has started. */
\r
174 vPrintDisplayMessage( &pcTaskStartMsg );
\r
178 /* Send the string to the serial port. */
\r
179 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
181 /* We have posted all the characters in the string - increment the variable
\r
182 used to check that this task is still running, then wait before re-sending
\r
186 xTimeToWait = xTaskGetTickCount();
\r
188 /* Make sure we don't wait too long... */
\r
189 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
191 /* ...but we do want to wait. */
\r
192 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
194 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
197 vTaskDelay( xTimeToWait );
\r
199 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
200 /*-----------------------------------------------------------*/
\r
202 static void vComRxTask( void *pvParameters )
\r
204 const portCHAR * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
205 const portCHAR * const pcTaskErrorMsg = "COM read error\r\n";
\r
206 const portCHAR * const pcTaskRestartMsg = "COM resynced\r\n";
\r
207 const portCHAR * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
208 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
209 const portCHAR *pcExpectedChar;
\r
210 portBASE_TYPE xGotChar;
\r
211 portCHAR cRxedChar;
\r
212 portSHORT sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
214 /* Stop warnings. */
\r
215 ( void ) pvParameters;
\r
217 /* Queue a message for printing to say the task has started. */
\r
218 vPrintDisplayMessage( &pcTaskStartMsg );
\r
220 /* The first expected character is the first character in the string. */
\r
221 pcExpectedChar = pcMessageToExchange;
\r
222 sResyncRequired = pdFALSE;
\r
223 sConsecutiveErrors = 0;
\r
224 sLatchedError = pdFALSE;
\r
228 /* Receive a message from the com port interrupt routine. If a message is
\r
229 not yet available the call will block the task. */
\r
230 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
231 if( xGotChar == pdTRUE )
\r
233 if( sResyncRequired == pdTRUE )
\r
235 /* We got out of sequence and are waiting for the start of the next
\r
236 transmission of the string. */
\r
237 if( cRxedChar == '\n' )
\r
239 /* This is the end of the message so we can start again - with
\r
240 the first character in the string being the next thing we expect
\r
242 pcExpectedChar = pcMessageToExchange;
\r
243 sResyncRequired = pdFALSE;
\r
245 /* Queue a message for printing to say that we are going to try
\r
247 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
249 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
250 sConsecutiveErrors++;
\r
251 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
253 sLatchedError = pdTRUE;
\r
259 /* We have received a character, but is it the expected character? */
\r
260 if( cRxedChar != *pcExpectedChar )
\r
262 /* This was not the expected character so post a message for
\r
263 printing to say that an error has occurred. We will then wait
\r
264 to resynchronise. */
\r
265 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
266 sResyncRequired = pdTRUE;
\r
270 /* This was the expected character so next time we will expect
\r
271 the next character in the string. Wrap back to the beginning
\r
272 of the string when the null terminator has been reached. */
\r
274 if( *pcExpectedChar == '\0' )
\r
276 pcExpectedChar = pcMessageToExchange;
\r
278 /* We have got through the entire string without error. */
\r
279 sConsecutiveErrors = 0;
\r
284 /* Increment the count that is used to check that this task is still
\r
285 running. This is only done if an error has never occurred. */
\r
286 if( sLatchedError == pdFALSE )
\r
293 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
296 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
297 /*-----------------------------------------------------------*/
\r
299 static void vSemTestTask( void * pvParameters )
\r
301 const portCHAR * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
302 portBASE_TYPE xError = pdFALSE;
\r
304 /* Stop warnings. */
\r
305 ( void ) pvParameters;
\r
307 /* Queue a message for printing to say the task has started. */
\r
308 vPrintDisplayMessage( &pcTaskStartMsg );
\r
312 if( xSerialWaitForSemaphore( xPort ) )
\r
314 if( xError == pdFALSE )
\r
324 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
325 /*-----------------------------------------------------------*/
\r
327 /* This is called to check that all the created tasks are still running. */
\r
328 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
330 static portSHORT sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
331 portBASE_TYPE xReturn;
\r
333 /* Not too worried about mutual exclusion on these variables as they are 16
\r
334 bits and we are only reading them. We also only care to see if they have
\r
337 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
346 sLastTxCount = sTxCount;
\r
347 sLastRxCount = sRxCount;
\r
348 sLastSemCount = sSemCount;
\r
352 /*-----------------------------------------------------------*/
\r
354 void vComTestUnsuspendTask( void )
\r
356 /* The task that is suspended on the semaphore will be referenced from the
\r
357 Suspended list as it is blocking indefinitely. This call just checks that
\r
358 the kernel correctly detects this and does not attempt to unsuspend the
\r
360 xTaskResumeFromISR( xSemTestTaskHandle );
\r