]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/comtest.c
Update version number ready to release the FAT file system demo.
[freertos] / FreeRTOS / Demo / Common / Full / comtest.c
1 /*\r
2     FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \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
6 \r
7     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
10      *    Complete, revised, and edited pdf reference manuals are also       *\r
11      *    available.                                                         *\r
12      *                                                                       *\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
19      *                                                                       *\r
20      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
21      *                                                                       *\r
22      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
23      *                                                                       *\r
24     ***************************************************************************\r
25 \r
26 \r
27     This file is part of the FreeRTOS distribution.\r
28 \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 \r
33     >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to\r
34     distribute a combined work that includes FreeRTOS without being obliged to\r
35     provide the source code for proprietary components outside of the FreeRTOS\r
36     kernel.\r
37 \r
38     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
39     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
40     FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more\r
41     details. You should have received a copy of the GNU General Public License\r
42     and the FreeRTOS license exception along with FreeRTOS; if not it can be\r
43     viewed here: http://www.freertos.org/a00114.html and also obtained by\r
44     writing to Real Time Engineers Ltd., contact details for whom are available\r
45     on the FreeRTOS WEB site.\r
46 \r
47     1 tab == 4 spaces!\r
48 \r
49     ***************************************************************************\r
50      *                                                                       *\r
51      *    Having a problem?  Start by reading the FAQ "My application does   *\r
52      *    not run, what could be wrong?"                                     *\r
53      *                                                                       *\r
54      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
55      *                                                                       *\r
56     ***************************************************************************\r
57 \r
58 \r
59     http://www.FreeRTOS.org - Documentation, books, training, latest versions, \r
60     license and Real Time Engineers Ltd. contact details.\r
61 \r
62     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
63     including FreeRTOS+Trace - an indispensable productivity tool, and our new\r
64     fully thread aware and reentrant UDP/IP stack.\r
65 \r
66     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High \r
67     Integrity Systems, who sell the code with commercial support, \r
68     indemnification and middleware, under the OpenRTOS brand.\r
69     \r
70     http://www.SafeRTOS.com - High Integrity Systems also provide a safety \r
71     engineered and independently SIL3 certified version for use in safety and \r
72     mission critical applications that require provable dependability.\r
73 */\r
74 \r
75 /**\r
76  * Creates two tasks that operate on an interrupt driven serial port.  A loopback \r
77  * connector should be used so that everything that is transmitted is also received.  \r
78  * The serial port does not use any flow control.  On a standard 9way 'D' connector \r
79  * pins two and three should be connected together.\r
80  *\r
81  * The first task repeatedly sends a string to a queue, character at a time.  The \r
82  * serial port interrupt will empty the queue and transmit the characters.  The \r
83  * task blocks for a pseudo random period before resending the string.\r
84  *\r
85  * The second task blocks on a queue waiting for a character to be received.  \r
86  * Characters received by the serial port interrupt routine are posted onto the \r
87  * queue - unblocking the task making it ready to execute.  If this is then the \r
88  * highest priority task ready to run it will run immediately - with a context \r
89  * switch occurring at the end of the interrupt service routine.  The task \r
90  * receiving characters is spawned with a higher priority than the task \r
91  * transmitting the characters.\r
92  *\r
93  * With the loop back connector in place, one task will transmit a string and the \r
94  * other will immediately receive it.  The receiving task knows the string it \r
95  * expects to receive so can detect an error.\r
96  *\r
97  * This also creates a third task.  This is used to test semaphore usage from an\r
98  * ISR and does nothing interesting.  \r
99  * \r
100  * \page ComTestC comtest.c\r
101  * \ingroup DemoFiles\r
102  * <HR>\r
103  */\r
104 \r
105 /*\r
106 Changes from V1.00:\r
107         \r
108         + The priority of the Rx task has been lowered.  Received characters are\r
109           now processed (read from the queue) at the idle priority, allowing low\r
110           priority tasks to run evenly at times of a high communications overhead.\r
111 \r
112 Changes from V1.01:\r
113 \r
114         + The Tx task now waits a pseudo random time between transissions.\r
115           Previously a fixed period was used but this was not such a good test as\r
116           interrupts fired at regular intervals.\r
117 \r
118 Changes From V1.2.0:\r
119 \r
120         + Use vSerialPutString() instead of single character puts.\r
121         + Only stop the check variable incrementing after two consecutive errors. \r
122 \r
123 Changed from V1.2.5\r
124 \r
125         + Made the Rx task 2 priorities higher than the Tx task.  Previously it was\r
126           only 1.  This is done to tie in better with the other demo application \r
127           tasks.\r
128 \r
129 Changes from V2.0.0\r
130 \r
131         + Delay periods are now specified using variables and constants of\r
132           portTickType rather than unsigned long.\r
133         + Slight modification to task priorities.\r
134 \r
135 */\r
136 \r
137 \r
138 /* Scheduler include files. */\r
139 #include <stdlib.h>\r
140 #include <string.h>\r
141 #include "FreeRTOS.h"\r
142 #include "task.h"\r
143 \r
144 /* Demo program include files. */\r
145 #include "serial.h"\r
146 #include "comtest.h"\r
147 #include "print.h"\r
148 \r
149 /* The Tx task will transmit the sequence of characters at a pseudo random\r
150 interval.  This is the maximum and minimum block time between sends. */\r
151 #define comTX_MAX_BLOCK_TIME            ( ( portTickType ) 0x15e )\r
152 #define comTX_MIN_BLOCK_TIME            ( ( portTickType ) 0xc8 )\r
153 \r
154 #define comMAX_CONSECUTIVE_ERRORS       ( 2 )\r
155 \r
156 #define comSTACK_SIZE                           ( ( unsigned short ) 256 )\r
157 \r
158 #define comRX_RELATIVE_PRIORITY         ( 1 )\r
159 \r
160 /* Handle to the com port used by both tasks. */\r
161 static xComPortHandle xPort;\r
162 \r
163 /* The transmit function as described at the top of the file. */\r
164 static void vComTxTask( void *pvParameters );\r
165 \r
166 /* The receive function as described at the top of the file. */\r
167 static void vComRxTask( void *pvParameters );\r
168 \r
169 /* The semaphore test function as described at the top of the file. */\r
170 static void vSemTestTask( void * pvParameters );\r
171 \r
172 /* The string that is repeatedly transmitted. */\r
173 const char * const pcMessageToExchange =        "Send this message over and over again to check communications interrupts. "\r
174                                                                                                 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";\r
175 \r
176 /* Variables that are incremented on each cycle of each task.  These are used to \r
177 check that both tasks are still executing. */\r
178 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;\r
179 \r
180 /* The handle to the semaphore test task. */\r
181 static xTaskHandle xSemTestTaskHandle = NULL;\r
182 \r
183 /*-----------------------------------------------------------*/\r
184 \r
185 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )\r
186 {\r
187 const unsigned portBASE_TYPE uxBufferLength = 255;\r
188 \r
189         /* Initialise the com port then spawn both tasks. */\r
190         xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );\r
191         xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );\r
192         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );\r
193         xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static void vComTxTask( void *pvParameters )\r
198 {\r
199 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";\r
200 portTickType xTimeToWait;\r
201 \r
202         /* Stop warnings. */\r
203         ( void ) pvParameters;\r
204 \r
205         /* Queue a message for printing to say the task has started. */\r
206         vPrintDisplayMessage( &pcTaskStartMsg );\r
207 \r
208         for( ;; )\r
209         {\r
210                 /* Send the string to the serial port. */\r
211                 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );\r
212 \r
213                 /* We have posted all the characters in the string - increment the variable \r
214                 used to check that this task is still running, then wait before re-sending \r
215                 the string. */\r
216                 sTxCount++;\r
217 \r
218                 xTimeToWait = xTaskGetTickCount();\r
219 \r
220                 /* Make sure we don't wait too long... */\r
221                 xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
222 \r
223                 /* ...but we do want to wait. */\r
224                 if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
225                 {\r
226                         xTimeToWait = comTX_MIN_BLOCK_TIME;\r
227                 }\r
228 \r
229                 vTaskDelay( xTimeToWait );\r
230         }\r
231 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
232 /*-----------------------------------------------------------*/\r
233 \r
234 static void vComRxTask( void *pvParameters )\r
235 {\r
236 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";\r
237 const char * const pcTaskErrorMsg = "COM read error\r\n";\r
238 const char * const pcTaskRestartMsg = "COM resynced\r\n";\r
239 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";\r
240 const portTickType xBlockTime = ( portTickType ) 0xffff / portTICK_RATE_MS;\r
241 const char *pcExpectedChar;\r
242 portBASE_TYPE xGotChar;\r
243 char cRxedChar;\r
244 short sResyncRequired, sConsecutiveErrors, sLatchedError;\r
245 \r
246         /* Stop warnings. */\r
247         ( void ) pvParameters;\r
248 \r
249         /* Queue a message for printing to say the task has started. */\r
250         vPrintDisplayMessage( &pcTaskStartMsg );\r
251         \r
252         /* The first expected character is the first character in the string. */\r
253         pcExpectedChar = pcMessageToExchange;\r
254         sResyncRequired = pdFALSE;\r
255         sConsecutiveErrors = 0;\r
256         sLatchedError = pdFALSE;\r
257 \r
258         for( ;; )\r
259         {\r
260                 /* Receive a message from the com port interrupt routine.  If a message is \r
261                 not yet available the call will block the task. */\r
262                 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );\r
263                 if( xGotChar == pdTRUE )\r
264                 {\r
265                         if( sResyncRequired == pdTRUE )\r
266                         {\r
267                                 /* We got out of sequence and are waiting for the start of the next \r
268                                 transmission of the string. */\r
269                                 if( cRxedChar == '\n' )\r
270                                 {\r
271                                         /* This is the end of the message so we can start again - with \r
272                                         the first character in the string being the next thing we expect \r
273                                         to receive. */\r
274                                         pcExpectedChar = pcMessageToExchange;\r
275                                         sResyncRequired = pdFALSE;\r
276 \r
277                                         /* Queue a message for printing to say that we are going to try \r
278                                         again. */\r
279                                         vPrintDisplayMessage( &pcTaskRestartMsg );\r
280 \r
281                                         /* Stop incrementing the check variable, if consecutive errors occur. */\r
282                                         sConsecutiveErrors++;\r
283                                         if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )\r
284                                         {\r
285                                                 sLatchedError = pdTRUE;\r
286                                         }\r
287                                 }\r
288                         }\r
289                         else\r
290                         {\r
291                                 /* We have received a character, but is it the expected character? */\r
292                                 if( cRxedChar != *pcExpectedChar )\r
293                                 {\r
294                                         /* This was not the expected character so post a message for \r
295                                         printing to say that an error has occurred.  We will then wait \r
296                                         to resynchronise. */\r
297                                         vPrintDisplayMessage( &pcTaskErrorMsg );                                        \r
298                                         sResyncRequired = pdTRUE;\r
299                                 }\r
300                                 else\r
301                                 {\r
302                                         /* This was the expected character so next time we will expect \r
303                                         the next character in the string.  Wrap back to the beginning \r
304                                         of the string when the null terminator has been reached. */\r
305                                         pcExpectedChar++;\r
306                                         if( *pcExpectedChar == '\0' )\r
307                                         {\r
308                                                 pcExpectedChar = pcMessageToExchange;\r
309 \r
310                                                 /* We have got through the entire string without error. */\r
311                                                 sConsecutiveErrors = 0;\r
312                                         }\r
313                                 }\r
314                         }\r
315 \r
316                         /* Increment the count that is used to check that this task is still \r
317                         running.  This is only done if an error has never occurred. */\r
318                         if( sLatchedError == pdFALSE )\r
319                         {\r
320                                 sRxCount++;                     \r
321                         }\r
322                 }\r
323                 else\r
324                 {\r
325                         vPrintDisplayMessage( &pcTaskTimeoutMsg );\r
326                 }\r
327         }\r
328 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
329 /*-----------------------------------------------------------*/\r
330 \r
331 static void vSemTestTask( void * pvParameters )\r
332 {\r
333 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";\r
334 portBASE_TYPE xError = pdFALSE;\r
335 \r
336         /* Stop warnings. */\r
337         ( void ) pvParameters;\r
338 \r
339         /* Queue a message for printing to say the task has started. */\r
340         vPrintDisplayMessage( &pcTaskStartMsg );\r
341 \r
342         for( ;; )\r
343         {\r
344                 if( xSerialWaitForSemaphore( xPort ) )\r
345                 {\r
346                         if( xError == pdFALSE )\r
347                         {\r
348                                 sSemCount++;\r
349                         }\r
350                 }\r
351                 else\r
352                 {\r
353                         xError = pdTRUE;\r
354                 }\r
355         }\r
356 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */\r
357 /*-----------------------------------------------------------*/\r
358 \r
359 /* This is called to check that all the created tasks are still running. */\r
360 portBASE_TYPE xAreComTestTasksStillRunning( void )\r
361 {\r
362 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;\r
363 portBASE_TYPE xReturn;\r
364 \r
365         /* Not too worried about mutual exclusion on these variables as they are 16 \r
366         bits and we are only reading them.  We also only care to see if they have \r
367         changed or not. */\r
368 \r
369         if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )\r
370         {\r
371                 xReturn = pdFALSE;\r
372         }\r
373         else\r
374         {\r
375                 xReturn = pdTRUE;\r
376         }\r
377 \r
378         sLastTxCount = sTxCount;\r
379         sLastRxCount = sRxCount;\r
380         sLastSemCount = sSemCount;\r
381 \r
382         return xReturn;\r
383 }\r
384 /*-----------------------------------------------------------*/\r
385 \r
386 void vComTestUnsuspendTask( void )\r
387 {\r
388         /* The task that is suspended on the semaphore will be referenced from the\r
389         Suspended list as it is blocking indefinitely.  This call just checks that\r
390         the kernel correctly detects this and does not attempt to unsuspend the\r
391         task. */\r
392         xTaskResumeFromISR( xSemTestTaskHandle );\r
393 }\r