]> git.sur5r.net Git - freertos/blob - Demo/Common/Full/comtest.c
Update version number to V7.0.1.
[freertos] / Demo / Common / Full / comtest.c
1 /*\r
2     FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.\r
3         \r
4 \r
5     ***************************************************************************\r
6      *                                                                       *\r
7      *    FreeRTOS tutorial books are available in pdf and paperback.        *\r
8      *    Complete, revised, and edited pdf reference manuals are also       *\r
9      *    available.                                                         *\r
10      *                                                                       *\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
17      *                                                                       *\r
18      *    >>> See http://www.FreeRTOS.org/Documentation for details. <<<     *\r
19      *                                                                       *\r
20      *    Thank you for using FreeRTOS, and thank you for your support!      *\r
21      *                                                                       *\r
22     ***************************************************************************\r
23 \r
24 \r
25     This file is part of the FreeRTOS distribution.\r
26 \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
40     FreeRTOS WEB site.\r
41 \r
42     1 tab == 4 spaces!\r
43 \r
44     http://www.FreeRTOS.org - Documentation, latest information, license and\r
45     contact details.\r
46 \r
47     http://www.SafeRTOS.com - A version that is certified for use in safety\r
48     critical systems.\r
49 \r
50     http://www.OpenRTOS.com - Commercial support, development, porting,\r
51     licensing and training services.\r
52 */\r
53 \r
54 /**\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
59  *\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
63  *\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
71  *\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
75  *\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
78  * \r
79  * \page ComTestC comtest.c\r
80  * \ingroup DemoFiles\r
81  * <HR>\r
82  */\r
83 \r
84 /*\r
85 Changes from V1.00:\r
86         \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
90 \r
91 Changes from V1.01:\r
92 \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
96 \r
97 Changes From V1.2.0:\r
98 \r
99         + Use vSerialPutString() instead of single character puts.\r
100         + Only stop the check variable incrementing after two consecutive errors. \r
101 \r
102 Changed from V1.2.5\r
103 \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
106           tasks.\r
107 \r
108 Changes from V2.0.0\r
109 \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
113 \r
114 */\r
115 \r
116 \r
117 /* Scheduler include files. */\r
118 #include <stdlib.h>\r
119 #include <string.h>\r
120 #include "FreeRTOS.h"\r
121 #include "task.h"\r
122 \r
123 /* Demo program include files. */\r
124 #include "serial.h"\r
125 #include "comtest.h"\r
126 #include "print.h"\r
127 \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
132 \r
133 #define comMAX_CONSECUTIVE_ERRORS       ( 2 )\r
134 \r
135 #define comSTACK_SIZE                           ( ( unsigned short ) 256 )\r
136 \r
137 #define comRX_RELATIVE_PRIORITY         ( 1 )\r
138 \r
139 /* Handle to the com port used by both tasks. */\r
140 static xComPortHandle xPort;\r
141 \r
142 /* The transmit function as described at the top of the file. */\r
143 static void vComTxTask( void *pvParameters );\r
144 \r
145 /* The receive function as described at the top of the file. */\r
146 static void vComRxTask( void *pvParameters );\r
147 \r
148 /* The semaphore test function as described at the top of the file. */\r
149 static void vSemTestTask( void * pvParameters );\r
150 \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
154 \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
158 \r
159 /* The handle to the semaphore test task. */\r
160 static xTaskHandle xSemTestTaskHandle = NULL;\r
161 \r
162 /*-----------------------------------------------------------*/\r
163 \r
164 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )\r
165 {\r
166 const unsigned portBASE_TYPE uxBufferLength = 255;\r
167 \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
173 }\r
174 /*-----------------------------------------------------------*/\r
175 \r
176 static void vComTxTask( void *pvParameters )\r
177 {\r
178 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";\r
179 portTickType xTimeToWait;\r
180 \r
181         /* Stop warnings. */\r
182         ( void ) pvParameters;\r
183 \r
184         /* Queue a message for printing to say the task has started. */\r
185         vPrintDisplayMessage( &pcTaskStartMsg );\r
186 \r
187         for( ;; )\r
188         {\r
189                 /* Send the string to the serial port. */\r
190                 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );\r
191 \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
194                 the string. */\r
195                 sTxCount++;\r
196 \r
197                 xTimeToWait = xTaskGetTickCount();\r
198 \r
199                 /* Make sure we don't wait too long... */\r
200                 xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
201 \r
202                 /* ...but we do want to wait. */\r
203                 if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
204                 {\r
205                         xTimeToWait = comTX_MIN_BLOCK_TIME;\r
206                 }\r
207 \r
208                 vTaskDelay( xTimeToWait );\r
209         }\r
210 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
211 /*-----------------------------------------------------------*/\r
212 \r
213 static void vComRxTask( void *pvParameters )\r
214 {\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
222 char cRxedChar;\r
223 short sResyncRequired, sConsecutiveErrors, sLatchedError;\r
224 \r
225         /* Stop warnings. */\r
226         ( void ) pvParameters;\r
227 \r
228         /* Queue a message for printing to say the task has started. */\r
229         vPrintDisplayMessage( &pcTaskStartMsg );\r
230         \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
236 \r
237         for( ;; )\r
238         {\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
243                 {\r
244                         if( sResyncRequired == pdTRUE )\r
245                         {\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
249                                 {\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
252                                         to receive. */\r
253                                         pcExpectedChar = pcMessageToExchange;\r
254                                         sResyncRequired = pdFALSE;\r
255 \r
256                                         /* Queue a message for printing to say that we are going to try \r
257                                         again. */\r
258                                         vPrintDisplayMessage( &pcTaskRestartMsg );\r
259 \r
260                                         /* Stop incrementing the check variable, if consecutive errors occur. */\r
261                                         sConsecutiveErrors++;\r
262                                         if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )\r
263                                         {\r
264                                                 sLatchedError = pdTRUE;\r
265                                         }\r
266                                 }\r
267                         }\r
268                         else\r
269                         {\r
270                                 /* We have received a character, but is it the expected character? */\r
271                                 if( cRxedChar != *pcExpectedChar )\r
272                                 {\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
278                                 }\r
279                                 else\r
280                                 {\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
284                                         pcExpectedChar++;\r
285                                         if( *pcExpectedChar == '\0' )\r
286                                         {\r
287                                                 pcExpectedChar = pcMessageToExchange;\r
288 \r
289                                                 /* We have got through the entire string without error. */\r
290                                                 sConsecutiveErrors = 0;\r
291                                         }\r
292                                 }\r
293                         }\r
294 \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
298                         {\r
299                                 sRxCount++;                     \r
300                         }\r
301                 }\r
302                 else\r
303                 {\r
304                         vPrintDisplayMessage( &pcTaskTimeoutMsg );\r
305                 }\r
306         }\r
307 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
308 /*-----------------------------------------------------------*/\r
309 \r
310 static void vSemTestTask( void * pvParameters )\r
311 {\r
312 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";\r
313 portBASE_TYPE xError = pdFALSE;\r
314 \r
315         /* Stop warnings. */\r
316         ( void ) pvParameters;\r
317 \r
318         /* Queue a message for printing to say the task has started. */\r
319         vPrintDisplayMessage( &pcTaskStartMsg );\r
320 \r
321         for( ;; )\r
322         {\r
323                 if( xSerialWaitForSemaphore( xPort ) )\r
324                 {\r
325                         if( xError == pdFALSE )\r
326                         {\r
327                                 sSemCount++;\r
328                         }\r
329                 }\r
330                 else\r
331                 {\r
332                         xError = pdTRUE;\r
333                 }\r
334         }\r
335 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */\r
336 /*-----------------------------------------------------------*/\r
337 \r
338 /* This is called to check that all the created tasks are still running. */\r
339 portBASE_TYPE xAreComTestTasksStillRunning( void )\r
340 {\r
341 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;\r
342 portBASE_TYPE xReturn;\r
343 \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
346         changed or not. */\r
347 \r
348         if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )\r
349         {\r
350                 xReturn = pdFALSE;\r
351         }\r
352         else\r
353         {\r
354                 xReturn = pdTRUE;\r
355         }\r
356 \r
357         sLastTxCount = sTxCount;\r
358         sLastRxCount = sRxCount;\r
359         sLastSemCount = sSemCount;\r
360 \r
361         return xReturn;\r
362 }\r
363 /*-----------------------------------------------------------*/\r
364 \r
365 void vComTestUnsuspendTask( void )\r
366 {\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
370         task. */\r
371         xTaskResumeFromISR( xSemTestTaskHandle );\r
372 }\r