]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/Common/Minimal/comtest_strings.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Demo / Common / Minimal / comtest_strings.c
1 /*\r
2     FreeRTOS V8.1.2 - 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     ***************************************************************************\r
8      *                                                                       *\r
9      *    FreeRTOS provides completely free yet professionally developed,    *\r
10      *    robust, strictly quality controlled, supported, and cross          *\r
11      *    platform software that has become a de facto standard.             *\r
12      *                                                                       *\r
13      *    Help yourself get started quickly and support the FreeRTOS         *\r
14      *    project by purchasing a FreeRTOS tutorial book, reference          *\r
15      *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
16      *                                                                       *\r
17      *    Thank you!                                                         *\r
18      *                                                                       *\r
19     ***************************************************************************\r
20 \r
21     This file is part of the FreeRTOS distribution.\r
22 \r
23     FreeRTOS is free software; you can redistribute it and/or modify it under\r
24     the terms of the GNU General Public License (version 2) as published by the\r
25     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
26 \r
27     >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
28     >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
29     >>!   obliged to provide the source code for proprietary components     !<<\r
30     >>!   outside of the FreeRTOS kernel.                                   !<<\r
31 \r
32     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
33     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
34     FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
35     link: http://www.freertos.org/a00114.html\r
36 \r
37     1 tab == 4 spaces!\r
38 \r
39     ***************************************************************************\r
40      *                                                                       *\r
41      *    Having a problem?  Start by reading the FAQ "My application does   *\r
42      *    not run, what could be wrong?"                                     *\r
43      *                                                                       *\r
44      *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
45      *                                                                       *\r
46     ***************************************************************************\r
47 \r
48     http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
49     license and Real Time Engineers Ltd. contact details.\r
50 \r
51     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
52     including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
53     compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
54 \r
55     http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
56     Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
57     licenses offer ticketed support, indemnification and middleware.\r
58 \r
59     http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
60     engineered and independently SIL3 certified version for use in safety and\r
61     mission critical applications that require provable dependability.\r
62 \r
63     1 tab == 4 spaces!\r
64 */\r
65 \r
66 \r
67 /*\r
68  * Creates a task and a timer that operate on an interrupt driven serial port.\r
69  * This demo assumes that the characters transmitted on a port will also be\r
70  * received on the same port.  Therefore, the UART must either be connected to\r
71  * an echo server, or the uart connector must have a loopback connector fitted.\r
72  * See http://www.serialporttool.com/CommEcho.htm for a suitable echo server\r
73  * for Windows hosts.\r
74  *\r
75  * The timer sends a string to the UART, toggles an LED, then resets itself by\r
76  * changing its own period.  The period is calculated as a pseudo random number\r
77  * between comTX_MAX_BLOCK_TIME and comTX_MIN_BLOCK_TIME.\r
78  *\r
79  * The task blocks on an Rx queue waiting for a character to become available.\r
80  * Received characters are checked to ensure they match those transmitted by the\r
81  * Tx timer.  An error is latched if characters are missing, incorrect, or\r
82  * arrive too slowly.\r
83  *\r
84  * How characters are actually transmitted and received is port specific.  Demos\r
85  * that include this test/demo file will provide example drivers.  The Tx timer\r
86  * executes in the context of the timer service (daemon) task, and must\r
87  * therefore never attempt to block.\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 #include "timers.h"\r
97 \r
98 #ifndef configUSE_TIMERS\r
99         #error This demo uses timers.  configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.\r
100 #endif\r
101 \r
102 #if configUSE_TIMERS != 1\r
103         #error This demo uses timers.  configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.\r
104 #endif\r
105 \r
106 \r
107 /* Demo program include files. */\r
108 #include "serial.h"\r
109 #include "comtest_strings.h"\r
110 #include "partest.h"\r
111 \r
112 /* The size of the stack given to the Rx task. */\r
113 #define comSTACK_SIZE                           configMINIMAL_STACK_SIZE\r
114 \r
115 /* See the comment above the declaraction of the uxBaseLED variable. */\r
116 #define comTX_LED_OFFSET                        ( 0 )\r
117 #define comRX_LED_OFFSET                        ( 1 )\r
118 \r
119 /* The Tx timer transmits the sequence of characters at a pseudo random\r
120 interval that is capped between comTX_MAX_BLOCK_TIME and\r
121 comTX_MIN_BLOCK_TIME. */\r
122 #define comTX_MAX_BLOCK_TIME            ( ( TickType_t ) 0x96 )\r
123 #define comTX_MIN_BLOCK_TIME            ( ( TickType_t ) 0x32 )\r
124 #define comOFFSET_TIME                          ( ( TickType_t ) 3 )\r
125 \r
126 /* States for the simple state machine implemented in the Rx task. */\r
127 #define comtstWAITING_START_OF_STRING   0\r
128 #define comtstWAITING_END_OF_STRING             1\r
129 \r
130 /* A short delay in ticks - this delay is used to allow the Rx queue to fill up\r
131 a bit so more than one character can be processed at a time.  This is relative\r
132 to comTX_MIN_BLOCK_TIME to ensure it is never longer than the shortest gap\r
133 between transmissions.  It could be worked out more scientifically from the\r
134 baud rate being used. */\r
135 #define comSHORT_DELAY                          ( comTX_MIN_BLOCK_TIME >> ( TickType_t ) 2 )\r
136 \r
137 /* The string that is transmitted and received. */\r
138 #define comTRANSACTED_STRING            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"\r
139 \r
140 /* A block time of 0 simply means "don't block". */\r
141 #define comtstDONT_BLOCK                        ( TickType_t ) 0\r
142 \r
143 /* Handle to the com port used by both tasks. */\r
144 static xComPortHandle xPort = NULL;\r
145 \r
146 /* The callback function allocated to the transmit timer, as described in the\r
147 comments at the top of this file. */\r
148 static void prvComTxTimerCallback( TimerHandle_t xTimer );\r
149 \r
150 /* The receive task as described in the comments at the top of this file. */\r
151 static void vComRxTask( void *pvParameters );\r
152 \r
153 /* The Rx task will toggle LED ( uxBaseLED + comRX_LED_OFFSET).  The Tx task\r
154 will toggle LED ( uxBaseLED + comTX_LED_OFFSET ). */\r
155 static UBaseType_t uxBaseLED = 0;\r
156 \r
157 /* The Rx task toggles uxRxLoops on each successful iteration of its defined\r
158 function - provided no errors have ever been latched.  If this variable stops\r
159 incrementing, then an error has occurred. */\r
160 static volatile UBaseType_t uxRxLoops = 0UL;\r
161 \r
162 /* The timer used to periodically transmit the string.  This is the timer that\r
163 has prvComTxTimerCallback allocated to it as its callback function. */\r
164 static TimerHandle_t xTxTimer = NULL;\r
165 \r
166 /* The string length is held at file scope so the Tx timer does not need to\r
167 calculate it each time it executes. */\r
168 static size_t xStringLength = 0U;\r
169 \r
170 /*-----------------------------------------------------------*/\r
171 \r
172 void vStartComTestStringsTasks( UBaseType_t uxPriority, uint32_t ulBaudRate, UBaseType_t uxLED )\r
173 {\r
174         /* Store values that are used at run time. */\r
175         uxBaseLED = uxLED;\r
176 \r
177         /* Calculate the string length here, rather than each time the Tx timer\r
178         executes. */\r
179         xStringLength = strlen( comTRANSACTED_STRING );\r
180 \r
181         /* Include the null terminator in the string length as this is used to\r
182         detect the end of the string in the Rx task. */\r
183         xStringLength++;\r
184 \r
185         /* Initialise the com port, then spawn the Rx task and create the Tx\r
186         timer. */\r
187         xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );\r
188 \r
189         /* Create the Rx task and the Tx timer.  The timer is started from the\r
190         Rx task. */\r
191         xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );\r
192         xTxTimer = xTimerCreate( "TxTimer", comTX_MIN_BLOCK_TIME, pdFALSE, NULL, prvComTxTimerCallback );\r
193         configASSERT( xTxTimer );\r
194 }\r
195 /*-----------------------------------------------------------*/\r
196 \r
197 static void prvComTxTimerCallback( TimerHandle_t xTimer )\r
198 {\r
199 TickType_t xTimeToWait;\r
200 \r
201         /* The parameter is not used in this case. */\r
202         ( void ) xTimer;\r
203 \r
204         /* Send the string.  How this is actually performed depends on the\r
205         sample driver provided with this demo.  However - as this is a timer,\r
206         it executes in the context of the timer task and therefore must not\r
207         block. */\r
208         vSerialPutString( xPort, comTRANSACTED_STRING, xStringLength );\r
209 \r
210         /* Toggle an LED to give a visible indication that another transmission\r
211         has been performed. */\r
212         vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );\r
213 \r
214         /* Wait a pseudo random time before sending the string again. */\r
215         xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;\r
216 \r
217         /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */\r
218         xTimeToWait %= comTX_MAX_BLOCK_TIME;\r
219 \r
220         /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */\r
221         if( xTimeToWait < comTX_MIN_BLOCK_TIME )\r
222         {\r
223                 xTimeToWait = comTX_MIN_BLOCK_TIME;\r
224         }\r
225 \r
226         /* Reset the timer to run again xTimeToWait ticks from now.  This function\r
227         is called from the context of the timer task, so the block time must not\r
228         be anything other than zero. */\r
229         xTimerChangePeriod( xTxTimer, xTimeToWait, comtstDONT_BLOCK );\r
230 }\r
231 /*-----------------------------------------------------------*/\r
232 \r
233 static void vComRxTask( void *pvParameters )\r
234 {\r
235 BaseType_t xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;\r
236 char *pcExpectedByte, cRxedChar;\r
237 const xComPortHandle xPort = NULL;\r
238 \r
239         /* The parameter is not used in this example. */\r
240         ( void ) pvParameters;\r
241 \r
242         /* Start the Tx timer.  This only needs to be started once, as it will\r
243         reset itself thereafter. */\r
244         xTimerStart( xTxTimer, portMAX_DELAY );\r
245 \r
246         /* The first expected Rx character is the first in the string that is\r
247         transmitted. */\r
248         pcExpectedByte = comTRANSACTED_STRING;\r
249 \r
250         for( ;; )\r
251         {\r
252                 /* Wait for the next character. */\r
253                 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )\r
254                 {\r
255                         /* A character definitely should have been received by now.  As a\r
256                         character was not received an error must have occurred (which might\r
257                         just be that the loopback connector is not fitted). */\r
258                         xErrorOccurred = pdTRUE;\r
259                 }\r
260 \r
261                 switch( xState )\r
262                 {\r
263                         case comtstWAITING_START_OF_STRING:\r
264                                 if( cRxedChar == *pcExpectedByte )\r
265                                 {\r
266                                         /* The received character was the first character of the\r
267                                         string.  Move to the next state to check each character\r
268                                         as it comes in until the entire string has been received. */\r
269                                         xState = comtstWAITING_END_OF_STRING;\r
270                                         pcExpectedByte++;\r
271 \r
272                                         /* Block for a short period.  This just allows the Rx queue\r
273                                         to contain more than one character, and therefore prevent\r
274                                         thrashing reads to the queue, and repetitive context\r
275                                         switches as     each character is received. */\r
276                                         vTaskDelay( comSHORT_DELAY );\r
277                                 }\r
278                                 break;\r
279 \r
280                         case comtstWAITING_END_OF_STRING:\r
281                                 if( cRxedChar == *pcExpectedByte )\r
282                                 {\r
283                                         /* The received character was the expected character.  Was\r
284                                         it the last character in the string - i.e. the null\r
285                                         terminator? */\r
286                                         if( cRxedChar == 0x00 )\r
287                                         {\r
288                                                 /* The entire string has been received.  If no errors\r
289                                                 have been latched, then increment the loop counter to\r
290                                                 show this task is still healthy. */\r
291                                                 if( xErrorOccurred == pdFALSE )\r
292                                                 {\r
293                                                         uxRxLoops++;\r
294 \r
295                                                         /* Toggle an LED to give a visible sign that a\r
296                                                         complete string has been received. */\r
297                                                         vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );\r
298                                                 }\r
299 \r
300                                                 /* Go back to wait for the start of the next string. */\r
301                                                 pcExpectedByte = comTRANSACTED_STRING;\r
302                                                 xState = comtstWAITING_START_OF_STRING;\r
303                                         }\r
304                                         else\r
305                                         {\r
306                                                 /* Wait for the next character in the string. */\r
307                                                 pcExpectedByte++;\r
308                                         }\r
309                                 }\r
310                                 else\r
311                                 {\r
312                                         /* The character received was not that expected. */\r
313                                         xErrorOccurred = pdTRUE;\r
314                                 }\r
315                                 break;\r
316 \r
317                         default:\r
318                                 /* Should not get here.  Stop the Rx loop counter from\r
319                                 incrementing to latch the error. */\r
320                                 xErrorOccurred = pdTRUE;\r
321                                 break;\r
322                 }\r
323         }\r
324 }\r
325 /*-----------------------------------------------------------*/\r
326 \r
327 BaseType_t xAreComTestTasksStillRunning( void )\r
328 {\r
329 BaseType_t xReturn;\r
330 \r
331         /* If the count of successful reception loops has not changed than at\r
332         some time an error occurred (i.e. a character was received out of sequence)\r
333         and false is returned. */\r
334         if( uxRxLoops == 0UL )\r
335         {\r
336                 xReturn = pdFALSE;\r
337         }\r
338         else\r
339         {\r
340                 xReturn = pdTRUE;\r
341         }\r
342 \r
343         /* Reset the count of successful Rx loops.  When this function is called\r
344         again it should have been incremented again. */\r
345         uxRxLoops = 0UL;\r
346 \r
347         return xReturn;\r
348 }\r
349 \r