2 FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\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
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
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\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
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\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
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\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
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
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
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
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
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
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
91 /* Scheduler include files. */
\r
94 #include "FreeRTOS.h"
\r
98 #ifndef configUSE_TIMERS
\r
99 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
102 #if configUSE_TIMERS != 1
\r
103 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
107 /* Demo program include files. */
\r
108 #include "serial.h"
\r
109 #include "comtest_strings.h"
\r
110 #include "partest.h"
\r
112 /* The size of the stack given to the Rx task. */
\r
113 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\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
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
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
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
137 /* The string that is transmitted and received. */
\r
138 #define comTRANSACTED_STRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
\r
140 /* A block time of 0 simply means "don't block". */
\r
141 #define comtstDONT_BLOCK ( TickType_t ) 0
\r
143 /* Handle to the com port used by both tasks. */
\r
144 static xComPortHandle xPort = NULL;
\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
150 /* The receive task as described in the comments at the top of this file. */
\r
151 static void vComRxTask( void *pvParameters );
\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 unsigned portBASE_TYPE uxBaseLED = 0;
\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 unsigned portBASE_TYPE uxRxLoops = 0UL;
\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
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
170 /*-----------------------------------------------------------*/
\r
172 void vStartComTestStringsTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )
\r
174 /* Store values that are used at run time. */
\r
177 /* Calculate the string length here, rather than each time the Tx timer
\r
179 xStringLength = strlen( comTRANSACTED_STRING );
\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
185 /* Initialise the com port, then spawn the Rx task and create the Tx
\r
187 xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );
\r
189 /* Create the Rx task and the Tx timer. The timer is started from the
\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
195 /*-----------------------------------------------------------*/
\r
197 static void prvComTxTimerCallback( TimerHandle_t xTimer )
\r
199 TickType_t xTimeToWait;
\r
201 /* The parameter is not used in this case. */
\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
208 vSerialPutString( xPort, comTRANSACTED_STRING, xStringLength );
\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
214 /* Wait a pseudo random time before sending the string again. */
\r
215 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
217 /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */
\r
218 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
220 /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */
\r
221 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
223 xTimeToWait = comTX_MIN_BLOCK_TIME;
\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
231 /*-----------------------------------------------------------*/
\r
233 static void vComRxTask( void *pvParameters )
\r
235 portBASE_TYPE xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;
\r
236 char *pcExpectedByte, cRxedChar;
\r
237 const xComPortHandle xPort = NULL;
\r
239 /* The parameter is not used in this example. */
\r
240 ( void ) pvParameters;
\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
246 /* The first expected Rx character is the first in the string that is
\r
248 pcExpectedByte = comTRANSACTED_STRING;
\r
252 /* Wait for the next character. */
\r
253 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )
\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
263 case comtstWAITING_START_OF_STRING:
\r
264 if( cRxedChar == *pcExpectedByte )
\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
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
280 case comtstWAITING_END_OF_STRING:
\r
281 if( cRxedChar == *pcExpectedByte )
\r
283 /* The received character was the expected character. Was
\r
284 it the last character in the string - i.e. the null
\r
286 if( cRxedChar == 0x00 )
\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
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
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
306 /* Wait for the next character in the string. */
\r
312 /* The character received was not that expected. */
\r
313 xErrorOccurred = pdTRUE;
\r
318 /* Should not get here. Stop the Rx loop counter from
\r
319 incrementing to latch the error. */
\r
320 xErrorOccurred = pdTRUE;
\r
325 /*-----------------------------------------------------------*/
\r
327 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
329 portBASE_TYPE xReturn;
\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
343 /* Reset the count of successful Rx loops. When this function is called
\r
344 again it should have been incremented again. */
\r