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