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