]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Full/comtest.c
Update to MIT licensed FreeRTOS V10.0.0 - see https://www.freertos.org/History.txt
[freertos] / FreeRTOS / Demo / Common / Full / comtest.c
1 /*\r
2  * FreeRTOS Kernel V10.0.0\r
3  * Copyright (C) 2017 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
4  *\r
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
6  * this software and associated documentation files (the "Software"), to deal in\r
7  * the Software without restriction, including without limitation the rights to\r
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
9  * the Software, and to permit persons to whom the Software is furnished to do so,\r
10  * subject to the following conditions:\r
11  *\r
12  * The above copyright notice and this permission notice shall be included in all\r
13  * copies or substantial portions of the Software. If you wish to use our Amazon\r
14  * FreeRTOS name, please do so in a fair use way that does not cause confusion.\r
15  *\r
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
18  * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
19  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
20  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
22  *\r
23  * http://www.FreeRTOS.org\r
24  * http://aws.amazon.com/freertos\r
25  *\r
26  * 1 tab == 4 spaces!\r
27  */\r
28 \r
29 /**\r
30  * Creates two tasks that operate on an interrupt driven serial port.  A loopback \r
31  * connector should be used so that everything that is transmitted is also received.  \r
32  * The serial port does not use any flow control.  On a standard 9way 'D' connector \r
33  * pins two and three should be connected together.\r
34  *\r
35  * The first task repeatedly sends a string to a queue, character at a time.  The \r
36  * serial port interrupt will empty the queue and transmit the characters.  The \r
37  * task blocks for a pseudo random period before resending the string.\r
38  *\r
39  * The second task blocks on a queue waiting for a character to be received.  \r
40  * Characters received by the serial port interrupt routine are posted onto the \r
41  * queue - unblocking the task making it ready to execute.  If this is then the \r
42  * highest priority task ready to run it will run immediately - with a context \r
43  * switch occurring at the end of the interrupt service routine.  The task \r
44  * receiving characters is spawned with a higher priority than the task \r
45  * transmitting the characters.\r
46  *\r
47  * With the loop back connector in place, one task will transmit a string and the \r
48  * other will immediately receive it.  The receiving task knows the string it \r
49  * expects to receive so can detect an error.\r
50  *\r
51  * This also creates a third task.  This is used to test semaphore usage from an\r
52  * ISR and does nothing interesting.  \r
53  * \r
54  * \page ComTestC comtest.c\r
55  * \ingroup DemoFiles\r
56  * <HR>\r
57  */\r
58 \r
59 /*\r
60 Changes from V1.00:\r
61         \r
62         + The priority of the Rx task has been lowered.  Received characters are\r
63           now processed (read from the queue) at the idle priority, allowing low\r
64           priority tasks to run evenly at times of a high communications overhead.\r
65 \r
66 Changes from V1.01:\r
67 \r
68         + The Tx task now waits a pseudo random time between transissions.\r
69           Previously a fixed period was used but this was not such a good test as\r
70           interrupts fired at regular intervals.\r
71 \r
72 Changes From V1.2.0:\r
73 \r
74         + Use vSerialPutString() instead of single character puts.\r
75         + Only stop the check variable incrementing after two consecutive errors. \r
76 \r
77 Changed from V1.2.5\r
78 \r
79         + Made the Rx task 2 priorities higher than the Tx task.  Previously it was\r
80           only 1.  This is done to tie in better with the other demo application \r
81           tasks.\r
82 \r
83 Changes from V2.0.0\r
84 \r
85         + Delay periods are now specified using variables and constants of\r
86           TickType_t rather than unsigned long.\r
87         + Slight modification to task priorities.\r
88 \r
89 */\r
90 \r
91 \r
92 /* Scheduler include files. */\r
93 #include <stdlib.h>\r
94 #include <string.h>\r
95 #include "FreeRTOS.h"\r
96 #include "task.h"\r
97 \r
98 /* Demo program include files. */\r
99 #include "serial.h"\r
100 #include "comtest.h"\r
101 #include "print.h"\r
102 \r
103 /* The Tx task will transmit the sequence of characters at a pseudo random\r
104 interval.  This is the maximum and minimum block time between sends. */\r
105 #define comTX_MAX_BLOCK_TIME            ( ( TickType_t ) 0x15e )\r
106 #define comTX_MIN_BLOCK_TIME            ( ( TickType_t ) 0xc8 )\r
107 \r
108 #define comMAX_CONSECUTIVE_ERRORS       ( 2 )\r
109 \r
110 #define comSTACK_SIZE                           ( ( unsigned short ) 256 )\r
111 \r
112 #define comRX_RELATIVE_PRIORITY         ( 1 )\r
113 \r
114 /* Handle to the com port used by both tasks. */\r
115 static xComPortHandle xPort;\r
116 \r
117 /* The transmit function as described at the top of the file. */\r
118 static void vComTxTask( void *pvParameters );\r
119 \r
120 /* The receive function as described at the top of the file. */\r
121 static void vComRxTask( void *pvParameters );\r
122 \r
123 /* The semaphore test function as described at the top of the file. */\r
124 static void vSemTestTask( void * pvParameters );\r
125 \r
126 /* The string that is repeatedly transmitted. */\r
127 const char * const pcMessageToExchange =        "Send this message over and over again to check communications interrupts. "\r
128                                                                                                 "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\r\n";\r
129 \r
130 /* Variables that are incremented on each cycle of each task.  These are used to \r
131 check that both tasks are still executing. */\r
132 volatile short sTxCount = 0, sRxCount = 0, sSemCount = 0;\r
133 \r
134 /* The handle to the semaphore test task. */\r
135 static TaskHandle_t xSemTestTaskHandle = NULL;\r
136 \r
137 /*-----------------------------------------------------------*/\r
138 \r
139 void vStartComTestTasks( unsigned portBASE_TYPE uxPriority, eCOMPort ePort, eBaud eBaudRate )\r
140 {\r
141 const unsigned portBASE_TYPE uxBufferLength = 255;\r
142 \r
143         /* Initialise the com port then spawn both tasks. */\r
144         xPort = xSerialPortInit( ePort, eBaudRate, serNO_PARITY, serBITS_8, serSTOP_1, uxBufferLength );\r
145         xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority, NULL );\r
146         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority + comRX_RELATIVE_PRIORITY, NULL );\r
147         xTaskCreate( vSemTestTask, "ISRSem", comSTACK_SIZE, NULL, tskIDLE_PRIORITY, &xSemTestTaskHandle );\r
148 }\r
149 /*-----------------------------------------------------------*/\r
150 \r
151 static void vComTxTask( void *pvParameters )\r
152 {\r
153 const char * const pcTaskStartMsg = "COM Tx task started.\r\n";\r
154 TickType_t xTimeToWait;\r
155 \r
156         /* Stop warnings. */\r
157         ( void ) pvParameters;\r
158 \r
159         /* Queue a message for printing to say the task has started. */\r
160         vPrintDisplayMessage( &pcTaskStartMsg );\r
161 \r
162         for( ;; )\r
163         {\r
164                 /* Send the string to the serial port. */\r
165                 vSerialPutString( xPort, pcMessageToExchange, strlen( pcMessageToExchange ) );\r
166 \r
167                 /* We have posted all the characters in the string - increment the variable \r
168                 used to check that this task is still running, then wait before re-sending \r
169                 the string. */\r
170                 sTxCount++;\r
171 \r
172                 xTimeToWait = xTaskGetTickCount();\r
173 \r
174                 /* Make sure we don't wait too long... */\r
175                 xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
176 \r
177                 /* ...but we do want to wait. */\r
178                 if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
179                 {\r
180                         xTimeToWait = comTX_MIN_BLOCK_TIME;\r
181                 }\r
182 \r
183                 vTaskDelay( xTimeToWait );\r
184         }\r
185 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
186 /*-----------------------------------------------------------*/\r
187 \r
188 static void vComRxTask( void *pvParameters )\r
189 {\r
190 const char * const pcTaskStartMsg = "COM Rx task started.\r\n";\r
191 const char * const pcTaskErrorMsg = "COM read error\r\n";\r
192 const char * const pcTaskRestartMsg = "COM resynced\r\n";\r
193 const char * const pcTaskTimeoutMsg = "COM Rx timed out\r\n";\r
194 const TickType_t xBlockTime = ( TickType_t ) 0xffff / portTICK_PERIOD_MS;\r
195 const char *pcExpectedChar;\r
196 portBASE_TYPE xGotChar;\r
197 char cRxedChar;\r
198 short sResyncRequired, sConsecutiveErrors, sLatchedError;\r
199 \r
200         /* Stop warnings. */\r
201         ( void ) pvParameters;\r
202 \r
203         /* Queue a message for printing to say the task has started. */\r
204         vPrintDisplayMessage( &pcTaskStartMsg );\r
205         \r
206         /* The first expected character is the first character in the string. */\r
207         pcExpectedChar = pcMessageToExchange;\r
208         sResyncRequired = pdFALSE;\r
209         sConsecutiveErrors = 0;\r
210         sLatchedError = pdFALSE;\r
211 \r
212         for( ;; )\r
213         {\r
214                 /* Receive a message from the com port interrupt routine.  If a message is \r
215                 not yet available the call will block the task. */\r
216                 xGotChar = xSerialGetChar( xPort, &cRxedChar, xBlockTime );\r
217                 if( xGotChar == pdTRUE )\r
218                 {\r
219                         if( sResyncRequired == pdTRUE )\r
220                         {\r
221                                 /* We got out of sequence and are waiting for the start of the next \r
222                                 transmission of the string. */\r
223                                 if( cRxedChar == '\n' )\r
224                                 {\r
225                                         /* This is the end of the message so we can start again - with \r
226                                         the first character in the string being the next thing we expect \r
227                                         to receive. */\r
228                                         pcExpectedChar = pcMessageToExchange;\r
229                                         sResyncRequired = pdFALSE;\r
230 \r
231                                         /* Queue a message for printing to say that we are going to try \r
232                                         again. */\r
233                                         vPrintDisplayMessage( &pcTaskRestartMsg );\r
234 \r
235                                         /* Stop incrementing the check variable, if consecutive errors occur. */\r
236                                         sConsecutiveErrors++;\r
237                                         if( sConsecutiveErrors >= comMAX_CONSECUTIVE_ERRORS )\r
238                                         {\r
239                                                 sLatchedError = pdTRUE;\r
240                                         }\r
241                                 }\r
242                         }\r
243                         else\r
244                         {\r
245                                 /* We have received a character, but is it the expected character? */\r
246                                 if( cRxedChar != *pcExpectedChar )\r
247                                 {\r
248                                         /* This was not the expected character so post a message for \r
249                                         printing to say that an error has occurred.  We will then wait \r
250                                         to resynchronise. */\r
251                                         vPrintDisplayMessage( &pcTaskErrorMsg );                                        \r
252                                         sResyncRequired = pdTRUE;\r
253                                 }\r
254                                 else\r
255                                 {\r
256                                         /* This was the expected character so next time we will expect \r
257                                         the next character in the string.  Wrap back to the beginning \r
258                                         of the string when the null terminator has been reached. */\r
259                                         pcExpectedChar++;\r
260                                         if( *pcExpectedChar == '\0' )\r
261                                         {\r
262                                                 pcExpectedChar = pcMessageToExchange;\r
263 \r
264                                                 /* We have got through the entire string without error. */\r
265                                                 sConsecutiveErrors = 0;\r
266                                         }\r
267                                 }\r
268                         }\r
269 \r
270                         /* Increment the count that is used to check that this task is still \r
271                         running.  This is only done if an error has never occurred. */\r
272                         if( sLatchedError == pdFALSE )\r
273                         {\r
274                                 sRxCount++;                     \r
275                         }\r
276                 }\r
277                 else\r
278                 {\r
279                         vPrintDisplayMessage( &pcTaskTimeoutMsg );\r
280                 }\r
281         }\r
282 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */\r
283 /*-----------------------------------------------------------*/\r
284 \r
285 static void vSemTestTask( void * pvParameters )\r
286 {\r
287 const char * const pcTaskStartMsg = "ISR Semaphore test started.\r\n";\r
288 portBASE_TYPE xError = pdFALSE;\r
289 \r
290         /* Stop warnings. */\r
291         ( void ) pvParameters;\r
292 \r
293         /* Queue a message for printing to say the task has started. */\r
294         vPrintDisplayMessage( &pcTaskStartMsg );\r
295 \r
296         for( ;; )\r
297         {\r
298                 if( xSerialWaitForSemaphore( xPort ) )\r
299                 {\r
300                         if( xError == pdFALSE )\r
301                         {\r
302                                 sSemCount++;\r
303                         }\r
304                 }\r
305                 else\r
306                 {\r
307                         xError = pdTRUE;\r
308                 }\r
309         }\r
310 } /*lint !e715 !e830 !e818 pvParameters not used but function prototype must be standard for task function. */\r
311 /*-----------------------------------------------------------*/\r
312 \r
313 /* This is called to check that all the created tasks are still running. */\r
314 portBASE_TYPE xAreComTestTasksStillRunning( void )\r
315 {\r
316 static short sLastTxCount = 0, sLastRxCount = 0, sLastSemCount = 0;\r
317 portBASE_TYPE xReturn;\r
318 \r
319         /* Not too worried about mutual exclusion on these variables as they are 16 \r
320         bits and we are only reading them.  We also only care to see if they have \r
321         changed or not. */\r
322 \r
323         if( ( sTxCount == sLastTxCount ) || ( sRxCount == sLastRxCount ) || ( sSemCount == sLastSemCount ) )\r
324         {\r
325                 xReturn = pdFALSE;\r
326         }\r
327         else\r
328         {\r
329                 xReturn = pdTRUE;\r
330         }\r
331 \r
332         sLastTxCount = sTxCount;\r
333         sLastRxCount = sRxCount;\r
334         sLastSemCount = sSemCount;\r
335 \r
336         return xReturn;\r
337 }\r
338 /*-----------------------------------------------------------*/\r
339 \r
340 void vComTestUnsuspendTask( void )\r
341 {\r
342         /* The task that is suspended on the semaphore will be referenced from the\r
343         Suspended list as it is blocking indefinitely.  This call just checks that\r
344         the kernel correctly detects this and does not attempt to unsuspend the\r
345         task. */\r
346         xTaskResumeFromISR( xSemTestTaskHandle );\r
347 }\r