]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/comtest_strings.c
286725daca16367fb8a9b4b2cabae25af283b439
[freertos] / FreeRTOS / Demo / Common / Minimal / comtest_strings.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 /*\r
30  * Creates a task and a timer that operate on an interrupt driven serial port.\r
31  * This demo assumes that the characters transmitted on a port will also be\r
32  * received on the same port.  Therefore, the UART must either be connected to\r
33  * an echo server, or the uart connector must have a loopback connector fitted.\r
34  * See http://www.serialporttool.com/CommEcho.htm for a suitable echo server\r
35  * for Windows hosts.\r
36  *\r
37  * The timer sends a string to the UART, toggles an LED, then resets itself by\r
38  * changing its own period.  The period is calculated as a pseudo random number\r
39  * between comTX_MAX_BLOCK_TIME and comTX_MIN_BLOCK_TIME.\r
40  *\r
41  * The task blocks on an Rx queue waiting for a character to become available.\r
42  * Received characters are checked to ensure they match those transmitted by the\r
43  * Tx timer.  An error is latched if characters are missing, incorrect, or\r
44  * arrive too slowly.\r
45  *\r
46  * How characters are actually transmitted and received is port specific.  Demos\r
47  * that include this test/demo file will provide example drivers.  The Tx timer\r
48  * executes in the context of the timer service (daemon) task, and must\r
49  * therefore never attempt to block.\r
50  *\r
51  */\r
52 \r
53 /* Scheduler include files. */\r
54 #include <stdlib.h>\r
55 #include <string.h>\r
56 #include "FreeRTOS.h"\r
57 #include "task.h"\r
58 #include "timers.h"\r
59 \r
60 #ifndef configUSE_TIMERS\r
61         #error This demo uses timers.  configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.\r
62 #endif\r
63 \r
64 #if configUSE_TIMERS != 1\r
65         #error This demo uses timers.  configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.\r
66 #endif\r
67 \r
68 \r
69 /* Demo program include files. */\r
70 #include "serial.h"\r
71 #include "comtest_strings.h"\r
72 #include "partest.h"\r
73 \r
74 /* The size of the stack given to the Rx task. */\r
75 #define comSTACK_SIZE                           configMINIMAL_STACK_SIZE\r
76 \r
77 /* See the comment above the declaraction of the uxBaseLED variable. */\r
78 #define comTX_LED_OFFSET                        ( 0 )\r
79 #define comRX_LED_OFFSET                        ( 1 )\r
80 \r
81 /* The Tx timer transmits the sequence of characters at a pseudo random\r
82 interval that is capped between comTX_MAX_BLOCK_TIME and\r
83 comTX_MIN_BLOCK_TIME. */\r
84 #define comTX_MAX_BLOCK_TIME            ( ( TickType_t ) 0x96 )\r
85 #define comTX_MIN_BLOCK_TIME            ( ( TickType_t ) 0x32 )\r
86 #define comOFFSET_TIME                          ( ( TickType_t ) 3 )\r
87 \r
88 /* States for the simple state machine implemented in the Rx task. */\r
89 #define comtstWAITING_START_OF_STRING   0\r
90 #define comtstWAITING_END_OF_STRING             1\r
91 \r
92 /* A short delay in ticks - this delay is used to allow the Rx queue to fill up\r
93 a bit so more than one character can be processed at a time.  This is relative\r
94 to comTX_MIN_BLOCK_TIME to ensure it is never longer than the shortest gap\r
95 between transmissions.  It could be worked out more scientifically from the\r
96 baud rate being used. */\r
97 #define comSHORT_DELAY                          ( comTX_MIN_BLOCK_TIME >> ( TickType_t ) 2 )\r
98 \r
99 /* The string that is transmitted and received. */\r
100 #define comTRANSACTED_STRING            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"\r
101 \r
102 /* A block time of 0 simply means "don't block". */\r
103 #define comtstDONT_BLOCK                        ( TickType_t ) 0\r
104 \r
105 /* Handle to the com port used by both tasks. */\r
106 static xComPortHandle xPort = NULL;\r
107 \r
108 /* The callback function allocated to the transmit timer, as described in the\r
109 comments at the top of this file. */\r
110 static void prvComTxTimerCallback( TimerHandle_t xTimer );\r
111 \r
112 /* The receive task as described in the comments at the top of this file. */\r
113 static void vComRxTask( void *pvParameters );\r
114 \r
115 /* The Rx task will toggle LED ( uxBaseLED + comRX_LED_OFFSET).  The Tx task\r
116 will toggle LED ( uxBaseLED + comTX_LED_OFFSET ). */\r
117 static UBaseType_t uxBaseLED = 0;\r
118 \r
119 /* The Rx task toggles uxRxLoops on each successful iteration of its defined\r
120 function - provided no errors have ever been latched.  If this variable stops\r
121 incrementing, then an error has occurred. */\r
122 static volatile UBaseType_t uxRxLoops = 0UL;\r
123 \r
124 /* The timer used to periodically transmit the string.  This is the timer that\r
125 has prvComTxTimerCallback allocated to it as its callback function. */\r
126 static TimerHandle_t xTxTimer = NULL;\r
127 \r
128 /* The string length is held at file scope so the Tx timer does not need to\r
129 calculate it each time it executes. */\r
130 static size_t xStringLength = 0U;\r
131 \r
132 /*-----------------------------------------------------------*/\r
133 \r
134 void vStartComTestStringsTasks( UBaseType_t uxPriority, uint32_t ulBaudRate, UBaseType_t uxLED )\r
135 {\r
136         /* Store values that are used at run time. */\r
137         uxBaseLED = uxLED;\r
138 \r
139         /* Calculate the string length here, rather than each time the Tx timer\r
140         executes. */\r
141         xStringLength = strlen( comTRANSACTED_STRING );\r
142 \r
143         /* Include the null terminator in the string length as this is used to\r
144         detect the end of the string in the Rx task. */\r
145         xStringLength++;\r
146 \r
147         /* Initialise the com port, then spawn the Rx task and create the Tx\r
148         timer. */\r
149         xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );\r
150 \r
151         /* Create the Rx task and the Tx timer.  The timer is started from the\r
152         Rx task. */\r
153         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );\r
154         xTxTimer = xTimerCreate( "TxTimer", comTX_MIN_BLOCK_TIME, pdFALSE, NULL, prvComTxTimerCallback );\r
155         configASSERT( xTxTimer );\r
156 }\r
157 /*-----------------------------------------------------------*/\r
158 \r
159 static void prvComTxTimerCallback( TimerHandle_t xTimer )\r
160 {\r
161 TickType_t xTimeToWait;\r
162 \r
163         /* The parameter is not used in this case. */\r
164         ( void ) xTimer;\r
165 \r
166         /* Send the string.  How this is actually performed depends on the\r
167         sample driver provided with this demo.  However - as this is a timer,\r
168         it executes in the context of the timer task and therefore must not\r
169         block. */\r
170         vSerialPutString( xPort, comTRANSACTED_STRING, xStringLength );\r
171 \r
172         /* Toggle an LED to give a visible indication that another transmission\r
173         has been performed. */\r
174         vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );\r
175 \r
176         /* Wait a pseudo random time before sending the string again. */\r
177         xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;\r
178 \r
179         /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */\r
180         xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
181 \r
182         /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */\r
183         if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
184         {\r
185                 xTimeToWait = comTX_MIN_BLOCK_TIME;\r
186         }\r
187 \r
188         /* Reset the timer to run again xTimeToWait ticks from now.  This function\r
189         is called from the context of the timer task, so the block time must not\r
190         be anything other than zero. */\r
191         xTimerChangePeriod( xTxTimer, xTimeToWait, comtstDONT_BLOCK );\r
192 }\r
193 /*-----------------------------------------------------------*/\r
194 \r
195 static void vComRxTask( void *pvParameters )\r
196 {\r
197 BaseType_t xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;\r
198 char *pcExpectedByte, cRxedChar;\r
199 const xComPortHandle xPort = NULL;\r
200 \r
201         /* The parameter is not used in this example. */\r
202         ( void ) pvParameters;\r
203 \r
204         /* Start the Tx timer.  This only needs to be started once, as it will\r
205         reset itself thereafter. */\r
206         xTimerStart( xTxTimer, portMAX_DELAY );\r
207 \r
208         /* The first expected Rx character is the first in the string that is\r
209         transmitted. */\r
210         pcExpectedByte = comTRANSACTED_STRING;\r
211 \r
212         for( ;; )\r
213         {\r
214                 /* Wait for the next character. */\r
215                 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )\r
216                 {\r
217                         /* A character definitely should have been received by now.  As a\r
218                         character was not received an error must have occurred (which might\r
219                         just be that the loopback connector is not fitted). */\r
220                         xErrorOccurred = pdTRUE;\r
221                 }\r
222 \r
223                 switch( xState )\r
224                 {\r
225                         case comtstWAITING_START_OF_STRING:\r
226                                 if( cRxedChar == *pcExpectedByte )\r
227                                 {\r
228                                         /* The received character was the first character of the\r
229                                         string.  Move to the next state to check each character\r
230                                         as it comes in until the entire string has been received. */\r
231                                         xState = comtstWAITING_END_OF_STRING;\r
232                                         pcExpectedByte++;\r
233 \r
234                                         /* Block for a short period.  This just allows the Rx queue\r
235                                         to contain more than one character, and therefore prevent\r
236                                         thrashing reads to the queue, and repetitive context\r
237                                         switches as     each character is received. */\r
238                                         vTaskDelay( comSHORT_DELAY );\r
239                                 }\r
240                                 break;\r
241 \r
242                         case comtstWAITING_END_OF_STRING:\r
243                                 if( cRxedChar == *pcExpectedByte )\r
244                                 {\r
245                                         /* The received character was the expected character.  Was\r
246                                         it the last character in the string - i.e. the null\r
247                                         terminator? */\r
248                                         if( cRxedChar == 0x00 )\r
249                                         {\r
250                                                 /* The entire string has been received.  If no errors\r
251                                                 have been latched, then increment the loop counter to\r
252                                                 show this task is still healthy. */\r
253                                                 if( xErrorOccurred == pdFALSE )\r
254                                                 {\r
255                                                         uxRxLoops++;\r
256 \r
257                                                         /* Toggle an LED to give a visible sign that a\r
258                                                         complete string has been received. */\r
259                                                         vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );\r
260                                                 }\r
261 \r
262                                                 /* Go back to wait for the start of the next string. */\r
263                                                 pcExpectedByte = comTRANSACTED_STRING;\r
264                                                 xState = comtstWAITING_START_OF_STRING;\r
265                                         }\r
266                                         else\r
267                                         {\r
268                                                 /* Wait for the next character in the string. */\r
269                                                 pcExpectedByte++;\r
270                                         }\r
271                                 }\r
272                                 else\r
273                                 {\r
274                                         /* The character received was not that expected. */\r
275                                         xErrorOccurred = pdTRUE;\r
276                                 }\r
277                                 break;\r
278 \r
279                         default:\r
280                                 /* Should not get here.  Stop the Rx loop counter from\r
281                                 incrementing to latch the error. */\r
282                                 xErrorOccurred = pdTRUE;\r
283                                 break;\r
284                 }\r
285         }\r
286 }\r
287 /*-----------------------------------------------------------*/\r
288 \r
289 BaseType_t xAreComTestTasksStillRunning( void )\r
290 {\r
291 BaseType_t xReturn;\r
292 \r
293         /* If the count of successful reception loops has not changed than at\r
294         some time an error occurred (i.e. a character was received out of sequence)\r
295         and false is returned. */\r
296         if( uxRxLoops == 0UL )\r
297         {\r
298                 xReturn = pdFALSE;\r
299         }\r
300         else\r
301         {\r
302                 xReturn = pdTRUE;\r
303         }\r
304 \r
305         /* Reset the count of successful Rx loops.  When this function is called\r
306         again it should have been incremented again. */\r
307         uxRxLoops = 0UL;\r
308 \r
309         return xReturn;\r
310 }\r
311 \r