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 demo application file demonstrates the use of queues to pass data 00056 * between co-routines. 00057 * 00058 * N represents the number of 'fixed delay' co-routines that are created and 00059 * is set during initialisation. 00060 * 00061 * N 'fixed delay' co-routines are created that just block for a fixed 00062 * period then post the number of an LED onto a queue. Each such co-routine 00063 * uses a different block period. A single 'flash' co-routine is also created 00064 * that blocks on the same queue, waiting for the number of the next LED it 00065 * should flash. Upon receiving a number it simply toggle the instructed LED 00066 * then blocks on the queue once more. In this manner each LED from LED 0 to 00067 * LED N-1 is caused to flash at a different rate. 00068 * 00069 * The 'fixed delay' co-routines are created with co-routine priority 0. The 00070 * flash co-routine is created with co-routine priority 1. This means that 00071 * the queue should never contain more than a single item. This is because 00072 * posting to the queue will unblock the 'flash' co-routine, and as this has 00073 * a priority greater than the tasks posting to the queue it is guaranteed to 00074 * have emptied the queue and blocked once again before the queue can contain 00075 * any more date. An error is indicated if an attempt to post data to the 00076 * queue fails - indicating that the queue is already full. 00077 * 00078 */ 00079 00080 /* Scheduler includes. */ 00081 #include "FreeRTOS.h" 00082 #include "croutine.h" 00083 #include "queue.h" 00084 00085 /* Demo application includes. */ 00086 #include "partest.h" 00087 #include "crflash.h" 00088 00089 /* The queue should only need to be of length 1. See the description at the 00090 top of the file. */ 00091 #define crfQUEUE_LENGTH 1 00092 00093 #define crfFIXED_DELAY_PRIORITY 0 00094 #define crfFLASH_PRIORITY 1 00095 00096 /* Only one flash co-routine is created so the index is not significant. */ 00097 #define crfFLASH_INDEX 0 00098 00099 /* Don't allow more than crfMAX_FLASH_TASKS 'fixed delay' co-routines to be 00100 created. */ 00101 #define crfMAX_FLASH_TASKS 8 00102 00103 /* We don't want to block when posting to the queue. */ 00104 #define crfPOSTING_BLOCK_TIME 0 00105 00106 /* 00107 * The 'fixed delay' co-routine as described at the top of the file. 00108 */ 00109 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ); 00110 00111 /* 00112 * The 'flash' co-routine as described at the top of the file. 00113 */ 00114 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ); 00115 00116 /* The queue used to pass data between the 'fixed delay' co-routines and the 00117 'flash' co-routine. */ 00118 static xQueueHandle xFlashQueue; 00119 00120 /* This will be set to pdFALSE if we detect an error. */ 00121 static portBASE_TYPE xCoRoutineFlashStatus = pdPASS; 00122 00123 /*-----------------------------------------------------------*/ 00124 00125 /* 00126 * See the header file for details. 00127 */ 00128 void vStartFlashCoRoutines( unsigned portBASE_TYPE uxNumberToCreate ) 00129 { 00130 unsigned portBASE_TYPE uxIndex; 00131 00132 if( uxNumberToCreate > crfMAX_FLASH_TASKS ) 00133 { 00134 uxNumberToCreate = crfMAX_FLASH_TASKS; 00135 } 00136 00137 /* Create the queue used to pass data between the co-routines. */ 00138 xFlashQueue = xQueueCreate( crfQUEUE_LENGTH, sizeof( unsigned portBASE_TYPE ) ); 00139 00140 if( xFlashQueue ) 00141 { 00142 /* Create uxNumberToCreate 'fixed delay' co-routines. */ 00143 for( uxIndex = 0; uxIndex < uxNumberToCreate; uxIndex++ ) 00144 { 00145 xCoRoutineCreate( prvFixedDelayCoRoutine, crfFIXED_DELAY_PRIORITY, uxIndex ); 00146 } 00147 00148 /* Create the 'flash' co-routine. */ 00149 xCoRoutineCreate( prvFlashCoRoutine, crfFLASH_PRIORITY, crfFLASH_INDEX ); 00150 } 00151 } 00152 /*-----------------------------------------------------------*/ 00153 00154 static void prvFixedDelayCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) 00155 { 00156 /* Even though this is a co-routine the xResult variable does not need to be 00157 static as we do not need it to maintain its state between blocks. */ 00158 signed portBASE_TYPE xResult; 00159 /* The uxIndex parameter of the co-routine function is used as an index into 00160 the xFlashRates array to obtain the delay period to use. */ 00161 static const portTickType xFlashRates[ crfMAX_FLASH_TASKS ] = { 150 / portTICK_RATE_MS, 00162 200 / portTICK_RATE_MS, 00163 250 / portTICK_RATE_MS, 00164 300 / portTICK_RATE_MS, 00165 350 / portTICK_RATE_MS, 00166 400 / portTICK_RATE_MS, 00167 450 / portTICK_RATE_MS, 00168 500 / portTICK_RATE_MS }; 00169 00170 /* Co-routines MUST start with a call to crSTART. */ 00171 crSTART( xHandle ); 00172 00173 for( ;; ) 00174 { 00175 /* Post our uxIndex value onto the queue. This is used as the LED to 00176 flash. */ 00177 crQUEUE_SEND( xHandle, xFlashQueue, ( void * ) &uxIndex, crfPOSTING_BLOCK_TIME, &xResult ); 00178 00179 if( xResult != pdPASS ) 00180 { 00181 /* For the reasons stated at the top of the file we should always 00182 find that we can post to the queue. If we could not then an error 00183 has occurred. */ 00184 xCoRoutineFlashStatus = pdFAIL; 00185 } 00186 00187 crDELAY( xHandle, xFlashRates[ uxIndex ] ); 00188 } 00189 00190 /* Co-routines MUST end with a call to crEND. */ 00191 crEND(); 00192 } 00193 /*-----------------------------------------------------------*/ 00194 00195 static void prvFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) 00196 { 00197 /* Even though this is a co-routine the variable do not need to be 00198 static as we do not need it to maintain their state between blocks. */ 00199 signed portBASE_TYPE xResult; 00200 unsigned portBASE_TYPE uxLEDToFlash; 00201 00202 /* Co-routines MUST start with a call to crSTART. */ 00203 crSTART( xHandle ); 00204 ( void ) uxIndex; 00205 00206 for( ;; ) 00207 { 00208 /* Block to wait for the number of the LED to flash. */ 00209 crQUEUE_RECEIVE( xHandle, xFlashQueue, &uxLEDToFlash, portMAX_DELAY, &xResult ); 00210 00211 if( xResult != pdPASS ) 00212 { 00213 /* We would not expect to wake unless we received something. */ 00214 xCoRoutineFlashStatus = pdFAIL; 00215 } 00216 else 00217 { 00218 /* We received the number of an LED to flash - flash it! */ 00219 vParTestToggleLED( uxLEDToFlash ); 00220 } 00221 } 00222 00223 /* Co-routines MUST end with a call to crEND. */ 00224 crEND(); 00225 } 00226 /*-----------------------------------------------------------*/ 00227 00228 portBASE_TYPE xAreFlashCoRoutinesStillRunning( void ) 00229 { 00230 /* Return pdPASS or pdFAIL depending on whether an error has been detected 00231 or not. */ 00232 return xCoRoutineFlashStatus; 00233 } 00234