2 FreeRTOS V8.2.0rc1 - Copyright (C) 2014 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
13 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
14 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
15 >>! obliged to provide the source code for proprietary components !<<
\r
16 >>! outside of the FreeRTOS kernel. !<<
\r
18 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
19 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
20 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
21 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * Having a problem? Start by reading the FAQ "My application does *
\r
28 * not run, what could be wrong?". Have you defined configASSERT()? *
\r
30 * http://www.FreeRTOS.org/FAQHelp.html *
\r
32 ***************************************************************************
\r
34 ***************************************************************************
\r
36 * FreeRTOS provides completely free yet professionally developed, *
\r
37 * robust, strictly quality controlled, supported, and cross *
\r
38 * platform software that is more than just the market leader, it *
\r
39 * is the industry's de facto standard. *
\r
41 * Help yourself get started quickly while simultaneously helping *
\r
42 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
43 * tutorial book, reference manual, or both: *
\r
44 * http://www.FreeRTOS.org/Documentation *
\r
46 ***************************************************************************
\r
48 ***************************************************************************
\r
50 * Investing in training allows your team to be as productive as *
\r
51 * possible as early as possible, lowering your overall development *
\r
52 * cost, and enabling you to bring a more robust product to market *
\r
53 * earlier than would otherwise be possible. Richard Barry is both *
\r
54 * the architect and key author of FreeRTOS, and so also the world's *
\r
55 * leading authority on what is the world's most popular real time *
\r
56 * kernel for deeply embedded MCU designs. Obtaining your training *
\r
57 * from Richard ensures your team will gain directly from his in-depth *
\r
58 * product knowledge and years of usage experience. Contact Real Time *
\r
59 * Engineers Ltd to enquire about the FreeRTOS Masterclass, presented *
\r
60 * by Richard Barry: http://www.FreeRTOS.org/contact
\r
62 ***************************************************************************
\r
64 ***************************************************************************
\r
66 * You are receiving this top quality software for free. Please play *
\r
67 * fair and reciprocate by reporting any suspected issues and *
\r
68 * participating in the community forum: *
\r
69 * http://www.FreeRTOS.org/support *
\r
73 ***************************************************************************
\r
75 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
76 license and Real Time Engineers Ltd. contact details.
\r
78 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
79 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
80 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
82 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
83 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
85 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
86 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
87 licenses offer ticketed support, indemnification and commercial middleware.
\r
89 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
90 engineered and independently SIL3 certified version for use in safety and
\r
91 mission critical applications that require provable dependability.
\r
98 * This version of comtest. c is for use on systems that have limited stack
\r
99 * space and no display facilities. The complete version can be found in
\r
100 * the Demo/Common/Full directory.
\r
102 * Creates two tasks that operate on an interrupt driven serial port. A
\r
103 * loopback connector should be used so that everything that is transmitted is
\r
104 * also received. The serial port does not use any flow control. On a
\r
105 * standard 9way 'D' connector pins two and three should be connected together.
\r
107 * The first task posts a sequence of characters to the Tx queue, toggling an
\r
108 * LED on each successful post. At the end of the sequence it sleeps for a
\r
109 * pseudo-random period before resending the same sequence.
\r
111 * The UART Tx end interrupt is enabled whenever data is available in the Tx
\r
112 * queue. The Tx end ISR removes a single character from the Tx queue and
\r
113 * passes it to the UART for transmission.
\r
115 * The second task blocks on the Rx queue waiting for a character to become
\r
116 * available. When the UART Rx end interrupt receives a character it places
\r
117 * it in the Rx queue, waking the second task. The second task checks that the
\r
118 * characters removed from the Rx queue form the same sequence as those posted
\r
119 * to the Tx queue, and toggles an LED for each correct character.
\r
121 * The receiving task is spawned with a higher priority than the transmitting
\r
122 * task. The receiver will therefore wake every time a character is
\r
123 * transmitted so neither the Tx or Rx queue should ever hold more than a few
\r
128 /* Scheduler include files. */
\r
129 #include <stdlib.h>
\r
130 #include "FreeRTOS.h"
\r
133 /* Demo program include files. */
\r
134 #include "serial.h"
\r
135 #include "comtest.h"
\r
136 #include "partest.h"
\r
138 #define comSTACK_SIZE configMINIMAL_STACK_SIZE
\r
139 #define comTX_LED_OFFSET ( 0 )
\r
140 #define comRX_LED_OFFSET ( 1 )
\r
141 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 )
\r
143 /* The Tx task will transmit the sequence of characters at a pseudo random
\r
144 interval. This is the maximum and minimum block time between sends. */
\r
145 #define comTX_MAX_BLOCK_TIME ( ( TickType_t ) 0x96 )
\r
146 #define comTX_MIN_BLOCK_TIME ( ( TickType_t ) 0x32 )
\r
147 #define comOFFSET_TIME ( ( TickType_t ) 3 )
\r
149 /* We should find that each character can be queued for Tx immediately and we
\r
150 don't have to block to send. */
\r
151 #define comNO_BLOCK ( ( TickType_t ) 0 )
\r
153 /* The Rx task will block on the Rx queue for a long period. */
\r
154 #define comRX_BLOCK_TIME ( ( TickType_t ) 0xffff )
\r
156 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */
\r
157 #define comFIRST_BYTE ( 'A' )
\r
158 #define comLAST_BYTE ( 'X' )
\r
160 #define comBUFFER_LEN ( ( UBaseType_t ) ( comLAST_BYTE - comFIRST_BYTE ) + ( UBaseType_t ) 1 )
\r
161 #define comINITIAL_RX_COUNT_VALUE ( 0 )
\r
163 /* Handle to the com port used by both tasks. */
\r
164 static xComPortHandle xPort = NULL;
\r
166 /* The transmit task as described at the top of the file. */
\r
167 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters );
\r
169 /* The receive task as described at the top of the file. */
\r
170 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters );
\r
172 /* The LED that should be toggled by the Rx and Tx tasks. The Rx task will
\r
173 toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED
\r
174 ( uxBaseLED + comTX_LED_OFFSET ). */
\r
175 static UBaseType_t uxBaseLED = 0;
\r
177 /* Check variable used to ensure no error have occurred. The Rx task will
\r
178 increment this variable after every successfully received sequence. If at any
\r
179 time the sequence is incorrect the the variable will stop being incremented. */
\r
180 static volatile UBaseType_t uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r
182 /*-----------------------------------------------------------*/
\r
184 void vAltStartComTestTasks( UBaseType_t uxPriority, uint32_t ulBaudRate, UBaseType_t uxLED )
\r
186 /* Initialise the com port then spawn the Rx and Tx tasks. */
\r
188 xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN );
\r
190 /* The Tx task is spawned with a lower priority than the Rx task. */
\r
191 xTaskCreate( vComTxTask, "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( TaskHandle_t * ) NULL );
\r
192 xTaskCreate( vComRxTask, "COMRx", comSTACK_SIZE, NULL, uxPriority, ( TaskHandle_t * ) NULL );
\r
194 /*-----------------------------------------------------------*/
\r
196 static portTASK_FUNCTION( vComTxTask, pvParameters )
\r
199 TickType_t xTimeToWait;
\r
201 /* Just to stop compiler warnings. */
\r
202 ( void ) pvParameters;
\r
206 /* Simply transmit a sequence of characters from comFIRST_BYTE to
\r
208 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ )
\r
210 if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS )
\r
212 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET );
\r
216 /* Turn the LED off while we are not doing anything. */
\r
217 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE );
\r
219 /* We have posted all the characters in the string - wait before
\r
220 re-sending. Wait a pseudo-random time as this will provide a better
\r
222 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME;
\r
224 /* Make sure we don't wait too long... */
\r
225 xTimeToWait %= comTX_MAX_BLOCK_TIME;
\r
227 /* ...but we do want to wait. */
\r
228 if( xTimeToWait < comTX_MIN_BLOCK_TIME )
\r
230 xTimeToWait = comTX_MIN_BLOCK_TIME;
\r
233 vTaskDelay( xTimeToWait );
\r
235 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
236 /*-----------------------------------------------------------*/
\r
238 static portTASK_FUNCTION( vComRxTask, pvParameters )
\r
240 signed char cExpectedByte, cByteRxed;
\r
241 BaseType_t xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE;
\r
243 /* Just to stop compiler warnings. */
\r
244 ( void ) pvParameters;
\r
248 /* We expect to receive the characters from comFIRST_BYTE to
\r
249 comLAST_BYTE in an incrementing order. Loop to receive each byte. */
\r
250 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ )
\r
252 /* Block on the queue that contains received bytes until a byte is
\r
254 if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) )
\r
256 /* Was this the byte we were expecting? If so, toggle the LED,
\r
257 otherwise we are out on sync and should break out of the loop
\r
258 until the expected character sequence is about to restart. */
\r
259 if( cByteRxed == cExpectedByte )
\r
261 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET );
\r
265 xResyncRequired = pdTRUE;
\r
266 break; /*lint !e960 Non-switch break allowed. */
\r
271 /* Turn the LED off while we are not doing anything. */
\r
272 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE );
\r
274 /* Did we break out of the loop because the characters were received in
\r
275 an unexpected order? If so wait here until the character sequence is
\r
276 about to restart. */
\r
277 if( xResyncRequired == pdTRUE )
\r
279 while( cByteRxed != comLAST_BYTE )
\r
281 /* Block until the next char is available. */
\r
282 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME );
\r
285 /* Note that an error occurred which caused us to have to resync.
\r
286 We use this to stop incrementing the loop counter so
\r
287 sAreComTestTasksStillRunning() will return false - indicating an
\r
291 /* We have now resynced with the Tx task and can continue. */
\r
292 xResyncRequired = pdFALSE;
\r
296 if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS )
\r
298 /* Increment the count of successful loops. As error
\r
299 occurring (i.e. an unexpected character being received) will
\r
300 prevent this counter being incremented for the rest of the
\r
301 execution. Don't worry about mutual exclusion on this
\r
302 variable - it doesn't really matter as we just want it
\r
308 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */
\r
309 /*-----------------------------------------------------------*/
\r
311 BaseType_t xAreComTestTasksStillRunning( void )
\r
313 BaseType_t xReturn;
\r
315 /* If the count of successful reception loops has not changed than at
\r
316 some time an error occurred (i.e. a character was received out of sequence)
\r
317 and we will return false. */
\r
318 if( uxRxLoops == comINITIAL_RX_COUNT_VALUE )
\r
327 /* Reset the count of successful Rx loops. When this function is called
\r
328 again we expect this to have been incremented. */
\r
329 uxRxLoops = comINITIAL_RX_COUNT_VALUE;
\r