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