2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
66 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
67 * connector should be used so that everything that is transmitted is also received.
\r
68 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
69 * pins two and three should be connected together.
\r
71 * The first task repeatedly sends a string to a queue, character at a time. The
\r
72 * serial port interrupt will empty the queue and transmit the characters. The
\r
73 * task blocks for a pseudo random period before resending the string.
\r
75 * The second task blocks on a queue waiting for a character to be received.
\r
76 * Characters received by the serial port interrupt routine are posted onto the
\r
77 * queue - unblocking the task making it ready to execute. If this is then the
\r
78 * highest priority task ready to run it will run immediately - with a context
\r
79 * switch occurring at the end of the interrupt service routine. The task
\r
80 * receiving characters is spawned with a higher priority than the task
\r
81 * transmitting the characters.
\r
83 * With the loop back connector in place, one task will transmit a string and the
\r
84 * other will immediately receive it. The receiving task knows the string it
\r
85 * expects to receive so can detect an error.
\r
87 * This also creates a third task. This is used to test semaphore usage from an
\r
88 * ISR and does nothing interesting.
\r
90 * \page ComTestC comtest.c
\r
91 * \ingroup DemoFiles
\r
98 + The priority of the Rx task has been lowered. Received characters are
\r
99 now processed (read from the queue) at the idle priority, allowing low
\r
100 priority tasks to run evenly at times of a high communications overhead.
\r
102 Changes from V1.01:
\r
104 + The Tx task now waits a pseudo random time between transissions.
\r
105 Previously a fixed period was used but this was not such a good test as
\r
106 interrupts fired at regular intervals.
\r
108 Changes From V1.2.0:
\r
110 + Use vSerialPutString() instead of single character puts.
\r
111 + Only stop the check variable incrementing after two consecutive errors.
\r
113 Changed from V1.2.5
\r
115 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
116 only 1. This is done to tie in better with the other demo application
\r
119 Changes from V2.0.0
\r
121 + Delay periods are now specified using variables and constants of
\r
122 portTickType rather than unsigned long.
\r
123 + Slight modification to task priorities.
\r
128 /* Scheduler include files. */
\r
129 #include <stdlib.h>
\r
130 #include <string.h>
\r
131 #include "FreeRTOS.h"
\r
134 /* Demo program include files. */
\r
135 #include "serial.h"
\r
136 #include "comtest.h"
\r
139 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
140 interval. This is the maximum and minimum block time between sends. */
\r
141 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
142 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
144 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
146 #define comSTACK_SIZE ( ( unsigned short ) 256 )
\r
148 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
150 /* Handle to the com port used by both tasks. */
\r
151 static xComPortHandle xPort;
\r
153 /* The transmit function as described at the top of the file. */
\r
154 static void vComTxTask( void *pvParameters );
\r
156 /* The receive function as described at the top of the file. */
\r
157 static void vComRxTask( void *pvParameters );
\r
159 /* The semaphore test function as described at the top of the file. */
\r
160 static void vSemTestTask( void * pvParameters );
\r
162 /* The string that is repeatedly transmitted. */
\r
163 const char * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
164 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
166 /* Variables that are incremented on each cycle of each task. These are used to
\r
167 check that both tasks are still executing. */
\r
168 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
170 /* The handle to the semaphore test task. */
\r
171 static xTaskHandle xSemTestTaskHandle = NULL;
\r
173 /*-----------------------------------------------------------*/
\r
175 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
177 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
179 /* Initialise the com port then spawn both tasks. */
\r
180 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
181 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
182 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
183 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
185 /*-----------------------------------------------------------*/
\r
187 static void vComTxTask( void *pvParameters )
\r
189 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
190 portTickType xTimeToWait;
\r
192 /* Stop warnings. */
\r
193 ( void ) pvParameters;
\r
195 /* Queue a message for printing to say the task has started. */
\r
196 vPrintDisplayMessage( &pcTaskStartMsg );
\r
200 /* Send the string to the serial port. */
\r
201 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
203 /* We have posted all the characters in the string - increment the variable
\r
204 used to check that this task is still running, then wait before re-sending
\r
208 xTimeToWait = xTaskGetTickCount();
\r
210 /* Make sure we don't wait too long... */
\r
211 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
213 /* ...but we do want to wait. */
\r
214 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
216 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
219 vTaskDelay( xTimeToWait );
\r
221 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
222 /*-----------------------------------------------------------*/
\r
224 static void vComRxTask( void *pvParameters )
\r
226 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
227 const char * const pcTaskErrorMsg = "COM read error\r\n";
\r
228 const char * const pcTaskRestartMsg = "COM resynced\r\n";
\r
229 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
230 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
231 const char *pcExpectedChar;
\r
232 portBASE_TYPE xGotChar;
\r
234 short sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
236 /* Stop warnings. */
\r
237 ( void ) pvParameters;
\r
239 /* Queue a message for printing to say the task has started. */
\r
240 vPrintDisplayMessage( &pcTaskStartMsg );
\r
242 /* The first expected character is the first character in the string. */
\r
243 pcExpectedChar = pcMessageToExchange;
\r
244 sResyncRequired = pdFALSE;
\r
245 sConsecutiveErrors = 0;
\r
246 sLatchedError = pdFALSE;
\r
250 /* Receive a message from the com port interrupt routine. If a message is
\r
251 not yet available the call will block the task. */
\r
252 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
253 if( xGotChar == pdTRUE )
\r
255 if( sResyncRequired == pdTRUE )
\r
257 /* We got out of sequence and are waiting for the start of the next
\r
258 transmission of the string. */
\r
259 if( cRxedChar == '\n' )
\r
261 /* This is the end of the message so we can start again - with
\r
262 the first character in the string being the next thing we expect
\r
264 pcExpectedChar = pcMessageToExchange;
\r
265 sResyncRequired = pdFALSE;
\r
267 /* Queue a message for printing to say that we are going to try
\r
269 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
271 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
272 sConsecutiveErrors++;
\r
273 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
275 sLatchedError = pdTRUE;
\r
281 /* We have received a character, but is it the expected character? */
\r
282 if( cRxedChar != *pcExpectedChar )
\r
284 /* This was not the expected character so post a message for
\r
285 printing to say that an error has occurred. We will then wait
\r
286 to resynchronise. */
\r
287 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
288 sResyncRequired = pdTRUE;
\r
292 /* This was the expected character so next time we will expect
\r
293 the next character in the string. Wrap back to the beginning
\r
294 of the string when the null terminator has been reached. */
\r
296 if( *pcExpectedChar == '\0' )
\r
298 pcExpectedChar = pcMessageToExchange;
\r
300 /* We have got through the entire string without error. */
\r
301 sConsecutiveErrors = 0;
\r
306 /* Increment the count that is used to check that this task is still
\r
307 running. This is only done if an error has never occurred. */
\r
308 if( sLatchedError == pdFALSE )
\r
315 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
318 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
319 /*-----------------------------------------------------------*/
\r
321 static void vSemTestTask( void * pvParameters )
\r
323 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
324 portBASE_TYPE xError = pdFALSE;
\r
326 /* Stop warnings. */
\r
327 ( void ) pvParameters;
\r
329 /* Queue a message for printing to say the task has started. */
\r
330 vPrintDisplayMessage( &pcTaskStartMsg );
\r
334 if( xSerialWaitForSemaphore( xPort ) )
\r
336 if( xError == pdFALSE )
\r
346 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
347 /*-----------------------------------------------------------*/
\r
349 /* This is called to check that all the created tasks are still running. */
\r
350 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
352 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
353 portBASE_TYPE xReturn;
\r
355 /* Not too worried about mutual exclusion on these variables as they are 16
\r
356 bits and we are only reading them. We also only care to see if they have
\r
359 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
368 sLastTxCount = sTxCount;
\r
369 sLastRxCount = sRxCount;
\r
370 sLastSemCount = sSemCount;
\r
374 /*-----------------------------------------------------------*/
\r
376 void vComTestUnsuspendTask( void )
\r
378 /* The task that is suspended on the semaphore will be referenced from the
\r
379 Suspended list as it is blocking indefinitely. This call just checks that
\r
380 the kernel correctly detects this and does not attempt to unsuspend the
\r
382 xTaskResumeFromISR( xSemTestTaskHandle );
\r