2 FreeRTOS.org V5.0.0 - Copyright (C) 2003-2008 Richard Barry.
\r
4 This file is part of the FreeRTOS.org distribution.
\r
6 FreeRTOS.org is free software; you can redistribute it and/or modify
\r
7 it under the terms of the GNU General Public License as published by
\r
8 the Free Software Foundation; either version 2 of the License, or
\r
9 (at your option) any later version.
\r
11 FreeRTOS.org is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 GNU General Public License for more details.
\r
16 You should have received a copy of the GNU General Public License
\r
17 along with FreeRTOS.org; if not, write to the Free Software
\r
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 A special exception to the GPL can be applied should you wish to distribute
\r
21 a combined work that includes FreeRTOS.org, without being obliged to provide
\r
22 the source code for any proprietary components. See the licensing section
\r
23 of http://www.FreeRTOS.org for full details of how and when the exception
\r
26 ***************************************************************************
\r
27 ***************************************************************************
\r
29 * SAVE TIME AND MONEY! We can port FreeRTOS.org to your own hardware, *
\r
30 * and even write all or part of your application on your behalf. *
\r
31 * See http://www.OpenRTOS.com for details of the services we provide to *
\r
32 * expedite your project. *
\r
34 ***************************************************************************
\r
35 ***************************************************************************
\r
37 Please ensure to read the configuration and relevant port sections of the
\r
38 online documentation.
\r
40 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
43 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
46 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
47 licensing and training services.
\r
52 * This version of comtest. c is for use on systems that have limited stack
\r
53 * space and no display facilities. The complete version can be found in
\r
54 * the Demo/Common/Full directory.
\r
56 * Creates two tasks that operate on an interrupt driven serial port. A
\r
57 * loopback connector should be used so that everything that is transmitted is
\r
58 * also received. The serial port does not use any flow control. On a
\r
59 * standard 9way 'D' connector pins two and three should be connected together.
\r
61 * The first task posts a sequence of characters to the Tx queue, toggling an
\r
62 * LED on each successful post. At the end of the sequence it sleeps for a
\r
63 * pseudo-random period before resending the same sequence.
\r
65 * The UART Tx end interrupt is enabled whenever data is available in the Tx
\r
66 * queue. The Tx end ISR removes a single character from the Tx queue and
\r
67 * passes it to the UART for transmission.
\r
69 * The second task blocks on the Rx queue waiting for a character to become
\r
70 * available. When the UART Rx end interrupt receives a character it places
\r
71 * it in the Rx queue, waking the second task. The second task checks that the
\r
72 * characters removed from the Rx queue form the same sequence as those posted
\r
73 * to the Tx queue, and toggles an LED for each correct character.
\r
75 * The receiving task is spawned with a higher priority than the transmitting
\r
76 * task. The receiver will therefore wake every time a character is
\r
77 * transmitted so neither the Tx or Rx queue should ever hold more than a few
\r
83 Changes from V1.2.0:
\r
85 + Reduced the maximum time between successive transmissions. This provides
\r
86 for a more rigorous test.
\r
90 + Delay periods are now specified using variables and constants of
\r
91 portTickType rather than unsigned portLONG.
\r
95 + The constant comOFFSET_TIME added to the delay period to ensure a more
\r
96 random delay period is used.
\r
99 /* Scheduler include files. */
\r
100 #include <stdlib.h>
\r
101 #include "FreeRTOS.h"
\r
104 /* Demo program include files. */
\r
105 #include "serial.h"
\r
106 #include "comtest.h"
\r
107 #include "partest.h"
\r
109 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\r
110 #define comTX_LED_OFFSET ( 0 )
\r
111 #define comRX_LED_OFFSET ( 1 )
\r
112 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )
\r
114 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
115 interval. This is the maximum and minimum block time between sends. */
\r
116 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 )
\r
117 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 )
\r
118 #define comOFFSET_TIME ( ( portTickType ) 3 )
\r
120 /* We should find that each character can be queued for Tx immediately and we
\r
121 don't have to block to send. */
\r
122 #define comNO_BLOCK ( ( portTickType ) 0 )
\r
124 /* The Rx task will block on the Rx queue for a long period. */
\r
125 #define comRX_BLOCK_TIME ( ( portTickType ) 0xffff )
\r
127 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */
\r
128 #define comFIRST_BYTE ( 'A' )
\r
129 #define comLAST_BYTE ( 'X' )
\r
131 #define comBUFFER_LEN ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )
\r
132 #define comINITIAL_RX_COUNT_VALUE ( 0 )
\r
134 /* Handle to the com port used by both tasks. */
\r
135 static xComPortHandle xPort = NULL;
\r
137 /* The transmit task as described at the top of the file. */
\r
138 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );
\r
140 /* The receive task as described at the top of the file. */
\r
141 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );
\r
143 /* The LED that should be toggled by the Rx and Tx tasks. The Rx task will
\r
144 toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED
\r
145 ( uxBaseLED + comTX_LED_OFFSET ). */
\r
146 static unsigned portBASE_TYPE uxBaseLED = 0;
\r
148 /* Check variable used to ensure no error have occurred. The Rx task will
\r
149 increment this variable after every successfully received sequence. If at any
\r
150 time the sequence is incorrect the the variable will stop being incremented. */
\r
151 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r
153 /*-----------------------------------------------------------*/
\r
155 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned portLONG ulBaudRate, unsigned portBASE_TYPE uxLED )
\r
157 /* Initialise the com port then spawn the Rx and Tx tasks. */
\r
159 xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );
\r
161 /* The Tx task is spawned with a lower priority than the Rx task. */
\r
162 xTaskCreate( vComTxTask, ( signed portCHAR * ) "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL );
\r
163 xTaskCreate( vComRxTask, ( signed portCHAR * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );
\r
165 /*-----------------------------------------------------------*/
\r
167 static portTASK_FUNCTION( vComTxTask, pvParameters )
\r
169 signed portCHAR cByteToSend;
\r
170 portTickType xTimeToWait;
\r
172 /* Just to stop compiler warnings. */
\r
173 ( void ) pvParameters;
\r
177 /* Simply transmit a sequence of characters from comFIRST_BYTE to
\r
179 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )
\r
181 if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )
\r
183 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
\r
187 /* Turn the LED off while we are not doing anything. */
\r
188 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );
\r
190 /* We have posted all the characters in the string - wait before
\r
191 re-sending. Wait a pseudo-random time as this will provide a better
\r
193 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
195 /* Make sure we don't wait too long... */
\r
196 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
198 /* ...but we do want to wait. */
\r
199 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
201 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
204 vTaskDelay( xTimeToWait );
\r
206 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
207 /*-----------------------------------------------------------*/
\r
209 static portTASK_FUNCTION( vComRxTask, pvParameters )
\r
211 signed portCHAR cExpectedByte, cByteRxed;
\r
212 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;
\r
214 /* Just to stop compiler warnings. */
\r
215 ( void ) pvParameters;
\r
219 /* We expect to receive the characters from comFIRST_BYTE to
\r
220 comLAST_BYTE in an incrementing order. Loop to receive each byte. */
\r
221 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )
\r
223 /* Block on the queue that contains received bytes until a byte is
\r
225 if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )
\r
227 /* Was this the byte we were expecting? If so, toggle the LED,
\r
228 otherwise we are out on sync and should break out of the loop
\r
229 until the expected character sequence is about to restart. */
\r
230 if( cByteRxed == cExpectedByte )
\r
232 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
\r
236 xResyncRequired = pdTRUE;
\r
237 break; /*lint !e960 Non-switch break allowed. */
\r
242 /* Turn the LED off while we are not doing anything. */
\r
243 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );
\r
245 /* Did we break out of the loop because the characters were received in
\r
246 an unexpected order? If so wait here until the character sequence is
\r
247 about to restart. */
\r
248 if( xResyncRequired == pdTRUE )
\r
250 while( cByteRxed != comLAST_BYTE )
\r
252 /* Block until the next char is available. */
\r
253 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );
\r
256 /* Note that an error occurred which caused us to have to resync.
\r
257 We use this to stop incrementing the loop counter so
\r
258 sAreComTestTasksStillRunning() will return false - indicating an
\r
262 /* We have now resynced with the Tx task and can continue. */
\r
263 xResyncRequired = pdFALSE;
\r
267 if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )
\r
269 /* Increment the count of successful loops. As error
\r
270 occurring (i.e. an unexpected character being received) will
\r
271 prevent this counter being incremented for the rest of the
\r
272 execution. Don't worry about mutual exclusion on this
\r
273 variable - it doesn't really matter as we just want it
\r
279 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
280 /*-----------------------------------------------------------*/
\r
282 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
284 portBASE_TYPE xReturn;
\r
286 /* If the count of successful reception loops has not changed than at
\r
287 some time an error occurred (i.e. a character was received out of sequence)
\r
288 and we will return false. */
\r
289 if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )
\r
298 /* Reset the count of successful Rx loops. When this function is called
\r
299 again we expect this to have been incremented. */
\r
300 uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r