brians | 0ab60bb | 2013-01-31 02:21:51 +0000 | [diff] [blame^] | 1 | /* |
| 2 | FreeRTOS V6.0.5 - Copyright (C) 2010 Real Time Engineers Ltd. |
| 3 | |
| 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 | *************************************************************************** |
| 24 | |
| 25 | This file is part of the FreeRTOS distribution. |
| 26 | |
| 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. |
| 41 | |
| 42 | 1 tab == 4 spaces! |
| 43 | |
| 44 | http://www.FreeRTOS.org - Documentation, latest information, license and |
| 45 | contact details. |
| 46 | |
| 47 | http://www.SafeRTOS.com - A version that is certified for use in safety |
| 48 | critical systems. |
| 49 | |
| 50 | http://www.OpenRTOS.com - Commercial support, development, porting, |
| 51 | licensing and training services. |
| 52 | */ |
| 53 | |
| 54 | /* |
| 55 | * Creates six tasks that operate on three queues as follows: |
| 56 | * |
| 57 | * The first two tasks send and receive an incrementing number to/from a queue. |
| 58 | * One task acts as a producer and the other as the consumer. The consumer is a |
| 59 | * higher priority than the producer and is set to block on queue reads. The queue |
| 60 | * only has space for one item - as soon as the producer posts a message on the |
| 61 | * queue the consumer will unblock, pre-empt the producer, and remove the item. |
| 62 | * |
| 63 | * The second two tasks work the other way around. Again the queue used only has |
| 64 | * enough space for one item. This time the consumer has a lower priority than the |
| 65 | * producer. The producer will try to post on the queue blocking when the queue is |
| 66 | * full. When the consumer wakes it will remove the item from the queue, causing |
| 67 | * the producer to unblock, pre-empt the consumer, and immediately re-fill the |
| 68 | * queue. |
| 69 | * |
| 70 | * The last two tasks use the same queue producer and consumer functions. This time the queue has |
| 71 | * enough space for lots of items and the tasks operate at the same priority. The |
| 72 | * producer will execute, placing items into the queue. The consumer will start |
| 73 | * executing when either the queue becomes full (causing the producer to block) or |
| 74 | * a context switch occurs (tasks of the same priority will time slice). |
| 75 | * |
| 76 | */ |
| 77 | |
| 78 | /* |
| 79 | |
| 80 | Changes from V4.1.1 |
| 81 | |
| 82 | + The second set of tasks were created the wrong way around. This has been |
| 83 | corrected. |
| 84 | */ |
| 85 | |
| 86 | |
| 87 | #include <stdlib.h> |
| 88 | |
| 89 | /* Scheduler include files. */ |
| 90 | #include "FreeRTOS.h" |
| 91 | #include "task.h" |
| 92 | #include "queue.h" |
| 93 | |
| 94 | /* Demo program include files. */ |
| 95 | #include "BlockQ.h" |
| 96 | |
| 97 | #define blckqSTACK_SIZE configMINIMAL_STACK_SIZE |
| 98 | #define blckqNUM_TASK_SETS ( 3 ) |
| 99 | |
| 100 | /* Structure used to pass parameters to the blocking queue tasks. */ |
| 101 | typedef struct BLOCKING_QUEUE_PARAMETERS { |
| 102 | xQueueHandle xQueue; /*< The queue to be used by the task. */ |
| 103 | portTickType xBlockTime; /*< The block time to use on queue reads/writes. */ |
| 104 | volatile short *psCheckVariable; /*< Incremented on each successful cycle to check the task is still running. */ |
| 105 | } xBlockingQueueParameters; |
| 106 | |
| 107 | /* Task function that creates an incrementing number and posts it on a queue. */ |
| 108 | static portTASK_FUNCTION_PROTO(vBlockingQueueProducer, pvParameters); |
| 109 | |
| 110 | /* Task function that removes the incrementing number from a queue and checks that |
| 111 | it is the expected number. */ |
| 112 | static portTASK_FUNCTION_PROTO(vBlockingQueueConsumer, pvParameters); |
| 113 | |
| 114 | /* Variables which are incremented each time an item is removed from a queue, and |
| 115 | found to be the expected value. |
| 116 | These are used to check that the tasks are still running. */ |
| 117 | static volatile short sBlockingConsumerCount[ blckqNUM_TASK_SETS ] = {(unsigned short) 0, (unsigned short) 0, (unsigned short) 0 }; |
| 118 | |
| 119 | /* Variable which are incremented each time an item is posted on a queue. These |
| 120 | are used to check that the tasks are still running. */ |
| 121 | static volatile short sBlockingProducerCount[ blckqNUM_TASK_SETS ] = {(unsigned short) 0, (unsigned short) 0, (unsigned short) 0 }; |
| 122 | |
| 123 | /*-----------------------------------------------------------*/ |
| 124 | |
| 125 | void vStartBlockingQueueTasks(unsigned portBASE_TYPE uxPriority) |
| 126 | { |
| 127 | xBlockingQueueParameters *pxQueueParameters1, *pxQueueParameters2; |
| 128 | xBlockingQueueParameters *pxQueueParameters3, *pxQueueParameters4; |
| 129 | xBlockingQueueParameters *pxQueueParameters5, *pxQueueParameters6; |
| 130 | const unsigned portBASE_TYPE uxQueueSize1 = 1, uxQueueSize5 = 5; |
| 131 | const portTickType xBlockTime = (portTickType) 1000 / portTICK_RATE_MS; |
| 132 | const portTickType xDontBlock = (portTickType) 0; |
| 133 | |
| 134 | /* Create the first two tasks as described at the top of the file. */ |
| 135 | |
| 136 | /* First create the structure used to pass parameters to the consumer tasks. */ |
| 137 | pxQueueParameters1 = (xBlockingQueueParameters *) pvPortMalloc(sizeof(xBlockingQueueParameters)); |
| 138 | |
| 139 | /* Create the queue used by the first two tasks to pass the incrementing number. |
| 140 | Pass a pointer to the queue in the parameter structure. */ |
| 141 | pxQueueParameters1->xQueue = xQueueCreate(uxQueueSize1, (unsigned portBASE_TYPE) sizeof(unsigned short)); |
| 142 | |
| 143 | /* The consumer is created first so gets a block time as described above. */ |
| 144 | pxQueueParameters1->xBlockTime = xBlockTime; |
| 145 | |
| 146 | /* Pass in the variable that this task is going to increment so we can check it |
| 147 | is still running. */ |
| 148 | pxQueueParameters1->psCheckVariable = &(sBlockingConsumerCount[ 0 ]); |
| 149 | |
| 150 | /* Create the structure used to pass parameters to the producer task. */ |
| 151 | pxQueueParameters2 = (xBlockingQueueParameters *) pvPortMalloc(sizeof(xBlockingQueueParameters)); |
| 152 | |
| 153 | /* Pass the queue to this task also, using the parameter structure. */ |
| 154 | pxQueueParameters2->xQueue = pxQueueParameters1->xQueue; |
| 155 | |
| 156 | /* The producer is not going to block - as soon as it posts the consumer will |
| 157 | wake and remove the item so the producer should always have room to post. */ |
| 158 | pxQueueParameters2->xBlockTime = xDontBlock; |
| 159 | |
| 160 | /* Pass in the variable that this task is going to increment so we can check |
| 161 | it is still running. */ |
| 162 | pxQueueParameters2->psCheckVariable = &(sBlockingProducerCount[ 0 ]); |
| 163 | |
| 164 | |
| 165 | /* Note the producer has a lower priority than the consumer when the tasks are |
| 166 | spawned. */ |
| 167 | xTaskCreate(vBlockingQueueConsumer, (signed char *) "QConsB1", blckqSTACK_SIZE, (void *) pxQueueParameters1, uxPriority, NULL); |
| 168 | xTaskCreate(vBlockingQueueProducer, (signed char *) "QProdB2", blckqSTACK_SIZE, (void *) pxQueueParameters2, tskIDLE_PRIORITY, NULL); |
| 169 | |
| 170 | |
| 171 | |
| 172 | /* Create the second two tasks as described at the top of the file. This uses |
| 173 | the same mechanism but reverses the task priorities. */ |
| 174 | |
| 175 | pxQueueParameters3 = (xBlockingQueueParameters *) pvPortMalloc(sizeof(xBlockingQueueParameters)); |
| 176 | pxQueueParameters3->xQueue = xQueueCreate(uxQueueSize1, (unsigned portBASE_TYPE) sizeof(unsigned short)); |
| 177 | pxQueueParameters3->xBlockTime = xDontBlock; |
| 178 | pxQueueParameters3->psCheckVariable = &(sBlockingProducerCount[ 1 ]); |
| 179 | |
| 180 | pxQueueParameters4 = (xBlockingQueueParameters *) pvPortMalloc(sizeof(xBlockingQueueParameters)); |
| 181 | pxQueueParameters4->xQueue = pxQueueParameters3->xQueue; |
| 182 | pxQueueParameters4->xBlockTime = xBlockTime; |
| 183 | pxQueueParameters4->psCheckVariable = &(sBlockingConsumerCount[ 1 ]); |
| 184 | |
| 185 | xTaskCreate(vBlockingQueueConsumer, (signed char *) "QProdB3", blckqSTACK_SIZE, (void *) pxQueueParameters3, tskIDLE_PRIORITY, NULL); |
| 186 | xTaskCreate(vBlockingQueueProducer, (signed char *) "QConsB4", blckqSTACK_SIZE, (void *) pxQueueParameters4, uxPriority, NULL); |
| 187 | |
| 188 | |
| 189 | |
| 190 | /* Create the last two tasks as described above. The mechanism is again just |
| 191 | the same. This time both parameter structures are given a block time. */ |
| 192 | pxQueueParameters5 = (xBlockingQueueParameters *) pvPortMalloc(sizeof(xBlockingQueueParameters)); |
| 193 | pxQueueParameters5->xQueue = xQueueCreate(uxQueueSize5, (unsigned portBASE_TYPE) sizeof(unsigned short)); |
| 194 | pxQueueParameters5->xBlockTime = xBlockTime; |
| 195 | pxQueueParameters5->psCheckVariable = &(sBlockingProducerCount[ 2 ]); |
| 196 | |
| 197 | pxQueueParameters6 = (xBlockingQueueParameters *) pvPortMalloc(sizeof(xBlockingQueueParameters)); |
| 198 | pxQueueParameters6->xQueue = pxQueueParameters5->xQueue; |
| 199 | pxQueueParameters6->xBlockTime = xBlockTime; |
| 200 | pxQueueParameters6->psCheckVariable = &(sBlockingConsumerCount[ 2 ]); |
| 201 | |
| 202 | xTaskCreate(vBlockingQueueProducer, (signed char *) "QProdB5", blckqSTACK_SIZE, (void *) pxQueueParameters5, tskIDLE_PRIORITY, NULL); |
| 203 | xTaskCreate(vBlockingQueueConsumer, (signed char *) "QConsB6", blckqSTACK_SIZE, (void *) pxQueueParameters6, tskIDLE_PRIORITY, NULL); |
| 204 | } |
| 205 | /*-----------------------------------------------------------*/ |
| 206 | |
| 207 | static portTASK_FUNCTION(vBlockingQueueProducer, pvParameters) |
| 208 | { |
| 209 | unsigned short usValue = 0; |
| 210 | xBlockingQueueParameters *pxQueueParameters; |
| 211 | short sErrorEverOccurred = pdFALSE; |
| 212 | |
| 213 | pxQueueParameters = (xBlockingQueueParameters *) pvParameters; |
| 214 | |
| 215 | for (;;) { |
| 216 | if (xQueueSend(pxQueueParameters->xQueue, (void *) &usValue, pxQueueParameters->xBlockTime) != pdPASS) { |
| 217 | sErrorEverOccurred = pdTRUE; |
| 218 | } else { |
| 219 | /* We have successfully posted a message, so increment the variable |
| 220 | used to check we are still running. */ |
| 221 | if (sErrorEverOccurred == pdFALSE) { |
| 222 | (*pxQueueParameters->psCheckVariable)++; |
| 223 | } |
| 224 | |
| 225 | /* Increment the variable we are going to post next time round. The |
| 226 | consumer will expect the numbers to follow in numerical order. */ |
| 227 | ++usValue; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | /*-----------------------------------------------------------*/ |
| 232 | |
| 233 | static portTASK_FUNCTION(vBlockingQueueConsumer, pvParameters) |
| 234 | { |
| 235 | unsigned short usData, usExpectedValue = 0; |
| 236 | xBlockingQueueParameters *pxQueueParameters; |
| 237 | short sErrorEverOccurred = pdFALSE; |
| 238 | |
| 239 | pxQueueParameters = (xBlockingQueueParameters *) pvParameters; |
| 240 | |
| 241 | for (;;) { |
| 242 | if (xQueueReceive(pxQueueParameters->xQueue, &usData, pxQueueParameters->xBlockTime) == pdPASS) { |
| 243 | if (usData != usExpectedValue) { |
| 244 | /* Catch-up. */ |
| 245 | usExpectedValue = usData; |
| 246 | |
| 247 | sErrorEverOccurred = pdTRUE; |
| 248 | } else { |
| 249 | /* We have successfully received a message, so increment the |
| 250 | variable used to check we are still running. */ |
| 251 | if (sErrorEverOccurred == pdFALSE) { |
| 252 | (*pxQueueParameters->psCheckVariable)++; |
| 253 | } |
| 254 | |
| 255 | /* Increment the value we expect to remove from the queue next time |
| 256 | round. */ |
| 257 | ++usExpectedValue; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | /*-----------------------------------------------------------*/ |
| 263 | |
| 264 | /* This is called to check that all the created tasks are still running. */ |
| 265 | portBASE_TYPE xAreBlockingQueuesStillRunning(void) |
| 266 | { |
| 267 | static short sLastBlockingConsumerCount[ blckqNUM_TASK_SETS ] = {(unsigned short) 0, (unsigned short) 0, (unsigned short) 0 }; |
| 268 | static short sLastBlockingProducerCount[ blckqNUM_TASK_SETS ] = {(unsigned short) 0, (unsigned short) 0, (unsigned short) 0 }; |
| 269 | portBASE_TYPE xReturn = pdPASS, xTasks; |
| 270 | |
| 271 | /* Not too worried about mutual exclusion on these variables as they are 16 |
| 272 | bits and we are only reading them. We also only care to see if they have |
| 273 | changed or not. |
| 274 | |
| 275 | Loop through each check variable to and return pdFALSE if any are found not |
| 276 | to have changed since the last call. */ |
| 277 | |
| 278 | for (xTasks = 0; xTasks < blckqNUM_TASK_SETS; xTasks++) { |
| 279 | if (sBlockingConsumerCount[ xTasks ] == sLastBlockingConsumerCount[ xTasks ]) { |
| 280 | xReturn = pdFALSE; |
| 281 | } |
| 282 | sLastBlockingConsumerCount[ xTasks ] = sBlockingConsumerCount[ xTasks ]; |
| 283 | |
| 284 | |
| 285 | if (sBlockingProducerCount[ xTasks ] == sLastBlockingProducerCount[ xTasks ]) { |
| 286 | xReturn = pdFALSE; |
| 287 | } |
| 288 | sLastBlockingProducerCount[ xTasks ] = sBlockingProducerCount[ xTasks ]; |
| 289 | } |
| 290 | |
| 291 | return xReturn; |
| 292 | } |
| 293 | |