2 FreeRTOS V7.0.1 - Copyright (C) 2011 Real Time Engineers Ltd.
\r
5 ***************************************************************************
\r
7 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
8 * Complete, revised, and edited pdf reference manuals are also *
\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
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
20 * Thank you for using FreeRTOS, and thank you for your support! *
\r
22 ***************************************************************************
\r
25 This file is part of the FreeRTOS distribution.
\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
44 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
47 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
50 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
51 licensing and training services.
\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
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
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
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
79 /* Scheduler include files. */
\r
82 #include "FreeRTOS.h"
\r
86 #ifndef configUSE_TIMERS
\r
87 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
90 #if configUSE_TIMERS != 1
\r
91 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
95 /* Demo program include files. */
\r
97 #include "comtest_strings.h"
\r
98 #include "partest.h"
\r
100 /* The size of the stack given to the Rx task. */
\r
101 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\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
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
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
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
125 /* The string that is transmitted and received. */
\r
126 #define comTRANSACTED_STRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
\r
128 /* A block time of 0 simply means "don't block". */
\r
129 #define comtstDONT_BLOCK ( portTickType ) 0
\r
131 /* Handle to the com port used by both tasks. */
\r
132 static xComPortHandle xPort = NULL;
\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
138 /* The receive task as described in the comments at the top of this file. */
\r
139 static void vComRxTask( void *pvParameters );
\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
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
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
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
158 /*-----------------------------------------------------------*/
\r
160 void vStartComTestStringsTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )
\r
162 /* Store values that are used at run time. */
\r
165 /* Calculate the string length here, rather than each time the Tx timer
\r
167 xStringLength = strlen( comTRANSACTED_STRING );
\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
173 /* Initialise the com port, then spawn the Rx task and create the Tx
\r
175 xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );
\r
177 /* Create the Rx task and the Tx timer. The timer is started from the
\r
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
183 /*-----------------------------------------------------------*/
\r
185 static void prvComTxTimerCallback( xTimerHandle xTimer )
\r
187 portTickType xTimeToWait;
\r
189 /* The parameter is not used in this case. */
\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
196 vSerialPutString( xPort, ( const signed char * const ) comTRANSACTED_STRING, xStringLength );
\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
202 /* Wait a pseudo random time before sending the string again. */
\r
203 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
205 /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */
\r
206 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
208 /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */
\r
209 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
211 xTimeToWait = comTX_MIN_BLOCK_TIME;
\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
219 /*-----------------------------------------------------------*/
\r
221 static void vComRxTask( void *pvParameters )
\r
223 portBASE_TYPE xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;
\r
224 signed char *pcExpectedByte, cRxedChar;
\r
225 const xComPortHandle xPort = NULL;
\r
227 /* The parameter is not used in this example. */
\r
228 ( void ) pvParameters;
\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
234 /* The first expected Rx character is the first in the string that is
\r
236 pcExpectedByte = ( signed char * ) comTRANSACTED_STRING;
\r
240 /* Wait for the next character. */
\r
241 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )
\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;
251 case comtstWAITING_START_OF_STRING:
\r
252 if( cRxedChar == *pcExpectedByte )
\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
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
268 case comtstWAITING_END_OF_STRING:
\r
269 if( cRxedChar == *pcExpectedByte )
\r
271 /* The received character was the expected character. Was
\r
272 it the last character in the string - i.e. the null
\r
274 if( cRxedChar == 0x00 )
\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
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
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
294 /* Wait for the next character in the string. */
\r
300 /* The character received was not that expected. */
\r
301 xErrorOccurred = pdTRUE;
\r
306 /* Should not get here. Stop the Rx loop counter from
\r
307 incrementing to latch the error. */
\r
308 xErrorOccurred = pdTRUE;
\r
313 /*-----------------------------------------------------------*/
\r
315 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
317 portBASE_TYPE xReturn;
\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
331 /* Reset the count of successful Rx loops. When this function is called
\r
332 again it should have been incremented again. */
\r