2 FreeRTOS V7.4.1 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
77 * This version of comtest. c is for use on systems that have limited stack
\r
78 * space and no display facilities. The complete version can be found in
\r
79 * the Demo/Common/Full directory.
\r
81 * Creates two tasks that operate on an interrupt driven serial port. A
\r
82 * loopback connector should be used so that everything that is transmitted is
\r
83 * also received. The serial port does not use any flow control. On a
\r
84 * standard 9way 'D' connector pins two and three should be connected together.
\r
86 * The first task posts a sequence of characters to the Tx queue, toggling an
\r
87 * LED on each successful post. At the end of the sequence it sleeps for a
\r
88 * pseudo-random period before resending the same sequence.
\r
90 * The UART Tx end interrupt is enabled whenever data is available in the Tx
\r
91 * queue. The Tx end ISR removes a single character from the Tx queue and
\r
92 * passes it to the UART for transmission.
\r
94 * The second task blocks on the Rx queue waiting for a character to become
\r
95 * available. When the UART Rx end interrupt receives a character it places
\r
96 * it in the Rx queue, waking the second task. The second task checks that the
\r
97 * characters removed from the Rx queue form the same sequence as those posted
\r
98 * to the Tx queue, and toggles an LED for each correct character.
\r
100 * The receiving task is spawned with a higher priority than the transmitting
\r
101 * task. The receiver will therefore wake every time a character is
\r
102 * transmitted so neither the Tx or Rx queue should ever hold more than a few
\r
107 /* Scheduler include files. */
\r
108 #include <stdlib.h>
\r
109 #include "FreeRTOS.h"
\r
112 /* Demo program include files. */
\r
113 #include "demo_serial.h"
\r
114 #include "comtest2.h"
\r
115 #include "partest.h"
\r
117 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\r
118 #define comTX_LED_OFFSET ( 0 )
\r
119 #define comRX_LED_OFFSET ( 1 )
\r
120 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )
\r
122 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
123 interval. This is the maximum and minimum block time between sends. */
\r
124 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 )
\r
125 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 )
\r
126 #define comOFFSET_TIME ( ( portTickType ) 3 )
\r
128 /* We should find that each character can be queued for Tx immediately and we
\r
129 don't have to block to send. */
\r
130 #define comNO_BLOCK ( ( portTickType ) 0 )
\r
132 /* The Rx task will block on the Rx queue for a long period. */
\r
133 #define comRX_BLOCK_TIME ( ( portTickType ) 0xffff )
\r
135 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */
\r
136 #define comFIRST_BYTE ( 'A' )
\r
137 #define comLAST_BYTE ( 'X' )
\r
139 #define comBUFFER_LEN ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )
\r
140 #define comINITIAL_RX_COUNT_VALUE ( 0 )
\r
142 /* Handle to the com port used by both tasks. */
\r
143 static xComPortHandle xPort = NULL;
\r
145 /* The transmit task as described at the top of the file. */
\r
146 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );
\r
148 /* The receive task as described at the top of the file. */
\r
149 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );
\r
151 /* The LED that should be toggled by the Rx and Tx tasks. The Rx task will
\r
152 toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED
\r
153 ( uxBaseLED + comTX_LED_OFFSET ). */
\r
154 static unsigned portBASE_TYPE uxBaseLED = 0;
\r
156 /* Check variable used to ensure no error have occurred. The Rx task will
\r
157 increment this variable after every successfully received sequence. If at any
\r
158 time the sequence is incorrect the the variable will stop being incremented. */
\r
159 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r
161 /*-----------------------------------------------------------*/
\r
163 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )
\r
165 /* Initialise the com port then spawn the Rx and Tx tasks. */
\r
167 xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );
\r
169 /* The Tx task is spawned with a lower priority than the Rx task. */
\r
170 xTaskCreate( vComTxTask, ( signed char * ) "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL );
\r
171 xTaskCreate( vComRxTask, ( signed char * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );
\r
173 /*-----------------------------------------------------------*/
\r
175 static portTASK_FUNCTION( vComTxTask, pvParameters )
\r
177 signed char cByteToSend;
\r
178 portTickType xTimeToWait;
\r
180 /* Just to stop compiler warnings. */
\r
181 ( void ) pvParameters;
\r
185 /* Simply transmit a sequence of characters from comFIRST_BYTE to
\r
187 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )
\r
189 if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )
\r
191 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
\r
195 /* Turn the LED off while we are not doing anything. */
\r
196 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );
\r
198 /* We have posted all the characters in the string - wait before
\r
199 re-sending. Wait a pseudo-random time as this will provide a better
\r
201 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
203 /* Make sure we don't wait too long... */
\r
204 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
206 /* ...but we do want to wait. */
\r
207 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
209 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
212 vTaskDelay( xTimeToWait );
\r
214 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
215 /*-----------------------------------------------------------*/
\r
217 static portTASK_FUNCTION( vComRxTask, pvParameters )
\r
219 signed char cExpectedByte, cByteRxed;
\r
220 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;
\r
222 /* Just to stop compiler warnings. */
\r
223 ( void ) pvParameters;
\r
227 /* We expect to receive the characters from comFIRST_BYTE to
\r
228 comLAST_BYTE in an incrementing order. Loop to receive each byte. */
\r
229 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )
\r
231 /* Block on the queue that contains received bytes until a byte is
\r
233 if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )
\r
235 /* Was this the byte we were expecting? If so, toggle the LED,
\r
236 otherwise we are out on sync and should break out of the loop
\r
237 until the expected character sequence is about to restart. */
\r
238 if( cByteRxed == cExpectedByte )
\r
240 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
\r
244 xResyncRequired = pdTRUE;
\r
245 break; /*lint !e960 Non-switch break allowed. */
\r
250 /* Turn the LED off while we are not doing anything. */
\r
251 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );
\r
253 /* Did we break out of the loop because the characters were received in
\r
254 an unexpected order? If so wait here until the character sequence is
\r
255 about to restart. */
\r
256 if( xResyncRequired == pdTRUE )
\r
258 while( cByteRxed != comLAST_BYTE )
\r
260 /* Block until the next char is available. */
\r
261 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );
\r
264 /* Note that an error occurred which caused us to have to resync.
\r
265 We use this to stop incrementing the loop counter so
\r
266 sAreComTestTasksStillRunning() will return false - indicating an
\r
270 /* We have now resynced with the Tx task and can continue. */
\r
271 xResyncRequired = pdFALSE;
\r
275 if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )
\r
277 /* Increment the count of successful loops. As error
\r
278 occurring (i.e. an unexpected character being received) will
\r
279 prevent this counter being incremented for the rest of the
\r
280 execution. Don't worry about mutual exclusion on this
\r
281 variable - it doesn't really matter as we just want it
\r
287 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
288 /*-----------------------------------------------------------*/
\r
290 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
292 portBASE_TYPE xReturn;
\r
294 /* If the count of successful reception loops has not changed than at
\r
295 some time an error occurred (i.e. a character was received out of sequence)
\r
296 and we will return false. */
\r
297 if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )
\r
306 /* Reset the count of successful Rx loops. When this function is called
\r
307 again we expect this to have been incremented. */
\r
308 uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r