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