2 FreeRTOS.org V5.3.0 - Copyright (C) 2003-2009 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 it
\r
7 under the terms of the GNU General Public License (version 2) as published
\r
8 by the Free Software Foundation and modified by the FreeRTOS exception.
\r
9 **NOTE** The exception to the GPL is included to allow you to distribute a
\r
10 combined work that includes FreeRTOS.org without being obliged to provide
\r
11 the source code for any proprietary components. Alternative commercial
\r
12 license and support terms are also available upon request. See the
\r
13 licensing section of http://www.FreeRTOS.org for full details.
\r
15 FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT
\r
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
20 You should have received a copy of the GNU General Public License along
\r
21 with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59
\r
22 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\r
25 ***************************************************************************
\r
27 * Get the FreeRTOS eBook! See http://www.FreeRTOS.org/Documentation *
\r
29 * This is a concise, step by step, 'hands on' guide that describes both *
\r
30 * general multitasking concepts and FreeRTOS specifics. It presents and *
\r
31 * explains numerous examples that are written using the FreeRTOS API. *
\r
32 * Full source code for all the examples is provided in an accompanying *
\r
35 ***************************************************************************
\r
39 Please ensure to read the configuration and relevant port sections of the
\r
40 online documentation.
\r
42 http://www.FreeRTOS.org - Documentation, latest information, license and
\r
45 http://www.SafeRTOS.com - A version that is certified for use in safety
\r
48 http://www.OpenRTOS.com - Commercial support, development, porting,
\r
49 licensing and training services.
\r
54 * This version of comtest. c is for use on systems that have limited stack
\r
55 * space and no display facilities. The complete version can be found in
\r
56 * the Demo/Common/Full directory.
\r
58 * Creates two tasks that operate on an interrupt driven serial port. A
\r
59 * loopback connector should be used so that everything that is transmitted is
\r
60 * also received. The serial port does not use any flow control. On a
\r
61 * standard 9way 'D' connector pins two and three should be connected together.
\r
63 * The first task posts a sequence of characters to the Tx queue, toggling an
\r
64 * LED on each successful post. At the end of the sequence it sleeps for a
\r
65 * pseudo-random period before resending the same sequence.
\r
67 * The UART Tx end interrupt is enabled whenever data is available in the Tx
\r
68 * queue. The Tx end ISR removes a single character from the Tx queue and
\r
69 * passes it to the UART for transmission.
\r
71 * The second task blocks on the Rx queue waiting for a character to become
\r
72 * available. When the UART Rx end interrupt receives a character it places
\r
73 * it in the Rx queue, waking the second task. The second task checks that the
\r
74 * characters removed from the Rx queue form the same sequence as those posted
\r
75 * to the Tx queue, and toggles an LED for each correct character.
\r
77 * The receiving task is spawned with a higher priority than the transmitting
\r
78 * task. The receiver will therefore wake every time a character is
\r
79 * transmitted so neither the Tx or Rx queue should ever hold more than a few
\r
84 /* Scheduler include files. */
\r
86 #include "FreeRTOS.h"
\r
89 /* Demo program include files. */
\r
91 #include "comtest.h"
\r
92 #include "partest.h"
\r
94 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\r
95 #define comTX_LED_OFFSET ( 0 )
\r
96 #define comRX_LED_OFFSET ( 1 )
\r
97 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )
\r
99 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
100 interval. This is the maximum and minimum block time between sends. */
\r
101 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 )
\r
102 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 )
\r
103 #define comOFFSET_TIME ( ( portTickType ) 3 )
\r
105 /* We should find that each character can be queued for Tx immediately and we
\r
106 don't have to block to send. */
\r
107 #define comNO_BLOCK ( ( portTickType ) 0 )
\r
109 /* The Rx task will block on the Rx queue for a long period. */
\r
110 #define comRX_BLOCK_TIME ( ( portTickType ) 0xffff )
\r
112 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */
\r
113 #define comFIRST_BYTE ( 'A' )
\r
114 #define comLAST_BYTE ( 'X' )
\r
116 #define comBUFFER_LEN ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )
\r
117 #define comINITIAL_RX_COUNT_VALUE ( 0 )
\r
119 /* Handle to the com port used by both tasks. */
\r
120 static xComPortHandle xPort = NULL;
\r
122 /* The transmit task as described at the top of the file. */
\r
123 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );
\r
125 /* The receive task as described at the top of the file. */
\r
126 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );
\r
128 /* The LED that should be toggled by the Rx and Tx tasks. The Rx task will
\r
129 toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED
\r
130 ( uxBaseLED + comTX_LED_OFFSET ). */
\r
131 static unsigned portBASE_TYPE uxBaseLED = 0;
\r
133 /* Check variable used to ensure no error have occurred. The Rx task will
\r
134 increment this variable after every successfully received sequence. If at any
\r
135 time the sequence is incorrect the the variable will stop being incremented. */
\r
136 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r
138 /*-----------------------------------------------------------*/
\r
140 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned portLONG ulBaudRate, unsigned portBASE_TYPE uxLED )
\r
142 /* Initialise the com port then spawn the Rx and Tx tasks. */
\r
144 xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );
\r
146 /* The Tx task is spawned with a lower priority than the Rx task. */
\r
147 xTaskCreate( vComTxTask, ( signed portCHAR * ) "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL );
\r
148 xTaskCreate( vComRxTask, ( signed portCHAR * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );
\r
150 /*-----------------------------------------------------------*/
\r
152 static portTASK_FUNCTION( vComTxTask, pvParameters )
\r
154 signed portCHAR cByteToSend;
\r
155 portTickType xTimeToWait;
\r
157 /* Just to stop compiler warnings. */
\r
158 ( void ) pvParameters;
\r
162 /* Simply transmit a sequence of characters from comFIRST_BYTE to
\r
164 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )
\r
166 if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )
\r
168 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
\r
172 /* Turn the LED off while we are not doing anything. */
\r
173 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );
\r
175 /* We have posted all the characters in the string - wait before
\r
176 re-sending. Wait a pseudo-random time as this will provide a better
\r
178 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
180 /* Make sure we don't wait too long... */
\r
181 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
183 /* ...but we do want to wait. */
\r
184 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
186 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
189 vTaskDelay( xTimeToWait );
\r
191 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
192 /*-----------------------------------------------------------*/
\r
194 static portTASK_FUNCTION( vComRxTask, pvParameters )
\r
196 signed portCHAR cExpectedByte, cByteRxed;
\r
197 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;
\r
199 /* Just to stop compiler warnings. */
\r
200 ( void ) pvParameters;
\r
204 /* We expect to receive the characters from comFIRST_BYTE to
\r
205 comLAST_BYTE in an incrementing order. Loop to receive each byte. */
\r
206 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )
\r
208 /* Block on the queue that contains received bytes until a byte is
\r
210 if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )
\r
212 /* Was this the byte we were expecting? If so, toggle the LED,
\r
213 otherwise we are out on sync and should break out of the loop
\r
214 until the expected character sequence is about to restart. */
\r
215 if( cByteRxed == cExpectedByte )
\r
217 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
\r
221 xResyncRequired = pdTRUE;
\r
222 break; /*lint !e960 Non-switch break allowed. */
\r
227 /* Turn the LED off while we are not doing anything. */
\r
228 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );
\r
230 /* Did we break out of the loop because the characters were received in
\r
231 an unexpected order? If so wait here until the character sequence is
\r
232 about to restart. */
\r
233 if( xResyncRequired == pdTRUE )
\r
235 while( cByteRxed != comLAST_BYTE )
\r
237 /* Block until the next char is available. */
\r
238 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );
\r
241 /* Note that an error occurred which caused us to have to resync.
\r
242 We use this to stop incrementing the loop counter so
\r
243 sAreComTestTasksStillRunning() will return false - indicating an
\r
247 /* We have now resynced with the Tx task and can continue. */
\r
248 xResyncRequired = pdFALSE;
\r
252 if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )
\r
254 /* Increment the count of successful loops. As error
\r
255 occurring (i.e. an unexpected character being received) will
\r
256 prevent this counter being incremented for the rest of the
\r
257 execution. Don't worry about mutual exclusion on this
\r
258 variable - it doesn't really matter as we just want it
\r
264 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
265 /*-----------------------------------------------------------*/
\r
267 portBASE_TYPE xAreComTestTasksStillRunning( void )
\r
269 portBASE_TYPE xReturn;
\r
271 /* If the count of successful reception loops has not changed than at
\r
272 some time an error occurred (i.e. a character was received out of sequence)
\r
273 and we will return false. */
\r
274 if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )
\r
283 /* Reset the count of successful Rx loops. When this function is called
\r
284 again we expect this to have been incremented. */
\r
285 uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r