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