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 * This is a version of PollQ.c that uses the alternative (Alt) API. 00056 * 00057 * Creates two tasks that communicate over a single queue. One task acts as a 00058 * producer, the other a consumer. 00059 * 00060 * The producer loops for three iteration, posting an incrementing number onto the 00061 * queue each cycle. It then delays for a fixed period before doing exactly the 00062 * same again. 00063 * 00064 * The consumer loops emptying the queue. Each item removed from the queue is 00065 * checked to ensure it contains the expected value. When the queue is empty it 00066 * blocks for a fixed period, then does the same again. 00067 * 00068 * All queue access is performed without blocking. The consumer completely empties 00069 * the queue each time it runs so the producer should never find the queue full. 00070 * 00071 * An error is flagged if the consumer obtains an unexpected value or the producer 00072 * find the queue is full. 00073 */ 00074 00075 /* 00076 Changes from V2.0.0 00077 00078 + Delay periods are now specified using variables and constants of 00079 portTickType rather than unsigned portLONG. 00080 */ 00081 00082 #include <stdlib.h> 00083 00084 /* Scheduler include files. */ 00085 #include "FreeRTOS.h" 00086 #include "task.h" 00087 #include "queue.h" 00088 00089 /* Demo program include files. */ 00090 #include "AltPollQ.h" 00091 00092 #define pollqSTACK_SIZE configMINIMAL_STACK_SIZE 00093 #define pollqQUEUE_SIZE ( 10 ) 00094 #define pollqPRODUCER_DELAY ( ( portTickType ) 200 / portTICK_RATE_MS ) 00095 #define pollqCONSUMER_DELAY ( pollqPRODUCER_DELAY - ( portTickType ) ( 20 / portTICK_RATE_MS ) ) 00096 #define pollqNO_DELAY ( ( portTickType ) 0 ) 00097 #define pollqVALUES_TO_PRODUCE ( ( signed portBASE_TYPE ) 3 ) 00098 #define pollqINITIAL_VALUE ( ( signed portBASE_TYPE ) 0 ) 00099 00100 /* The task that posts the incrementing number onto the queue. */ 00101 static portTASK_FUNCTION_PROTO( vPolledQueueProducer, pvParameters ); 00102 00103 /* The task that empties the queue. */ 00104 static portTASK_FUNCTION_PROTO( vPolledQueueConsumer, pvParameters ); 00105 00106 /* Variables that are used to check that the tasks are still running with no 00107 errors. */ 00108 static volatile signed portBASE_TYPE xPollingConsumerCount = pollqINITIAL_VALUE, xPollingProducerCount = pollqINITIAL_VALUE; 00109 00110 /*-----------------------------------------------------------*/ 00111 00112 void vStartAltPolledQueueTasks( unsigned portBASE_TYPE uxPriority ) 00113 { 00114 static xQueueHandle xPolledQueue; 00115 00116 /* Create the queue used by the producer and consumer. */ 00117 xPolledQueue = xQueueCreate( pollqQUEUE_SIZE, ( unsigned portBASE_TYPE ) sizeof( unsigned portSHORT ) ); 00118 00119 /* vQueueAddToRegistry() adds the queue to the queue registry, if one is 00120 in use. The queue registry is provided as a means for kernel aware 00121 debuggers to locate queues and has no purpose if a kernel aware debugger 00122 is not being used. The call to vQueueAddToRegistry() will be removed 00123 by the pre-processor if configQUEUE_REGISTRY_SIZE is not defined or is 00124 defined to be less than 1. */ 00125 vQueueAddToRegistry( xPolledQueue, ( signed portCHAR * ) "AltPollQueue" ); 00126 00127 00128 /* Spawn the producer and consumer. */ 00129 xTaskCreate( vPolledQueueConsumer, ( signed portCHAR * ) "QConsNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL ); 00130 xTaskCreate( vPolledQueueProducer, ( signed portCHAR * ) "QProdNB", pollqSTACK_SIZE, ( void * ) &xPolledQueue, uxPriority, ( xTaskHandle * ) NULL ); 00131 } 00132 /*-----------------------------------------------------------*/ 00133 00134 static portTASK_FUNCTION( vPolledQueueProducer, pvParameters ) 00135 { 00136 unsigned portSHORT usValue = ( unsigned portSHORT ) 0; 00137 signed portBASE_TYPE xError = pdFALSE, xLoop; 00138 00139 #ifdef USE_STDIO 00140 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend ); 00141 00142 const portCHAR * const pcTaskStartMsg = "Alt polling queue producer task started.\r\n"; 00143 00144 /* Queue a message for printing to say the task has started. */ 00145 vPrintDisplayMessage( &pcTaskStartMsg ); 00146 #endif 00147 00148 for( ;; ) 00149 { 00150 for( xLoop = 0; xLoop < pollqVALUES_TO_PRODUCE; xLoop++ ) 00151 { 00152 /* Send an incrementing number on the queue without blocking. */ 00153 if( xQueueAltSendToBack( *( ( xQueueHandle * ) pvParameters ), ( void * ) &usValue, pollqNO_DELAY ) != pdPASS ) 00154 { 00155 /* We should never find the queue full so if we get here there 00156 has been an error. */ 00157 xError = pdTRUE; 00158 } 00159 else 00160 { 00161 if( xError == pdFALSE ) 00162 { 00163 /* If an error has ever been recorded we stop incrementing the 00164 check variable. */ 00165 portENTER_CRITICAL(); 00166 xPollingProducerCount++; 00167 portEXIT_CRITICAL(); 00168 } 00169 00170 /* Update the value we are going to post next time around. */ 00171 usValue++; 00172 } 00173 } 00174 00175 /* Wait before we start posting again to ensure the consumer runs and 00176 empties the queue. */ 00177 vTaskDelay( pollqPRODUCER_DELAY ); 00178 } 00179 } /*lint !e818 Function prototype must conform to API. */ 00180 /*-----------------------------------------------------------*/ 00181 00182 static portTASK_FUNCTION( vPolledQueueConsumer, pvParameters ) 00183 { 00184 unsigned portSHORT usData, usExpectedValue = ( unsigned portSHORT ) 0; 00185 signed portBASE_TYPE xError = pdFALSE; 00186 00187 #ifdef USE_STDIO 00188 void vPrintDisplayMessage( const portCHAR * const * ppcMessageToSend ); 00189 00190 const portCHAR * const pcTaskStartMsg = "Alt blocking queue consumer task started.\r\n"; 00191 00192 /* Queue a message for printing to say the task has started. */ 00193 vPrintDisplayMessage( &pcTaskStartMsg ); 00194 #endif 00195 00196 for( ;; ) 00197 { 00198 /* Loop until the queue is empty. */ 00199 while( uxQueueMessagesWaiting( *( ( xQueueHandle * ) pvParameters ) ) ) 00200 { 00201 if( xQueueAltReceive( *( ( xQueueHandle * ) pvParameters ), &usData, pollqNO_DELAY ) == pdPASS ) 00202 { 00203 if( usData != usExpectedValue ) 00204 { 00205 /* This is not what we expected to receive so an error has 00206 occurred. */ 00207 xError = pdTRUE; 00208 00209 /* Catch-up to the value we received so our next expected 00210 value should again be correct. */ 00211 usExpectedValue = usData; 00212 } 00213 else 00214 { 00215 if( xError == pdFALSE ) 00216 { 00217 /* Only increment the check variable if no errors have 00218 occurred. */ 00219 portENTER_CRITICAL(); 00220 xPollingConsumerCount++; 00221 portEXIT_CRITICAL(); 00222 } 00223 } 00224 00225 /* Next time round we would expect the number to be one higher. */ 00226 usExpectedValue++; 00227 } 00228 } 00229 00230 /* Now the queue is empty we block, allowing the producer to place more 00231 items in the queue. */ 00232 vTaskDelay( pollqCONSUMER_DELAY ); 00233 } 00234 } /*lint !e818 Function prototype must conform to API. */ 00235 /*-----------------------------------------------------------*/ 00236 00237 /* This is called to check that all the created tasks are still running with no errors. */ 00238 portBASE_TYPE xAreAltPollingQueuesStillRunning( void ) 00239 { 00240 portBASE_TYPE xReturn; 00241 00242 /* Check both the consumer and producer poll count to check they have both 00243 been changed since out last trip round. We do not need a critical section 00244 around the check variables as this is called from a higher priority than 00245 the other tasks that access the same variables. */ 00246 if( ( xPollingConsumerCount == pollqINITIAL_VALUE ) || 00247 ( xPollingProducerCount == pollqINITIAL_VALUE ) 00248 ) 00249 { 00250 xReturn = pdFALSE; 00251 } 00252 else 00253 { 00254 xReturn = pdTRUE; 00255 } 00256 00257 /* Set the check variables back down so we know if they have been 00258 incremented the next time around. */ 00259 xPollingConsumerCount = pollqINITIAL_VALUE; 00260 xPollingProducerCount = pollqINITIAL_VALUE; 00261 00262 return xReturn; 00263 }