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