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