2 FreeRTOS V7.5.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\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
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
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\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
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
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
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\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
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
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
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
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
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
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
90 /* Scheduler include files. */
\r
93 #include "FreeRTOS.h"
\r
97 #ifndef configUSE_TIMERS
\r
98 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
101 #if configUSE_TIMERS != 1
\r
102 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
106 /* Demo program include files. */
\r
107 #include "serial.h"
\r
108 #include "comtest_strings.h"
\r
109 #include "partest.h"
\r
111 /* The size of the stack given to the Rx task. */
\r
112 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\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
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
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
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
136 /* The string that is transmitted and received. */
\r
137 #define comTRANSACTED_STRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
\r
139 /* A block time of 0 simply means "don't block". */
\r
140 #define comtstDONT_BLOCK ( portTickType ) 0
\r
142 /* Handle to the com port used by both tasks. */
\r
143 static xComPortHandle xPort = NULL;
\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
149 /* The receive task as described in the comments at the top of this file. */
\r
150 static void vComRxTask( void *pvParameters );
\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
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
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
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
169 /*-----------------------------------------------------------*/
\r
171 void vStartComTestStringsTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )
\r
173 /* Store values that are used at run time. */
\r
176 /* Calculate the string length here, rather than each time the Tx timer
\r
178 xStringLength = strlen( comTRANSACTED_STRING );
\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
184 /* Initialise the com port, then spawn the Rx task and create the Tx
\r
186 xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );
\r
188 /* Create the Rx task and the Tx timer. The timer is started from the
\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
194 /*-----------------------------------------------------------*/
\r
196 static void prvComTxTimerCallback( xTimerHandle xTimer )
\r
198 portTickType xTimeToWait;
\r
200 /* The parameter is not used in this case. */
\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
207 vSerialPutString( xPort, ( const signed char * const ) comTRANSACTED_STRING, xStringLength );
\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
213 /* Wait a pseudo random time before sending the string again. */
\r
214 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
216 /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */
\r
217 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
219 /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */
\r
220 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
222 xTimeToWait = comTX_MIN_BLOCK_TIME;
\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
230 /*-----------------------------------------------------------*/
\r
232 static void vComRxTask( void *pvParameters )
\r
234 portBASE_TYPE xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;
\r
235 signed char *pcExpectedByte, cRxedChar;
\r
236 const xComPortHandle xPort = NULL;
\r
238 /* The parameter is not used in this example. */
\r
239 ( void ) pvParameters;
\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
245 /* The first expected Rx character is the first in the string that is
\r
247 pcExpectedByte = ( signed char * ) comTRANSACTED_STRING;
\r
251 /* Wait for the next character. */
\r
252 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )
\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
262 case comtstWAITING_START_OF_STRING:
\r
263 if( cRxedChar == *pcExpectedByte )
\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
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
279 case comtstWAITING_END_OF_STRING:
\r
280 if( cRxedChar == *pcExpectedByte )
\r
282 /* The received character was the expected character. Was
\r
283 it the last character in the string - i.e. the null
\r
285 if( cRxedChar == 0x00 )
\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
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
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
305 /* Wait for the next character in the string. */
\r
311 /* The character received was not that expected. */
\r
312 xErrorOccurred = pdTRUE;
\r
317 /* Should not get here. Stop the Rx loop counter from
\r
318 incrementing to latch the error. */
\r
319 xErrorOccurred = pdTRUE;
\r
324 /*-----------------------------------------------------------*/
\r
326 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
328 portBASE_TYPE xReturn;
\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
342 /* Reset the count of successful Rx loops. When this function is called
\r
343 again it should have been incremented again. */
\r