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