2 FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
72 * This version of comtest. c is for use on systems that have limited stack
73 * space and no display facilities. The complete version can be found in
74 * the Demo/Common/Full directory.
76 * Creates two tasks that operate on an interrupt driven serial port. A
77 * loopback connector should be used so that everything that is transmitted is
78 * also received. The serial port does not use any flow control. On a
79 * standard 9way 'D' connector pins two and three should be connected together.
81 * The first task posts a sequence of characters to the Tx queue, toggling an
82 * LED on each successful post. At the end of the sequence it sleeps for a
83 * pseudo-random period before resending the same sequence.
85 * The UART Tx end interrupt is enabled whenever data is available in the Tx
86 * queue. The Tx end ISR removes a single character from the Tx queue and
87 * passes it to the UART for transmission.
89 * The second task blocks on the Rx queue waiting for a character to become
90 * available. When the UART Rx end interrupt receives a character it places
91 * it in the Rx queue, waking the second task. The second task checks that the
92 * characters removed from the Rx queue form the same sequence as those posted
93 * to the Tx queue, and toggles an LED for each correct character.
95 * The receiving task is spawned with a higher priority than the transmitting
96 * task. The receiver will therefore wake every time a character is
97 * transmitted so neither the Tx or Rx queue should ever hold more than a few
102 /* Scheduler include files. */
104 #include "FreeRTOS.h"
107 /* Demo program include files. */
112 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
113 #define comTX_LED_OFFSET ( 0 )
114 #define comRX_LED_OFFSET ( 1 )
115 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )
117 /* The Tx task will transmit the sequence of characters at a pseudo random
118 interval. This is the maximum and minimum block time between sends. */
119 #define comTX_MAX_BLOCK_TIME ( ( TickType_t ) 0x96 )
120 #define comTX_MIN_BLOCK_TIME ( ( TickType_t ) 0x32 )
121 #define comOFFSET_TIME ( ( TickType_t ) 3 )
123 /* We should find that each character can be queued for Tx immediately and we
124 don't have to block to send. */
125 #define comNO_BLOCK ( ( TickType_t ) 0 )
127 /* The Rx task will block on the Rx queue for a long period. */
128 #define comRX_BLOCK_TIME ( ( TickType_t ) 0xffff )
130 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */
131 #define comFIRST_BYTE ( 'A' )
132 #define comLAST_BYTE ( 'X' )
134 #define comBUFFER_LEN ( ( UBaseType_t ) ( comLAST_BYTE - comFIRST_BYTE ) + ( UBaseType_t ) 1 )
135 #define comINITIAL_RX_COUNT_VALUE ( 0 )
137 /* Handle to the com port used by both tasks. */
138 static xComPortHandle xPort = NULL;
140 /* The transmit task as described at the top of the file. */
141 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );
143 /* The receive task as described at the top of the file. */
144 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );
146 /* The LED that should be toggled by the Rx and Tx tasks. The Rx task will
147 toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED
148 ( uxBaseLED + comTX_LED_OFFSET ). */
149 static UBaseType_t uxBaseLED = 0;
151 /* Check variable used to ensure no error have occurred. The Rx task will
152 increment this variable after every successfully received sequence. If at any
153 time the sequence is incorrect the the variable will stop being incremented. */
154 static volatile UBaseType_t uxRxLoops = comINITIAL_RX_COUNT_VALUE;
156 /*-----------------------------------------------------------*/
158 void vAltStartComTestTasks( UBaseType_t uxPriority, uint32_t ulBaudRate, UBaseType_t uxLED )
160 /* Initialise the com port then spawn the Rx and Tx tasks. */
162 xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );
164 /* The Tx task is spawned with a lower priority than the Rx task. */
165 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( TaskHandle_t * ) NULL );
166 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );
168 /*-----------------------------------------------------------*/
170 static portTASK_FUNCTION( vComTxTask, pvParameters )
173 TickType_t xTimeToWait;
175 /* Just to stop compiler warnings. */
176 ( void ) pvParameters;
180 /* Simply transmit a sequence of characters from comFIRST_BYTE to
182 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )
184 if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )
186 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
190 /* Turn the LED off while we are not doing anything. */
191 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );
193 /* We have posted all the characters in the string - wait before
194 re-sending. Wait a pseudo-random time as this will provide a better
196 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
198 /* Make sure we don't wait too long... */
199 xTimeToWait %= comTX_MAX_BLOCK_TIME;
201 /* ...but we do want to wait. */
202 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
204 xTimeToWait = comTX_MIN_BLOCK_TIME;
207 vTaskDelay( xTimeToWait );
209 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
210 /*-----------------------------------------------------------*/
212 static portTASK_FUNCTION( vComRxTask, pvParameters )
214 signed char cExpectedByte, cByteRxed;
215 BaseType_t xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;
217 /* Just to stop compiler warnings. */
218 ( void ) pvParameters;
222 /* We expect to receive the characters from comFIRST_BYTE to
223 comLAST_BYTE in an incrementing order. Loop to receive each byte. */
224 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )
226 /* Block on the queue that contains received bytes until a byte is
228 if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )
230 /* Was this the byte we were expecting? If so, toggle the LED,
231 otherwise we are out on sync and should break out of the loop
232 until the expected character sequence is about to restart. */
233 if( cByteRxed == cExpectedByte )
235 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
239 xResyncRequired = pdTRUE;
240 break; /*lint !e960 Non-switch break allowed. */
245 /* Turn the LED off while we are not doing anything. */
246 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );
248 /* Did we break out of the loop because the characters were received in
249 an unexpected order? If so wait here until the character sequence is
251 if( xResyncRequired == pdTRUE )
253 while( cByteRxed != comLAST_BYTE )
255 /* Block until the next char is available. */
256 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );
259 /* Note that an error occurred which caused us to have to resync.
260 We use this to stop incrementing the loop counter so
261 sAreComTestTasksStillRunning() will return false - indicating an
265 /* We have now resynced with the Tx task and can continue. */
266 xResyncRequired = pdFALSE;
270 if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )
272 /* Increment the count of successful loops. As error
273 occurring (i.e. an unexpected character being received) will
274 prevent this counter being incremented for the rest of the
275 execution. Don't worry about mutual exclusion on this
276 variable - it doesn't really matter as we just want it
282 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
283 /*-----------------------------------------------------------*/
285 BaseType_t xAreComTestTasksStillRunning( void )
289 /* If the count of successful reception loops has not changed than at
290 some time an error occurred (i.e. a character was received out of sequence)
291 and we will return false. */
292 if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )
301 /* Reset the count of successful Rx loops. When this function is called
302 again we expect this to have been incremented. */
303 uxRxLoops = comINITIAL_RX_COUNT_VALUE;