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