2 FreeRTOS V4.0.1 - Copyright (C) 2003-2006 Richard Barry.
\r
4 This file is part of the FreeRTOS distribution.
\r
6 FreeRTOS 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 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; 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, 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
30 ***************************************************************************
\r
34 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
35 * connector should be used so that everything that is transmitted is also received.
\r
36 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
37 * pins two and three should be connected together.
\r
39 * The first task repeatedly sends a string to a queue, character at a time. The
\r
40 * serial port interrupt will empty the queue and transmit the characters. The
\r
41 * task blocks for a pseudo random period before resending the string.
\r
43 * The second task blocks on a queue waiting for a character to be received.
\r
44 * Characters received by the serial port interrupt routine are posted onto the
\r
45 * queue - unblocking the task making it ready to execute. If this is then the
\r
46 * highest priority task ready to run it will run immediately - with a context
\r
47 * switch occurring at the end of the interrupt service routine. The task
\r
48 * receiving characters is spawned with a higher priority than the task
\r
49 * transmitting the characters.
\r
51 * With the loop back connector in place, one task will transmit a string and the
\r
52 * other will immediately receive it. The receiving task knows the string it
\r
53 * expects to receive so can detect an error.
\r
55 * This also creates a third task. This is used to test semaphore usage from an
\r
56 * ISR and does nothing interesting.
\r
58 * \page ComTestC comtest.c
\r
59 * \ingroup DemoFiles
\r
66 + The priority of the Rx task has been lowered. Received characters are
\r
67 now processed (read from the queue) at the idle priority, allowing low
\r
68 priority tasks to run evenly at times of a high communications overhead.
\r
72 + The Tx task now waits a pseudo random time between transissions.
\r
73 Previously a fixed period was used but this was not such a good test as
\r
74 interrupts fired at regular intervals.
\r
76 Changes From V1.2.0:
\r
78 + Use vSerialPutString() instead of single character puts.
\r
79 + Only stop the check variable incrementing after two consecutive errors.
\r
83 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
84 only 1. This is done to tie in better with the other demo application
\r
89 + Delay periods are now specified using variables and constants of
\r
90 portTickType rather than unsigned portLONG.
\r
91 + Slight modification to task priorities.
\r
96 /* Scheduler include files. */
\r
99 #include "FreeRTOS.h"
\r
102 /* Demo program include files. */
\r
103 #include "serial.h"
\r
104 #include "comtest.h"
\r
107 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
108 interval. This is the maximum and minimum block time between sends. */
\r
109 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
110 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
112 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
114 #define comSTACK_SIZE ( ( unsigned portSHORT ) 256 )
\r
116 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
118 /* Handle to the com port used by both tasks. */
\r
119 static xComPortHandle xPort;
\r
121 /* The transmit function as described at the top of the file. */
\r
122 static void vComTxTask( void *pvParameters );
\r
124 /* The receive function as described at the top of the file. */
\r
125 static void vComRxTask( void *pvParameters );
\r
127 /* The semaphore test function as described at the top of the file. */
\r
128 static void vSemTestTask( void * pvParameters );
\r
130 /* The string that is repeatedly transmitted. */
\r
131 const portCHAR * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
132 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
134 /* Variables that are incremented on each cycle of each task. These are used to
\r
135 check that both tasks are still executing. */
\r
136 volatile portSHORT sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
139 /*-----------------------------------------------------------*/
\r
141 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
143 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
145 /* Initialise the com port then spawn both tasks. */
\r
146 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
147 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
148 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
149 xTaskCreate( vSemTestTask, "ISRSemTst", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
\r
151 /*-----------------------------------------------------------*/
\r
153 static void vComTxTask( void *pvParameters )
\r
155 const portCHAR * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
156 portTickType xTimeToWait;
\r
158 /* Stop warnings. */
\r
159 ( void ) pvParameters;
\r
161 /* Queue a message for printing to say the task has started. */
\r
162 vPrintDisplayMessage( &pcTaskStartMsg );
\r
166 /* Send the string to the serial port. */
\r
167 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
169 /* We have posted all the characters in the string - increment the variable
\r
170 used to check that this task is still running, then wait before re-sending
\r
174 xTimeToWait = xTaskGetTickCount();
\r
176 /* Make sure we don't wait too long... */
\r
177 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
179 /* ...but we do want to wait. */
\r
180 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
182 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
185 vTaskDelay( xTimeToWait );
\r
187 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
188 /*-----------------------------------------------------------*/
\r
190 static void vComRxTask( void *pvParameters )
\r
192 const portCHAR * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
193 const portCHAR * const pcTaskErrorMsg = "COM read error\r\n";
\r
194 const portCHAR * const pcTaskRestartMsg = "COM resynced\r\n";
\r
195 const portCHAR * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
196 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
197 const portCHAR *pcExpectedChar;
\r
198 portBASE_TYPE xGotChar;
\r
199 portCHAR cRxedChar;
\r
200 portSHORT sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
202 /* Stop warnings. */
\r
203 ( void ) pvParameters;
\r
205 /* Queue a message for printing to say the task has started. */
\r
206 vPrintDisplayMessage( &pcTaskStartMsg );
\r
208 /* The first expected character is the first character in the string. */
\r
209 pcExpectedChar = pcMessageToExchange;
\r
210 sResyncRequired = pdFALSE;
\r
211 sConsecutiveErrors = 0;
\r
212 sLatchedError = pdFALSE;
\r
216 /* Receive a message from the com port interrupt routine. If a message is
\r
217 not yet available the call will block the task. */
\r
218 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
219 if( xGotChar == pdTRUE )
\r
221 if( sResyncRequired == pdTRUE )
\r
223 /* We got out of sequence and are waiting for the start of the next
\r
224 transmission of the string. */
\r
225 if( cRxedChar == '\n' )
\r
227 /* This is the end of the message so we can start again - with
\r
228 the first character in the string being the next thing we expect
\r
230 pcExpectedChar = pcMessageToExchange;
\r
231 sResyncRequired = pdFALSE;
\r
233 /* Queue a message for printing to say that we are going to try
\r
235 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
237 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
238 sConsecutiveErrors++;
\r
239 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
241 sLatchedError = pdTRUE;
\r
247 /* We have received a character, but is it the expected character? */
\r
248 if( cRxedChar != *pcExpectedChar )
\r
250 /* This was not the expected character so post a message for
\r
251 printing to say that an error has occurred. We will then wait
\r
252 to resynchronise. */
\r
253 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
254 sResyncRequired = pdTRUE;
\r
258 /* This was the expected character so next time we will expect
\r
259 the next character in the string. Wrap back to the beginning
\r
260 of the string when the null terminator has been reached. */
\r
262 if( *pcExpectedChar == '\0' )
\r
264 pcExpectedChar = pcMessageToExchange;
\r
266 /* We have got through the entire string without error. */
\r
267 sConsecutiveErrors = 0;
\r
272 /* Increment the count that is used to check that this task is still
\r
273 running. This is only done if an error has never occurred. */
\r
274 if( sLatchedError == pdFALSE )
\r
281 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
284 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
285 /*-----------------------------------------------------------*/
\r
287 static void vSemTestTask( void * pvParameters )
\r
289 const portCHAR * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
291 /* Stop warnings. */
\r
292 ( void ) pvParameters;
\r
294 /* Queue a message for printing to say the task has started. */
\r
295 vPrintDisplayMessage( &pcTaskStartMsg );
\r
299 if( xSerialWaitForSemaphore( xPort ) )
\r
304 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
305 /*-----------------------------------------------------------*/
\r
307 /* This is called to check that all the created tasks are still running. */
\r
308 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
310 static portSHORT sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
311 portBASE_TYPE xReturn;
\r
313 /* Not too worried about mutual exclusion on these variables as they are 16
\r
314 bits and we are only reading them. We also only care to see if they have
\r
317 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
326 sLastTxCount = sTxCount;
\r
327 sLastRxCount = sRxCount;
\r
328 sLastSemCount = sSemCount;
\r