2 FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
7 This file is part of the FreeRTOS distribution.
9 FreeRTOS is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License (version 2) as published by the
11 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
13 ***************************************************************************
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
15 >>! distribute a combined work that includes FreeRTOS without being !<<
16 >>! obliged to provide the source code for proprietary components !<<
17 >>! outside of the FreeRTOS kernel. !<<
18 ***************************************************************************
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
23 link: http://www.freertos.org/a00114.html
25 ***************************************************************************
27 * FreeRTOS provides completely free yet professionally developed, *
28 * robust, strictly quality controlled, supported, and cross *
29 * platform software that is more than just the market leader, it *
30 * is the industry's de facto standard. *
32 * Help yourself get started quickly while simultaneously helping *
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
34 * tutorial book, reference manual, or both: *
35 * http://www.FreeRTOS.org/Documentation *
37 ***************************************************************************
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
40 the FAQ page "My application does not run, what could be wrong?". Have you
41 defined configASSERT()?
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
44 embedded software for free we request you assist our global community by
45 participating in the support forum.
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
48 be as productive as possible as early as possible. Now you can receive
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50 Ltd, and the world's leading authority on the world's leading RTOS.
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
61 licenses offer ticketed support, indemnification and commercial middleware.
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64 engineered and independently SIL3 certified version for use in safety and
65 mission critical applications that require provable dependability.
73 #ifndef INC_FREERTOS_H
74 #error "include FreeRTOS.h" must appear in source files before "include semphr.h"
79 typedef QueueHandle_t SemaphoreHandle_t;
81 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( uint8_t ) 1U )
82 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( uint8_t ) 0U )
83 #define semGIVE_BLOCK_TIME ( ( TickType_t ) 0U )
87 * This old vSemaphoreCreateBinary() macro is now deprecated in favour of the
88 * xSemaphoreCreateBinary() function. Note that binary semaphores created using
89 * the vSemaphoreCreateBinary() macro are created in a state such that the
90 * first call to 'take' the semaphore would pass, whereas binary semaphores
91 * created using xSemaphoreCreateBinary() are created in a state such that the
92 * the semaphore must first be 'given' before it can be 'taken'.
94 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
95 * The queue length is 1 as this is a binary semaphore. The data size is 0
96 * as we don't want to actually store any data - we just want to know if the
97 * queue is empty or full.
99 * This type of semaphore can be used for pure synchronisation between tasks or
100 * between an interrupt and a task. The semaphore need not be given back once
101 * obtained, so one task/interrupt can continuously 'give' the semaphore while
102 * another continuously 'takes' the semaphore. For this reason this type of
103 * semaphore does not use a priority inheritance mechanism. For an alternative
104 * that does use priority inheritance see xSemaphoreCreateMutex().
106 * @param xSemaphore Handle to the created semaphore. Should be of type SemaphoreHandle_t.
110 * SemaphoreHandle_t xSemaphore = NULL;
112 * void vATask( void * pvParameters )
114 * // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
115 * // This is a macro so pass the variable in directly.
116 * vSemaphoreCreateBinary( xSemaphore );
118 * if( xSemaphore != NULL )
120 * // The semaphore was created successfully.
121 * // The semaphore can now be used.
125 * \ingroup Semaphores
127 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
128 #define vSemaphoreCreateBinary( xSemaphore ) \
130 ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \
131 if( ( xSemaphore ) != NULL ) \
133 ( void ) xSemaphoreGive( ( xSemaphore ) ); \
140 * Creates a new binary semaphore instance, and returns a handle by which the
141 * new semaphore can be referenced.
143 * In many usage scenarios it is faster and more memory efficient to use a
144 * direct to task notification in place of a binary semaphore!
145 * http://www.freertos.org/RTOS-task-notifications.html
147 * Internally, within the FreeRTOS implementation, binary semaphores use a block
148 * of memory, in which the semaphore structure is stored. If a binary semaphore
149 * is created using xSemaphoreCreateBinary() then the required memory is
150 * automatically dynamically allocated inside the xSemaphoreCreateBinary()
151 * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
152 * is created using xSemaphoreCreateBinaryStatic() then the application writer
153 * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
154 * binary semaphore to be created without using any dynamic memory allocation.
156 * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this
157 * xSemaphoreCreateBinary() function. Note that binary semaphores created using
158 * the vSemaphoreCreateBinary() macro are created in a state such that the
159 * first call to 'take' the semaphore would pass, whereas binary semaphores
160 * created using xSemaphoreCreateBinary() are created in a state such that the
161 * the semaphore must first be 'given' before it can be 'taken'.
163 * Function that creates a semaphore by using the existing queue mechanism.
164 * The queue length is 1 as this is a binary semaphore. The data size is 0
165 * as nothing is actually stored - all that is important is whether the queue is
166 * empty or full (the binary semaphore is available or not).
168 * This type of semaphore can be used for pure synchronisation between tasks or
169 * between an interrupt and a task. The semaphore need not be given back once
170 * obtained, so one task/interrupt can continuously 'give' the semaphore while
171 * another continuously 'takes' the semaphore. For this reason this type of
172 * semaphore does not use a priority inheritance mechanism. For an alternative
173 * that does use priority inheritance see xSemaphoreCreateMutex().
175 * @return Handle to the created semaphore.
179 * SemaphoreHandle_t xSemaphore = NULL;
181 * void vATask( void * pvParameters )
183 * // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
184 * // This is a macro so pass the variable in directly.
185 * xSemaphore = xSemaphoreCreateBinary();
187 * if( xSemaphore != NULL )
189 * // The semaphore was created successfully.
190 * // The semaphore can now be used.
194 * \ingroup Semaphores
196 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
197 #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
201 * Creates a new binary semaphore instance, and returns a handle by which the
202 * new semaphore can be referenced.
204 * NOTE: In many usage scenarios it is faster and more memory efficient to use a
205 * direct to task notification in place of a binary semaphore!
206 * http://www.freertos.org/RTOS-task-notifications.html
208 * Internally, within the FreeRTOS implementation, binary semaphores use a block
209 * of memory, in which the semaphore structure is stored. If a binary semaphore
210 * is created using xSemaphoreCreateBinary() then the required memory is
211 * automatically dynamically allocated inside the xSemaphoreCreateBinary()
212 * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
213 * is created using xSemaphoreCreateBinaryStatic() then the application writer
214 * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
215 * binary semaphore to be created without using any dynamic memory allocation.
217 * This type of semaphore can be used for pure synchronisation between tasks or
218 * between an interrupt and a task. The semaphore need not be given back once
219 * obtained, so one task/interrupt can continuously 'give' the semaphore while
220 * another continuously 'takes' the semaphore. For this reason this type of
221 * semaphore does not use a priority inheritance mechanism. For an alternative
222 * that does use priority inheritance see xSemaphoreCreateMutex().
224 * @param pxStaticSemaphore Must point to a variable of type StaticSemaphore_t,
225 * which will then be used to hold the semaphore's data structure, removing the
226 * need for the memory to be allocated dynamically.
228 * @return If the semaphore is created then a handle to the created semaphore is
229 * returned. If pxSemaphoreBuffer is NULL then NULL is returned.
233 * SemaphoreHandle_t xSemaphore = NULL;
234 * StaticSemaphore_t xSemaphoreBuffer;
236 * void vATask( void * pvParameters )
238 * // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
239 * // The semaphore's data structures will be placed in the xSemaphoreBuffer
240 * // variable, the address of which is passed into the function. The
241 * // function's parameter is not NULL, so the function will not attempt any
242 * // dynamic memory allocation, and therefore the function will not return
244 * xSemaphore = xSemaphoreCreateBinary( &xSemaphoreBuffer );
246 * // Rest of task code goes here.
249 * \ingroup Semaphores
251 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
252 #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )
253 #endif /* configSUPPORT_STATIC_ALLOCATION */
256 * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
257 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
258 * xSemaphoreCreateCounting().
260 * @param xSemaphore A handle to the semaphore being taken - obtained when
261 * the semaphore was created.
263 * @param xBlockTime The time in ticks to wait for the semaphore to become
264 * available. The macro portTICK_PERIOD_MS can be used to convert this to a
265 * real time. A block time of zero can be used to poll the semaphore. A block
266 * time of portMAX_DELAY can be used to block indefinitely (provided
267 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
269 * @return pdTRUE if the semaphore was obtained. pdFALSE
270 * if xBlockTime expired without the semaphore becoming available.
274 * SemaphoreHandle_t xSemaphore = NULL;
276 * // A task that creates a semaphore.
277 * void vATask( void * pvParameters )
279 * // Create the semaphore to guard a shared resource.
280 * vSemaphoreCreateBinary( xSemaphore );
283 * // A task that uses the semaphore.
284 * void vAnotherTask( void * pvParameters )
286 * // ... Do other things.
288 * if( xSemaphore != NULL )
290 * // See if we can obtain the semaphore. If the semaphore is not available
291 * // wait 10 ticks to see if it becomes free.
292 * if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
294 * // We were able to obtain the semaphore and can now access the
295 * // shared resource.
299 * // We have finished accessing the shared resource. Release the
301 * xSemaphoreGive( xSemaphore );
305 * // We could not obtain the semaphore and can therefore not access
306 * // the shared resource safely.
311 * \ingroup Semaphores
313 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueGenericReceive( ( QueueHandle_t ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )
316 * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
317 * The mutex must have previously been created using a call to
318 * xSemaphoreCreateRecursiveMutex();
320 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
321 * macro to be available.
323 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
325 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
326 * doesn't become available again until the owner has called
327 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
328 * if a task successfully 'takes' the same mutex 5 times then the mutex will
329 * not be available to any other task until it has also 'given' the mutex back
330 * exactly five times.
332 * @param xMutex A handle to the mutex being obtained. This is the
333 * handle returned by xSemaphoreCreateRecursiveMutex();
335 * @param xBlockTime The time in ticks to wait for the semaphore to become
336 * available. The macro portTICK_PERIOD_MS can be used to convert this to a
337 * real time. A block time of zero can be used to poll the semaphore. If
338 * the task already owns the semaphore then xSemaphoreTakeRecursive() will
339 * return immediately no matter what the value of xBlockTime.
341 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
342 * expired without the semaphore becoming available.
346 * SemaphoreHandle_t xMutex = NULL;
348 * // A task that creates a mutex.
349 * void vATask( void * pvParameters )
351 * // Create the mutex to guard a shared resource.
352 * xMutex = xSemaphoreCreateRecursiveMutex();
355 * // A task that uses the mutex.
356 * void vAnotherTask( void * pvParameters )
358 * // ... Do other things.
360 * if( xMutex != NULL )
362 * // See if we can obtain the mutex. If the mutex is not available
363 * // wait 10 ticks to see if it becomes free.
364 * if( xSemaphoreTakeRecursive( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
366 * // We were able to obtain the mutex and can now access the
367 * // shared resource.
370 * // For some reason due to the nature of the code further calls to
371 * // xSemaphoreTakeRecursive() are made on the same mutex. In real
372 * // code these would not be just sequential calls as this would make
373 * // no sense. Instead the calls are likely to be buried inside
374 * // a more complex call structure.
375 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
376 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
378 * // The mutex has now been 'taken' three times, so will not be
379 * // available to another task until it has also been given back
380 * // three times. Again it is unlikely that real code would have
381 * // these calls sequentially, but instead buried in a more complex
382 * // call structure. This is just for illustrative purposes.
383 * xSemaphoreGiveRecursive( xMutex );
384 * xSemaphoreGiveRecursive( xMutex );
385 * xSemaphoreGiveRecursive( xMutex );
387 * // Now the mutex can be taken by other tasks.
391 * // We could not obtain the mutex and can therefore not access
392 * // the shared resource safely.
397 * \ingroup Semaphores
399 #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )
403 * xSemaphoreAltTake() is an alternative version of xSemaphoreTake().
405 * The source code that implements the alternative (Alt) API is much
406 * simpler because it executes everything from within a critical section.
407 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
408 * preferred fully featured API too. The fully featured API has more
409 * complex code that takes longer to execute, but makes much less use of
410 * critical sections. Therefore the alternative API sacrifices interrupt
411 * responsiveness to gain execution speed, whereas the fully featured API
412 * sacrifices execution speed to ensure better interrupt responsiveness.
414 #define xSemaphoreAltTake( xSemaphore, xBlockTime ) xQueueAltGenericReceive( ( QueueHandle_t ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )
418 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
419 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
420 * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
422 * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
423 * an alternative which can be used from an ISR.
425 * This macro must also not be used on semaphores created using
426 * xSemaphoreCreateRecursiveMutex().
428 * @param xSemaphore A handle to the semaphore being released. This is the
429 * handle returned when the semaphore was created.
431 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
432 * Semaphores are implemented using queues. An error can occur if there is
433 * no space on the queue to post a message - indicating that the
434 * semaphore was not first obtained correctly.
438 * SemaphoreHandle_t xSemaphore = NULL;
440 * void vATask( void * pvParameters )
442 * // Create the semaphore to guard a shared resource.
443 * vSemaphoreCreateBinary( xSemaphore );
445 * if( xSemaphore != NULL )
447 * if( xSemaphoreGive( xSemaphore ) != pdTRUE )
449 * // We would expect this call to fail because we cannot give
450 * // a semaphore without first "taking" it!
453 * // Obtain the semaphore - don't block if the semaphore is not
454 * // immediately available.
455 * if( xSemaphoreTake( xSemaphore, ( TickType_t ) 0 ) )
457 * // We now have the semaphore and can access the shared resource.
461 * // We have finished accessing the shared resource so can free the
463 * if( xSemaphoreGive( xSemaphore ) != pdTRUE )
465 * // We would not expect this call to fail because we must have
466 * // obtained the semaphore to get here.
472 * \ingroup Semaphores
474 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
477 * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
478 * The mutex must have previously been created using a call to
479 * xSemaphoreCreateRecursiveMutex();
481 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
482 * macro to be available.
484 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
486 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
487 * doesn't become available again until the owner has called
488 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
489 * if a task successfully 'takes' the same mutex 5 times then the mutex will
490 * not be available to any other task until it has also 'given' the mutex back
491 * exactly five times.
493 * @param xMutex A handle to the mutex being released, or 'given'. This is the
494 * handle returned by xSemaphoreCreateMutex();
496 * @return pdTRUE if the semaphore was given.
500 * SemaphoreHandle_t xMutex = NULL;
502 * // A task that creates a mutex.
503 * void vATask( void * pvParameters )
505 * // Create the mutex to guard a shared resource.
506 * xMutex = xSemaphoreCreateRecursiveMutex();
509 * // A task that uses the mutex.
510 * void vAnotherTask( void * pvParameters )
512 * // ... Do other things.
514 * if( xMutex != NULL )
516 * // See if we can obtain the mutex. If the mutex is not available
517 * // wait 10 ticks to see if it becomes free.
518 * if( xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 ) == pdTRUE )
520 * // We were able to obtain the mutex and can now access the
521 * // shared resource.
524 * // For some reason due to the nature of the code further calls to
525 * // xSemaphoreTakeRecursive() are made on the same mutex. In real
526 * // code these would not be just sequential calls as this would make
527 * // no sense. Instead the calls are likely to be buried inside
528 * // a more complex call structure.
529 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
530 * xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
532 * // The mutex has now been 'taken' three times, so will not be
533 * // available to another task until it has also been given back
534 * // three times. Again it is unlikely that real code would have
535 * // these calls sequentially, it would be more likely that the calls
536 * // to xSemaphoreGiveRecursive() would be called as a call stack
537 * // unwound. This is just for demonstrative purposes.
538 * xSemaphoreGiveRecursive( xMutex );
539 * xSemaphoreGiveRecursive( xMutex );
540 * xSemaphoreGiveRecursive( xMutex );
542 * // Now the mutex can be taken by other tasks.
546 * // We could not obtain the mutex and can therefore not access
547 * // the shared resource safely.
552 * \ingroup Semaphores
554 #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( ( xMutex ) )
558 * xSemaphoreAltGive() is an alternative version of xSemaphoreGive().
560 * The source code that implements the alternative (Alt) API is much
561 * simpler because it executes everything from within a critical section.
562 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
563 * preferred fully featured API too. The fully featured API has more
564 * complex code that takes longer to execute, but makes much less use of
565 * critical sections. Therefore the alternative API sacrifices interrupt
566 * responsiveness to gain execution speed, whereas the fully featured API
567 * sacrifices execution speed to ensure better interrupt responsiveness.
569 #define xSemaphoreAltGive( xSemaphore ) xQueueAltGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
574 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
575 * created with a call to vSemaphoreCreateBinary() or xSemaphoreCreateCounting().
577 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
578 * must not be used with this macro.
580 * This macro can be used from an ISR.
582 * @param xSemaphore A handle to the semaphore being released. This is the
583 * handle returned when the semaphore was created.
585 * @param[out] pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set
586 * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task
587 * to unblock, and the unblocked task has a priority higher than the currently
588 * running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then
589 * a context switch should be requested before the interrupt is exited.
591 * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
595 * \#define LONG_TIME 0xffff
596 * \#define TICKS_TO_WAIT 10
597 * SemaphoreHandle_t xSemaphore = NULL;
599 * // Repetitive task.
600 * void vATask( void * pvParameters )
604 * // We want this task to run every 10 ticks of a timer. The semaphore
605 * // was created before this task was started.
607 * // Block waiting for the semaphore to become available.
608 * if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
610 * // It is time to execute.
614 * // We have finished our task. Return to the top of the loop where
615 * // we will block on the semaphore until it is time to execute
616 * // again. Note when using the semaphore for synchronisation with an
617 * // ISR in this manner there is no need to 'give' the semaphore back.
623 * void vTimerISR( void * pvParameters )
625 * static uint8_t ucLocalTickCount = 0;
626 * static BaseType_t xHigherPriorityTaskWoken;
628 * // A timer tick has occurred.
630 * // ... Do other time functions.
632 * // Is it time for vATask () to run?
633 * xHigherPriorityTaskWoken = pdFALSE;
634 * ucLocalTickCount++;
635 * if( ucLocalTickCount >= TICKS_TO_WAIT )
637 * // Unblock the task by releasing the semaphore.
638 * xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
640 * // Reset the count so we release the semaphore again in 10 ticks time.
641 * ucLocalTickCount = 0;
644 * if( xHigherPriorityTaskWoken != pdFALSE )
646 * // We can force a context switch here. Context switching from an
647 * // ISR uses port specific syntax. Check the demo task for your port
648 * // to find the syntax required.
652 * \ingroup Semaphores
654 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) )
657 * <i>Macro</i> to take a semaphore from an ISR. The semaphore must have
658 * previously been created with a call to vSemaphoreCreateBinary() or
659 * xSemaphoreCreateCounting().
661 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
662 * must not be used with this macro.
664 * This macro can be used from an ISR, however taking a semaphore from an ISR
665 * is not a common operation. It is likely to only be useful when taking a
666 * counting semaphore when an interrupt is obtaining an object from a resource
667 * pool (when the semaphore count indicates the number of resources available).
669 * @param xSemaphore A handle to the semaphore being taken. This is the
670 * handle returned when the semaphore was created.
672 * @param[out] pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set
673 * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task
674 * to unblock, and the unblocked task has a priority higher than the currently
675 * running task. If xSemaphoreTakeFromISR() sets this value to pdTRUE then
676 * a context switch should be requested before the interrupt is exited.
678 * @return pdTRUE if the semaphore was successfully taken, otherwise
681 #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueReceiveFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )
684 * <i>Macro</i> that implements a mutex semaphore by using the existing queue
687 * Internally, within the FreeRTOS implementation, mutex semaphores use a block
688 * of memory, in which the mutex structure is stored. If a mutex is created
689 * using xSemaphoreCreateMutex() then the required memory is automatically
690 * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
691 * http://www.freertos.org/a00111.html). If a mutex is created using
692 * xSemaphoreCreateMutexStatic() then the application writer must provided the
693 * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
694 * without using any dynamic memory allocation.
696 * Mutexes created using this function can be accessed using the xSemaphoreTake()
697 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
698 * xSemaphoreGiveRecursive() macros must not be used.
700 * This type of semaphore uses a priority inheritance mechanism so a task
701 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
702 * semaphore it is no longer required.
704 * Mutex type semaphores cannot be used from within interrupt service routines.
706 * See vSemaphoreCreateBinary() for an alternative implementation that can be
707 * used for pure synchronisation (where one task or interrupt always 'gives' the
708 * semaphore and another always 'takes' the semaphore) and from within interrupt
711 * @return If the mutex was successfully created then a handle to the created
712 * semaphore is returned. If there was not enough heap to allocate the mutex
713 * data structures then NULL is returned.
717 * SemaphoreHandle_t xSemaphore;
719 * void vATask( void * pvParameters )
721 * // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
722 * // This is a macro so pass the variable in directly.
723 * xSemaphore = xSemaphoreCreateMutex();
725 * if( xSemaphore != NULL )
727 * // The semaphore was created successfully.
728 * // The semaphore can now be used.
732 * \ingroup Semaphores
734 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
735 #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
739 * Creates a new mutex type semaphore instance, and returns a handle by which
740 * the new mutex can be referenced.
742 * Internally, within the FreeRTOS implementation, mutex semaphores use a block
743 * of memory, in which the mutex structure is stored. If a mutex is created
744 * using xSemaphoreCreateMutex() then the required memory is automatically
745 * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
746 * http://www.freertos.org/a00111.html). If a mutex is created using
747 * xSemaphoreCreateMutexStatic() then the application writer must provided the
748 * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
749 * without using any dynamic memory allocation.
751 * Mutexes created using this function can be accessed using the xSemaphoreTake()
752 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
753 * xSemaphoreGiveRecursive() macros must not be used.
755 * This type of semaphore uses a priority inheritance mechanism so a task
756 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
757 * semaphore it is no longer required.
759 * Mutex type semaphores cannot be used from within interrupt service routines.
761 * See xSemaphoreCreateBinary() for an alternative implementation that can be
762 * used for pure synchronisation (where one task or interrupt always 'gives' the
763 * semaphore and another always 'takes' the semaphore) and from within interrupt
766 * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
767 * which will be used to hold the mutex's data structure, removing the need for
768 * the memory to be allocated dynamically.
770 * @return If the mutex was successfully created then a handle to the created
771 * mutex is returned. If pxMutexBuffer was NULL then NULL is returned.
775 * SemaphoreHandle_t xSemaphore;
776 * StaticSemaphore_t xMutexBuffer;
778 * void vATask( void * pvParameters )
780 * // A mutex cannot be used before it has been created. xMutexBuffer is
781 * // into xSemaphoreCreateMutexStatic() so no dynamic memory allocation is
783 * xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );
785 * // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
786 * // so there is no need to check it.
789 * \ingroup Semaphores
791 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
792 #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )
793 #endif /* configSUPPORT_STATIC_ALLOCATION */
797 * Creates a new recursive mutex type semaphore instance, and returns a handle
798 * by which the new recursive mutex can be referenced.
800 * Internally, within the FreeRTOS implementation, recursive mutexs use a block
801 * of memory, in which the mutex structure is stored. If a recursive mutex is
802 * created using xSemaphoreCreateRecursiveMutex() then the required memory is
803 * automatically dynamically allocated inside the
804 * xSemaphoreCreateRecursiveMutex() function. (see
805 * http://www.freertos.org/a00111.html). If a recursive mutex is created using
806 * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
807 * provide the memory that will get used by the mutex.
808 * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
809 * be created without using any dynamic memory allocation.
811 * Mutexes created using this macro can be accessed using the
812 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
813 * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
815 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
816 * doesn't become available again until the owner has called
817 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
818 * if a task successfully 'takes' the same mutex 5 times then the mutex will
819 * not be available to any other task until it has also 'given' the mutex back
820 * exactly five times.
822 * This type of semaphore uses a priority inheritance mechanism so a task
823 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
824 * semaphore it is no longer required.
826 * Mutex type semaphores cannot be used from within interrupt service routines.
828 * See vSemaphoreCreateBinary() for an alternative implementation that can be
829 * used for pure synchronisation (where one task or interrupt always 'gives' the
830 * semaphore and another always 'takes' the semaphore) and from within interrupt
833 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
838 * SemaphoreHandle_t xSemaphore;
840 * void vATask( void * pvParameters )
842 * // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
843 * // This is a macro so pass the variable in directly.
844 * xSemaphore = xSemaphoreCreateRecursiveMutex();
846 * if( xSemaphore != NULL )
848 * // The semaphore was created successfully.
849 * // The semaphore can now be used.
853 * \ingroup Semaphores
855 #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
856 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
860 * Creates a new recursive mutex type semaphore instance, and returns a handle
861 * by which the new recursive mutex can be referenced.
863 * Internally, within the FreeRTOS implementation, recursive mutexs use a block
864 * of memory, in which the mutex structure is stored. If a recursive mutex is
865 * created using xSemaphoreCreateRecursiveMutex() then the required memory is
866 * automatically dynamically allocated inside the
867 * xSemaphoreCreateRecursiveMutex() function. (see
868 * http://www.freertos.org/a00111.html). If a recursive mutex is created using
869 * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
870 * provide the memory that will get used by the mutex.
871 * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
872 * be created without using any dynamic memory allocation.
874 * Mutexes created using this macro can be accessed using the
875 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
876 * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
878 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
879 * doesn't become available again until the owner has called
880 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
881 * if a task successfully 'takes' the same mutex 5 times then the mutex will
882 * not be available to any other task until it has also 'given' the mutex back
883 * exactly five times.
885 * This type of semaphore uses a priority inheritance mechanism so a task
886 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
887 * semaphore it is no longer required.
889 * Mutex type semaphores cannot be used from within interrupt service routines.
891 * See xSemaphoreCreateBinary() for an alternative implementation that can be
892 * used for pure synchronisation (where one task or interrupt always 'gives' the
893 * semaphore and another always 'takes' the semaphore) and from within interrupt
896 * @param pxStaticSemaphore Must point to a variable of type StaticSemaphore_t,
897 * which will then be used to hold the recursive mutex's data structure,
898 * removing the need for the memory to be allocated dynamically.
900 * @return If the recursive mutex was successfully created then a handle to the
901 * created recursive mutex is returned. If pxMutexBuffer was NULL then NULL is
906 * SemaphoreHandle_t xSemaphore;
907 * StaticSemaphore_t xMutexBuffer;
909 * void vATask( void * pvParameters )
911 * // A recursive semaphore cannot be used before it is created. Here a
912 * // recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic().
913 * // The address of xMutexBuffer is passed into the function, and will hold
914 * // the mutexes data structures - so no dynamic memory allocation will be
916 * xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xMutexBuffer );
918 * // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
919 * // so there is no need to check it.
922 * \ingroup Semaphores
924 #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
925 #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )
926 #endif /* configSUPPORT_STATIC_ALLOCATION */
929 * Creates a new counting semaphore instance, and returns a handle by which the
930 * new counting semaphore can be referenced.
932 * In many usage scenarios it is faster and more memory efficient to use a
933 * direct to task notification in place of a counting semaphore!
934 * http://www.freertos.org/RTOS-task-notifications.html
936 * Internally, within the FreeRTOS implementation, counting semaphores use a
937 * block of memory, in which the counting semaphore structure is stored. If a
938 * counting semaphore is created using xSemaphoreCreateCounting() then the
939 * required memory is automatically dynamically allocated inside the
940 * xSemaphoreCreateCounting() function. (see
941 * http://www.freertos.org/a00111.html). If a counting semaphore is created
942 * using xSemaphoreCreateCountingStatic() then the application writer can
943 * instead optionally provide the memory that will get used by the counting
944 * semaphore. xSemaphoreCreateCountingStatic() therefore allows a counting
945 * semaphore to be created without using any dynamic memory allocation.
947 * Counting semaphores are typically used for two things:
949 * 1) Counting events.
951 * In this usage scenario an event handler will 'give' a semaphore each time
952 * an event occurs (incrementing the semaphore count value), and a handler
953 * task will 'take' a semaphore each time it processes an event
954 * (decrementing the semaphore count value). The count value is therefore
955 * the difference between the number of events that have occurred and the
956 * number that have been processed. In this case it is desirable for the
957 * initial count value to be zero.
959 * 2) Resource management.
961 * In this usage scenario the count value indicates the number of resources
962 * available. To obtain control of a resource a task must first obtain a
963 * semaphore - decrementing the semaphore count value. When the count value
964 * reaches zero there are no free resources. When a task finishes with the
965 * resource it 'gives' the semaphore back - incrementing the semaphore count
966 * value. In this case it is desirable for the initial count value to be
967 * equal to the maximum count value, indicating that all resources are free.
969 * @param uxMaxCount The maximum count value that can be reached. When the
970 * semaphore reaches this value it can no longer be 'given'.
972 * @param uxInitialCount The count value assigned to the semaphore when it is
975 * @return Handle to the created semaphore. Null if the semaphore could not be
980 * SemaphoreHandle_t xSemaphore;
982 * void vATask( void * pvParameters )
984 * SemaphoreHandle_t xSemaphore = NULL;
986 * // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
987 * // The max value to which the semaphore can count should be 10, and the
988 * // initial value assigned to the count should be 0.
989 * xSemaphore = xSemaphoreCreateCounting( 10, 0 );
991 * if( xSemaphore != NULL )
993 * // The semaphore was created successfully.
994 * // The semaphore can now be used.
998 * \ingroup Semaphores
1000 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1001 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
1005 * Creates a new counting semaphore instance, and returns a handle by which the
1006 * new counting semaphore can be referenced.
1008 * In many usage scenarios it is faster and more memory efficient to use a
1009 * direct to task notification in place of a counting semaphore!
1010 * http://www.freertos.org/RTOS-task-notifications.html
1012 * Internally, within the FreeRTOS implementation, counting semaphores use a
1013 * block of memory, in which the counting semaphore structure is stored. If a
1014 * counting semaphore is created using xSemaphoreCreateCounting() then the
1015 * required memory is automatically dynamically allocated inside the
1016 * xSemaphoreCreateCounting() function. (see
1017 * http://www.freertos.org/a00111.html). If a counting semaphore is created
1018 * using xSemaphoreCreateCountingStatic() then the application writer must
1019 * provide the memory. xSemaphoreCreateCountingStatic() therefore allows a
1020 * counting semaphore to be created without using any dynamic memory allocation.
1022 * Counting semaphores are typically used for two things:
1024 * 1) Counting events.
1026 * In this usage scenario an event handler will 'give' a semaphore each time
1027 * an event occurs (incrementing the semaphore count value), and a handler
1028 * task will 'take' a semaphore each time it processes an event
1029 * (decrementing the semaphore count value). The count value is therefore
1030 * the difference between the number of events that have occurred and the
1031 * number that have been processed. In this case it is desirable for the
1032 * initial count value to be zero.
1034 * 2) Resource management.
1036 * In this usage scenario the count value indicates the number of resources
1037 * available. To obtain control of a resource a task must first obtain a
1038 * semaphore - decrementing the semaphore count value. When the count value
1039 * reaches zero there are no free resources. When a task finishes with the
1040 * resource it 'gives' the semaphore back - incrementing the semaphore count
1041 * value. In this case it is desirable for the initial count value to be
1042 * equal to the maximum count value, indicating that all resources are free.
1044 * @param uxMaxCount The maximum count value that can be reached. When the
1045 * semaphore reaches this value it can no longer be 'given'.
1047 * @param uxInitialCount The count value assigned to the semaphore when it is
1050 * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
1051 * which will then be used to hold the semaphore's data structure, removing the
1052 * need for the memory to be allocated dynamically.
1054 * @return If the counting semaphore was successfully created then a handle to
1055 * the created counting semaphore is returned. If pxSemaphoreBuffer was NULL
1056 * then NULL is returned.
1060 * SemaphoreHandle_t xSemaphore;
1061 * StaticSemaphore_t xSemaphoreBuffer;
1063 * void vATask( void * pvParameters )
1065 * SemaphoreHandle_t xSemaphore = NULL;
1067 * // Counting semaphore cannot be used before they have been created. Create
1068 * // a counting semaphore using xSemaphoreCreateCountingStatic(). The max
1069 * // value to which the semaphore can count is 10, and the initial value
1070 * // assigned to the count will be 0. The address of xSemaphoreBuffer is
1071 * // passed in and will be used to hold the semaphore structure, so no dynamic
1072 * // memory allocation will be used.
1073 * xSemaphore = xSemaphoreCreateCounting( 10, 0, &xSemaphoreBuffer );
1075 * // No memory allocation was attempted so xSemaphore cannot be NULL, so there
1076 * // is no need to check its value.
1079 * \ingroup Semaphores
1081 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
1082 #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )
1083 #endif /* configSUPPORT_STATIC_ALLOCATION */
1086 * Delete a semaphore. This function must be used with care. For example,
1087 * do not delete a mutex type semaphore if the mutex is held by a task.
1089 * @param xSemaphore A handle to the semaphore to be deleted.
1091 * \ingroup Semaphores
1093 #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )
1096 * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
1097 * If xMutex is not a mutex type semaphore, or the mutex is available (not held
1098 * by a task), return NULL.
1100 * Note: This is a good way of determining if the calling task is the mutex
1101 * holder, but not a good way of determining the identity of the mutex holder as
1102 * the holder may change between the function exiting and the returned value
1105 #define xSemaphoreGetMutexHolder( xSemaphore ) xQueueGetMutexHolder( ( xSemaphore ) )
1108 * If the semaphore is a counting semaphore then uxSemaphoreGetCount() returns
1109 * its current count value. If the semaphore is a binary semaphore then
1110 * uxSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the
1111 * semaphore is not available.
1114 #define uxSemaphoreGetCount( xSemaphore ) uxQueueMessagesWaiting( ( QueueHandle_t ) ( xSemaphore ) )
1116 #endif /* SEMAPHORE_H */