FreeRTOS
[armadillo_firmware.git] / FreeRTOS / Common / Minimal / comtest.c
blob9ce43f8c965c61422ac08235a9eff45f6ceda132
1 /*
2 FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd.
4 ***************************************************************************
5 * *
6 * If you are: *
7 * *
8 * + New to FreeRTOS, *
9 * + Wanting to learn FreeRTOS or multitasking in general quickly *
10 * + Looking for basic training, *
11 * + Wanting to improve your FreeRTOS skills and productivity *
12 * *
13 * then take a look at the FreeRTOS eBook *
14 * *
15 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" *
16 * http://www.FreeRTOS.org/Documentation *
17 * *
18 * A pdf reference manual is also available. Both are usually delivered *
19 * to your inbox within 20 minutes to two hours when purchased between 8am *
20 * and 8pm GMT (although please allow up to 24 hours in case of *
21 * exceptional circumstances). Thank you for your support! *
22 * *
23 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 ***NOTE*** The exception to the GPL is included to allow you to distribute
31 a combined work that includes FreeRTOS without being obliged to provide the
32 source code for proprietary components outside of the FreeRTOS kernel.
33 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT
34 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
40 FreeRTOS WEB site.
42 1 tab == 4 spaces!
44 http://www.FreeRTOS.org - Documentation, latest information, license and
45 contact details.
47 http://www.SafeRTOS.com - A version that is certified for use in safety
48 critical systems.
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
56 * This version of comtest. c is for use on systems that have limited stack
57 * space and no display facilities. The complete version can be found in
58 * the Demo/Common/Full directory.
60 * Creates two tasks that operate on an interrupt driven serial port. A
61 * loopback connector should be used so that everything that is transmitted is
62 * also received. The serial port does not use any flow control. On a
63 * standard 9way 'D' connector pins two and three should be connected together.
65 * The first task posts a sequence of characters to the Tx queue, toggling an
66 * LED on each successful post. At the end of the sequence it sleeps for a
67 * pseudo-random period before resending the same sequence.
69 * The UART Tx end interrupt is enabled whenever data is available in the Tx
70 * queue. The Tx end ISR removes a single character from the Tx queue and
71 * passes it to the UART for transmission.
73 * The second task blocks on the Rx queue waiting for a character to become
74 * available. When the UART Rx end interrupt receives a character it places
75 * it in the Rx queue, waking the second task. The second task checks that the
76 * characters removed from the Rx queue form the same sequence as those posted
77 * to the Tx queue, and toggles an LED for each correct character.
79 * The receiving task is spawned with a higher priority than the transmitting
80 * task. The receiver will therefore wake every time a character is
81 * transmitted so neither the Tx or Rx queue should ever hold more than a few
82 * characters.
86 /* Scheduler include files. */
87 #include <stdlib.h>
88 #include "FreeRTOS.h"
89 #include "task.h"
91 /* Demo program include files. */
92 #include "serial.h"
93 #include "comtest.h"
94 #include "partest.h"
96 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
97 #define comTX_LED_OFFSET ( 0 )
98 #define comRX_LED_OFFSET ( 1 )
99 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )
101 /* The Tx task will transmit the sequence of characters at a pseudo random
102 interval. This is the maximum and minimum block time between sends. */
103 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 )
104 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 )
105 #define comOFFSET_TIME ( ( portTickType ) 3 )
107 /* We should find that each character can be queued for Tx immediately and we
108 don't have to block to send. */
109 #define comNO_BLOCK ( ( portTickType ) 0 )
111 /* The Rx task will block on the Rx queue for a long period. */
112 #define comRX_BLOCK_TIME ( ( portTickType ) 0xffff )
114 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */
115 #define comFIRST_BYTE ( 'A' )
116 #define comLAST_BYTE ( 'X' )
118 #define comBUFFER_LEN ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 )
119 #define comINITIAL_RX_COUNT_VALUE ( 0 )
121 /* Handle to the com port used by both tasks. */
122 static xComPortHandle xPort = NULL;
124 /* The transmit task as described at the top of the file. */
125 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );
127 /* The receive task as described at the top of the file. */
128 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );
130 /* The LED that should be toggled by the Rx and Tx tasks. The Rx task will
131 toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED
132 ( uxBaseLED + comTX_LED_OFFSET ). */
133 static unsigned portBASE_TYPE uxBaseLED = 0;
135 /* Check variable used to ensure no error have occurred. The Rx task will
136 increment this variable after every successfully received sequence. If at any
137 time the sequence is incorrect the the variable will stop being incremented. */
138 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE;
140 /*-----------------------------------------------------------*/
142 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED )
144 /* Initialise the com port then spawn the Rx and Tx tasks. */
145 uxBaseLED = uxLED;
146 xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );
148 /* The Tx task is spawned with a lower priority than the Rx task. */
149 xTaskCreate( vComTxTask, ( signed char * ) "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL );
150 xTaskCreate( vComRxTask, ( signed char * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL );
152 /*-----------------------------------------------------------*/
154 static portTASK_FUNCTION( vComTxTask, pvParameters )
156 signed char cByteToSend;
157 portTickType xTimeToWait;
159 /* Just to stop compiler warnings. */
160 ( void ) pvParameters;
162 for( ;; )
164 /* Simply transmit a sequence of characters from comFIRST_BYTE to
165 comLAST_BYTE. */
166 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )
168 if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )
170 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
174 /* Turn the LED off while we are not doing anything. */
175 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );
177 /* We have posted all the characters in the string - wait before
178 re-sending. Wait a pseudo-random time as this will provide a better
179 test. */
180 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
182 /* Make sure we don't wait too long... */
183 xTimeToWait %= comTX_MAX_BLOCK_TIME;
185 /* ...but we do want to wait. */
186 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
188 xTimeToWait = comTX_MIN_BLOCK_TIME;
191 vTaskDelay( xTimeToWait );
193 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
194 /*-----------------------------------------------------------*/
196 static portTASK_FUNCTION( vComRxTask, pvParameters )
198 signed char cExpectedByte, cByteRxed;
199 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;
201 /* Just to stop compiler warnings. */
202 ( void ) pvParameters;
204 for( ;; )
206 /* We expect to receive the characters from comFIRST_BYTE to
207 comLAST_BYTE in an incrementing order. Loop to receive each byte. */
208 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )
210 /* Block on the queue that contains received bytes until a byte is
211 available. */
212 if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )
214 /* Was this the byte we were expecting? If so, toggle the LED,
215 otherwise we are out on sync and should break out of the loop
216 until the expected character sequence is about to restart. */
217 if( cByteRxed == cExpectedByte )
219 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
221 else
223 xResyncRequired = pdTRUE;
224 break; /*lint !e960 Non-switch break allowed. */
229 /* Turn the LED off while we are not doing anything. */
230 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );
232 /* Did we break out of the loop because the characters were received in
233 an unexpected order? If so wait here until the character sequence is
234 about to restart. */
235 if( xResyncRequired == pdTRUE )
237 while( cByteRxed != comLAST_BYTE )
239 /* Block until the next char is available. */
240 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );
243 /* Note that an error occurred which caused us to have to resync.
244 We use this to stop incrementing the loop counter so
245 sAreComTestTasksStillRunning() will return false - indicating an
246 error. */
247 xErrorOccurred++;
249 /* We have now resynced with the Tx task and can continue. */
250 xResyncRequired = pdFALSE;
252 else
254 if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )
256 /* Increment the count of successful loops. As error
257 occurring (i.e. an unexpected character being received) will
258 prevent this counter being incremented for the rest of the
259 execution. Don't worry about mutual exclusion on this
260 variable - it doesn't really matter as we just want it
261 to change. */
262 uxRxLoops++;
266 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
267 /*-----------------------------------------------------------*/
269 portBASE_TYPE xAreComTestTasksStillRunning( void )
271 portBASE_TYPE xReturn;
273 /* If the count of successful reception loops has not changed than at
274 some time an error occurred (i.e. a character was received out of sequence)
275 and we will return false. */
276 if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )
278 xReturn = pdFALSE;
280 else
282 xReturn = pdTRUE;
285 /* Reset the count of successful Rx loops. When this function is called
286 again we expect this to have been incremented. */
287 uxRxLoops = comINITIAL_RX_COUNT_VALUE;
289 return xReturn;