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