00001 /* 00002 FreeRTOS V6.0.0 - Copyright (C) 2009 Real Time Engineers Ltd. 00003 00004 *************************************************************************** 00005 * * 00006 * If you are: * 00007 * * 00008 * + New to FreeRTOS, * 00009 * + Wanting to learn FreeRTOS or multitasking in general quickly * 00010 * + Looking for basic training, * 00011 * + Wanting to improve your FreeRTOS skills and productivity * 00012 * * 00013 * then take a look at the FreeRTOS eBook * 00014 * * 00015 * "Using the FreeRTOS Real Time Kernel - a Practical Guide" * 00016 * http://www.FreeRTOS.org/Documentation * 00017 * * 00018 * A pdf reference manual is also available. Both are usually delivered * 00019 * to your inbox within 20 minutes to two hours when purchased between 8am * 00020 * and 8pm GMT (although please allow up to 24 hours in case of * 00021 * exceptional circumstances). Thank you for your support! * 00022 * * 00023 *************************************************************************** 00024 00025 This file is part of the FreeRTOS distribution. 00026 00027 FreeRTOS is free software; you can redistribute it and/or modify it under 00028 the terms of the GNU General Public License (version 2) as published by the 00029 Free Software Foundation AND MODIFIED BY the FreeRTOS exception. 00030 ***NOTE*** The exception to the GPL is included to allow you to distribute 00031 a combined work that includes FreeRTOS without being obliged to provide the 00032 source code for proprietary components outside of the FreeRTOS kernel. 00033 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT 00034 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00035 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00036 more details. You should have received a copy of the GNU General Public 00037 License and the FreeRTOS license exception along with FreeRTOS; if not it 00038 can be viewed here: http://www.freertos.org/a00114.html and also obtained 00039 by writing to Richard Barry, contact details for whom are available on the 00040 FreeRTOS WEB site. 00041 00042 1 tab == 4 spaces! 00043 00044 http://www.FreeRTOS.org - Documentation, latest information, license and 00045 contact details. 00046 00047 http://www.SafeRTOS.com - A version that is certified for use in safety 00048 critical systems. 00049 00050 http://www.OpenRTOS.com - Commercial support, development, porting, 00051 licensing and training services. 00052 */ 00053 00054 00055 /* 00056 * This version of comtest. c is for use on systems that have limited stack 00057 * space and no display facilities. The complete version can be found in 00058 * the Demo/Common/Full directory. 00059 * 00060 * Creates two tasks that operate on an interrupt driven serial port. A 00061 * loopback connector should be used so that everything that is transmitted is 00062 * also received. The serial port does not use any flow control. On a 00063 * standard 9way 'D' connector pins two and three should be connected together. 00064 * 00065 * The first task posts a sequence of characters to the Tx queue, toggling an 00066 * LED on each successful post. At the end of the sequence it sleeps for a 00067 * pseudo-random period before resending the same sequence. 00068 * 00069 * The UART Tx end interrupt is enabled whenever data is available in the Tx 00070 * queue. The Tx end ISR removes a single character from the Tx queue and 00071 * passes it to the UART for transmission. 00072 * 00073 * The second task blocks on the Rx queue waiting for a character to become 00074 * available. When the UART Rx end interrupt receives a character it places 00075 * it in the Rx queue, waking the second task. The second task checks that the 00076 * characters removed from the Rx queue form the same sequence as those posted 00077 * to the Tx queue, and toggles an LED for each correct character. 00078 * 00079 * The receiving task is spawned with a higher priority than the transmitting 00080 * task. The receiver will therefore wake every time a character is 00081 * transmitted so neither the Tx or Rx queue should ever hold more than a few 00082 * characters. 00083 * 00084 */ 00085 00086 /* Scheduler include files. */ 00087 #include <stdlib.h> 00088 #include "FreeRTOS.h" 00089 #include "task.h" 00090 00091 /* Demo program include files. */ 00092 #include "serial.h" 00093 #include "comtest.h" 00094 #include "partest.h" 00095 00096 #define comSTACK_SIZE configMINIMAL_STACK_SIZE 00097 #define comTX_LED_OFFSET ( 0 ) 00098 #define comRX_LED_OFFSET ( 1 ) 00099 #define comTOTAL_PERMISSIBLE_ERRORS ( 2 ) 00100 00101 /* The Tx task will transmit the sequence of characters at a pseudo random 00102 interval. This is the maximum and minimum block time between sends. */ 00103 #define comTX_MAX_BLOCK_TIME ( ( portTickType ) 0x96 ) 00104 #define comTX_MIN_BLOCK_TIME ( ( portTickType ) 0x32 ) 00105 #define comOFFSET_TIME ( ( portTickType ) 3 ) 00106 00107 /* We should find that each character can be queued for Tx immediately and we 00108 don't have to block to send. */ 00109 #define comNO_BLOCK ( ( portTickType ) 0 ) 00110 00111 /* The Rx task will block on the Rx queue for a long period. */ 00112 #define comRX_BLOCK_TIME ( ( portTickType ) 0xffff ) 00113 00114 /* The sequence transmitted is from comFIRST_BYTE to and including comLAST_BYTE. */ 00115 #define comFIRST_BYTE ( 'A' ) 00116 #define comLAST_BYTE ( 'X' ) 00117 00118 #define comBUFFER_LEN ( ( unsigned portBASE_TYPE ) ( comLAST_BYTE - comFIRST_BYTE ) + ( unsigned portBASE_TYPE ) 1 ) 00119 #define comINITIAL_RX_COUNT_VALUE ( 0 ) 00120 00121 /* Handle to the com port used by both tasks. */ 00122 static xComPortHandle xPort = NULL; 00123 00124 /* The transmit task as described at the top of the file. */ 00125 static portTASK_FUNCTION_PROTO( vComTxTask, pvParameters ); 00126 00127 /* The receive task as described at the top of the file. */ 00128 static portTASK_FUNCTION_PROTO( vComRxTask, pvParameters ); 00129 00130 /* The LED that should be toggled by the Rx and Tx tasks. The Rx task will 00131 toggle LED ( uxBaseLED + comRX_LED_OFFSET). The Tx task will toggle LED 00132 ( uxBaseLED + comTX_LED_OFFSET ). */ 00133 static unsigned portBASE_TYPE uxBaseLED = 0; 00134 00135 /* Check variable used to ensure no error have occurred. The Rx task will 00136 increment this variable after every successfully received sequence. If at any 00137 time the sequence is incorrect the the variable will stop being incremented. */ 00138 static volatile unsigned portBASE_TYPE uxRxLoops = comINITIAL_RX_COUNT_VALUE; 00139 00140 /*-----------------------------------------------------------*/ 00141 00142 void vAltStartComTestTasks( unsigned portBASE_TYPE uxPriority, unsigned long ulBaudRate, unsigned portBASE_TYPE uxLED ) 00143 { 00144 /* Initialise the com port then spawn the Rx and Tx tasks. */ 00145 uxBaseLED = uxLED; 00146 xSerialPortInitMinimal( ulBaudRate, comBUFFER_LEN ); 00147 00148 /* The Tx task is spawned with a lower priority than the Rx task. */ 00149 xTaskCreate( vComTxTask, ( signed char * ) "COMTx", comSTACK_SIZE, NULL, uxPriority - 1, ( xTaskHandle * ) NULL ); 00150 xTaskCreate( vComRxTask, ( signed char * ) "COMRx", comSTACK_SIZE, NULL, uxPriority, ( xTaskHandle * ) NULL ); 00151 } 00152 /*-----------------------------------------------------------*/ 00153 00154 static portTASK_FUNCTION( vComTxTask, pvParameters ) 00155 { 00156 signed char cByteToSend; 00157 portTickType xTimeToWait; 00158 00159 /* Just to stop compiler warnings. */ 00160 ( void ) pvParameters; 00161 00162 for( ;; ) 00163 { 00164 /* Simply transmit a sequence of characters from comFIRST_BYTE to 00165 comLAST_BYTE. */ 00166 for( cByteToSend = comFIRST_BYTE; cByteToSend <= comLAST_BYTE; cByteToSend++ ) 00167 { 00168 if( xSerialPutChar( xPort, cByteToSend, comNO_BLOCK ) == pdPASS ) 00169 { 00170 vParTestToggleLED( uxBaseLED + comTX_LED_OFFSET ); 00171 } 00172 } 00173 00174 /* Turn the LED off while we are not doing anything. */ 00175 vParTestSetLED( uxBaseLED + comTX_LED_OFFSET, pdFALSE ); 00176 00177 /* We have posted all the characters in the string - wait before 00178 re-sending. Wait a pseudo-random time as this will provide a better 00179 test. */ 00180 xTimeToWait = xTaskGetTickCount() + comOFFSET_TIME; 00181 00182 /* Make sure we don't wait too long... */ 00183 xTimeToWait %= comTX_MAX_BLOCK_TIME; 00184 00185 /* ...but we do want to wait. */ 00186 if( xTimeToWait < comTX_MIN_BLOCK_TIME ) 00187 { 00188 xTimeToWait = comTX_MIN_BLOCK_TIME; 00189 } 00190 00191 vTaskDelay( xTimeToWait ); 00192 } 00193 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */ 00194 /*-----------------------------------------------------------*/ 00195 00196 static portTASK_FUNCTION( vComRxTask, pvParameters ) 00197 { 00198 signed char cExpectedByte, cByteRxed; 00199 portBASE_TYPE xResyncRequired = pdFALSE, xErrorOccurred = pdFALSE; 00200 00201 /* Just to stop compiler warnings. */ 00202 ( void ) pvParameters; 00203 00204 for( ;; ) 00205 { 00206 /* We expect to receive the characters from comFIRST_BYTE to 00207 comLAST_BYTE in an incrementing order. Loop to receive each byte. */ 00208 for( cExpectedByte = comFIRST_BYTE; cExpectedByte <= comLAST_BYTE; cExpectedByte++ ) 00209 { 00210 /* Block on the queue that contains received bytes until a byte is 00211 available. */ 00212 if( xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ) ) 00213 { 00214 /* Was this the byte we were expecting? If so, toggle the LED, 00215 otherwise we are out on sync and should break out of the loop 00216 until the expected character sequence is about to restart. */ 00217 if( cByteRxed == cExpectedByte ) 00218 { 00219 vParTestToggleLED( uxBaseLED + comRX_LED_OFFSET ); 00220 } 00221 else 00222 { 00223 xResyncRequired = pdTRUE; 00224 break; /*lint !e960 Non-switch break allowed. */ 00225 } 00226 } 00227 } 00228 00229 /* Turn the LED off while we are not doing anything. */ 00230 vParTestSetLED( uxBaseLED + comRX_LED_OFFSET, pdFALSE ); 00231 00232 /* Did we break out of the loop because the characters were received in 00233 an unexpected order? If so wait here until the character sequence is 00234 about to restart. */ 00235 if( xResyncRequired == pdTRUE ) 00236 { 00237 while( cByteRxed != comLAST_BYTE ) 00238 { 00239 /* Block until the next char is available. */ 00240 xSerialGetChar( xPort, &cByteRxed, comRX_BLOCK_TIME ); 00241 } 00242 00243 /* Note that an error occurred which caused us to have to resync. 00244 We use this to stop incrementing the loop counter so 00245 sAreComTestTasksStillRunning() will return false - indicating an 00246 error. */ 00247 xErrorOccurred++; 00248 00249 /* We have now resynced with the Tx task and can continue. */ 00250 xResyncRequired = pdFALSE; 00251 } 00252 else 00253 { 00254 if( xErrorOccurred < comTOTAL_PERMISSIBLE_ERRORS ) 00255 { 00256 /* Increment the count of successful loops. As error 00257 occurring (i.e. an unexpected character being received) will 00258 prevent this counter being incremented for the rest of the 00259 execution. Don't worry about mutual exclusion on this 00260 variable - it doesn't really matter as we just want it 00261 to change. */ 00262 uxRxLoops++; 00263 } 00264 } 00265 } 00266 } /*lint !e715 !e818 pvParameters is required for a task function even if it is not referenced. */ 00267 /*-----------------------------------------------------------*/ 00268 00269 portBASE_TYPE xAreComTestTasksStillRunning( void ) 00270 { 00271 portBASE_TYPE xReturn; 00272 00273 /* If the count of successful reception loops has not changed than at 00274 some time an error occurred (i.e. a character was received out of sequence) 00275 and we will return false. */ 00276 if( uxRxLoops == comINITIAL_RX_COUNT_VALUE ) 00277 { 00278 xReturn = pdFALSE; 00279 } 00280 else 00281 { 00282 xReturn = pdTRUE; 00283 } 00284 00285 /* Reset the count of successful Rx loops. When this function is called 00286 again we expect this to have been incremented. */ 00287 uxRxLoops = comINITIAL_RX_COUNT_VALUE; 00288 00289 return xReturn; 00290 } 00291