2 FreeRTOS V7.4.2 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
78 #ifndef INC_FREERTOS_H
\r
79 #error "include FreeRTOS.h" must appear in source files before "include semphr.h"
\r
84 typedef xQueueHandle xSemaphoreHandle;
\r
86 #define semBINARY_SEMAPHORE_QUEUE_LENGTH ( ( unsigned char ) 1U )
\r
87 #define semSEMAPHORE_QUEUE_ITEM_LENGTH ( ( unsigned char ) 0U )
\r
88 #define semGIVE_BLOCK_TIME ( ( portTickType ) 0U )
\r
93 * <pre>vSemaphoreCreateBinary( xSemaphoreHandle xSemaphore )</pre>
\r
95 * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
\r
96 * The queue length is 1 as this is a binary semaphore. The data size is 0
\r
97 * as we don't want to actually store any data - we just want to know if the
\r
98 * queue is empty or full.
\r
100 * This type of semaphore can be used for pure synchronisation between tasks or
\r
101 * between an interrupt and a task. The semaphore need not be given back once
\r
102 * obtained, so one task/interrupt can continuously 'give' the semaphore while
\r
103 * another continuously 'takes' the semaphore. For this reason this type of
\r
104 * semaphore does not use a priority inheritance mechanism. For an alternative
\r
105 * that does use priority inheritance see xSemaphoreCreateMutex().
\r
107 * @param xSemaphore Handle to the created semaphore. Should be of type xSemaphoreHandle.
\r
111 xSemaphoreHandle xSemaphore;
\r
113 void vATask( void * pvParameters )
\r
115 // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
\r
116 // This is a macro so pass the variable in directly.
\r
117 vSemaphoreCreateBinary( xSemaphore );
\r
119 if( xSemaphore != NULL )
\r
121 // The semaphore was created successfully.
\r
122 // The semaphore can now be used.
\r
126 * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
\r
127 * \ingroup Semaphores
\r
129 #define vSemaphoreCreateBinary( xSemaphore ) \
\r
131 ( xSemaphore ) = xQueueGenericCreate( ( unsigned portBASE_TYPE ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \
\r
132 if( ( xSemaphore ) != NULL ) \
\r
134 xSemaphoreGive( ( xSemaphore ) ); \
\r
140 * <pre>xSemaphoreTake(
\r
141 * xSemaphoreHandle xSemaphore,
\r
142 * portTickType xBlockTime
\r
145 * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
\r
146 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
\r
147 * xSemaphoreCreateCounting().
\r
149 * @param xSemaphore A handle to the semaphore being taken - obtained when
\r
150 * the semaphore was created.
\r
152 * @param xBlockTime The time in ticks to wait for the semaphore to become
\r
153 * available. The macro portTICK_RATE_MS can be used to convert this to a
\r
154 * real time. A block time of zero can be used to poll the semaphore. A block
\r
155 * time of portMAX_DELAY can be used to block indefinitely (provided
\r
156 * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
\r
158 * @return pdTRUE if the semaphore was obtained. pdFALSE
\r
159 * if xBlockTime expired without the semaphore becoming available.
\r
163 xSemaphoreHandle xSemaphore = NULL;
\r
165 // A task that creates a semaphore.
\r
166 void vATask( void * pvParameters )
\r
168 // Create the semaphore to guard a shared resource.
\r
169 vSemaphoreCreateBinary( xSemaphore );
\r
172 // A task that uses the semaphore.
\r
173 void vAnotherTask( void * pvParameters )
\r
175 // ... Do other things.
\r
177 if( xSemaphore != NULL )
\r
179 // See if we can obtain the semaphore. If the semaphore is not available
\r
180 // wait 10 ticks to see if it becomes free.
\r
181 if( xSemaphoreTake( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
183 // We were able to obtain the semaphore and can now access the
\r
184 // shared resource.
\r
188 // We have finished accessing the shared resource. Release the
\r
190 xSemaphoreGive( xSemaphore );
\r
194 // We could not obtain the semaphore and can therefore not access
\r
195 // the shared resource safely.
\r
200 * \defgroup xSemaphoreTake xSemaphoreTake
\r
201 * \ingroup Semaphores
\r
203 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueGenericReceive( ( xQueueHandle ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )
\r
207 * xSemaphoreTakeRecursive(
\r
208 * xSemaphoreHandle xMutex,
\r
209 * portTickType xBlockTime
\r
212 * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
\r
213 * The mutex must have previously been created using a call to
\r
214 * xSemaphoreCreateRecursiveMutex();
\r
216 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
\r
217 * macro to be available.
\r
219 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
\r
221 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
222 * doesn't become available again until the owner has called
\r
223 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
224 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
225 * not be available to any other task until it has also 'given' the mutex back
\r
226 * exactly five times.
\r
228 * @param xMutex A handle to the mutex being obtained. This is the
\r
229 * handle returned by xSemaphoreCreateRecursiveMutex();
\r
231 * @param xBlockTime The time in ticks to wait for the semaphore to become
\r
232 * available. The macro portTICK_RATE_MS can be used to convert this to a
\r
233 * real time. A block time of zero can be used to poll the semaphore. If
\r
234 * the task already owns the semaphore then xSemaphoreTakeRecursive() will
\r
235 * return immediately no matter what the value of xBlockTime.
\r
237 * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
\r
238 * expired without the semaphore becoming available.
\r
242 xSemaphoreHandle xMutex = NULL;
\r
244 // A task that creates a mutex.
\r
245 void vATask( void * pvParameters )
\r
247 // Create the mutex to guard a shared resource.
\r
248 xMutex = xSemaphoreCreateRecursiveMutex();
\r
251 // A task that uses the mutex.
\r
252 void vAnotherTask( void * pvParameters )
\r
254 // ... Do other things.
\r
256 if( xMutex != NULL )
\r
258 // See if we can obtain the mutex. If the mutex is not available
\r
259 // wait 10 ticks to see if it becomes free.
\r
260 if( xSemaphoreTakeRecursive( xSemaphore, ( portTickType ) 10 ) == pdTRUE )
\r
262 // We were able to obtain the mutex and can now access the
\r
263 // shared resource.
\r
266 // For some reason due to the nature of the code further calls to
\r
267 // xSemaphoreTakeRecursive() are made on the same mutex. In real
\r
268 // code these would not be just sequential calls as this would make
\r
269 // no sense. Instead the calls are likely to be buried inside
\r
270 // a more complex call structure.
\r
271 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
272 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
274 // The mutex has now been 'taken' three times, so will not be
\r
275 // available to another task until it has also been given back
\r
276 // three times. Again it is unlikely that real code would have
\r
277 // these calls sequentially, but instead buried in a more complex
\r
278 // call structure. This is just for illustrative purposes.
\r
279 xSemaphoreGiveRecursive( xMutex );
\r
280 xSemaphoreGiveRecursive( xMutex );
\r
281 xSemaphoreGiveRecursive( xMutex );
\r
283 // Now the mutex can be taken by other tasks.
\r
287 // We could not obtain the mutex and can therefore not access
\r
288 // the shared resource safely.
\r
293 * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive
\r
294 * \ingroup Semaphores
\r
296 #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )
\r
300 * xSemaphoreAltTake() is an alternative version of xSemaphoreTake().
\r
302 * The source code that implements the alternative (Alt) API is much
\r
303 * simpler because it executes everything from within a critical section.
\r
304 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
305 * preferred fully featured API too. The fully featured API has more
\r
306 * complex code that takes longer to execute, but makes much less use of
\r
307 * critical sections. Therefore the alternative API sacrifices interrupt
\r
308 * responsiveness to gain execution speed, whereas the fully featured API
\r
309 * sacrifices execution speed to ensure better interrupt responsiveness.
\r
311 #define xSemaphoreAltTake( xSemaphore, xBlockTime ) xQueueAltGenericReceive( ( xQueueHandle ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )
\r
315 * <pre>xSemaphoreGive( xSemaphoreHandle xSemaphore )</pre>
\r
317 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
\r
318 * created with a call to vSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
\r
319 * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
\r
321 * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
\r
322 * an alternative which can be used from an ISR.
\r
324 * This macro must also not be used on semaphores created using
\r
325 * xSemaphoreCreateRecursiveMutex().
\r
327 * @param xSemaphore A handle to the semaphore being released. This is the
\r
328 * handle returned when the semaphore was created.
\r
330 * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
\r
331 * Semaphores are implemented using queues. An error can occur if there is
\r
332 * no space on the queue to post a message - indicating that the
\r
333 * semaphore was not first obtained correctly.
\r
337 xSemaphoreHandle xSemaphore = NULL;
\r
339 void vATask( void * pvParameters )
\r
341 // Create the semaphore to guard a shared resource.
\r
342 vSemaphoreCreateBinary( xSemaphore );
\r
344 if( xSemaphore != NULL )
\r
346 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
348 // We would expect this call to fail because we cannot give
\r
349 // a semaphore without first "taking" it!
\r
352 // Obtain the semaphore - don't block if the semaphore is not
\r
353 // immediately available.
\r
354 if( xSemaphoreTake( xSemaphore, ( portTickType ) 0 ) )
\r
356 // We now have the semaphore and can access the shared resource.
\r
360 // We have finished accessing the shared resource so can free the
\r
362 if( xSemaphoreGive( xSemaphore ) != pdTRUE )
\r
364 // We would not expect this call to fail because we must have
\r
365 // obtained the semaphore to get here.
\r
371 * \defgroup xSemaphoreGive xSemaphoreGive
\r
372 * \ingroup Semaphores
\r
374 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( xQueueHandle ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
378 * <pre>xSemaphoreGiveRecursive( xSemaphoreHandle xMutex )</pre>
\r
380 * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
\r
381 * The mutex must have previously been created using a call to
\r
382 * xSemaphoreCreateRecursiveMutex();
\r
384 * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
\r
385 * macro to be available.
\r
387 * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
\r
389 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
390 * doesn't become available again until the owner has called
\r
391 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
392 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
393 * not be available to any other task until it has also 'given' the mutex back
\r
394 * exactly five times.
\r
396 * @param xMutex A handle to the mutex being released, or 'given'. This is the
\r
397 * handle returned by xSemaphoreCreateMutex();
\r
399 * @return pdTRUE if the semaphore was given.
\r
403 xSemaphoreHandle xMutex = NULL;
\r
405 // A task that creates a mutex.
\r
406 void vATask( void * pvParameters )
\r
408 // Create the mutex to guard a shared resource.
\r
409 xMutex = xSemaphoreCreateRecursiveMutex();
\r
412 // A task that uses the mutex.
\r
413 void vAnotherTask( void * pvParameters )
\r
415 // ... Do other things.
\r
417 if( xMutex != NULL )
\r
419 // See if we can obtain the mutex. If the mutex is not available
\r
420 // wait 10 ticks to see if it becomes free.
\r
421 if( xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 ) == pdTRUE )
\r
423 // We were able to obtain the mutex and can now access the
\r
424 // shared resource.
\r
427 // For some reason due to the nature of the code further calls to
\r
428 // xSemaphoreTakeRecursive() are made on the same mutex. In real
\r
429 // code these would not be just sequential calls as this would make
\r
430 // no sense. Instead the calls are likely to be buried inside
\r
431 // a more complex call structure.
\r
432 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
433 xSemaphoreTakeRecursive( xMutex, ( portTickType ) 10 );
\r
435 // The mutex has now been 'taken' three times, so will not be
\r
436 // available to another task until it has also been given back
\r
437 // three times. Again it is unlikely that real code would have
\r
438 // these calls sequentially, it would be more likely that the calls
\r
439 // to xSemaphoreGiveRecursive() would be called as a call stack
\r
440 // unwound. This is just for demonstrative purposes.
\r
441 xSemaphoreGiveRecursive( xMutex );
\r
442 xSemaphoreGiveRecursive( xMutex );
\r
443 xSemaphoreGiveRecursive( xMutex );
\r
445 // Now the mutex can be taken by other tasks.
\r
449 // We could not obtain the mutex and can therefore not access
\r
450 // the shared resource safely.
\r
455 * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive
\r
456 * \ingroup Semaphores
\r
458 #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( ( xMutex ) )
\r
461 * xSemaphoreAltGive() is an alternative version of xSemaphoreGive().
\r
463 * The source code that implements the alternative (Alt) API is much
\r
464 * simpler because it executes everything from within a critical section.
\r
465 * This is the approach taken by many other RTOSes, but FreeRTOS.org has the
\r
466 * preferred fully featured API too. The fully featured API has more
\r
467 * complex code that takes longer to execute, but makes much less use of
\r
468 * critical sections. Therefore the alternative API sacrifices interrupt
\r
469 * responsiveness to gain execution speed, whereas the fully featured API
\r
470 * sacrifices execution speed to ensure better interrupt responsiveness.
\r
472 #define xSemaphoreAltGive( xSemaphore ) xQueueAltGenericSend( ( xQueueHandle ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
\r
477 xSemaphoreGiveFromISR(
\r
478 xSemaphoreHandle xSemaphore,
\r
479 signed portBASE_TYPE *pxHigherPriorityTaskWoken
\r
482 * <i>Macro</i> to release a semaphore. The semaphore must have previously been
\r
483 * created with a call to vSemaphoreCreateBinary() or xSemaphoreCreateCounting().
\r
485 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
\r
486 * must not be used with this macro.
\r
488 * This macro can be used from an ISR.
\r
490 * @param xSemaphore A handle to the semaphore being released. This is the
\r
491 * handle returned when the semaphore was created.
\r
493 * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set
\r
494 * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task
\r
495 * to unblock, and the unblocked task has a priority higher than the currently
\r
496 * running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then
\r
497 * a context switch should be requested before the interrupt is exited.
\r
499 * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
\r
503 \#define LONG_TIME 0xffff
\r
504 \#define TICKS_TO_WAIT 10
\r
505 xSemaphoreHandle xSemaphore = NULL;
\r
507 // Repetitive task.
\r
508 void vATask( void * pvParameters )
\r
512 // We want this task to run every 10 ticks of a timer. The semaphore
\r
513 // was created before this task was started.
\r
515 // Block waiting for the semaphore to become available.
\r
516 if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
\r
518 // It is time to execute.
\r
522 // We have finished our task. Return to the top of the loop where
\r
523 // we will block on the semaphore until it is time to execute
\r
524 // again. Note when using the semaphore for synchronisation with an
\r
525 // ISR in this manner there is no need to 'give' the semaphore back.
\r
531 void vTimerISR( void * pvParameters )
\r
533 static unsigned char ucLocalTickCount = 0;
\r
534 static signed portBASE_TYPE xHigherPriorityTaskWoken;
\r
536 // A timer tick has occurred.
\r
538 // ... Do other time functions.
\r
540 // Is it time for vATask () to run?
\r
541 xHigherPriorityTaskWoken = pdFALSE;
\r
542 ucLocalTickCount++;
\r
543 if( ucLocalTickCount >= TICKS_TO_WAIT )
\r
545 // Unblock the task by releasing the semaphore.
\r
546 xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
\r
548 // Reset the count so we release the semaphore again in 10 ticks time.
\r
549 ucLocalTickCount = 0;
\r
552 if( xHigherPriorityTaskWoken != pdFALSE )
\r
554 // We can force a context switch here. Context switching from an
\r
555 // ISR uses port specific syntax. Check the demo task for your port
\r
556 // to find the syntax required.
\r
560 * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
\r
561 * \ingroup Semaphores
\r
563 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGenericSendFromISR( ( xQueueHandle ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ), queueSEND_TO_BACK )
\r
568 xSemaphoreTakeFromISR(
\r
569 xSemaphoreHandle xSemaphore,
\r
570 signed portBASE_TYPE *pxHigherPriorityTaskWoken
\r
573 * <i>Macro</i> to take a semaphore from an ISR. The semaphore must have
\r
574 * previously been created with a call to vSemaphoreCreateBinary() or
\r
575 * xSemaphoreCreateCounting().
\r
577 * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
\r
578 * must not be used with this macro.
\r
580 * This macro can be used from an ISR, however taking a semaphore from an ISR
\r
581 * is not a common operation. It is likely to only be useful when taking a
\r
582 * counting semaphore when an interrupt is obtaining an object from a resource
\r
583 * pool (when the semaphore count indicates the number of resources available).
\r
585 * @param xSemaphore A handle to the semaphore being taken. This is the
\r
586 * handle returned when the semaphore was created.
\r
588 * @param pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set
\r
589 * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task
\r
590 * to unblock, and the unblocked task has a priority higher than the currently
\r
591 * running task. If xSemaphoreTakeFromISR() sets this value to pdTRUE then
\r
592 * a context switch should be requested before the interrupt is exited.
\r
594 * @return pdTRUE if the semaphore was successfully taken, otherwise
\r
597 #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueReceiveFromISR( ( xQueueHandle ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )
\r
601 * <pre>xSemaphoreHandle xSemaphoreCreateMutex( void )</pre>
\r
603 * <i>Macro</i> that implements a mutex semaphore by using the existing queue
\r
606 * Mutexes created using this macro can be accessed using the xSemaphoreTake()
\r
607 * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
\r
608 * xSemaphoreGiveRecursive() macros should not be used.
\r
610 * This type of semaphore uses a priority inheritance mechanism so a task
\r
611 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
\r
612 * semaphore it is no longer required.
\r
614 * Mutex type semaphores cannot be used from within interrupt service routines.
\r
616 * See vSemaphoreCreateBinary() for an alternative implementation that can be
\r
617 * used for pure synchronisation (where one task or interrupt always 'gives' the
\r
618 * semaphore and another always 'takes' the semaphore) and from within interrupt
\r
619 * service routines.
\r
621 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
\r
622 * xSemaphoreHandle.
\r
626 xSemaphoreHandle xSemaphore;
\r
628 void vATask( void * pvParameters )
\r
630 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
\r
631 // This is a macro so pass the variable in directly.
\r
632 xSemaphore = xSemaphoreCreateMutex();
\r
634 if( xSemaphore != NULL )
\r
636 // The semaphore was created successfully.
\r
637 // The semaphore can now be used.
\r
641 * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
\r
642 * \ingroup Semaphores
\r
644 #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
\r
649 * <pre>xSemaphoreHandle xSemaphoreCreateRecursiveMutex( void )</pre>
\r
651 * <i>Macro</i> that implements a recursive mutex by using the existing queue
\r
654 * Mutexes created using this macro can be accessed using the
\r
655 * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
\r
656 * xSemaphoreTake() and xSemaphoreGive() macros should not be used.
\r
658 * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
\r
659 * doesn't become available again until the owner has called
\r
660 * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
\r
661 * if a task successfully 'takes' the same mutex 5 times then the mutex will
\r
662 * not be available to any other task until it has also 'given' the mutex back
\r
663 * exactly five times.
\r
665 * This type of semaphore uses a priority inheritance mechanism so a task
\r
666 * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
\r
667 * semaphore it is no longer required.
\r
669 * Mutex type semaphores cannot be used from within interrupt service routines.
\r
671 * See vSemaphoreCreateBinary() for an alternative implementation that can be
\r
672 * used for pure synchronisation (where one task or interrupt always 'gives' the
\r
673 * semaphore and another always 'takes' the semaphore) and from within interrupt
\r
674 * service routines.
\r
676 * @return xSemaphore Handle to the created mutex semaphore. Should be of type
\r
677 * xSemaphoreHandle.
\r
681 xSemaphoreHandle xSemaphore;
\r
683 void vATask( void * pvParameters )
\r
685 // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
\r
686 // This is a macro so pass the variable in directly.
\r
687 xSemaphore = xSemaphoreCreateRecursiveMutex();
\r
689 if( xSemaphore != NULL )
\r
691 // The semaphore was created successfully.
\r
692 // The semaphore can now be used.
\r
696 * \defgroup vSemaphoreCreateMutex vSemaphoreCreateMutex
\r
697 * \ingroup Semaphores
\r
699 #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
\r
703 * <pre>xSemaphoreHandle xSemaphoreCreateCounting( unsigned portBASE_TYPE uxMaxCount, unsigned portBASE_TYPE uxInitialCount )</pre>
\r
705 * <i>Macro</i> that creates a counting semaphore by using the existing
\r
706 * queue mechanism.
\r
708 * Counting semaphores are typically used for two things:
\r
710 * 1) Counting events.
\r
712 * In this usage scenario an event handler will 'give' a semaphore each time
\r
713 * an event occurs (incrementing the semaphore count value), and a handler
\r
714 * task will 'take' a semaphore each time it processes an event
\r
715 * (decrementing the semaphore count value). The count value is therefore
\r
716 * the difference between the number of events that have occurred and the
\r
717 * number that have been processed. In this case it is desirable for the
\r
718 * initial count value to be zero.
\r
720 * 2) Resource management.
\r
722 * In this usage scenario the count value indicates the number of resources
\r
723 * available. To obtain control of a resource a task must first obtain a
\r
724 * semaphore - decrementing the semaphore count value. When the count value
\r
725 * reaches zero there are no free resources. When a task finishes with the
\r
726 * resource it 'gives' the semaphore back - incrementing the semaphore count
\r
727 * value. In this case it is desirable for the initial count value to be
\r
728 * equal to the maximum count value, indicating that all resources are free.
\r
730 * @param uxMaxCount The maximum count value that can be reached. When the
\r
731 * semaphore reaches this value it can no longer be 'given'.
\r
733 * @param uxInitialCount The count value assigned to the semaphore when it is
\r
736 * @return Handle to the created semaphore. Null if the semaphore could not be
\r
741 xSemaphoreHandle xSemaphore;
\r
743 void vATask( void * pvParameters )
\r
745 xSemaphoreHandle xSemaphore = NULL;
\r
747 // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
\r
748 // The max value to which the semaphore can count should be 10, and the
\r
749 // initial value assigned to the count should be 0.
\r
750 xSemaphore = xSemaphoreCreateCounting( 10, 0 );
\r
752 if( xSemaphore != NULL )
\r
754 // The semaphore was created successfully.
\r
755 // The semaphore can now be used.
\r
759 * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
\r
760 * \ingroup Semaphores
\r
762 #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
\r
766 * <pre>void vSemaphoreDelete( xSemaphoreHandle xSemaphore );</pre>
\r
768 * Delete a semaphore. This function must be used with care. For example,
\r
769 * do not delete a mutex type semaphore if the mutex is held by a task.
\r
771 * @param xSemaphore A handle to the semaphore to be deleted.
\r
773 * \defgroup vSemaphoreDelete vSemaphoreDelete
\r
774 * \ingroup Semaphores
\r
776 #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( xQueueHandle ) ( xSemaphore ) )
\r
780 * <pre>xTaskHandle xSemaphoreGetMutexHolder( xSemaphoreHandle xMutex );</pre>
\r
782 * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
\r
783 * If xMutex is not a mutex type semaphore, or the mutex is available (not held
\r
784 * by a task), return NULL.
\r
786 * Note: This Is is a good way of determining if the calling task is the mutex
\r
787 * holder, but not a good way of determining the identity of the mutex holder as
\r
788 * the holder may change between the function exiting and the returned value
\r
791 #define xSemaphoreGetMutexHolder( xSemaphore ) xQueueGetMutexHolder( ( xSemaphore ) )
\r
793 #endif /* SEMAPHORE_H */
\r