2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
70 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
71 * connector should be used so that everything that is transmitted is also received.
\r
72 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
73 * pins two and three should be connected together.
\r
75 * The first task repeatedly sends a string to a queue, character at a time. The
\r
76 * serial port interrupt will empty the queue and transmit the characters. The
\r
77 * task blocks for a pseudo random period before resending the string.
\r
79 * The second task blocks on a queue waiting for a character to be received.
\r
80 * Characters received by the serial port interrupt routine are posted onto the
\r
81 * queue - unblocking the task making it ready to execute. If this is then the
\r
82 * highest priority task ready to run it will run immediately - with a context
\r
83 * switch occurring at the end of the interrupt service routine. The task
\r
84 * receiving characters is spawned with a higher priority than the task
\r
85 * transmitting the characters.
\r
87 * With the loop back connector in place, one task will transmit a string and the
\r
88 * other will immediately receive it. The receiving task knows the string it
\r
89 * expects to receive so can detect an error.
\r
91 * This also creates a third task. This is used to test semaphore usage from an
\r
92 * ISR and does nothing interesting.
\r
94 * \page ComTestC comtest.c
\r
95 * \ingroup DemoFiles
\r
100 Changes from V1.00:
\r
102 + The priority of the Rx task has been lowered. Received characters are
\r
103 now processed (read from the queue) at the idle priority, allowing low
\r
104 priority tasks to run evenly at times of a high communications overhead.
\r
106 Changes from V1.01:
\r
108 + The Tx task now waits a pseudo random time between transissions.
\r
109 Previously a fixed period was used but this was not such a good test as
\r
110 interrupts fired at regular intervals.
\r
112 Changes From V1.2.0:
\r
114 + Use vSerialPutString() instead of single character puts.
\r
115 + Only stop the check variable incrementing after two consecutive errors.
\r
117 Changed from V1.2.5
\r
119 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
120 only 1. This is done to tie in better with the other demo application
\r
123 Changes from V2.0.0
\r
125 + Delay periods are now specified using variables and constants of
\r
126 portTickType rather than unsigned long.
\r
127 + Slight modification to task priorities.
\r
132 /* Scheduler include files. */
\r
133 #include <stdlib.h>
\r
134 #include <string.h>
\r
135 #include "FreeRTOS.h"
\r
138 /* Demo program include files. */
\r
139 #include "serial.h"
\r
140 #include "comtest.h"
\r
143 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
144 interval. This is the maximum and minimum block time between sends. */
\r
145 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
146 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
148 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
150 #define comSTACK_SIZE ( ( unsigned short ) 256 )
\r
152 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
154 /* Handle to the com port used by both tasks. */
\r
155 static xComPortHandle xPort;
\r
157 /* The transmit function as described at the top of the file. */
\r
158 static void vComTxTask( void *pvParameters );
\r
160 /* The receive function as described at the top of the file. */
\r
161 static void vComRxTask( void *pvParameters );
\r
163 /* The semaphore test function as described at the top of the file. */
\r
164 static void vSemTestTask( void * pvParameters );
\r
166 /* The string that is repeatedly transmitted. */
\r
167 const char * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
168 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
170 /* Variables that are incremented on each cycle of each task. These are used to
\r
171 check that both tasks are still executing. */
\r
172 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
174 /* The handle to the semaphore test task. */
\r
175 static xTaskHandle xSemTestTaskHandle = NULL;
\r
177 /*-----------------------------------------------------------*/
\r
179 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
181 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
183 /* Initialise the com port then spawn both tasks. */
\r
184 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
185 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
186 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
187 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
189 /*-----------------------------------------------------------*/
\r
191 static void vComTxTask( void *pvParameters )
\r
193 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
194 portTickType xTimeToWait;
\r
196 /* Stop warnings. */
\r
197 ( void ) pvParameters;
\r
199 /* Queue a message for printing to say the task has started. */
\r
200 vPrintDisplayMessage( &pcTaskStartMsg );
\r
204 /* Send the string to the serial port. */
\r
205 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
207 /* We have posted all the characters in the string - increment the variable
\r
208 used to check that this task is still running, then wait before re-sending
\r
212 xTimeToWait = xTaskGetTickCount();
\r
214 /* Make sure we don't wait too long... */
\r
215 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
217 /* ...but we do want to wait. */
\r
218 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
220 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
223 vTaskDelay( xTimeToWait );
\r
225 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
226 /*-----------------------------------------------------------*/
\r
228 static void vComRxTask( void *pvParameters )
\r
230 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
231 const char * const pcTaskErrorMsg = "COM read error\r\n";
\r
232 const char * const pcTaskRestartMsg = "COM resynced\r\n";
\r
233 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
234 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
235 const char *pcExpectedChar;
\r
236 portBASE_TYPE xGotChar;
\r
238 short sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
240 /* Stop warnings. */
\r
241 ( void ) pvParameters;
\r
243 /* Queue a message for printing to say the task has started. */
\r
244 vPrintDisplayMessage( &pcTaskStartMsg );
\r
246 /* The first expected character is the first character in the string. */
\r
247 pcExpectedChar = pcMessageToExchange;
\r
248 sResyncRequired = pdFALSE;
\r
249 sConsecutiveErrors = 0;
\r
250 sLatchedError = pdFALSE;
\r
254 /* Receive a message from the com port interrupt routine. If a message is
\r
255 not yet available the call will block the task. */
\r
256 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
257 if( xGotChar == pdTRUE )
\r
259 if( sResyncRequired == pdTRUE )
\r
261 /* We got out of sequence and are waiting for the start of the next
\r
262 transmission of the string. */
\r
263 if( cRxedChar == '\n' )
\r
265 /* This is the end of the message so we can start again - with
\r
266 the first character in the string being the next thing we expect
\r
268 pcExpectedChar = pcMessageToExchange;
\r
269 sResyncRequired = pdFALSE;
\r
271 /* Queue a message for printing to say that we are going to try
\r
273 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
275 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
276 sConsecutiveErrors++;
\r
277 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
279 sLatchedError = pdTRUE;
\r
285 /* We have received a character, but is it the expected character? */
\r
286 if( cRxedChar != *pcExpectedChar )
\r
288 /* This was not the expected character so post a message for
\r
289 printing to say that an error has occurred. We will then wait
\r
290 to resynchronise. */
\r
291 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
292 sResyncRequired = pdTRUE;
\r
296 /* This was the expected character so next time we will expect
\r
297 the next character in the string. Wrap back to the beginning
\r
298 of the string when the null terminator has been reached. */
\r
300 if( *pcExpectedChar == '\0' )
\r
302 pcExpectedChar = pcMessageToExchange;
\r
304 /* We have got through the entire string without error. */
\r
305 sConsecutiveErrors = 0;
\r
310 /* Increment the count that is used to check that this task is still
\r
311 running. This is only done if an error has never occurred. */
\r
312 if( sLatchedError == pdFALSE )
\r
319 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
322 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
323 /*-----------------------------------------------------------*/
\r
325 static void vSemTestTask( void * pvParameters )
\r
327 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
328 portBASE_TYPE xError = pdFALSE;
\r
330 /* Stop warnings. */
\r
331 ( void ) pvParameters;
\r
333 /* Queue a message for printing to say the task has started. */
\r
334 vPrintDisplayMessage( &pcTaskStartMsg );
\r
338 if( xSerialWaitForSemaphore( xPort ) )
\r
340 if( xError == pdFALSE )
\r
350 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
351 /*-----------------------------------------------------------*/
\r
353 /* This is called to check that all the created tasks are still running. */
\r
354 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
356 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
357 portBASE_TYPE xReturn;
\r
359 /* Not too worried about mutual exclusion on these variables as they are 16
\r
360 bits and we are only reading them. We also only care to see if they have
\r
363 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
372 sLastTxCount = sTxCount;
\r
373 sLastRxCount = sRxCount;
\r
374 sLastSemCount = sSemCount;
\r
378 /*-----------------------------------------------------------*/
\r
380 void vComTestUnsuspendTask( void )
\r
382 /* The task that is suspended on the semaphore will be referenced from the
\r
383 Suspended list as it is blocking indefinitely. This call just checks that
\r
384 the kernel correctly detects this and does not attempt to unsuspend the
\r
386 xTaskResumeFromISR( xSemTestTaskHandle );
\r