2 FreeRTOS V8.2.2 - Copyright (C) 2015 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
72 * Creates a task and a timer that operate on an interrupt driven serial port.
\r
73 * This demo assumes that the characters transmitted on a port will also be
\r
74 * received on the same port. Therefore, the UART must either be connected to
\r
75 * an echo server, or the uart connector must have a loopback connector fitted.
\r
76 * See http://www.serialporttool.com/CommEcho.htm for a suitable echo server
\r
77 * for Windows hosts.
\r
79 * The timer sends a string to the UART, toggles an LED, then resets itself by
\r
80 * changing its own period. The period is calculated as a pseudo random number
\r
81 * between comTX_MAX_BLOCK_TIME and comTX_MIN_BLOCK_TIME.
\r
83 * The task blocks on an Rx queue waiting for a character to become available.
\r
84 * Received characters are checked to ensure they match those transmitted by the
\r
85 * Tx timer. An error is latched if characters are missing, incorrect, or
\r
86 * arrive too slowly.
\r
88 * How characters are actually transmitted and received is port specific. Demos
\r
89 * that include this test/demo file will provide example drivers. The Tx timer
\r
90 * executes in the context of the timer service (daemon) task, and must
\r
91 * therefore never attempt to block.
\r
95 /* Scheduler include files. */
\r
98 #include "FreeRTOS.h"
\r
100 #include "timers.h"
\r
102 #ifndef configUSE_TIMERS
\r
103 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
106 #if configUSE_TIMERS != 1
\r
107 #error This demo uses timers. configUSE_TIMERS must be set to 1 in FreeRTOSConfig.h.
\r
111 /* Demo program include files. */
\r
112 #include "serial.h"
\r
113 #include "comtest_strings.h"
\r
114 #include "partest.h"
\r
116 /* The size of the stack given to the Rx task. */
\r
117 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\r
119 /* See the comment above the declaraction of the uxBaseLED variable. */
\r
120 #define comTX_LED_OFFSET ( 0 )
\r
121 #define comRX_LED_OFFSET ( 1 )
\r
123 /* The Tx timer transmits the sequence of characters at a pseudo random
\r
124 interval that is capped between comTX_MAX_BLOCK_TIME and
\r
125 comTX_MIN_BLOCK_TIME. */
\r
126 #define comTX_MAX_BLOCK_TIME ( ( TickType_t ) 0x96 )
\r
127 #define comTX_MIN_BLOCK_TIME ( ( TickType_t ) 0x32 )
\r
128 #define comOFFSET_TIME ( ( TickType_t ) 3 )
\r
130 /* States for the simple state machine implemented in the Rx task. */
\r
131 #define comtstWAITING_START_OF_STRING 0
\r
132 #define comtstWAITING_END_OF_STRING 1
\r
134 /* A short delay in ticks - this delay is used to allow the Rx queue to fill up
\r
135 a bit so more than one character can be processed at a time. This is relative
\r
136 to comTX_MIN_BLOCK_TIME to ensure it is never longer than the shortest gap
\r
137 between transmissions. It could be worked out more scientifically from the
\r
138 baud rate being used. */
\r
139 #define comSHORT_DELAY ( comTX_MIN_BLOCK_TIME >> ( TickType_t ) 2 )
\r
141 /* The string that is transmitted and received. */
\r
142 #define comTRANSACTED_STRING "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
\r
144 /* A block time of 0 simply means "don't block". */
\r
145 #define comtstDONT_BLOCK ( TickType_t ) 0
\r
147 /* Handle to the com port used by both tasks. */
\r
148 static xComPortHandle xPort = NULL;
\r
150 /* The callback function allocated to the transmit timer, as described in the
\r
151 comments at the top of this file. */
\r
152 static void prvComTxTimerCallback( TimerHandle_t xTimer );
\r
154 /* The receive task as described in the comments at the top of this file. */
\r
155 static void vComRxTask( void *pvParameters );
\r
157 /* The Rx task will toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task
\r
158 will toggle LED ( uxBaseLED + comTX_LED_OFFSET ). */
\r
159 static UBaseType_t uxBaseLED = 0;
\r
161 /* The Rx task toggles uxRxLoops on each successful iteration of its defined
\r
162 function - provided no errors have ever been latched. If this variable stops
\r
163 incrementing, then an error has occurred. */
\r
164 static volatile UBaseType_t uxRxLoops = 0UL;
\r
166 /* The timer used to periodically transmit the string. This is the timer that
\r
167 has prvComTxTimerCallback allocated to it as its callback function. */
\r
168 static TimerHandle_t xTxTimer = NULL;
\r
170 /* The string length is held at file scope so the Tx timer does not need to
\r
171 calculate it each time it executes. */
\r
172 static size_t xStringLength = 0U;
\r
174 /*-----------------------------------------------------------*/
\r
176 void vStartComTestStringsTasks( UBaseType_t uxPriority, uint32_t ulBaudRate, UBaseType_t uxLED )
\r
178 /* Store values that are used at run time. */
\r
181 /* Calculate the string length here, rather than each time the Tx timer
\r
183 xStringLength = strlen( comTRANSACTED_STRING );
\r
185 /* Include the null terminator in the string length as this is used to
\r
186 detect the end of the string in the Rx task. */
\r
189 /* Initialise the com port, then spawn the Rx task and create the Tx
\r
191 xSerialPortInitMinimal( ulBaudRate, ( xStringLength * 2U ) );
\r
193 /* Create the Rx task and the Tx timer. The timer is started from the
\r
195 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );
\r
196 xTxTimer = xTimerCreate( "TxTimer", comTX_MIN_BLOCK_TIME, pdFALSE, NULL, prvComTxTimerCallback );
\r
197 configASSERT( xTxTimer );
\r
199 /*-----------------------------------------------------------*/
\r
201 static void prvComTxTimerCallback( TimerHandle_t xTimer )
\r
203 TickType_t xTimeToWait;
\r
205 /* The parameter is not used in this case. */
\r
208 /* Send the string. How this is actually performed depends on the
\r
209 sample driver provided with this demo. However - as this is a timer,
\r
210 it executes in the context of the timer task and therefore must not
\r
212 vSerialPutString( xPort, comTRANSACTED_STRING, xStringLength );
\r
214 /* Toggle an LED to give a visible indication that another transmission
\r
215 has been performed. */
\r
216 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
\r
218 /* Wait a pseudo random time before sending the string again. */
\r
219 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
221 /* Ensure the time to wait is not greater than comTX_MAX_BLOCK_TIME. */
\r
222 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
224 /* Ensure the time to wait is not less than comTX_MIN_BLOCK_TIME. */
\r
225 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
227 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
230 /* Reset the timer to run again xTimeToWait ticks from now. This function
\r
231 is called from the context of the timer task, so the block time must not
\r
232 be anything other than zero. */
\r
233 xTimerChangePeriod( xTxTimer, xTimeToWait, comtstDONT_BLOCK );
\r
235 /*-----------------------------------------------------------*/
\r
237 static void vComRxTask( void *pvParameters )
\r
239 BaseType_t xState = comtstWAITING_START_OF_STRING, xErrorOccurred = pdFALSE;
\r
240 char *pcExpectedByte, cRxedChar;
\r
241 const xComPortHandle xPort = NULL;
\r
243 /* The parameter is not used in this example. */
\r
244 ( void ) pvParameters;
\r
246 /* Start the Tx timer. This only needs to be started once, as it will
\r
247 reset itself thereafter. */
\r
248 xTimerStart( xTxTimer, portMAX_DELAY );
\r
250 /* The first expected Rx character is the first in the string that is
\r
252 pcExpectedByte = comTRANSACTED_STRING;
\r
256 /* Wait for the next character. */
\r
257 if( xSerialGetChar( xPort, &cRxedChar, ( comTX_MAX_BLOCK_TIME * 2 ) ) == pdFALSE )
\r
259 /* A character definitely should have been received by now. As a
\r
260 character was not received an error must have occurred (which might
\r
261 just be that the loopback connector is not fitted). */
\r
262 xErrorOccurred = pdTRUE;
\r
267 case comtstWAITING_START_OF_STRING:
\r
268 if( cRxedChar == *pcExpectedByte )
\r
270 /* The received character was the first character of the
\r
271 string. Move to the next state to check each character
\r
272 as it comes in until the entire string has been received. */
\r
273 xState = comtstWAITING_END_OF_STRING;
\r
276 /* Block for a short period. This just allows the Rx queue
\r
277 to contain more than one character, and therefore prevent
\r
278 thrashing reads to the queue, and repetitive context
\r
279 switches as each character is received. */
\r
280 vTaskDelay( comSHORT_DELAY );
\r
284 case comtstWAITING_END_OF_STRING:
\r
285 if( cRxedChar == *pcExpectedByte )
\r
287 /* The received character was the expected character. Was
\r
288 it the last character in the string - i.e. the null
\r
290 if( cRxedChar == 0x00 )
\r
292 /* The entire string has been received. If no errors
\r
293 have been latched, then increment the loop counter to
\r
294 show this task is still healthy. */
\r
295 if( xErrorOccurred == pdFALSE )
\r
299 /* Toggle an LED to give a visible sign that a
\r
300 complete string has been received. */
\r
301 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
\r
304 /* Go back to wait for the start of the next string. */
\r
305 pcExpectedByte = comTRANSACTED_STRING;
\r
306 xState = comtstWAITING_START_OF_STRING;
\r
310 /* Wait for the next character in the string. */
\r
316 /* The character received was not that expected. */
\r
317 xErrorOccurred = pdTRUE;
\r
322 /* Should not get here. Stop the Rx loop counter from
\r
323 incrementing to latch the error. */
\r
324 xErrorOccurred = pdTRUE;
\r
329 /*-----------------------------------------------------------*/
\r
331 BaseType_t xAreComTestTasksStillRunning( void )
\r
333 BaseType_t xReturn;
\r
335 /* If the count of successful reception loops has not changed than at
\r
336 some time an error occurred (i.e. a character was received out of sequence)
\r
337 and false is returned. */
\r
338 if( uxRxLoops == 0UL )
\r
347 /* Reset the count of successful Rx loops. When this function is called
\r
348 again it should have been incremented again. */
\r