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