2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\r
11 * Purchasing FreeRTOS documentation will not only help you, by *
\r
12 * ensuring you get running as quickly as possible and with an *
\r
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
14 * the FreeRTOS project to continue with its mission of providing *
\r
15 * professional grade, cross platform, de facto standard solutions *
\r
16 * for microcontrollers - completely free of charge! *
\r
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\r
27 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
28 the terms of the GNU General Public License (version 2) as published by the
\r
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
30 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
31 distribute a combined work that includes FreeRTOS without being obliged to
\r
32 provide the source code for proprietary components outside of the FreeRTOS
\r
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
36 more details. You should have received a copy of the GNU General Public
\r
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
39 by writing to Richard Barry, contact details for whom are available on the
\r
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\r
55 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
56 * connector should be used so that everything that is transmitted is also received.
\r
57 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
58 * pins two and three should be connected together.
\r
60 * The first task repeatedly sends a string to a queue, character at a time. The
\r
61 * serial port interrupt will empty the queue and transmit the characters. The
\r
62 * task blocks for a pseudo random period before resending the string.
\r
64 * The second task blocks on a queue waiting for a character to be received.
\r
65 * Characters received by the serial port interrupt routine are posted onto the
\r
66 * queue - unblocking the task making it ready to execute. If this is then the
\r
67 * highest priority task ready to run it will run immediately - with a context
\r
68 * switch occurring at the end of the interrupt service routine. The task
\r
69 * receiving characters is spawned with a higher priority than the task
\r
70 * transmitting the characters.
\r
72 * With the loop back connector in place, one task will transmit a string and the
\r
73 * other will immediately receive it. The receiving task knows the string it
\r
74 * expects to receive so can detect an error.
\r
76 * This also creates a third task. This is used to test semaphore usage from an
\r
77 * ISR and does nothing interesting.
\r
79 * \page ComTestC comtest.c
\r
80 * \ingroup DemoFiles
\r
87 + The priority of the Rx task has been lowered. Received characters are
\r
88 now processed (read from the queue) at the idle priority, allowing low
\r
89 priority tasks to run evenly at times of a high communications overhead.
\r
93 + The Tx task now waits a pseudo random time between transissions.
\r
94 Previously a fixed period was used but this was not such a good test as
\r
95 interrupts fired at regular intervals.
\r
97 Changes From V1.2.0:
\r
99 + Use vSerialPutString() instead of single character puts.
\r
100 + Only stop the check variable incrementing after two consecutive errors.
\r
102 Changed from V1.2.5
\r
104 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
105 only 1. This is done to tie in better with the other demo application
\r
108 Changes from V2.0.0
\r
110 + Delay periods are now specified using variables and constants of
\r
111 portTickType rather than unsigned long.
\r
112 + Slight modification to task priorities.
\r
117 /* Scheduler include files. */
\r
118 #include <stdlib.h>
\r
119 #include <string.h>
\r
120 #include "FreeRTOS.h"
\r
123 /* Demo program include files. */
\r
124 #include "serial.h"
\r
125 #include "comtest.h"
\r
128 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
129 interval. This is the maximum and minimum block time between sends. */
\r
130 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x15e )
\r
131 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0xc8 )
\r
133 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
135 #define comSTACK_SIZE ( ( unsigned short ) 256 )
\r
137 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
139 /* Handle to the com port used by both tasks. */
\r
140 static xComPortHandle xPort;
\r
142 /* The transmit function as described at the top of the file. */
\r
143 static void vComTxTask( void *pvParameters );
\r
145 /* The receive function as described at the top of the file. */
\r
146 static void vComRxTask( void *pvParameters );
\r
148 /* The semaphore test function as described at the top of the file. */
\r
149 static void vSemTestTask( void * pvParameters );
\r
151 /* The string that is repeatedly transmitted. */
\r
152 const char * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
153 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
155 /* Variables that are incremented on each cycle of each task. These are used to
\r
156 check that both tasks are still executing. */
\r
157 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
159 /* The handle to the semaphore test task. */
\r
160 static xTaskHandle xSemTestTaskHandle = NULL;
\r
162 /*-----------------------------------------------------------*/
\r
164 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
166 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
168 /* Initialise the com port then spawn both tasks. */
\r
169 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
170 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
171 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
172 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
174 /*-----------------------------------------------------------*/
\r
176 static void vComTxTask( void *pvParameters )
\r
178 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
179 portTickType xTimeToWait;
\r
181 /* Stop warnings. */
\r
182 ( void ) pvParameters;
\r
184 /* Queue a message for printing to say the task has started. */
\r
185 vPrintDisplayMessage( &pcTaskStartMsg );
\r
189 /* Send the string to the serial port. */
\r
190 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
192 /* We have posted all the characters in the string - increment the variable
\r
193 used to check that this task is still running, then wait before re-sending
\r
197 xTimeToWait = xTaskGetTickCount();
\r
199 /* Make sure we don't wait too long... */
\r
200 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
202 /* ...but we do want to wait. */
\r
203 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
205 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
208 vTaskDelay( xTimeToWait );
\r
210 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
211 /*-----------------------------------------------------------*/
\r
213 static void vComRxTask( void *pvParameters )
\r
215 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
216 const char * const pcTaskErrorMsg = "COM read error\r\n";
\r
217 const char * const pcTaskRestartMsg = "COM resynced\r\n";
\r
218 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
219 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;
\r
220 const char *pcExpectedChar;
\r
221 portBASE_TYPE xGotChar;
\r
223 short sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
225 /* Stop warnings. */
\r
226 ( void ) pvParameters;
\r
228 /* Queue a message for printing to say the task has started. */
\r
229 vPrintDisplayMessage( &pcTaskStartMsg );
\r
231 /* The first expected character is the first character in the string. */
\r
232 pcExpectedChar = pcMessageToExchange;
\r
233 sResyncRequired = pdFALSE;
\r
234 sConsecutiveErrors = 0;
\r
235 sLatchedError = pdFALSE;
\r
239 /* Receive a message from the com port interrupt routine. If a message is
\r
240 not yet available the call will block the task. */
\r
241 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
242 if( xGotChar == pdTRUE )
\r
244 if( sResyncRequired == pdTRUE )
\r
246 /* We got out of sequence and are waiting for the start of the next
\r
247 transmission of the string. */
\r
248 if( cRxedChar == '\n' )
\r
250 /* This is the end of the message so we can start again - with
\r
251 the first character in the string being the next thing we expect
\r
253 pcExpectedChar = pcMessageToExchange;
\r
254 sResyncRequired = pdFALSE;
\r
256 /* Queue a message for printing to say that we are going to try
\r
258 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
260 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
261 sConsecutiveErrors++;
\r
262 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
264 sLatchedError = pdTRUE;
\r
270 /* We have received a character, but is it the expected character? */
\r
271 if( cRxedChar != *pcExpectedChar )
\r
273 /* This was not the expected character so post a message for
\r
274 printing to say that an error has occurred. We will then wait
\r
275 to resynchronise. */
\r
276 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
277 sResyncRequired = pdTRUE;
\r
281 /* This was the expected character so next time we will expect
\r
282 the next character in the string. Wrap back to the beginning
\r
283 of the string when the null terminator has been reached. */
\r
285 if( *pcExpectedChar == '\0' )
\r
287 pcExpectedChar = pcMessageToExchange;
\r
289 /* We have got through the entire string without error. */
\r
290 sConsecutiveErrors = 0;
\r
295 /* Increment the count that is used to check that this task is still
\r
296 running. This is only done if an error has never occurred. */
\r
297 if( sLatchedError == pdFALSE )
\r
304 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
307 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
308 /*-----------------------------------------------------------*/
\r
310 static void vSemTestTask( void * pvParameters )
\r
312 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
313 portBASE_TYPE xError = pdFALSE;
\r
315 /* Stop warnings. */
\r
316 ( void ) pvParameters;
\r
318 /* Queue a message for printing to say the task has started. */
\r
319 vPrintDisplayMessage( &pcTaskStartMsg );
\r
323 if( xSerialWaitForSemaphore( xPort ) )
\r
325 if( xError == pdFALSE )
\r
335 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
336 /*-----------------------------------------------------------*/
\r
338 /* This is called to check that all the created tasks are still running. */
\r
339 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
341 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
342 portBASE_TYPE xReturn;
\r
344 /* Not too worried about mutual exclusion on these variables as they are 16
\r
345 bits and we are only reading them. We also only care to see if they have
\r
348 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
357 sLastTxCount = sTxCount;
\r
358 sLastRxCount = sRxCount;
\r
359 sLastSemCount = sSemCount;
\r
363 /*-----------------------------------------------------------*/
\r
365 void vComTestUnsuspendTask( void )
\r
367 /* The task that is suspended on the semaphore will be referenced from the
\r
368 Suspended list as it is blocking indefinitely. This call just checks that
\r
369 the kernel correctly detects this and does not attempt to unsuspend the
\r
371 xTaskResumeFromISR( xSemTestTaskHandle );
\r