2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
97 * Creates two tasks that operate on an interrupt driven serial port. A loopback
\r
98 * connector should be used so that everything that is transmitted is also received.
\r
99 * The serial port does not use any flow control. On a standard 9way 'D' connector
\r
100 * pins two and three should be connected together.
\r
102 * The first task repeatedly sends a string to a queue, character at a time. The
\r
103 * serial port interrupt will empty the queue and transmit the characters. The
\r
104 * task blocks for a pseudo random period before resending the string.
\r
106 * The second task blocks on a queue waiting for a character to be received.
\r
107 * Characters received by the serial port interrupt routine are posted onto the
\r
108 * queue - unblocking the task making it ready to execute. If this is then the
\r
109 * highest priority task ready to run it will run immediately - with a context
\r
110 * switch occurring at the end of the interrupt service routine. The task
\r
111 * receiving characters is spawned with a higher priority than the task
\r
112 * transmitting the characters.
\r
114 * With the loop back connector in place, one task will transmit a string and the
\r
115 * other will immediately receive it. The receiving task knows the string it
\r
116 * expects to receive so can detect an error.
\r
118 * This also creates a third task. This is used to test semaphore usage from an
\r
119 * ISR and does nothing interesting.
\r
121 * \page ComTestC comtest.c
\r
122 * \ingroup DemoFiles
\r
127 Changes from V1.00:
\r
129 + The priority of the Rx task has been lowered. Received characters are
\r
130 now processed (read from the queue) at the idle priority, allowing low
\r
131 priority tasks to run evenly at times of a high communications overhead.
\r
133 Changes from V1.01:
\r
135 + The Tx task now waits a pseudo random time between transissions.
\r
136 Previously a fixed period was used but this was not such a good test as
\r
137 interrupts fired at regular intervals.
\r
139 Changes From V1.2.0:
\r
141 + Use vSerialPutString() instead of single character puts.
\r
142 + Only stop the check variable incrementing after two consecutive errors.
\r
144 Changed from V1.2.5
\r
146 + Made the Rx task 2 priorities higher than the Tx task. Previously it was
\r
147 only 1. This is done to tie in better with the other demo application
\r
150 Changes from V2.0.0
\r
152 + Delay periods are now specified using variables and constants of
\r
153 TickType_t rather than unsigned long.
\r
154 + Slight modification to task priorities.
\r
159 /* Scheduler include files. */
\r
160 #include <stdlib.h>
\r
161 #include <string.h>
\r
162 #include "FreeRTOS.h"
\r
165 /* Demo program include files. */
\r
166 #include "serial.h"
\r
167 #include "comtest.h"
\r
170 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
171 interval. This is the maximum and minimum block time between sends. */
\r
172 #define comTX_MAX_BLOCK_TIME ( ( TickType_t ) 0x15e )
\r
173 #define comTX_MIN_BLOCK_TIME ( ( TickType_t ) 0xc8 )
\r
175 #define comMAX_CONSECUTIVE_ERRORS ( 2 )
\r
177 #define comSTACK_SIZE ( ( unsigned short ) 256 )
\r
179 #define comRX_RELATIVE_PRIORITY ( 1 )
\r
181 /* Handle to the com port used by both tasks. */
\r
182 static xComPortHandle xPort;
\r
184 /* The transmit function as described at the top of the file. */
\r
185 static void vComTxTask( void *pvParameters );
\r
187 /* The receive function as described at the top of the file. */
\r
188 static void vComRxTask( void *pvParameters );
\r
190 /* The semaphore test function as described at the top of the file. */
\r
191 static void vSemTestTask( void * pvParameters );
\r
193 /* The string that is repeatedly transmitted. */
\r
194 const char * const pcMessageToExchange = "Send this message over and over again to check communications interrupts. "
\r
195 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";
\r
197 /* Variables that are incremented on each cycle of each task. These are used to
\r
198 check that both tasks are still executing. */
\r
199 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;
\r
201 /* The handle to the semaphore test task. */
\r
202 static TaskHandle_t xSemTestTaskHandle = NULL;
\r
204 /*-----------------------------------------------------------*/
\r
206 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )
\r
208 const unsigned portBASE_TYPE uxBufferLength = 255;
\r
210 /* Initialise the com port then spawn both tasks. */
\r
211 xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );
\r
212 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );
\r
213 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );
\r
214 xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );
\r
216 /*-----------------------------------------------------------*/
\r
218 static void vComTxTask( void *pvParameters )
\r
220 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";
\r
221 TickType_t xTimeToWait;
\r
223 /* Stop warnings. */
\r
224 ( void ) pvParameters;
\r
226 /* Queue a message for printing to say the task has started. */
\r
227 vPrintDisplayMessage( &pcTaskStartMsg );
\r
231 /* Send the string to the serial port. */
\r
232 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );
\r
234 /* We have posted all the characters in the string - increment the variable
\r
235 used to check that this task is still running, then wait before re-sending
\r
239 xTimeToWait = xTaskGetTickCount();
\r
241 /* Make sure we don't wait too long... */
\r
242 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
244 /* ...but we do want to wait. */
\r
245 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
247 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
250 vTaskDelay( xTimeToWait );
\r
252 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
253 /*-----------------------------------------------------------*/
\r
255 static void vComRxTask( void *pvParameters )
\r
257 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";
\r
258 const char * const pcTaskErrorMsg = "COM read error\r\n";
\r
259 const char * const pcTaskRestartMsg = "COM resynced\r\n";
\r
260 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";
\r
261 const TickType_t xBlockTime = ( TickType_t ) 0xffff / portTICK_PERIOD_MS;
\r
262 const char *pcExpectedChar;
\r
263 portBASE_TYPE xGotChar;
\r
265 short sResyncRequired, sConsecutiveErrors, sLatchedError;
\r
267 /* Stop warnings. */
\r
268 ( void ) pvParameters;
\r
270 /* Queue a message for printing to say the task has started. */
\r
271 vPrintDisplayMessage( &pcTaskStartMsg );
\r
273 /* The first expected character is the first character in the string. */
\r
274 pcExpectedChar = pcMessageToExchange;
\r
275 sResyncRequired = pdFALSE;
\r
276 sConsecutiveErrors = 0;
\r
277 sLatchedError = pdFALSE;
\r
281 /* Receive a message from the com port interrupt routine. If a message is
\r
282 not yet available the call will block the task. */
\r
283 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );
\r
284 if( xGotChar == pdTRUE )
\r
286 if( sResyncRequired == pdTRUE )
\r
288 /* We got out of sequence and are waiting for the start of the next
\r
289 transmission of the string. */
\r
290 if( cRxedChar == '\n' )
\r
292 /* This is the end of the message so we can start again - with
\r
293 the first character in the string being the next thing we expect
\r
295 pcExpectedChar = pcMessageToExchange;
\r
296 sResyncRequired = pdFALSE;
\r
298 /* Queue a message for printing to say that we are going to try
\r
300 vPrintDisplayMessage( &pcTaskRestartMsg );
\r
302 /* Stop incrementing the check variable, if consecutive errors occur. */
\r
303 sConsecutiveErrors++;
\r
304 if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )
\r
306 sLatchedError = pdTRUE;
\r
312 /* We have received a character, but is it the expected character? */
\r
313 if( cRxedChar != *pcExpectedChar )
\r
315 /* This was not the expected character so post a message for
\r
316 printing to say that an error has occurred. We will then wait
\r
317 to resynchronise. */
\r
318 vPrintDisplayMessage( &pcTaskErrorMsg );
\r
319 sResyncRequired = pdTRUE;
\r
323 /* This was the expected character so next time we will expect
\r
324 the next character in the string. Wrap back to the beginning
\r
325 of the string when the null terminator has been reached. */
\r
327 if( *pcExpectedChar == '\0' )
\r
329 pcExpectedChar = pcMessageToExchange;
\r
331 /* We have got through the entire string without error. */
\r
332 sConsecutiveErrors = 0;
\r
337 /* Increment the count that is used to check that this task is still
\r
338 running. This is only done if an error has never occurred. */
\r
339 if( sLatchedError == pdFALSE )
\r
346 vPrintDisplayMessage( &pcTaskTimeoutMsg );
\r
349 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
350 /*-----------------------------------------------------------*/
\r
352 static void vSemTestTask( void * pvParameters )
\r
354 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";
\r
355 portBASE_TYPE xError = pdFALSE;
\r
357 /* Stop warnings. */
\r
358 ( void ) pvParameters;
\r
360 /* Queue a message for printing to say the task has started. */
\r
361 vPrintDisplayMessage( &pcTaskStartMsg );
\r
365 if( xSerialWaitForSemaphore( xPort ) )
\r
367 if( xError == pdFALSE )
\r
377 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */
\r
378 /*-----------------------------------------------------------*/
\r
380 /* This is called to check that all the created tasks are still running. */
\r
381 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
383 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;
\r
384 portBASE_TYPE xReturn;
\r
386 /* Not too worried about mutual exclusion on these variables as they are 16
\r
387 bits and we are only reading them. We also only care to see if they have
\r
390 if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )
\r
399 sLastTxCount = sTxCount;
\r
400 sLastRxCount = sRxCount;
\r
401 sLastSemCount = sSemCount;
\r
405 /*-----------------------------------------------------------*/
\r
407 void vComTestUnsuspendTask( void )
\r
409 /* The task that is suspended on the semaphore will be referenced from the
\r
410 Suspended list as it is blocking indefinitely. This call just checks that
\r
411 the kernel correctly detects this and does not attempt to unsuspend the
\r
413 xTaskResumeFromISR( xSemTestTaskHandle );
\r