1 /* 2 * FreeRTOS Kernel V10.3.1 3 * Copyright (C) 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a copy 6 * of this software and associated documentation files (the "Software"), to deal 7 * in the Software without restriction, including without limitation the rights 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 * copies of the Software, and to permit persons to whom the Software is 10 * furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 * SOFTWARE. 22 * 23 * http://www.FreeRTOS.org 24 * http://aws.amazon.com/freertos 25 * 26 * 1 tab == 4 spaces! 27 */ 28 29 #ifndef QUEUE_H 30 #define QUEUE_H 31 32 #ifndef INC_FREERTOS_H 33 # error "include FreeRTOS.h" must appear in source files before "include queue.h" 34 #endif 35 36 #ifdef __cplusplus 37 extern "C" { 38 #endif 39 40 #include "task.h" 41 42 /** 43 * Type by which queues are referenced. For example, a call to xQueueCreate() 44 * returns an QueueHandle_t variable that can then be used as a parameter to 45 * xQueueSend(), xQueueReceive(), etc. 46 */ 47 struct QueueDefinition; /* Using old naming convention so as not to break kernel 48 aware debuggers. */ 49 typedef struct QueueDefinition *QueueHandle_t; 50 51 /** 52 * Type by which queue sets are referenced. For example, a call to 53 * xQueueCreateSet() returns an xQueueSet variable that can then be used as a 54 * parameter to xQueueSelectFromSet(), xQueueAddToSet(), etc. 55 */ 56 typedef struct QueueDefinition *QueueSetHandle_t; 57 58 /** 59 * Queue sets can contain both queues and semaphores, so the 60 * QueueSetMemberHandle_t is defined as a type to be used where a parameter or 61 * return value can be either an QueueHandle_t or an SemaphoreHandle_t. 62 */ 63 typedef struct QueueDefinition *QueueSetMemberHandle_t; 64 65 /* For internal use only. */ 66 #define queueSEND_TO_BACK ((BaseType_t)0) 67 #define queueSEND_TO_FRONT ((BaseType_t)1) 68 #define queueOVERWRITE ((BaseType_t)2) 69 70 /* For internal use only. These definitions *must* match those in queue.c. */ 71 #define queueQUEUE_TYPE_BASE ((uint8_t)0U) 72 #define queueQUEUE_TYPE_SET ((uint8_t)0U) 73 #define queueQUEUE_TYPE_MUTEX ((uint8_t)1U) 74 #define queueQUEUE_TYPE_COUNTING_SEMAPHORE ((uint8_t)2U) 75 #define queueQUEUE_TYPE_BINARY_SEMAPHORE ((uint8_t)3U) 76 #define queueQUEUE_TYPE_RECURSIVE_MUTEX ((uint8_t)4U) 77 78 /** 79 * queue. h 80 * <pre> 81 QueueHandle_t xQueueCreate( 82 UBaseType_t uxQueueLength, 83 UBaseType_t uxItemSize 84 ); 85 * </pre> 86 * 87 * Creates a new queue instance, and returns a handle by which the new queue 88 * can be referenced. 89 * 90 * Internally, within the FreeRTOS implementation, queues use two blocks of 91 * memory. The first block is used to hold the queue's data structures. The 92 * second block is used to hold items placed into the queue. If a queue is 93 * created using xQueueCreate() then both blocks of memory are automatically 94 * dynamically allocated inside the xQueueCreate() function. (see 95 * http://www.freertos.org/a00111.html). If a queue is created using 96 * xQueueCreateStatic() then the application writer must provide the memory that 97 * will get used by the queue. xQueueCreateStatic() therefore allows a queue to 98 * be created without using any dynamic memory allocation. 99 * 100 * http://www.FreeRTOS.org/Embedded-RTOS-Queues.html 101 * 102 * @param uxQueueLength The maximum number of items that the queue can contain. 103 * 104 * @param uxItemSize The number of bytes each item in the queue will require. 105 * Items are queued by copy, not by reference, so this is the number of bytes 106 * that will be copied for each posted item. Each item on the queue must be 107 * the same size. 108 * 109 * @return If the queue is successfully create then a handle to the newly 110 * created queue is returned. If the queue cannot be created then 0 is 111 * returned. 112 * 113 * Example usage: 114 <pre> 115 struct AMessage 116 { 117 char ucMessageID; 118 char ucData[ 20 ]; 119 }; 120 121 void vATask( void *pvParameters ) 122 { 123 QueueHandle_t xQueue1, xQueue2; 124 125 // Create a queue capable of containing 10 uint32_t values. 126 xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) ); 127 if( xQueue1 == 0 ) 128 { 129 // Queue was not created and must not be used. 130 } 131 132 // Create a queue capable of containing 10 pointers to AMessage structures. 133 // These should be passed by pointer as they contain a lot of data. 134 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) ); 135 if( xQueue2 == 0 ) 136 { 137 // Queue was not created and must not be used. 138 } 139 140 // ... Rest of task code. 141 } 142 </pre> 143 * \defgroup xQueueCreate xQueueCreate 144 * \ingroup QueueManagement 145 */ 146 #if (configSUPPORT_DYNAMIC_ALLOCATION == 1) 147 # define xQueueCreate(uxQueueLength, uxItemSize) \ 148 xQueueGenericCreate( \ 149 (uxQueueLength), (uxItemSize), (queueQUEUE_TYPE_BASE)) 150 #endif 151 152 /** 153 * queue. h 154 * <pre> 155 QueueHandle_t xQueueCreateStatic( 156 UBaseType_t uxQueueLength, 157 UBaseType_t uxItemSize, 158 uint8_t *pucQueueStorageBuffer, 159 StaticQueue_t *pxQueueBuffer 160 ); 161 * </pre> 162 * 163 * Creates a new queue instance, and returns a handle by which the new queue 164 * can be referenced. 165 * 166 * Internally, within the FreeRTOS implementation, queues use two blocks of 167 * memory. The first block is used to hold the queue's data structures. The 168 * second block is used to hold items placed into the queue. If a queue is 169 * created using xQueueCreate() then both blocks of memory are automatically 170 * dynamically allocated inside the xQueueCreate() function. (see 171 * http://www.freertos.org/a00111.html). If a queue is created using 172 * xQueueCreateStatic() then the application writer must provide the memory that 173 * will get used by the queue. xQueueCreateStatic() therefore allows a queue to 174 * be created without using any dynamic memory allocation. 175 * 176 * http://www.FreeRTOS.org/Embedded-RTOS-Queues.html 177 * 178 * @param uxQueueLength The maximum number of items that the queue can contain. 179 * 180 * @param uxItemSize The number of bytes each item in the queue will require. 181 * Items are queued by copy, not by reference, so this is the number of bytes 182 * that will be copied for each posted item. Each item on the queue must be 183 * the same size. 184 * 185 * @param pucQueueStorageBuffer If uxItemSize is not zero then 186 * pucQueueStorageBuffer must point to a uint8_t array that is at least large 187 * enough to hold the maximum number of items that can be in the queue at any 188 * one time - which is ( uxQueueLength * uxItemsSize ) bytes. If uxItemSize is 189 * zero then pucQueueStorageBuffer can be NULL. 190 * 191 * @param pxQueueBuffer Must point to a variable of type StaticQueue_t, which 192 * will be used to hold the queue's data structure. 193 * 194 * @return If the queue is created then a handle to the created queue is 195 * returned. If pxQueueBuffer is NULL then NULL is returned. 196 * 197 * Example usage: 198 <pre> 199 struct AMessage 200 { 201 char ucMessageID; 202 char ucData[ 20 ]; 203 }; 204 205 #define QUEUE_LENGTH 10 206 #define ITEM_SIZE sizeof( uint32_t ) 207 208 // xQueueBuffer will hold the queue structure. 209 StaticQueue_t xQueueBuffer; 210 211 // ucQueueStorage will hold the items posted to the queue. Must be at least 212 // [(queue length) * ( queue item size)] bytes long. 213 uint8_t ucQueueStorage[ QUEUE_LENGTH * ITEM_SIZE ]; 214 215 void vATask( void *pvParameters ) 216 { 217 QueueHandle_t xQueue1; 218 219 // Create a queue capable of containing 10 uint32_t values. 220 xQueue1 = xQueueCreate( QUEUE_LENGTH, // The number of items the queue can 221 hold. ITEM_SIZE // The size of each item in the queue 222 &( ucQueueStorage[ 0 ] ), // The buffer that will 223 hold the items in the queue. &xQueueBuffer ); // The buffer that will hold the 224 queue structure. 225 226 // The queue is guaranteed to be created successfully as no dynamic memory 227 // allocation is used. Therefore xQueue1 is now a handle to a valid queue. 228 229 // ... Rest of task code. 230 } 231 </pre> 232 * \defgroup xQueueCreateStatic xQueueCreateStatic 233 * \ingroup QueueManagement 234 */ 235 #if (configSUPPORT_STATIC_ALLOCATION == 1) 236 # define xQueueCreateStatic( \ 237 uxQueueLength, uxItemSize, pucQueueStorage, pxQueueBuffer) \ 238 xQueueGenericCreateStatic( \ 239 (uxQueueLength), \ 240 (uxItemSize), \ 241 (pucQueueStorage), \ 242 (pxQueueBuffer), \ 243 (queueQUEUE_TYPE_BASE)) 244 #endif /* configSUPPORT_STATIC_ALLOCATION */ 245 246 /** 247 * queue. h 248 * <pre> 249 BaseType_t xQueueSendToToFront( 250 QueueHandle_t xQueue, 251 const void *pvItemToQueue, 252 TickType_t xTicksToWait 253 ); 254 * </pre> 255 * 256 * Post an item to the front of a queue. The item is queued by copy, not by 257 * reference. This function must not be called from an interrupt service 258 * routine. See xQueueSendFromISR () for an alternative which may be used 259 * in an ISR. 260 * 261 * @param xQueue The handle to the queue on which the item is to be posted. 262 * 263 * @param pvItemToQueue A pointer to the item that is to be placed on the 264 * queue. The size of the items the queue will hold was defined when the 265 * queue was created, so this many bytes will be copied from pvItemToQueue 266 * into the queue storage area. 267 * 268 * @param xTicksToWait The maximum amount of time the task should block 269 * waiting for space to become available on the queue, should it already 270 * be full. The call will return immediately if this is set to 0 and the 271 * queue is full. The time is defined in tick periods so the constant 272 * portTICK_PERIOD_MS should be used to convert to real time if this is 273 required. 274 * 275 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL. 276 * 277 * Example usage: 278 <pre> 279 struct AMessage 280 { 281 char ucMessageID; 282 char ucData[ 20 ]; 283 } xMessage; 284 285 uint32_t ulVar = 10UL; 286 287 void vATask( void *pvParameters ) 288 { 289 QueueHandle_t xQueue1, xQueue2; 290 struct AMessage *pxMessage; 291 292 // Create a queue capable of containing 10 uint32_t values. 293 xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) ); 294 295 // Create a queue capable of containing 10 pointers to AMessage structures. 296 // These should be passed by pointer as they contain a lot of data. 297 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) ); 298 299 // ... 300 301 if( xQueue1 != 0 ) 302 { 303 // Send an uint32_t. Wait for 10 ticks for space to become 304 // available if necessary. 305 if( xQueueSendToFront( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10 ) 306 != pdPASS ) 307 { 308 // Failed to post the message, even after 10 ticks. 309 } 310 } 311 312 if( xQueue2 != 0 ) 313 { 314 // Send a pointer to a struct AMessage object. Don't block if the 315 // queue is already full. 316 pxMessage = & xMessage; 317 xQueueSendToFront( xQueue2, ( void * ) &pxMessage, ( TickType_t ) 0 ); 318 } 319 320 // ... Rest of task code. 321 } 322 </pre> 323 * \defgroup xQueueSend xQueueSend 324 * \ingroup QueueManagement 325 */ 326 #define xQueueSendToFront(xQueue, pvItemToQueue, xTicksToWait) \ 327 xQueueGenericSend( \ 328 (xQueue), (pvItemToQueue), (xTicksToWait), queueSEND_TO_FRONT) 329 330 /** 331 * queue. h 332 * <pre> 333 BaseType_t xQueueSendToBack( 334 QueueHandle_t xQueue, 335 const void *pvItemToQueue, 336 TickType_t xTicksToWait 337 ); 338 * </pre> 339 * 340 * This is a macro that calls xQueueGenericSend(). 341 * 342 * Post an item to the back of a queue. The item is queued by copy, not by 343 * reference. This function must not be called from an interrupt service 344 * routine. See xQueueSendFromISR () for an alternative which may be used 345 * in an ISR. 346 * 347 * @param xQueue The handle to the queue on which the item is to be posted. 348 * 349 * @param pvItemToQueue A pointer to the item that is to be placed on the 350 * queue. The size of the items the queue will hold was defined when the 351 * queue was created, so this many bytes will be copied from pvItemToQueue 352 * into the queue storage area. 353 * 354 * @param xTicksToWait The maximum amount of time the task should block 355 * waiting for space to become available on the queue, should it already 356 * be full. The call will return immediately if this is set to 0 and the queue 357 * is full. The time is defined in tick periods so the constant 358 * portTICK_PERIOD_MS should be used to convert to real time if this is 359 required. 360 * 361 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL. 362 * 363 * Example usage: 364 <pre> 365 struct AMessage 366 { 367 char ucMessageID; 368 char ucData[ 20 ]; 369 } xMessage; 370 371 uint32_t ulVar = 10UL; 372 373 void vATask( void *pvParameters ) 374 { 375 QueueHandle_t xQueue1, xQueue2; 376 struct AMessage *pxMessage; 377 378 // Create a queue capable of containing 10 uint32_t values. 379 xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) ); 380 381 // Create a queue capable of containing 10 pointers to AMessage structures. 382 // These should be passed by pointer as they contain a lot of data. 383 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) ); 384 385 // ... 386 387 if( xQueue1 != 0 ) 388 { 389 // Send an uint32_t. Wait for 10 ticks for space to become 390 // available if necessary. 391 if( xQueueSendToBack( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10 ) != 392 pdPASS ) 393 { 394 // Failed to post the message, even after 10 ticks. 395 } 396 } 397 398 if( xQueue2 != 0 ) 399 { 400 // Send a pointer to a struct AMessage object. Don't block if the 401 // queue is already full. 402 pxMessage = & xMessage; 403 xQueueSendToBack( xQueue2, ( void * ) &pxMessage, ( TickType_t ) 0 ); 404 } 405 406 // ... Rest of task code. 407 } 408 </pre> 409 * \defgroup xQueueSend xQueueSend 410 * \ingroup QueueManagement 411 */ 412 #define xQueueSendToBack(xQueue, pvItemToQueue, xTicksToWait) \ 413 xQueueGenericSend( \ 414 (xQueue), (pvItemToQueue), (xTicksToWait), queueSEND_TO_BACK) 415 416 /** 417 * queue. h 418 * <pre> 419 BaseType_t xQueueSend( 420 QueueHandle_t xQueue, 421 const void * pvItemToQueue, 422 TickType_t xTicksToWait 423 ); 424 * </pre> 425 * 426 * This is a macro that calls xQueueGenericSend(). It is included for 427 * backward compatibility with versions of FreeRTOS.org that did not 428 * include the xQueueSendToFront() and xQueueSendToBack() macros. It is 429 * equivalent to xQueueSendToBack(). 430 * 431 * Post an item on a queue. The item is queued by copy, not by reference. 432 * This function must not be called from an interrupt service routine. 433 * See xQueueSendFromISR () for an alternative which may be used in an ISR. 434 * 435 * @param xQueue The handle to the queue on which the item is to be posted. 436 * 437 * @param pvItemToQueue A pointer to the item that is to be placed on the 438 * queue. The size of the items the queue will hold was defined when the 439 * queue was created, so this many bytes will be copied from pvItemToQueue 440 * into the queue storage area. 441 * 442 * @param xTicksToWait The maximum amount of time the task should block 443 * waiting for space to become available on the queue, should it already 444 * be full. The call will return immediately if this is set to 0 and the 445 * queue is full. The time is defined in tick periods so the constant 446 * portTICK_PERIOD_MS should be used to convert to real time if this is 447 required. 448 * 449 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL. 450 * 451 * Example usage: 452 <pre> 453 struct AMessage 454 { 455 char ucMessageID; 456 char ucData[ 20 ]; 457 } xMessage; 458 459 uint32_t ulVar = 10UL; 460 461 void vATask( void *pvParameters ) 462 { 463 QueueHandle_t xQueue1, xQueue2; 464 struct AMessage *pxMessage; 465 466 // Create a queue capable of containing 10 uint32_t values. 467 xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) ); 468 469 // Create a queue capable of containing 10 pointers to AMessage structures. 470 // These should be passed by pointer as they contain a lot of data. 471 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) ); 472 473 // ... 474 475 if( xQueue1 != 0 ) 476 { 477 // Send an uint32_t. Wait for 10 ticks for space to become 478 // available if necessary. 479 if( xQueueSend( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10 ) != 480 pdPASS ) 481 { 482 // Failed to post the message, even after 10 ticks. 483 } 484 } 485 486 if( xQueue2 != 0 ) 487 { 488 // Send a pointer to a struct AMessage object. Don't block if the 489 // queue is already full. 490 pxMessage = & xMessage; 491 xQueueSend( xQueue2, ( void * ) &pxMessage, ( TickType_t ) 0 ); 492 } 493 494 // ... Rest of task code. 495 } 496 </pre> 497 * \defgroup xQueueSend xQueueSend 498 * \ingroup QueueManagement 499 */ 500 #define xQueueSend(xQueue, pvItemToQueue, xTicksToWait) \ 501 xQueueGenericSend( \ 502 (xQueue), (pvItemToQueue), (xTicksToWait), queueSEND_TO_BACK) 503 504 /** 505 * queue. h 506 * <pre> 507 BaseType_t xQueueOverwrite( 508 QueueHandle_t xQueue, 509 const void * pvItemToQueue 510 ); 511 * </pre> 512 * 513 * Only for use with queues that have a length of one - so the queue is either 514 * empty or full. 515 * 516 * Post an item on a queue. If the queue is already full then overwrite the 517 * value held in the queue. The item is queued by copy, not by reference. 518 * 519 * This function must not be called from an interrupt service routine. 520 * See xQueueOverwriteFromISR () for an alternative which may be used in an ISR. 521 * 522 * @param xQueue The handle of the queue to which the data is being sent. 523 * 524 * @param pvItemToQueue A pointer to the item that is to be placed on the 525 * queue. The size of the items the queue will hold was defined when the 526 * queue was created, so this many bytes will be copied from pvItemToQueue 527 * into the queue storage area. 528 * 529 * @return xQueueOverwrite() is a macro that calls xQueueGenericSend(), and 530 * therefore has the same return values as xQueueSendToFront(). However, pdPASS 531 * is the only value that can be returned because xQueueOverwrite() will write 532 * to the queue even when the queue is already full. 533 * 534 * Example usage: 535 <pre> 536 537 void vFunction( void *pvParameters ) 538 { 539 QueueHandle_t xQueue; 540 uint32_t ulVarToSend, ulValReceived; 541 542 // Create a queue to hold one uint32_t value. It is strongly 543 // recommended *not* to use xQueueOverwrite() on queues that can 544 // contain more than one value, and doing so will trigger an assertion 545 // if configASSERT() is defined. 546 xQueue = xQueueCreate( 1, sizeof( uint32_t ) ); 547 548 // Write the value 10 to the queue using xQueueOverwrite(). 549 ulVarToSend = 10; 550 xQueueOverwrite( xQueue, &ulVarToSend ); 551 552 // Peeking the queue should now return 10, but leave the value 10 in 553 // the queue. A block time of zero is used as it is known that the 554 // queue holds a value. 555 ulValReceived = 0; 556 xQueuePeek( xQueue, &ulValReceived, 0 ); 557 558 if( ulValReceived != 10 ) 559 { 560 // Error unless the item was removed by a different task. 561 } 562 563 // The queue is still full. Use xQueueOverwrite() to overwrite the 564 // value held in the queue with 100. 565 ulVarToSend = 100; 566 xQueueOverwrite( xQueue, &ulVarToSend ); 567 568 // This time read from the queue, leaving the queue empty once more. 569 // A block time of 0 is used again. 570 xQueueReceive( xQueue, &ulValReceived, 0 ); 571 572 // The value read should be the last value written, even though the 573 // queue was already full when the value was written. 574 if( ulValReceived != 100 ) 575 { 576 // Error! 577 } 578 579 // ... 580 } 581 </pre> 582 * \defgroup xQueueOverwrite xQueueOverwrite 583 * \ingroup QueueManagement 584 */ 585 #define xQueueOverwrite(xQueue, pvItemToQueue) \ 586 xQueueGenericSend((xQueue), (pvItemToQueue), 0, queueOVERWRITE) 587 588 /** 589 * queue. h 590 * <pre> 591 BaseType_t xQueueGenericSend( 592 QueueHandle_t xQueue, 593 const void * pvItemToQueue, 594 TickType_t xTicksToWait 595 BaseType_t xCopyPosition 596 ); 597 * </pre> 598 * 599 * It is preferred that the macros xQueueSend(), xQueueSendToFront() and 600 * xQueueSendToBack() are used in place of calling this function directly. 601 * 602 * Post an item on a queue. The item is queued by copy, not by reference. 603 * This function must not be called from an interrupt service routine. 604 * See xQueueSendFromISR () for an alternative which may be used in an ISR. 605 * 606 * @param xQueue The handle to the queue on which the item is to be posted. 607 * 608 * @param pvItemToQueue A pointer to the item that is to be placed on the 609 * queue. The size of the items the queue will hold was defined when the 610 * queue was created, so this many bytes will be copied from pvItemToQueue 611 * into the queue storage area. 612 * 613 * @param xTicksToWait The maximum amount of time the task should block 614 * waiting for space to become available on the queue, should it already 615 * be full. The call will return immediately if this is set to 0 and the 616 * queue is full. The time is defined in tick periods so the constant 617 * portTICK_PERIOD_MS should be used to convert to real time if this is 618 required. 619 * 620 * @param xCopyPosition Can take the value queueSEND_TO_BACK to place the 621 * item at the back of the queue, or queueSEND_TO_FRONT to place the item 622 * at the front of the queue (for high priority messages). 623 * 624 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL. 625 * 626 * Example usage: 627 <pre> 628 struct AMessage 629 { 630 char ucMessageID; 631 char ucData[ 20 ]; 632 } xMessage; 633 634 uint32_t ulVar = 10UL; 635 636 void vATask( void *pvParameters ) 637 { 638 QueueHandle_t xQueue1, xQueue2; 639 struct AMessage *pxMessage; 640 641 // Create a queue capable of containing 10 uint32_t values. 642 xQueue1 = xQueueCreate( 10, sizeof( uint32_t ) ); 643 644 // Create a queue capable of containing 10 pointers to AMessage structures. 645 // These should be passed by pointer as they contain a lot of data. 646 xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) ); 647 648 // ... 649 650 if( xQueue1 != 0 ) 651 { 652 // Send an uint32_t. Wait for 10 ticks for space to become 653 // available if necessary. 654 if( xQueueGenericSend( xQueue1, ( void * ) &ulVar, ( TickType_t ) 10, 655 queueSEND_TO_BACK ) != pdPASS ) 656 { 657 // Failed to post the message, even after 10 ticks. 658 } 659 } 660 661 if( xQueue2 != 0 ) 662 { 663 // Send a pointer to a struct AMessage object. Don't block if the 664 // queue is already full. 665 pxMessage = & xMessage; 666 xQueueGenericSend( xQueue2, ( void * ) &pxMessage, ( TickType_t ) 0, 667 queueSEND_TO_BACK ); 668 } 669 670 // ... Rest of task code. 671 } 672 </pre> 673 * \defgroup xQueueSend xQueueSend 674 * \ingroup QueueManagement 675 */ 676 BaseType_t xQueueGenericSend( 677 QueueHandle_t xQueue, 678 const void *const pvItemToQueue, 679 TickType_t xTicksToWait, 680 const BaseType_t xCopyPosition) PRIVILEGED_FUNCTION; 681 682 /** 683 * queue. h 684 * <pre> 685 BaseType_t xQueuePeek( 686 QueueHandle_t xQueue, 687 void * const pvBuffer, 688 TickType_t xTicksToWait 689 );</pre> 690 * 691 * Receive an item from a queue without removing the item from the queue. 692 * The item is received by copy so a buffer of adequate size must be 693 * provided. The number of bytes copied into the buffer was defined when 694 * the queue was created. 695 * 696 * Successfully received items remain on the queue so will be returned again 697 * by the next call, or a call to xQueueReceive(). 698 * 699 * This macro must not be used in an interrupt service routine. See 700 * xQueuePeekFromISR() for an alternative that can be called from an interrupt 701 * service routine. 702 * 703 * @param xQueue The handle to the queue from which the item is to be 704 * received. 705 * 706 * @param pvBuffer Pointer to the buffer into which the received item will 707 * be copied. 708 * 709 * @param xTicksToWait The maximum amount of time the task should block 710 * waiting for an item to receive should the queue be empty at the time 711 * of the call. The time is defined in tick periods so the constant 712 * portTICK_PERIOD_MS should be used to convert to real time if this is 713 required. 714 * xQueuePeek() will return immediately if xTicksToWait is 0 and the queue 715 * is empty. 716 * 717 * @return pdTRUE if an item was successfully received from the queue, 718 * otherwise pdFALSE. 719 * 720 * Example usage: 721 <pre> 722 struct AMessage 723 { 724 char ucMessageID; 725 char ucData[ 20 ]; 726 } xMessage; 727 728 QueueHandle_t xQueue; 729 730 // Task to create a queue and post a value. 731 void vATask( void *pvParameters ) 732 { 733 struct AMessage *pxMessage; 734 735 // Create a queue capable of containing 10 pointers to AMessage structures. 736 // These should be passed by pointer as they contain a lot of data. 737 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) ); 738 if( xQueue == 0 ) 739 { 740 // Failed to create the queue. 741 } 742 743 // ... 744 745 // Send a pointer to a struct AMessage object. Don't block if the 746 // queue is already full. 747 pxMessage = & xMessage; 748 xQueueSend( xQueue, ( void * ) &pxMessage, ( TickType_t ) 0 ); 749 750 // ... Rest of task code. 751 } 752 753 // Task to peek the data from the queue. 754 void vADifferentTask( void *pvParameters ) 755 { 756 struct AMessage *pxRxedMessage; 757 758 if( xQueue != 0 ) 759 { 760 // Peek a message on the created queue. Block for 10 ticks if a 761 // message is not immediately available. 762 if( xQueuePeek( xQueue, &( pxRxedMessage ), ( TickType_t ) 10 ) ) 763 { 764 // pcRxedMessage now points to the struct AMessage variable posted 765 // by vATask, but the item still remains on the queue. 766 } 767 } 768 769 // ... Rest of task code. 770 } 771 </pre> 772 * \defgroup xQueuePeek xQueuePeek 773 * \ingroup QueueManagement 774 */ 775 BaseType_t xQueuePeek( 776 QueueHandle_t xQueue, 777 void *const pvBuffer, 778 TickType_t xTicksToWait) PRIVILEGED_FUNCTION; 779 780 /** 781 * queue. h 782 * <pre> 783 BaseType_t xQueuePeekFromISR( 784 QueueHandle_t xQueue, 785 void *pvBuffer, 786 );</pre> 787 * 788 * A version of xQueuePeek() that can be called from an interrupt service 789 * routine (ISR). 790 * 791 * Receive an item from a queue without removing the item from the queue. 792 * The item is received by copy so a buffer of adequate size must be 793 * provided. The number of bytes copied into the buffer was defined when 794 * the queue was created. 795 * 796 * Successfully received items remain on the queue so will be returned again 797 * by the next call, or a call to xQueueReceive(). 798 * 799 * @param xQueue The handle to the queue from which the item is to be 800 * received. 801 * 802 * @param pvBuffer Pointer to the buffer into which the received item will 803 * be copied. 804 * 805 * @return pdTRUE if an item was successfully received from the queue, 806 * otherwise pdFALSE. 807 * 808 * \defgroup xQueuePeekFromISR xQueuePeekFromISR 809 * \ingroup QueueManagement 810 */ 811 BaseType_t xQueuePeekFromISR(QueueHandle_t xQueue, void *const pvBuffer) 812 PRIVILEGED_FUNCTION; 813 814 /** 815 * queue. h 816 * <pre> 817 BaseType_t xQueueReceive( 818 QueueHandle_t xQueue, 819 void *pvBuffer, 820 TickType_t xTicksToWait 821 );</pre> 822 * 823 * Receive an item from a queue. The item is received by copy so a buffer of 824 * adequate size must be provided. The number of bytes copied into the buffer 825 * was defined when the queue was created. 826 * 827 * Successfully received items are removed from the queue. 828 * 829 * This function must not be used in an interrupt service routine. See 830 * xQueueReceiveFromISR for an alternative that can. 831 * 832 * @param xQueue The handle to the queue from which the item is to be 833 * received. 834 * 835 * @param pvBuffer Pointer to the buffer into which the received item will 836 * be copied. 837 * 838 * @param xTicksToWait The maximum amount of time the task should block 839 * waiting for an item to receive should the queue be empty at the time 840 * of the call. xQueueReceive() will return immediately if xTicksToWait 841 * is zero and the queue is empty. The time is defined in tick periods so the 842 * constant portTICK_PERIOD_MS should be used to convert to real time if this is 843 * required. 844 * 845 * @return pdTRUE if an item was successfully received from the queue, 846 * otherwise pdFALSE. 847 * 848 * Example usage: 849 <pre> 850 struct AMessage 851 { 852 char ucMessageID; 853 char ucData[ 20 ]; 854 } xMessage; 855 856 QueueHandle_t xQueue; 857 858 // Task to create a queue and post a value. 859 void vATask( void *pvParameters ) 860 { 861 struct AMessage *pxMessage; 862 863 // Create a queue capable of containing 10 pointers to AMessage structures. 864 // These should be passed by pointer as they contain a lot of data. 865 xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) ); 866 if( xQueue == 0 ) 867 { 868 // Failed to create the queue. 869 } 870 871 // ... 872 873 // Send a pointer to a struct AMessage object. Don't block if the 874 // queue is already full. 875 pxMessage = & xMessage; 876 xQueueSend( xQueue, ( void * ) &pxMessage, ( TickType_t ) 0 ); 877 878 // ... Rest of task code. 879 } 880 881 // Task to receive from the queue. 882 void vADifferentTask( void *pvParameters ) 883 { 884 struct AMessage *pxRxedMessage; 885 886 if( xQueue != 0 ) 887 { 888 // Receive a message on the created queue. Block for 10 ticks if a 889 // message is not immediately available. 890 if( xQueueReceive( xQueue, &( pxRxedMessage ), ( TickType_t ) 10 ) ) 891 { 892 // pcRxedMessage now points to the struct AMessage variable posted 893 // by vATask. 894 } 895 } 896 897 // ... Rest of task code. 898 } 899 </pre> 900 * \defgroup xQueueReceive xQueueReceive 901 * \ingroup QueueManagement 902 */ 903 BaseType_t xQueueReceive( 904 QueueHandle_t xQueue, 905 void *const pvBuffer, 906 TickType_t xTicksToWait) PRIVILEGED_FUNCTION; 907 908 /** 909 * queue. h 910 * <pre>UBaseType_t uxQueueMessagesWaiting( const QueueHandle_t xQueue );</pre> 911 * 912 * Return the number of messages stored in a queue. 913 * 914 * @param xQueue A handle to the queue being queried. 915 * 916 * @return The number of messages available in the queue. 917 * 918 * \defgroup uxQueueMessagesWaiting uxQueueMessagesWaiting 919 * \ingroup QueueManagement 920 */ 921 UBaseType_t uxQueueMessagesWaiting(const QueueHandle_t xQueue) 922 PRIVILEGED_FUNCTION; 923 924 /** 925 * queue. h 926 * <pre>UBaseType_t uxQueueSpacesAvailable( const QueueHandle_t xQueue );</pre> 927 * 928 * Return the number of free spaces available in a queue. This is equal to the 929 * number of items that can be sent to the queue before the queue becomes full 930 * if no items are removed. 931 * 932 * @param xQueue A handle to the queue being queried. 933 * 934 * @return The number of spaces available in the queue. 935 * 936 * \defgroup uxQueueMessagesWaiting uxQueueMessagesWaiting 937 * \ingroup QueueManagement 938 */ 939 UBaseType_t uxQueueSpacesAvailable(const QueueHandle_t xQueue) 940 PRIVILEGED_FUNCTION; 941 942 /** 943 * queue. h 944 * <pre>void vQueueDelete( QueueHandle_t xQueue );</pre> 945 * 946 * Delete a queue - freeing all the memory allocated for storing of items 947 * placed on the queue. 948 * 949 * @param xQueue A handle to the queue to be deleted. 950 * 951 * \defgroup vQueueDelete vQueueDelete 952 * \ingroup QueueManagement 953 */ 954 void vQueueDelete(QueueHandle_t xQueue) PRIVILEGED_FUNCTION; 955 956 /** 957 * queue. h 958 * <pre> 959 BaseType_t xQueueSendToFrontFromISR( 960 QueueHandle_t xQueue, 961 const void *pvItemToQueue, 962 BaseType_t *pxHigherPriorityTaskWoken 963 ); 964 </pre> 965 * 966 * This is a macro that calls xQueueGenericSendFromISR(). 967 * 968 * Post an item to the front of a queue. It is safe to use this macro from 969 * within an interrupt service routine. 970 * 971 * Items are queued by copy not reference so it is preferable to only 972 * queue small items, especially when called from an ISR. In most cases 973 * it would be preferable to store a pointer to the item being queued. 974 * 975 * @param xQueue The handle to the queue on which the item is to be posted. 976 * 977 * @param pvItemToQueue A pointer to the item that is to be placed on the 978 * queue. The size of the items the queue will hold was defined when the 979 * queue was created, so this many bytes will be copied from pvItemToQueue 980 * into the queue storage area. 981 * 982 * @param pxHigherPriorityTaskWoken xQueueSendToFrontFromISR() will set 983 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task 984 * to unblock, and the unblocked task has a priority higher than the currently 985 * running task. If xQueueSendToFromFromISR() sets this value to pdTRUE then 986 * a context switch should be requested before the interrupt is exited. 987 * 988 * @return pdTRUE if the data was successfully sent to the queue, otherwise 989 * errQUEUE_FULL. 990 * 991 * Example usage for buffered IO (where the ISR can obtain more than one value 992 * per call): 993 <pre> 994 void vBufferISR( void ) 995 { 996 char cIn; 997 BaseType_t xHigherPrioritTaskWoken; 998 999 // We have not woken a task at the start of the ISR. 1000 xHigherPriorityTaskWoken = pdFALSE; 1001 1002 // Loop until the buffer is empty. 1003 do 1004 { 1005 // Obtain a byte from the buffer. 1006 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS ); 1007 1008 // Post the byte. 1009 xQueueSendToFrontFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken ); 1010 1011 } while( portINPUT_BYTE( BUFFER_COUNT ) ); 1012 1013 // Now the buffer is empty we can switch context if necessary. 1014 if( xHigherPriorityTaskWoken ) 1015 { 1016 taskYIELD (); 1017 } 1018 } 1019 </pre> 1020 * 1021 * \defgroup xQueueSendFromISR xQueueSendFromISR 1022 * \ingroup QueueManagement 1023 */ 1024 #define xQueueSendToFrontFromISR( \ 1025 xQueue, pvItemToQueue, pxHigherPriorityTaskWoken) \ 1026 xQueueGenericSendFromISR( \ 1027 (xQueue), \ 1028 (pvItemToQueue), \ 1029 (pxHigherPriorityTaskWoken), \ 1030 queueSEND_TO_FRONT) 1031 1032 /** 1033 * queue. h 1034 * <pre> 1035 BaseType_t xQueueSendToBackFromISR( 1036 QueueHandle_t xQueue, 1037 const void *pvItemToQueue, 1038 BaseType_t *pxHigherPriorityTaskWoken 1039 ); 1040 </pre> 1041 * 1042 * This is a macro that calls xQueueGenericSendFromISR(). 1043 * 1044 * Post an item to the back of a queue. It is safe to use this macro from 1045 * within an interrupt service routine. 1046 * 1047 * Items are queued by copy not reference so it is preferable to only 1048 * queue small items, especially when called from an ISR. In most cases 1049 * it would be preferable to store a pointer to the item being queued. 1050 * 1051 * @param xQueue The handle to the queue on which the item is to be posted. 1052 * 1053 * @param pvItemToQueue A pointer to the item that is to be placed on the 1054 * queue. The size of the items the queue will hold was defined when the 1055 * queue was created, so this many bytes will be copied from pvItemToQueue 1056 * into the queue storage area. 1057 * 1058 * @param pxHigherPriorityTaskWoken xQueueSendToBackFromISR() will set 1059 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task 1060 * to unblock, and the unblocked task has a priority higher than the currently 1061 * running task. If xQueueSendToBackFromISR() sets this value to pdTRUE then 1062 * a context switch should be requested before the interrupt is exited. 1063 * 1064 * @return pdTRUE if the data was successfully sent to the queue, otherwise 1065 * errQUEUE_FULL. 1066 * 1067 * Example usage for buffered IO (where the ISR can obtain more than one value 1068 * per call): 1069 <pre> 1070 void vBufferISR( void ) 1071 { 1072 char cIn; 1073 BaseType_t xHigherPriorityTaskWoken; 1074 1075 // We have not woken a task at the start of the ISR. 1076 xHigherPriorityTaskWoken = pdFALSE; 1077 1078 // Loop until the buffer is empty. 1079 do 1080 { 1081 // Obtain a byte from the buffer. 1082 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS ); 1083 1084 // Post the byte. 1085 xQueueSendToBackFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken ); 1086 1087 } while( portINPUT_BYTE( BUFFER_COUNT ) ); 1088 1089 // Now the buffer is empty we can switch context if necessary. 1090 if( xHigherPriorityTaskWoken ) 1091 { 1092 taskYIELD (); 1093 } 1094 } 1095 </pre> 1096 * 1097 * \defgroup xQueueSendFromISR xQueueSendFromISR 1098 * \ingroup QueueManagement 1099 */ 1100 #define xQueueSendToBackFromISR( \ 1101 xQueue, pvItemToQueue, pxHigherPriorityTaskWoken) \ 1102 xQueueGenericSendFromISR( \ 1103 (xQueue), \ 1104 (pvItemToQueue), \ 1105 (pxHigherPriorityTaskWoken), \ 1106 queueSEND_TO_BACK) 1107 1108 /** 1109 * queue. h 1110 * <pre> 1111 BaseType_t xQueueOverwriteFromISR( 1112 QueueHandle_t xQueue, 1113 const void * pvItemToQueue, 1114 BaseType_t *pxHigherPriorityTaskWoken 1115 ); 1116 * </pre> 1117 * 1118 * A version of xQueueOverwrite() that can be used in an interrupt service 1119 * routine (ISR). 1120 * 1121 * Only for use with queues that can hold a single item - so the queue is either 1122 * empty or full. 1123 * 1124 * Post an item on a queue. If the queue is already full then overwrite the 1125 * value held in the queue. The item is queued by copy, not by reference. 1126 * 1127 * @param xQueue The handle to the queue on which the item is to be posted. 1128 * 1129 * @param pvItemToQueue A pointer to the item that is to be placed on the 1130 * queue. The size of the items the queue will hold was defined when the 1131 * queue was created, so this many bytes will be copied from pvItemToQueue 1132 * into the queue storage area. 1133 * 1134 * @param pxHigherPriorityTaskWoken xQueueOverwriteFromISR() will set 1135 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task 1136 * to unblock, and the unblocked task has a priority higher than the currently 1137 * running task. If xQueueOverwriteFromISR() sets this value to pdTRUE then 1138 * a context switch should be requested before the interrupt is exited. 1139 * 1140 * @return xQueueOverwriteFromISR() is a macro that calls 1141 * xQueueGenericSendFromISR(), and therefore has the same return values as 1142 * xQueueSendToFrontFromISR(). However, pdPASS is the only value that can be 1143 * returned because xQueueOverwriteFromISR() will write to the queue even when 1144 * the queue is already full. 1145 * 1146 * Example usage: 1147 <pre> 1148 1149 QueueHandle_t xQueue; 1150 1151 void vFunction( void *pvParameters ) 1152 { 1153 // Create a queue to hold one uint32_t value. It is strongly 1154 // recommended *not* to use xQueueOverwriteFromISR() on queues that can 1155 // contain more than one value, and doing so will trigger an assertion 1156 // if configASSERT() is defined. 1157 xQueue = xQueueCreate( 1, sizeof( uint32_t ) ); 1158 } 1159 1160 void vAnInterruptHandler( void ) 1161 { 1162 // xHigherPriorityTaskWoken must be set to pdFALSE before it is used. 1163 BaseType_t xHigherPriorityTaskWoken = pdFALSE; 1164 uint32_t ulVarToSend, ulValReceived; 1165 1166 // Write the value 10 to the queue using xQueueOverwriteFromISR(). 1167 ulVarToSend = 10; 1168 xQueueOverwriteFromISR( xQueue, &ulVarToSend, &xHigherPriorityTaskWoken ); 1169 1170 // The queue is full, but calling xQueueOverwriteFromISR() again will still 1171 // pass because the value held in the queue will be overwritten with the 1172 // new value. 1173 ulVarToSend = 100; 1174 xQueueOverwriteFromISR( xQueue, &ulVarToSend, &xHigherPriorityTaskWoken ); 1175 1176 // Reading from the queue will now return 100. 1177 1178 // ... 1179 1180 if( xHigherPrioritytaskWoken == pdTRUE ) 1181 { 1182 // Writing to the queue caused a task to unblock and the unblocked task 1183 // has a priority higher than or equal to the priority of the currently 1184 // executing task (the task this interrupt interrupted). Perform a 1185 context 1186 // switch so this interrupt returns directly to the unblocked task. 1187 portYIELD_FROM_ISR(); // or portEND_SWITCHING_ISR() depending on the 1188 port. 1189 } 1190 } 1191 </pre> 1192 * \defgroup xQueueOverwriteFromISR xQueueOverwriteFromISR 1193 * \ingroup QueueManagement 1194 */ 1195 #define xQueueOverwriteFromISR( \ 1196 xQueue, pvItemToQueue, pxHigherPriorityTaskWoken) \ 1197 xQueueGenericSendFromISR( \ 1198 (xQueue), \ 1199 (pvItemToQueue), \ 1200 (pxHigherPriorityTaskWoken), \ 1201 queueOVERWRITE) 1202 1203 /** 1204 * queue. h 1205 * <pre> 1206 BaseType_t xQueueSendFromISR( 1207 QueueHandle_t xQueue, 1208 const void *pvItemToQueue, 1209 BaseType_t *pxHigherPriorityTaskWoken 1210 ); 1211 </pre> 1212 * 1213 * This is a macro that calls xQueueGenericSendFromISR(). It is included 1214 * for backward compatibility with versions of FreeRTOS.org that did not 1215 * include the xQueueSendToBackFromISR() and xQueueSendToFrontFromISR() 1216 * macros. 1217 * 1218 * Post an item to the back of a queue. It is safe to use this function from 1219 * within an interrupt service routine. 1220 * 1221 * Items are queued by copy not reference so it is preferable to only 1222 * queue small items, especially when called from an ISR. In most cases 1223 * it would be preferable to store a pointer to the item being queued. 1224 * 1225 * @param xQueue The handle to the queue on which the item is to be posted. 1226 * 1227 * @param pvItemToQueue A pointer to the item that is to be placed on the 1228 * queue. The size of the items the queue will hold was defined when the 1229 * queue was created, so this many bytes will be copied from pvItemToQueue 1230 * into the queue storage area. 1231 * 1232 * @param pxHigherPriorityTaskWoken xQueueSendFromISR() will set 1233 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task 1234 * to unblock, and the unblocked task has a priority higher than the currently 1235 * running task. If xQueueSendFromISR() sets this value to pdTRUE then 1236 * a context switch should be requested before the interrupt is exited. 1237 * 1238 * @return pdTRUE if the data was successfully sent to the queue, otherwise 1239 * errQUEUE_FULL. 1240 * 1241 * Example usage for buffered IO (where the ISR can obtain more than one value 1242 * per call): 1243 <pre> 1244 void vBufferISR( void ) 1245 { 1246 char cIn; 1247 BaseType_t xHigherPriorityTaskWoken; 1248 1249 // We have not woken a task at the start of the ISR. 1250 xHigherPriorityTaskWoken = pdFALSE; 1251 1252 // Loop until the buffer is empty. 1253 do 1254 { 1255 // Obtain a byte from the buffer. 1256 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS ); 1257 1258 // Post the byte. 1259 xQueueSendFromISR( xRxQueue, &cIn, &xHigherPriorityTaskWoken ); 1260 1261 } while( portINPUT_BYTE( BUFFER_COUNT ) ); 1262 1263 // Now the buffer is empty we can switch context if necessary. 1264 if( xHigherPriorityTaskWoken ) 1265 { 1266 // Actual macro used here is port specific. 1267 portYIELD_FROM_ISR (); 1268 } 1269 } 1270 </pre> 1271 * 1272 * \defgroup xQueueSendFromISR xQueueSendFromISR 1273 * \ingroup QueueManagement 1274 */ 1275 #define xQueueSendFromISR(xQueue, pvItemToQueue, pxHigherPriorityTaskWoken) \ 1276 xQueueGenericSendFromISR( \ 1277 (xQueue), \ 1278 (pvItemToQueue), \ 1279 (pxHigherPriorityTaskWoken), \ 1280 queueSEND_TO_BACK) 1281 1282 /** 1283 * queue. h 1284 * <pre> 1285 BaseType_t xQueueGenericSendFromISR( 1286 QueueHandle_t xQueue, 1287 const void *pvItemToQueue, 1288 BaseType_t 1289 *pxHigherPriorityTaskWoken, BaseType_t xCopyPosition 1290 ); 1291 </pre> 1292 * 1293 * It is preferred that the macros xQueueSendFromISR(), 1294 * xQueueSendToFrontFromISR() and xQueueSendToBackFromISR() be used in place 1295 * of calling this function directly. xQueueGiveFromISR() is an 1296 * equivalent for use by semaphores that don't actually copy any data. 1297 * 1298 * Post an item on a queue. It is safe to use this function from within an 1299 * interrupt service routine. 1300 * 1301 * Items are queued by copy not reference so it is preferable to only 1302 * queue small items, especially when called from an ISR. In most cases 1303 * it would be preferable to store a pointer to the item being queued. 1304 * 1305 * @param xQueue The handle to the queue on which the item is to be posted. 1306 * 1307 * @param pvItemToQueue A pointer to the item that is to be placed on the 1308 * queue. The size of the items the queue will hold was defined when the 1309 * queue was created, so this many bytes will be copied from pvItemToQueue 1310 * into the queue storage area. 1311 * 1312 * @param pxHigherPriorityTaskWoken xQueueGenericSendFromISR() will set 1313 * *pxHigherPriorityTaskWoken to pdTRUE if sending to the queue caused a task 1314 * to unblock, and the unblocked task has a priority higher than the currently 1315 * running task. If xQueueGenericSendFromISR() sets this value to pdTRUE then 1316 * a context switch should be requested before the interrupt is exited. 1317 * 1318 * @param xCopyPosition Can take the value queueSEND_TO_BACK to place the 1319 * item at the back of the queue, or queueSEND_TO_FRONT to place the item 1320 * at the front of the queue (for high priority messages). 1321 * 1322 * @return pdTRUE if the data was successfully sent to the queue, otherwise 1323 * errQUEUE_FULL. 1324 * 1325 * Example usage for buffered IO (where the ISR can obtain more than one value 1326 * per call): 1327 <pre> 1328 void vBufferISR( void ) 1329 { 1330 char cIn; 1331 BaseType_t xHigherPriorityTaskWokenByPost; 1332 1333 // We have not woken a task at the start of the ISR. 1334 xHigherPriorityTaskWokenByPost = pdFALSE; 1335 1336 // Loop until the buffer is empty. 1337 do 1338 { 1339 // Obtain a byte from the buffer. 1340 cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS ); 1341 1342 // Post each byte. 1343 xQueueGenericSendFromISR( xRxQueue, &cIn, 1344 &xHigherPriorityTaskWokenByPost, queueSEND_TO_BACK ); 1345 1346 } while( portINPUT_BYTE( BUFFER_COUNT ) ); 1347 1348 // Now the buffer is empty we can switch context if necessary. Note that 1349 the 1350 // name of the yield function required is port specific. 1351 if( xHigherPriorityTaskWokenByPost ) 1352 { 1353 portYIELD_FROM_ISR(); 1354 } 1355 } 1356 </pre> 1357 * 1358 * \defgroup xQueueSendFromISR xQueueSendFromISR 1359 * \ingroup QueueManagement 1360 */ 1361 BaseType_t xQueueGenericSendFromISR( 1362 QueueHandle_t xQueue, 1363 const void *const pvItemToQueue, 1364 BaseType_t *const pxHigherPriorityTaskWoken, 1365 const BaseType_t xCopyPosition) PRIVILEGED_FUNCTION; 1366 BaseType_t xQueueGiveFromISR( 1367 QueueHandle_t xQueue, 1368 BaseType_t *const pxHigherPriorityTaskWoken) PRIVILEGED_FUNCTION; 1369 1370 /** 1371 * queue. h 1372 * <pre> 1373 BaseType_t xQueueReceiveFromISR( 1374 QueueHandle_t xQueue, 1375 void *pvBuffer, 1376 BaseType_t *pxTaskWoken 1377 ); 1378 * </pre> 1379 * 1380 * Receive an item from a queue. It is safe to use this function from within an 1381 * interrupt service routine. 1382 * 1383 * @param xQueue The handle to the queue from which the item is to be 1384 * received. 1385 * 1386 * @param pvBuffer Pointer to the buffer into which the received item will 1387 * be copied. 1388 * 1389 * @param pxTaskWoken A task may be blocked waiting for space to become 1390 * available on the queue. If xQueueReceiveFromISR causes such a task to 1391 * unblock *pxTaskWoken will get set to pdTRUE, otherwise *pxTaskWoken will 1392 * remain unchanged. 1393 * 1394 * @return pdTRUE if an item was successfully received from the queue, 1395 * otherwise pdFALSE. 1396 * 1397 * Example usage: 1398 <pre> 1399 1400 QueueHandle_t xQueue; 1401 1402 // Function to create a queue and post some values. 1403 void vAFunction( void *pvParameters ) 1404 { 1405 char cValueToPost; 1406 const TickType_t xTicksToWait = ( TickType_t )0xff; 1407 1408 // Create a queue capable of containing 10 characters. 1409 xQueue = xQueueCreate( 10, sizeof( char ) ); 1410 if( xQueue == 0 ) 1411 { 1412 // Failed to create the queue. 1413 } 1414 1415 // ... 1416 1417 // Post some characters that will be used within an ISR. If the queue 1418 // is full then this task will block for xTicksToWait ticks. 1419 cValueToPost = 'a'; 1420 xQueueSend( xQueue, ( void * ) &cValueToPost, xTicksToWait ); 1421 cValueToPost = 'b'; 1422 xQueueSend( xQueue, ( void * ) &cValueToPost, xTicksToWait ); 1423 1424 // ... keep posting characters ... this task may block when the queue 1425 // becomes full. 1426 1427 cValueToPost = 'c'; 1428 xQueueSend( xQueue, ( void * ) &cValueToPost, xTicksToWait ); 1429 } 1430 1431 // ISR that outputs all the characters received on the queue. 1432 void vISR_Routine( void ) 1433 { 1434 BaseType_t xTaskWokenByReceive = pdFALSE; 1435 char cRxedChar; 1436 1437 while( xQueueReceiveFromISR( xQueue, ( void * ) &cRxedChar, 1438 &xTaskWokenByReceive) ) 1439 { 1440 // A character was received. Output the character now. 1441 vOutputCharacter( cRxedChar ); 1442 1443 // If removing the character from the queue woke the task that was 1444 // posting onto the queue cTaskWokenByReceive will have been set to 1445 // pdTRUE. No matter how many times this loop iterates only one 1446 // task will be woken. 1447 } 1448 1449 if( cTaskWokenByPost != ( char ) pdFALSE; 1450 { 1451 taskYIELD (); 1452 } 1453 } 1454 </pre> 1455 * \defgroup xQueueReceiveFromISR xQueueReceiveFromISR 1456 * \ingroup QueueManagement 1457 */ 1458 BaseType_t xQueueReceiveFromISR( 1459 QueueHandle_t xQueue, 1460 void *const pvBuffer, 1461 BaseType_t *const pxHigherPriorityTaskWoken) PRIVILEGED_FUNCTION; 1462 1463 /* 1464 * Utilities to query queues that are safe to use from an ISR. These utilities 1465 * should be used only from witin an ISR, or within a critical section. 1466 */ 1467 BaseType_t xQueueIsQueueEmptyFromISR(const QueueHandle_t xQueue) 1468 PRIVILEGED_FUNCTION; 1469 BaseType_t xQueueIsQueueFullFromISR(const QueueHandle_t xQueue) 1470 PRIVILEGED_FUNCTION; 1471 UBaseType_t uxQueueMessagesWaitingFromISR(const QueueHandle_t xQueue) 1472 PRIVILEGED_FUNCTION; 1473 1474 /* 1475 * The functions defined above are for passing data to and from tasks. The 1476 * functions below are the equivalents for passing data to and from 1477 * co-routines. 1478 * 1479 * These functions are called from the co-routine macro implementation and 1480 * should not be called directly from application code. Instead use the macro 1481 * wrappers defined within croutine.h. 1482 */ 1483 BaseType_t xQueueCRSendFromISR( 1484 QueueHandle_t xQueue, 1485 const void *pvItemToQueue, 1486 BaseType_t xCoRoutinePreviouslyWoken); 1487 BaseType_t xQueueCRReceiveFromISR( 1488 QueueHandle_t xQueue, 1489 void *pvBuffer, 1490 BaseType_t *pxTaskWoken); 1491 BaseType_t xQueueCRSend( 1492 QueueHandle_t xQueue, 1493 const void *pvItemToQueue, 1494 TickType_t xTicksToWait); 1495 BaseType_t xQueueCRReceive( 1496 QueueHandle_t xQueue, 1497 void *pvBuffer, 1498 TickType_t xTicksToWait); 1499 1500 /* 1501 * For internal use only. Use xSemaphoreCreateMutex(), 1502 * xSemaphoreCreateCounting() or xSemaphoreGetMutexHolder() instead of calling 1503 * these functions directly. 1504 */ 1505 QueueHandle_t xQueueCreateMutex(const uint8_t ucQueueType) PRIVILEGED_FUNCTION; 1506 QueueHandle_t xQueueCreateMutexStatic( 1507 const uint8_t ucQueueType, 1508 StaticQueue_t *pxStaticQueue) PRIVILEGED_FUNCTION; 1509 QueueHandle_t xQueueCreateCountingSemaphore( 1510 const UBaseType_t uxMaxCount, 1511 const UBaseType_t uxInitialCount) PRIVILEGED_FUNCTION; 1512 QueueHandle_t xQueueCreateCountingSemaphoreStatic( 1513 const UBaseType_t uxMaxCount, 1514 const UBaseType_t uxInitialCount, 1515 StaticQueue_t *pxStaticQueue) PRIVILEGED_FUNCTION; 1516 BaseType_t xQueueSemaphoreTake(QueueHandle_t xQueue, TickType_t xTicksToWait) 1517 PRIVILEGED_FUNCTION; 1518 TaskHandle_t xQueueGetMutexHolder(QueueHandle_t xSemaphore) PRIVILEGED_FUNCTION; 1519 TaskHandle_t xQueueGetMutexHolderFromISR(QueueHandle_t xSemaphore) 1520 PRIVILEGED_FUNCTION; 1521 1522 /* 1523 * For internal use only. Use xSemaphoreTakeMutexRecursive() or 1524 * xSemaphoreGiveMutexRecursive() instead of calling these functions directly. 1525 */ 1526 BaseType_t xQueueTakeMutexRecursive( 1527 QueueHandle_t xMutex, 1528 TickType_t xTicksToWait) PRIVILEGED_FUNCTION; 1529 BaseType_t xQueueGiveMutexRecursive(QueueHandle_t xMutex) PRIVILEGED_FUNCTION; 1530 1531 /* 1532 * Reset a queue back to its original empty state. The return value is now 1533 * obsolete and is always set to pdPASS. 1534 */ 1535 #define xQueueReset(xQueue) xQueueGenericReset(xQueue, pdFALSE) 1536 1537 /* 1538 * The registry is provided as a means for kernel aware debuggers to 1539 * locate queues, semaphores and mutexes. Call vQueueAddToRegistry() add 1540 * a queue, semaphore or mutex handle to the registry if you want the handle 1541 * to be available to a kernel aware debugger. If you are not using a kernel 1542 * aware debugger then this function can be ignored. 1543 * 1544 * configQUEUE_REGISTRY_SIZE defines the maximum number of handles the 1545 * registry can hold. configQUEUE_REGISTRY_SIZE must be greater than 0 1546 * within FreeRTOSConfig.h for the registry to be available. Its value 1547 * does not effect the number of queues, semaphores and mutexes that can be 1548 * created - just the number that the registry can hold. 1549 * 1550 * @param xQueue The handle of the queue being added to the registry. This 1551 * is the handle returned by a call to xQueueCreate(). Semaphore and mutex 1552 * handles can also be passed in here. 1553 * 1554 * @param pcName The name to be associated with the handle. This is the 1555 * name that the kernel aware debugger will display. The queue registry only 1556 * stores a pointer to the string - so the string must be persistent (global or 1557 * preferably in ROM/Flash), not on the stack. 1558 */ 1559 #if (configQUEUE_REGISTRY_SIZE > 0) 1560 void vQueueAddToRegistry(QueueHandle_t xQueue, const char *pcQueueName) 1561 PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for 1562 strings and single characters only. */ 1563 #endif 1564 1565 /* 1566 * The registry is provided as a means for kernel aware debuggers to 1567 * locate queues, semaphores and mutexes. Call vQueueAddToRegistry() add 1568 * a queue, semaphore or mutex handle to the registry if you want the handle 1569 * to be available to a kernel aware debugger, and vQueueUnregisterQueue() to 1570 * remove the queue, semaphore or mutex from the register. If you are not using 1571 * a kernel aware debugger then this function can be ignored. 1572 * 1573 * @param xQueue The handle of the queue being removed from the registry. 1574 */ 1575 #if (configQUEUE_REGISTRY_SIZE > 0) 1576 void vQueueUnregisterQueue(QueueHandle_t xQueue) PRIVILEGED_FUNCTION; 1577 #endif 1578 1579 /* 1580 * The queue registry is provided as a means for kernel aware debuggers to 1581 * locate queues, semaphores and mutexes. Call pcQueueGetName() to look 1582 * up and return the name of a queue in the queue registry from the queue's 1583 * handle. 1584 * 1585 * @param xQueue The handle of the queue the name of which will be returned. 1586 * @return If the queue is in the registry then a pointer to the name of the 1587 * queue is returned. If the queue is not in the registry then NULL is 1588 * returned. 1589 */ 1590 #if (configQUEUE_REGISTRY_SIZE > 0) 1591 const char *pcQueueGetName(QueueHandle_t xQueue) 1592 PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for 1593 strings and single characters only. */ 1594 #endif 1595 1596 /* 1597 * Generic version of the function used to creaet a queue using dynamic memory 1598 * allocation. This is called by other functions and macros that create other 1599 * RTOS objects that use the queue structure as their base. 1600 */ 1601 #if (configSUPPORT_DYNAMIC_ALLOCATION == 1) 1602 QueueHandle_t xQueueGenericCreate( 1603 const UBaseType_t uxQueueLength, 1604 const UBaseType_t uxItemSize, 1605 const uint8_t ucQueueType) PRIVILEGED_FUNCTION; 1606 #endif 1607 1608 /* 1609 * Generic version of the function used to creaet a queue using dynamic memory 1610 * allocation. This is called by other functions and macros that create other 1611 * RTOS objects that use the queue structure as their base. 1612 */ 1613 #if (configSUPPORT_STATIC_ALLOCATION == 1) 1614 QueueHandle_t xQueueGenericCreateStatic( 1615 const UBaseType_t uxQueueLength, 1616 const UBaseType_t uxItemSize, 1617 uint8_t *pucQueueStorage, 1618 StaticQueue_t *pxStaticQueue, 1619 const uint8_t ucQueueType) PRIVILEGED_FUNCTION; 1620 #endif 1621 1622 /* 1623 * Queue sets provide a mechanism to allow a task to block (pend) on a read 1624 * operation from multiple queues or semaphores simultaneously. 1625 * 1626 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this 1627 * function. 1628 * 1629 * A queue set must be explicitly created using a call to xQueueCreateSet() 1630 * before it can be used. Once created, standard FreeRTOS queues and semaphores 1631 * can be added to the set using calls to xQueueAddToSet(). 1632 * xQueueSelectFromSet() is then used to determine which, if any, of the queues 1633 * or semaphores contained in the set is in a state where a queue read or 1634 * semaphore take operation would be successful. 1635 * 1636 * Note 1: See the documentation on http://wwwFreeRTOS.org/RTOS-queue-sets.html 1637 * for reasons why queue sets are very rarely needed in practice as there are 1638 * simpler methods of blocking on multiple objects. 1639 * 1640 * Note 2: Blocking on a queue set that contains a mutex will not cause the 1641 * mutex holder to inherit the priority of the blocked task. 1642 * 1643 * Note 3: An additional 4 bytes of RAM is required for each space in a every 1644 * queue added to a queue set. Therefore counting semaphores that have a high 1645 * maximum count value should not be added to a queue set. 1646 * 1647 * Note 4: A receive (in the case of a queue) or take (in the case of a 1648 * semaphore) operation must not be performed on a member of a queue set unless 1649 * a call to xQueueSelectFromSet() has first returned a handle to that set 1650 * member. 1651 * 1652 * @param uxEventQueueLength Queue sets store events that occur on 1653 * the queues and semaphores contained in the set. uxEventQueueLength specifies 1654 * the maximum number of events that can be queued at once. To be absolutely 1655 * certain that events are not lost uxEventQueueLength should be set to the 1656 * total sum of the length of the queues added to the set, where binary 1657 * semaphores and mutexes have a length of 1, and counting semaphores have a 1658 * length set by their maximum count value. Examples: 1659 * + If a queue set is to hold a queue of length 5, another queue of length 12, 1660 * and a binary semaphore, then uxEventQueueLength should be set to 1661 * (5 + 12 + 1), or 18. 1662 * + If a queue set is to hold three binary semaphores then uxEventQueueLength 1663 * should be set to (1 + 1 + 1 ), or 3. 1664 * + If a queue set is to hold a counting semaphore that has a maximum count of 1665 * 5, and a counting semaphore that has a maximum count of 3, then 1666 * uxEventQueueLength should be set to (5 + 3), or 8. 1667 * 1668 * @return If the queue set is created successfully then a handle to the created 1669 * queue set is returned. Otherwise NULL is returned. 1670 */ 1671 QueueSetHandle_t xQueueCreateSet(const UBaseType_t uxEventQueueLength) 1672 PRIVILEGED_FUNCTION; 1673 1674 /* 1675 * Adds a queue or semaphore to a queue set that was previously created by a 1676 * call to xQueueCreateSet(). 1677 * 1678 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this 1679 * function. 1680 * 1681 * Note 1: A receive (in the case of a queue) or take (in the case of a 1682 * semaphore) operation must not be performed on a member of a queue set unless 1683 * a call to xQueueSelectFromSet() has first returned a handle to that set 1684 * member. 1685 * 1686 * @param xQueueOrSemaphore The handle of the queue or semaphore being added to 1687 * the queue set (cast to an QueueSetMemberHandle_t type). 1688 * 1689 * @param xQueueSet The handle of the queue set to which the queue or semaphore 1690 * is being added. 1691 * 1692 * @return If the queue or semaphore was successfully added to the queue set 1693 * then pdPASS is returned. If the queue could not be successfully added to the 1694 * queue set because it is already a member of a different queue set then pdFAIL 1695 * is returned. 1696 */ 1697 BaseType_t xQueueAddToSet( 1698 QueueSetMemberHandle_t xQueueOrSemaphore, 1699 QueueSetHandle_t xQueueSet) PRIVILEGED_FUNCTION; 1700 1701 /* 1702 * Removes a queue or semaphore from a queue set. A queue or semaphore can only 1703 * be removed from a set if the queue or semaphore is empty. 1704 * 1705 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this 1706 * function. 1707 * 1708 * @param xQueueOrSemaphore The handle of the queue or semaphore being removed 1709 * from the queue set (cast to an QueueSetMemberHandle_t type). 1710 * 1711 * @param xQueueSet The handle of the queue set in which the queue or semaphore 1712 * is included. 1713 * 1714 * @return If the queue or semaphore was successfully removed from the queue set 1715 * then pdPASS is returned. If the queue was not in the queue set, or the 1716 * queue (or semaphore) was not empty, then pdFAIL is returned. 1717 */ 1718 BaseType_t xQueueRemoveFromSet( 1719 QueueSetMemberHandle_t xQueueOrSemaphore, 1720 QueueSetHandle_t xQueueSet) PRIVILEGED_FUNCTION; 1721 1722 /* 1723 * xQueueSelectFromSet() selects from the members of a queue set a queue or 1724 * semaphore that either contains data (in the case of a queue) or is available 1725 * to take (in the case of a semaphore). xQueueSelectFromSet() effectively 1726 * allows a task to block (pend) on a read operation on all the queues and 1727 * semaphores in a queue set simultaneously. 1728 * 1729 * See FreeRTOS/Source/Demo/Common/Minimal/QueueSet.c for an example using this 1730 * function. 1731 * 1732 * Note 1: See the documentation on http://wwwFreeRTOS.org/RTOS-queue-sets.html 1733 * for reasons why queue sets are very rarely needed in practice as there are 1734 * simpler methods of blocking on multiple objects. 1735 * 1736 * Note 2: Blocking on a queue set that contains a mutex will not cause the 1737 * mutex holder to inherit the priority of the blocked task. 1738 * 1739 * Note 3: A receive (in the case of a queue) or take (in the case of a 1740 * semaphore) operation must not be performed on a member of a queue set unless 1741 * a call to xQueueSelectFromSet() has first returned a handle to that set 1742 * member. 1743 * 1744 * @param xQueueSet The queue set on which the task will (potentially) block. 1745 * 1746 * @param xTicksToWait The maximum time, in ticks, that the calling task will 1747 * remain in the Blocked state (with other tasks executing) to wait for a member 1748 * of the queue set to be ready for a successful queue read or semaphore take 1749 * operation. 1750 * 1751 * @return xQueueSelectFromSet() will return the handle of a queue (cast to 1752 * a QueueSetMemberHandle_t type) contained in the queue set that contains data, 1753 * or the handle of a semaphore (cast to a QueueSetMemberHandle_t type) 1754 * contained in the queue set that is available, or NULL if no such queue or 1755 * semaphore exists before before the specified block time expires. 1756 */ 1757 QueueSetMemberHandle_t xQueueSelectFromSet( 1758 QueueSetHandle_t xQueueSet, 1759 const TickType_t xTicksToWait) PRIVILEGED_FUNCTION; 1760 1761 /* 1762 * A version of xQueueSelectFromSet() that can be used from an ISR. 1763 */ 1764 QueueSetMemberHandle_t xQueueSelectFromSetFromISR(QueueSetHandle_t xQueueSet) 1765 PRIVILEGED_FUNCTION; 1766 1767 /* Not public API functions. */ 1768 void vQueueWaitForMessageRestricted( 1769 QueueHandle_t xQueue, 1770 TickType_t xTicksToWait, 1771 const BaseType_t xWaitIndefinitely) PRIVILEGED_FUNCTION; 1772 BaseType_t xQueueGenericReset(QueueHandle_t xQueue, BaseType_t xNewQueue) 1773 PRIVILEGED_FUNCTION; 1774 void vQueueSetQueueNumber(QueueHandle_t xQueue, UBaseType_t uxQueueNumber) 1775 PRIVILEGED_FUNCTION; 1776 UBaseType_t uxQueueGetQueueNumber(QueueHandle_t xQueue) PRIVILEGED_FUNCTION; 1777 uint8_t ucQueueGetQueueType(QueueHandle_t xQueue) PRIVILEGED_FUNCTION; 1778 1779 #ifdef __cplusplus 1780 } 1781 #endif 1782 1783 #endif /* QUEUE_H */ 1784