2 FreeRTOS V7.1.1 - Copyright (C) 2012 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 ***************************************************************************
\r
46 * Having a problem? Start by reading the FAQ "My application does *
\r
47 * not run, what could be wrong? *
\r
49 * http://www.FreeRTOS.org/FAQHelp.html *
\r
51 ***************************************************************************
\r
54 http://www.FreeRTOS.org - Documentation, training, latest information,
\r
55 license and contact details.
\r
57 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
58 including FreeRTOS+Trace - an indispensable productivity tool.
\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
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
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
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
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
92 /* Scheduler include files. */
\r
95 #include "FreeRTOS.h"
\r
99 #ifndef configUSE_TIMERS
\r
100 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
103 #if configUSE_TIMERS != 1
\r
104 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
108 /* Demo program include files. */
\r
109 #include "serial.h"
\r
110 #include "comtest_strings.h"
\r
111 #include "partest.h"
\r
113 /* The size of the stack given to the Rx task. */
\r
114 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\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
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
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
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
138 /* The string that is transmitted and received. */
\r
139 #define comTRANSACTED_STRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
\r
141 /* A block time of 0 simply means "don't block". */
\r
142 #define comtstDONT_BLOCK ( portTickType ) 0
\r
144 /* Handle to the com port used by both tasks. */
\r
145 static xComPortHandle xPort = NULL;
\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
151 /* The receive task as described in the comments at the top of this file. */
\r
152 static void vComRxTask( void *pvParameters );
\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
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
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
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
171 /*-----------------------------------------------------------*/
\r
173 void vStartComTestStringsTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )
\r
175 /* Store values that are used at run time. */
\r
178 /* Calculate the string length here, rather than each time the Tx timer
\r
180 xStringLength = strlen( comTRANSACTED_STRING );
\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
186 /* Initialise the com port, then spawn the Rx task and create the Tx
\r
188 xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );
\r
190 /* Create the Rx task and the Tx timer. The timer is started from the
\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
196 /*-----------------------------------------------------------*/
\r
198 static void prvComTxTimerCallback( xTimerHandle xTimer )
\r
200 portTickType xTimeToWait;
\r
202 /* The parameter is not used in this case. */
\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
209 vSerialPutString( xPort, ( const signed char * const ) comTRANSACTED_STRING, xStringLength );
\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
215 /* Wait a pseudo random time before sending the string again. */
\r
216 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
218 /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */
\r
219 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
221 /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */
\r
222 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
224 xTimeToWait = comTX_MIN_BLOCK_TIME;
\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
232 /*-----------------------------------------------------------*/
\r
234 static void vComRxTask( void *pvParameters )
\r
236 portBASE_TYPE xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;
\r
237 signed char *pcExpectedByte, cRxedChar;
\r
238 const xComPortHandle xPort = NULL;
\r
240 /* The parameter is not used in this example. */
\r
241 ( void ) pvParameters;
\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
247 /* The first expected Rx character is the first in the string that is
\r
249 pcExpectedByte = ( signed char * ) comTRANSACTED_STRING;
\r
253 /* Wait for the next character. */
\r
254 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )
\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
264 case comtstWAITING_START_OF_STRING:
\r
265 if( cRxedChar == *pcExpectedByte )
\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
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
281 case comtstWAITING_END_OF_STRING:
\r
282 if( cRxedChar == *pcExpectedByte )
\r
284 /* The received character was the expected character. Was
\r
285 it the last character in the string - i.e. the null
\r
287 if( cRxedChar == 0x00 )
\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
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
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
307 /* Wait for the next character in the string. */
\r
313 /* The character received was not that expected. */
\r
314 xErrorOccurred = pdTRUE;
\r
319 /* Should not get here. Stop the Rx loop counter from
\r
320 incrementing to latch the error. */
\r
321 xErrorOccurred = pdTRUE;
\r
326 /*-----------------------------------------------------------*/
\r
328 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
330 portBASE_TYPE xReturn;
\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
344 /* Reset the count of successful Rx loops. When this function is called
\r
345 again it should have been incremented again. */
\r