]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/comtest.c
Minor updates and change version number for V7.5.0 release.
[freertos] / FreeRTOS / Demo / Common / Full / comtest.c
1 /*\r
2     FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
3 \r
4     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
5 \r
6     ***************************************************************************\r
7      *                                                                       *\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
11      *                                                                       *\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
15      *                                                                       *\r
16      *    Thank you!                                                         *\r
17      *                                                                       *\r
18     ***************************************************************************\r
19 \r
20     This file is part of the FreeRTOS distribution.\r
21 \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
25 \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
29     >>! kernel.\r
30 \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
35 \r
36     1 tab == 4 spaces!\r
37 \r
38     ***************************************************************************\r
39      *                                                                       *\r
40      *    Having a problem?  Start by reading the FAQ "My application does   *\r
41      *    not run, what could be wrong?"                                     *\r
42      *                                                                       *\r
43      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
44      *                                                                       *\r
45     ***************************************************************************\r
46 \r
47     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
48     license and Real Time Engineers Ltd. contact details.\r
49 \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
53 \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
57 \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
61 \r
62     1 tab == 4 spaces!\r
63 */\r
64 \r
65 /**\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
70  *\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
74  *\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
82  *\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
86  *\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
89  * \r
90  * \page ComTestC comtest.c\r
91  * \ingroup DemoFiles\r
92  * <HR>\r
93  */\r
94 \r
95 /*\r
96 Changes from V1.00:\r
97         \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
101 \r
102 Changes from V1.01:\r
103 \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
107 \r
108 Changes From V1.2.0:\r
109 \r
110         + Use vSerialPutString() instead of single character puts.\r
111         + Only stop the check variable incrementing after two consecutive errors. \r
112 \r
113 Changed from V1.2.5\r
114 \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
117           tasks.\r
118 \r
119 Changes from V2.0.0\r
120 \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
124 \r
125 */\r
126 \r
127 \r
128 /* Scheduler include files. */\r
129 #include <stdlib.h>\r
130 #include <string.h>\r
131 #include "FreeRTOS.h"\r
132 #include "task.h"\r
133 \r
134 /* Demo program include files. */\r
135 #include "serial.h"\r
136 #include "comtest.h"\r
137 #include "print.h"\r
138 \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
143 \r
144 #define comMAX_CONSECUTIVE_ERRORS       ( 2 )\r
145 \r
146 #define comSTACK_SIZE                           ( ( unsigned short ) 256 )\r
147 \r
148 #define comRX_RELATIVE_PRIORITY         ( 1 )\r
149 \r
150 /* Handle to the com port used by both tasks. */\r
151 static xComPortHandle xPort;\r
152 \r
153 /* The transmit function as described at the top of the file. */\r
154 static void vComTxTask( void *pvParameters );\r
155 \r
156 /* The receive function as described at the top of the file. */\r
157 static void vComRxTask( void *pvParameters );\r
158 \r
159 /* The semaphore test function as described at the top of the file. */\r
160 static void vSemTestTask( void * pvParameters );\r
161 \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
165 \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
169 \r
170 /* The handle to the semaphore test task. */\r
171 static xTaskHandle xSemTestTaskHandle = NULL;\r
172 \r
173 /*-----------------------------------------------------------*/\r
174 \r
175 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )\r
176 {\r
177 const unsigned portBASE_TYPE uxBufferLength = 255;\r
178 \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
184 }\r
185 /*-----------------------------------------------------------*/\r
186 \r
187 static void vComTxTask( void *pvParameters )\r
188 {\r
189 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";\r
190 portTickType xTimeToWait;\r
191 \r
192         /* Stop warnings. */\r
193         ( void ) pvParameters;\r
194 \r
195         /* Queue a message for printing to say the task has started. */\r
196         vPrintDisplayMessage( &pcTaskStartMsg );\r
197 \r
198         for( ;; )\r
199         {\r
200                 /* Send the string to the serial port. */\r
201                 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );\r
202 \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
205                 the string. */\r
206                 sTxCount++;\r
207 \r
208                 xTimeToWait = xTaskGetTickCount();\r
209 \r
210                 /* Make sure we don't wait too long... */\r
211                 xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
212 \r
213                 /* ...but we do want to wait. */\r
214                 if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
215                 {\r
216                         xTimeToWait = comTX_MIN_BLOCK_TIME;\r
217                 }\r
218 \r
219                 vTaskDelay( xTimeToWait );\r
220         }\r
221 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
222 /*-----------------------------------------------------------*/\r
223 \r
224 static void vComRxTask( void *pvParameters )\r
225 {\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
233 char cRxedChar;\r
234 short sResyncRequired, sConsecutiveErrors, sLatchedError;\r
235 \r
236         /* Stop warnings. */\r
237         ( void ) pvParameters;\r
238 \r
239         /* Queue a message for printing to say the task has started. */\r
240         vPrintDisplayMessage( &pcTaskStartMsg );\r
241         \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
247 \r
248         for( ;; )\r
249         {\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
254                 {\r
255                         if( sResyncRequired == pdTRUE )\r
256                         {\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
260                                 {\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
263                                         to receive. */\r
264                                         pcExpectedChar = pcMessageToExchange;\r
265                                         sResyncRequired = pdFALSE;\r
266 \r
267                                         /* Queue a message for printing to say that we are going to try \r
268                                         again. */\r
269                                         vPrintDisplayMessage( &pcTaskRestartMsg );\r
270 \r
271                                         /* Stop incrementing the check variable, if consecutive errors occur. */\r
272                                         sConsecutiveErrors++;\r
273                                         if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )\r
274                                         {\r
275                                                 sLatchedError = pdTRUE;\r
276                                         }\r
277                                 }\r
278                         }\r
279                         else\r
280                         {\r
281                                 /* We have received a character, but is it the expected character? */\r
282                                 if( cRxedChar != *pcExpectedChar )\r
283                                 {\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
289                                 }\r
290                                 else\r
291                                 {\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
295                                         pcExpectedChar++;\r
296                                         if( *pcExpectedChar == '\0' )\r
297                                         {\r
298                                                 pcExpectedChar = pcMessageToExchange;\r
299 \r
300                                                 /* We have got through the entire string without error. */\r
301                                                 sConsecutiveErrors = 0;\r
302                                         }\r
303                                 }\r
304                         }\r
305 \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
309                         {\r
310                                 sRxCount++;                     \r
311                         }\r
312                 }\r
313                 else\r
314                 {\r
315                         vPrintDisplayMessage( &pcTaskTimeoutMsg );\r
316                 }\r
317         }\r
318 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
319 /*-----------------------------------------------------------*/\r
320 \r
321 static void vSemTestTask( void * pvParameters )\r
322 {\r
323 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";\r
324 portBASE_TYPE xError = pdFALSE;\r
325 \r
326         /* Stop warnings. */\r
327         ( void ) pvParameters;\r
328 \r
329         /* Queue a message for printing to say the task has started. */\r
330         vPrintDisplayMessage( &pcTaskStartMsg );\r
331 \r
332         for( ;; )\r
333         {\r
334                 if( xSerialWaitForSemaphore( xPort ) )\r
335                 {\r
336                         if( xError == pdFALSE )\r
337                         {\r
338                                 sSemCount++;\r
339                         }\r
340                 }\r
341                 else\r
342                 {\r
343                         xError = pdTRUE;\r
344                 }\r
345         }\r
346 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */\r
347 /*-----------------------------------------------------------*/\r
348 \r
349 /* This is called to check that all the created tasks are still running. */\r
350 portBASE_TYPE xAreComTestTasksStillRunning( void )\r
351 {\r
352 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;\r
353 portBASE_TYPE xReturn;\r
354 \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
357         changed or not. */\r
358 \r
359         if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )\r
360         {\r
361                 xReturn = pdFALSE;\r
362         }\r
363         else\r
364         {\r
365                 xReturn = pdTRUE;\r
366         }\r
367 \r
368         sLastTxCount = sTxCount;\r
369         sLastRxCount = sRxCount;\r
370         sLastSemCount = sSemCount;\r
371 \r
372         return xReturn;\r
373 }\r
374 /*-----------------------------------------------------------*/\r
375 \r
376 void vComTestUnsuspendTask( void )\r
377 {\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
381         task. */\r
382         xTaskResumeFromISR( xSemTestTaskHandle );\r
383 }\r