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