Prusa MINI Firmware overview
semphr.h
Go to the documentation of this file.
1 /*
2  FreeRTOS V9.0.0 - Copyright (C) 2016 Real Time Engineers Ltd.
3  All rights reserved
4 
5  VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6 
7  This file is part of the FreeRTOS distribution.
8 
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.
12 
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  ***************************************************************************
19 
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
24 
25  ***************************************************************************
26  * *
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. *
31  * *
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 *
36  * *
37  ***************************************************************************
38 
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()?
42 
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.
46 
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.
51 
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.
55 
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.
58 
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.
62 
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.
66 
67  1 tab == 4 spaces!
68 */
69 
70 #ifndef SEMAPHORE_H
71 #define SEMAPHORE_H
72 
73 #ifndef INC_FREERTOS_H
74  #error "include FreeRTOS.h" must appear in source files before "include semphr.h"
75 #endif
76 
77 #include "queue.h"
78 
80 
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 )
84 
85 
86 /**
87  * semphr. h
88  * <pre>vSemaphoreCreateBinary( SemaphoreHandle_t xSemaphore )</pre>
89  *
90  * In many usage scenarios it is faster and more memory efficient to use a
91  * direct to task notification in place of a binary semaphore!
92  * http://www.freertos.org/RTOS-task-notifications.html
93  *
94  * This old vSemaphoreCreateBinary() macro is now deprecated in favour of the
95  * xSemaphoreCreateBinary() function. Note that binary semaphores created using
96  * the vSemaphoreCreateBinary() macro are created in a state such that the
97  * first call to 'take' the semaphore would pass, whereas binary semaphores
98  * created using xSemaphoreCreateBinary() are created in a state such that the
99  * the semaphore must first be 'given' before it can be 'taken'.
100  *
101  * <i>Macro</i> that implements a semaphore by using the existing queue mechanism.
102  * The queue length is 1 as this is a binary semaphore. The data size is 0
103  * as we don't want to actually store any data - we just want to know if the
104  * queue is empty or full.
105  *
106  * This type of semaphore can be used for pure synchronisation between tasks or
107  * between an interrupt and a task. The semaphore need not be given back once
108  * obtained, so one task/interrupt can continuously 'give' the semaphore while
109  * another continuously 'takes' the semaphore. For this reason this type of
110  * semaphore does not use a priority inheritance mechanism. For an alternative
111  * that does use priority inheritance see xSemaphoreCreateMutex().
112  *
113  * @param xSemaphore Handle to the created semaphore. Should be of type SemaphoreHandle_t.
114  *
115  * Example usage:
116  <pre>
117  SemaphoreHandle_t xSemaphore = NULL;
118 
119  void vATask( void * pvParameters )
120  {
121  // Semaphore cannot be used before a call to vSemaphoreCreateBinary ().
122  // This is a macro so pass the variable in directly.
123  vSemaphoreCreateBinary( xSemaphore );
124 
125  if( xSemaphore != NULL )
126  {
127  // The semaphore was created successfully.
128  // The semaphore can now be used.
129  }
130  }
131  </pre>
132  * \defgroup vSemaphoreCreateBinary vSemaphoreCreateBinary
133  * \ingroup Semaphores
134  */
135 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
136  #define vSemaphoreCreateBinary( xSemaphore ) \
137  { \
138  ( xSemaphore ) = xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE ); \
139  if( ( xSemaphore ) != NULL ) \
140  { \
141  ( void ) xSemaphoreGive( ( xSemaphore ) ); \
142  } \
143  }
144 #endif
145 
146 /**
147  * semphr. h
148  * <pre>SemaphoreHandle_t xSemaphoreCreateBinary( void )</pre>
149  *
150  * Creates a new binary semaphore instance, and returns a handle by which the
151  * new semaphore can be referenced.
152  *
153  * In many usage scenarios it is faster and more memory efficient to use a
154  * direct to task notification in place of a binary semaphore!
155  * http://www.freertos.org/RTOS-task-notifications.html
156  *
157  * Internally, within the FreeRTOS implementation, binary semaphores use a block
158  * of memory, in which the semaphore structure is stored. If a binary semaphore
159  * is created using xSemaphoreCreateBinary() then the required memory is
160  * automatically dynamically allocated inside the xSemaphoreCreateBinary()
161  * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
162  * is created using xSemaphoreCreateBinaryStatic() then the application writer
163  * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
164  * binary semaphore to be created without using any dynamic memory allocation.
165  *
166  * The old vSemaphoreCreateBinary() macro is now deprecated in favour of this
167  * xSemaphoreCreateBinary() function. Note that binary semaphores created using
168  * the vSemaphoreCreateBinary() macro are created in a state such that the
169  * first call to 'take' the semaphore would pass, whereas binary semaphores
170  * created using xSemaphoreCreateBinary() are created in a state such that the
171  * the semaphore must first be 'given' before it can be 'taken'.
172  *
173  * This type of semaphore can be used for pure synchronisation between tasks or
174  * between an interrupt and a task. The semaphore need not be given back once
175  * obtained, so one task/interrupt can continuously 'give' the semaphore while
176  * another continuously 'takes' the semaphore. For this reason this type of
177  * semaphore does not use a priority inheritance mechanism. For an alternative
178  * that does use priority inheritance see xSemaphoreCreateMutex().
179  *
180  * @return Handle to the created semaphore, or NULL if the memory required to
181  * hold the semaphore's data structures could not be allocated.
182  *
183  * Example usage:
184  <pre>
185  SemaphoreHandle_t xSemaphore = NULL;
186 
187  void vATask( void * pvParameters )
188  {
189  // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
190  // This is a macro so pass the variable in directly.
191  xSemaphore = xSemaphoreCreateBinary();
192 
193  if( xSemaphore != NULL )
194  {
195  // The semaphore was created successfully.
196  // The semaphore can now be used.
197  }
198  }
199  </pre>
200  * \defgroup xSemaphoreCreateBinary xSemaphoreCreateBinary
201  * \ingroup Semaphores
202  */
203 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
204  #define xSemaphoreCreateBinary() xQueueGenericCreate( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, queueQUEUE_TYPE_BINARY_SEMAPHORE )
205 #endif
206 
207 /**
208  * semphr. h
209  * <pre>SemaphoreHandle_t xSemaphoreCreateBinaryStatic( StaticSemaphore_t *pxSemaphoreBuffer )</pre>
210  *
211  * Creates a new binary semaphore instance, and returns a handle by which the
212  * new semaphore can be referenced.
213  *
214  * NOTE: In many usage scenarios it is faster and more memory efficient to use a
215  * direct to task notification in place of a binary semaphore!
216  * http://www.freertos.org/RTOS-task-notifications.html
217  *
218  * Internally, within the FreeRTOS implementation, binary semaphores use a block
219  * of memory, in which the semaphore structure is stored. If a binary semaphore
220  * is created using xSemaphoreCreateBinary() then the required memory is
221  * automatically dynamically allocated inside the xSemaphoreCreateBinary()
222  * function. (see http://www.freertos.org/a00111.html). If a binary semaphore
223  * is created using xSemaphoreCreateBinaryStatic() then the application writer
224  * must provide the memory. xSemaphoreCreateBinaryStatic() therefore allows a
225  * binary semaphore to be created without using any dynamic memory allocation.
226  *
227  * This type of semaphore can be used for pure synchronisation between tasks or
228  * between an interrupt and a task. The semaphore need not be given back once
229  * obtained, so one task/interrupt can continuously 'give' the semaphore while
230  * another continuously 'takes' the semaphore. For this reason this type of
231  * semaphore does not use a priority inheritance mechanism. For an alternative
232  * that does use priority inheritance see xSemaphoreCreateMutex().
233  *
234  * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
235  * which will then be used to hold the semaphore's data structure, removing the
236  * need for the memory to be allocated dynamically.
237  *
238  * @return If the semaphore is created then a handle to the created semaphore is
239  * returned. If pxSemaphoreBuffer is NULL then NULL is returned.
240  *
241  * Example usage:
242  <pre>
243  SemaphoreHandle_t xSemaphore = NULL;
244  StaticSemaphore_t xSemaphoreBuffer;
245 
246  void vATask( void * pvParameters )
247  {
248  // Semaphore cannot be used before a call to xSemaphoreCreateBinary().
249  // The semaphore's data structures will be placed in the xSemaphoreBuffer
250  // variable, the address of which is passed into the function. The
251  // function's parameter is not NULL, so the function will not attempt any
252  // dynamic memory allocation, and therefore the function will not return
253  // return NULL.
254  xSemaphore = xSemaphoreCreateBinary( &xSemaphoreBuffer );
255 
256  // Rest of task code goes here.
257  }
258  </pre>
259  * \defgroup xSemaphoreCreateBinaryStatic xSemaphoreCreateBinaryStatic
260  * \ingroup Semaphores
261  */
262 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
263  #define xSemaphoreCreateBinaryStatic( pxStaticSemaphore ) xQueueGenericCreateStatic( ( UBaseType_t ) 1, semSEMAPHORE_QUEUE_ITEM_LENGTH, NULL, pxStaticSemaphore, queueQUEUE_TYPE_BINARY_SEMAPHORE )
264 #endif /* configSUPPORT_STATIC_ALLOCATION */
265 
266 /**
267  * semphr. h
268  * <pre>xSemaphoreTake(
269  * SemaphoreHandle_t xSemaphore,
270  * TickType_t xBlockTime
271  * )</pre>
272  *
273  * <i>Macro</i> to obtain a semaphore. The semaphore must have previously been
274  * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
275  * xSemaphoreCreateCounting().
276  *
277  * @param xSemaphore A handle to the semaphore being taken - obtained when
278  * the semaphore was created.
279  *
280  * @param xBlockTime The time in ticks to wait for the semaphore to become
281  * available. The macro portTICK_PERIOD_MS can be used to convert this to a
282  * real time. A block time of zero can be used to poll the semaphore. A block
283  * time of portMAX_DELAY can be used to block indefinitely (provided
284  * INCLUDE_vTaskSuspend is set to 1 in FreeRTOSConfig.h).
285  *
286  * @return pdTRUE if the semaphore was obtained. pdFALSE
287  * if xBlockTime expired without the semaphore becoming available.
288  *
289  * Example usage:
290  <pre>
291  SemaphoreHandle_t xSemaphore = NULL;
292 
293  // A task that creates a semaphore.
294  void vATask( void * pvParameters )
295  {
296  // Create the semaphore to guard a shared resource.
297  xSemaphore = xSemaphoreCreateBinary();
298  }
299 
300  // A task that uses the semaphore.
301  void vAnotherTask( void * pvParameters )
302  {
303  // ... Do other things.
304 
305  if( xSemaphore != NULL )
306  {
307  // See if we can obtain the semaphore. If the semaphore is not available
308  // wait 10 ticks to see if it becomes free.
309  if( xSemaphoreTake( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
310  {
311  // We were able to obtain the semaphore and can now access the
312  // shared resource.
313 
314  // ...
315 
316  // We have finished accessing the shared resource. Release the
317  // semaphore.
318  xSemaphoreGive( xSemaphore );
319  }
320  else
321  {
322  // We could not obtain the semaphore and can therefore not access
323  // the shared resource safely.
324  }
325  }
326  }
327  </pre>
328  * \defgroup xSemaphoreTake xSemaphoreTake
329  * \ingroup Semaphores
330  */
331 #define xSemaphoreTake( xSemaphore, xBlockTime ) xQueueGenericReceive( ( QueueHandle_t ) ( xSemaphore ), NULL, ( xBlockTime ), pdFALSE )
332 
333 /**
334  * semphr. h
335  * xSemaphoreTakeRecursive(
336  * SemaphoreHandle_t xMutex,
337  * TickType_t xBlockTime
338  * )
339  *
340  * <i>Macro</i> to recursively obtain, or 'take', a mutex type semaphore.
341  * The mutex must have previously been created using a call to
342  * xSemaphoreCreateRecursiveMutex();
343  *
344  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
345  * macro to be available.
346  *
347  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
348  *
349  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
350  * doesn't become available again until the owner has called
351  * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
352  * if a task successfully 'takes' the same mutex 5 times then the mutex will
353  * not be available to any other task until it has also 'given' the mutex back
354  * exactly five times.
355  *
356  * @param xMutex A handle to the mutex being obtained. This is the
357  * handle returned by xSemaphoreCreateRecursiveMutex();
358  *
359  * @param xBlockTime The time in ticks to wait for the semaphore to become
360  * available. The macro portTICK_PERIOD_MS can be used to convert this to a
361  * real time. A block time of zero can be used to poll the semaphore. If
362  * the task already owns the semaphore then xSemaphoreTakeRecursive() will
363  * return immediately no matter what the value of xBlockTime.
364  *
365  * @return pdTRUE if the semaphore was obtained. pdFALSE if xBlockTime
366  * expired without the semaphore becoming available.
367  *
368  * Example usage:
369  <pre>
370  SemaphoreHandle_t xMutex = NULL;
371 
372  // A task that creates a mutex.
373  void vATask( void * pvParameters )
374  {
375  // Create the mutex to guard a shared resource.
376  xMutex = xSemaphoreCreateRecursiveMutex();
377  }
378 
379  // A task that uses the mutex.
380  void vAnotherTask( void * pvParameters )
381  {
382  // ... Do other things.
383 
384  if( xMutex != NULL )
385  {
386  // See if we can obtain the mutex. If the mutex is not available
387  // wait 10 ticks to see if it becomes free.
388  if( xSemaphoreTakeRecursive( xSemaphore, ( TickType_t ) 10 ) == pdTRUE )
389  {
390  // We were able to obtain the mutex and can now access the
391  // shared resource.
392 
393  // ...
394  // For some reason due to the nature of the code further calls to
395  // xSemaphoreTakeRecursive() are made on the same mutex. In real
396  // code these would not be just sequential calls as this would make
397  // no sense. Instead the calls are likely to be buried inside
398  // a more complex call structure.
399  xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
400  xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
401 
402  // The mutex has now been 'taken' three times, so will not be
403  // available to another task until it has also been given back
404  // three times. Again it is unlikely that real code would have
405  // these calls sequentially, but instead buried in a more complex
406  // call structure. This is just for illustrative purposes.
407  xSemaphoreGiveRecursive( xMutex );
408  xSemaphoreGiveRecursive( xMutex );
409  xSemaphoreGiveRecursive( xMutex );
410 
411  // Now the mutex can be taken by other tasks.
412  }
413  else
414  {
415  // We could not obtain the mutex and can therefore not access
416  // the shared resource safely.
417  }
418  }
419  }
420  </pre>
421  * \defgroup xSemaphoreTakeRecursive xSemaphoreTakeRecursive
422  * \ingroup Semaphores
423  */
424 #if( configUSE_RECURSIVE_MUTEXES == 1 )
425  #define xSemaphoreTakeRecursive( xMutex, xBlockTime ) xQueueTakeMutexRecursive( ( xMutex ), ( xBlockTime ) )
426 #endif
427 
428 /**
429  * semphr. h
430  * <pre>xSemaphoreGive( SemaphoreHandle_t xSemaphore )</pre>
431  *
432  * <i>Macro</i> to release a semaphore. The semaphore must have previously been
433  * created with a call to xSemaphoreCreateBinary(), xSemaphoreCreateMutex() or
434  * xSemaphoreCreateCounting(). and obtained using sSemaphoreTake().
435  *
436  * This macro must not be used from an ISR. See xSemaphoreGiveFromISR () for
437  * an alternative which can be used from an ISR.
438  *
439  * This macro must also not be used on semaphores created using
440  * xSemaphoreCreateRecursiveMutex().
441  *
442  * @param xSemaphore A handle to the semaphore being released. This is the
443  * handle returned when the semaphore was created.
444  *
445  * @return pdTRUE if the semaphore was released. pdFALSE if an error occurred.
446  * Semaphores are implemented using queues. An error can occur if there is
447  * no space on the queue to post a message - indicating that the
448  * semaphore was not first obtained correctly.
449  *
450  * Example usage:
451  <pre>
452  SemaphoreHandle_t xSemaphore = NULL;
453 
454  void vATask( void * pvParameters )
455  {
456  // Create the semaphore to guard a shared resource.
457  xSemaphore = vSemaphoreCreateBinary();
458 
459  if( xSemaphore != NULL )
460  {
461  if( xSemaphoreGive( xSemaphore ) != pdTRUE )
462  {
463  // We would expect this call to fail because we cannot give
464  // a semaphore without first "taking" it!
465  }
466 
467  // Obtain the semaphore - don't block if the semaphore is not
468  // immediately available.
469  if( xSemaphoreTake( xSemaphore, ( TickType_t ) 0 ) )
470  {
471  // We now have the semaphore and can access the shared resource.
472 
473  // ...
474 
475  // We have finished accessing the shared resource so can free the
476  // semaphore.
477  if( xSemaphoreGive( xSemaphore ) != pdTRUE )
478  {
479  // We would not expect this call to fail because we must have
480  // obtained the semaphore to get here.
481  }
482  }
483  }
484  }
485  </pre>
486  * \defgroup xSemaphoreGive xSemaphoreGive
487  * \ingroup Semaphores
488  */
489 #define xSemaphoreGive( xSemaphore ) xQueueGenericSend( ( QueueHandle_t ) ( xSemaphore ), NULL, semGIVE_BLOCK_TIME, queueSEND_TO_BACK )
490 
491 /**
492  * semphr. h
493  * <pre>xSemaphoreGiveRecursive( SemaphoreHandle_t xMutex )</pre>
494  *
495  * <i>Macro</i> to recursively release, or 'give', a mutex type semaphore.
496  * The mutex must have previously been created using a call to
497  * xSemaphoreCreateRecursiveMutex();
498  *
499  * configUSE_RECURSIVE_MUTEXES must be set to 1 in FreeRTOSConfig.h for this
500  * macro to be available.
501  *
502  * This macro must not be used on mutexes created using xSemaphoreCreateMutex().
503  *
504  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
505  * doesn't become available again until the owner has called
506  * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
507  * if a task successfully 'takes' the same mutex 5 times then the mutex will
508  * not be available to any other task until it has also 'given' the mutex back
509  * exactly five times.
510  *
511  * @param xMutex A handle to the mutex being released, or 'given'. This is the
512  * handle returned by xSemaphoreCreateMutex();
513  *
514  * @return pdTRUE if the semaphore was given.
515  *
516  * Example usage:
517  <pre>
518  SemaphoreHandle_t xMutex = NULL;
519 
520  // A task that creates a mutex.
521  void vATask( void * pvParameters )
522  {
523  // Create the mutex to guard a shared resource.
524  xMutex = xSemaphoreCreateRecursiveMutex();
525  }
526 
527  // A task that uses the mutex.
528  void vAnotherTask( void * pvParameters )
529  {
530  // ... Do other things.
531 
532  if( xMutex != NULL )
533  {
534  // See if we can obtain the mutex. If the mutex is not available
535  // wait 10 ticks to see if it becomes free.
536  if( xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 ) == pdTRUE )
537  {
538  // We were able to obtain the mutex and can now access the
539  // shared resource.
540 
541  // ...
542  // For some reason due to the nature of the code further calls to
543  // xSemaphoreTakeRecursive() are made on the same mutex. In real
544  // code these would not be just sequential calls as this would make
545  // no sense. Instead the calls are likely to be buried inside
546  // a more complex call structure.
547  xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
548  xSemaphoreTakeRecursive( xMutex, ( TickType_t ) 10 );
549 
550  // The mutex has now been 'taken' three times, so will not be
551  // available to another task until it has also been given back
552  // three times. Again it is unlikely that real code would have
553  // these calls sequentially, it would be more likely that the calls
554  // to xSemaphoreGiveRecursive() would be called as a call stack
555  // unwound. This is just for demonstrative purposes.
556  xSemaphoreGiveRecursive( xMutex );
557  xSemaphoreGiveRecursive( xMutex );
558  xSemaphoreGiveRecursive( xMutex );
559 
560  // Now the mutex can be taken by other tasks.
561  }
562  else
563  {
564  // We could not obtain the mutex and can therefore not access
565  // the shared resource safely.
566  }
567  }
568  }
569  </pre>
570  * \defgroup xSemaphoreGiveRecursive xSemaphoreGiveRecursive
571  * \ingroup Semaphores
572  */
573 #if( configUSE_RECURSIVE_MUTEXES == 1 )
574  #define xSemaphoreGiveRecursive( xMutex ) xQueueGiveMutexRecursive( ( xMutex ) )
575 #endif
576 
577 /**
578  * semphr. h
579  * <pre>
580  xSemaphoreGiveFromISR(
581  SemaphoreHandle_t xSemaphore,
582  BaseType_t *pxHigherPriorityTaskWoken
583  )</pre>
584  *
585  * <i>Macro</i> to release a semaphore. The semaphore must have previously been
586  * created with a call to xSemaphoreCreateBinary() or xSemaphoreCreateCounting().
587  *
588  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
589  * must not be used with this macro.
590  *
591  * This macro can be used from an ISR.
592  *
593  * @param xSemaphore A handle to the semaphore being released. This is the
594  * handle returned when the semaphore was created.
595  *
596  * @param pxHigherPriorityTaskWoken xSemaphoreGiveFromISR() will set
597  * *pxHigherPriorityTaskWoken to pdTRUE if giving the semaphore caused a task
598  * to unblock, and the unblocked task has a priority higher than the currently
599  * running task. If xSemaphoreGiveFromISR() sets this value to pdTRUE then
600  * a context switch should be requested before the interrupt is exited.
601  *
602  * @return pdTRUE if the semaphore was successfully given, otherwise errQUEUE_FULL.
603  *
604  * Example usage:
605  <pre>
606  \#define LONG_TIME 0xffff
607  \#define TICKS_TO_WAIT 10
608  SemaphoreHandle_t xSemaphore = NULL;
609 
610  // Repetitive task.
611  void vATask( void * pvParameters )
612  {
613  for( ;; )
614  {
615  // We want this task to run every 10 ticks of a timer. The semaphore
616  // was created before this task was started.
617 
618  // Block waiting for the semaphore to become available.
619  if( xSemaphoreTake( xSemaphore, LONG_TIME ) == pdTRUE )
620  {
621  // It is time to execute.
622 
623  // ...
624 
625  // We have finished our task. Return to the top of the loop where
626  // we will block on the semaphore until it is time to execute
627  // again. Note when using the semaphore for synchronisation with an
628  // ISR in this manner there is no need to 'give' the semaphore back.
629  }
630  }
631  }
632 
633  // Timer ISR
634  void vTimerISR( void * pvParameters )
635  {
636  static uint8_t ucLocalTickCount = 0;
637  static BaseType_t xHigherPriorityTaskWoken;
638 
639  // A timer tick has occurred.
640 
641  // ... Do other time functions.
642 
643  // Is it time for vATask () to run?
644  xHigherPriorityTaskWoken = pdFALSE;
645  ucLocalTickCount++;
646  if( ucLocalTickCount >= TICKS_TO_WAIT )
647  {
648  // Unblock the task by releasing the semaphore.
649  xSemaphoreGiveFromISR( xSemaphore, &xHigherPriorityTaskWoken );
650 
651  // Reset the count so we release the semaphore again in 10 ticks time.
652  ucLocalTickCount = 0;
653  }
654 
655  if( xHigherPriorityTaskWoken != pdFALSE )
656  {
657  // We can force a context switch here. Context switching from an
658  // ISR uses port specific syntax. Check the demo task for your port
659  // to find the syntax required.
660  }
661  }
662  </pre>
663  * \defgroup xSemaphoreGiveFromISR xSemaphoreGiveFromISR
664  * \ingroup Semaphores
665  */
666 #define xSemaphoreGiveFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueGiveFromISR( ( QueueHandle_t ) ( xSemaphore ), ( pxHigherPriorityTaskWoken ) )
667 
668 /**
669  * semphr. h
670  * <pre>
671  xSemaphoreTakeFromISR(
672  SemaphoreHandle_t xSemaphore,
673  BaseType_t *pxHigherPriorityTaskWoken
674  )</pre>
675  *
676  * <i>Macro</i> to take a semaphore from an ISR. The semaphore must have
677  * previously been created with a call to xSemaphoreCreateBinary() or
678  * xSemaphoreCreateCounting().
679  *
680  * Mutex type semaphores (those created using a call to xSemaphoreCreateMutex())
681  * must not be used with this macro.
682  *
683  * This macro can be used from an ISR, however taking a semaphore from an ISR
684  * is not a common operation. It is likely to only be useful when taking a
685  * counting semaphore when an interrupt is obtaining an object from a resource
686  * pool (when the semaphore count indicates the number of resources available).
687  *
688  * @param xSemaphore A handle to the semaphore being taken. This is the
689  * handle returned when the semaphore was created.
690  *
691  * @param pxHigherPriorityTaskWoken xSemaphoreTakeFromISR() will set
692  * *pxHigherPriorityTaskWoken to pdTRUE if taking the semaphore caused a task
693  * to unblock, and the unblocked task has a priority higher than the currently
694  * running task. If xSemaphoreTakeFromISR() sets this value to pdTRUE then
695  * a context switch should be requested before the interrupt is exited.
696  *
697  * @return pdTRUE if the semaphore was successfully taken, otherwise
698  * pdFALSE
699  */
700 #define xSemaphoreTakeFromISR( xSemaphore, pxHigherPriorityTaskWoken ) xQueueReceiveFromISR( ( QueueHandle_t ) ( xSemaphore ), NULL, ( pxHigherPriorityTaskWoken ) )
701 
702 /**
703  * semphr. h
704  * <pre>SemaphoreHandle_t xSemaphoreCreateMutex( void )</pre>
705  *
706  * Creates a new mutex type semaphore instance, and returns a handle by which
707  * the new mutex can be referenced.
708  *
709  * Internally, within the FreeRTOS implementation, mutex semaphores use a block
710  * of memory, in which the mutex structure is stored. If a mutex is created
711  * using xSemaphoreCreateMutex() then the required memory is automatically
712  * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
713  * http://www.freertos.org/a00111.html). If a mutex is created using
714  * xSemaphoreCreateMutexStatic() then the application writer must provided the
715  * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
716  * without using any dynamic memory allocation.
717  *
718  * Mutexes created using this function can be accessed using the xSemaphoreTake()
719  * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
720  * xSemaphoreGiveRecursive() macros must not be used.
721  *
722  * This type of semaphore uses a priority inheritance mechanism so a task
723  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
724  * semaphore it is no longer required.
725  *
726  * Mutex type semaphores cannot be used from within interrupt service routines.
727  *
728  * See xSemaphoreCreateBinary() for an alternative implementation that can be
729  * used for pure synchronisation (where one task or interrupt always 'gives' the
730  * semaphore and another always 'takes' the semaphore) and from within interrupt
731  * service routines.
732  *
733  * @return If the mutex was successfully created then a handle to the created
734  * semaphore is returned. If there was not enough heap to allocate the mutex
735  * data structures then NULL is returned.
736  *
737  * Example usage:
738  <pre>
739  SemaphoreHandle_t xSemaphore;
740 
741  void vATask( void * pvParameters )
742  {
743  // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
744  // This is a macro so pass the variable in directly.
745  xSemaphore = xSemaphoreCreateMutex();
746 
747  if( xSemaphore != NULL )
748  {
749  // The semaphore was created successfully.
750  // The semaphore can now be used.
751  }
752  }
753  </pre>
754  * \defgroup xSemaphoreCreateMutex xSemaphoreCreateMutex
755  * \ingroup Semaphores
756  */
757 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
758  #define xSemaphoreCreateMutex() xQueueCreateMutex( queueQUEUE_TYPE_MUTEX )
759 #endif
760 
761 /**
762  * semphr. h
763  * <pre>SemaphoreHandle_t xSemaphoreCreateMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
764  *
765  * Creates a new mutex type semaphore instance, and returns a handle by which
766  * the new mutex can be referenced.
767  *
768  * Internally, within the FreeRTOS implementation, mutex semaphores use a block
769  * of memory, in which the mutex structure is stored. If a mutex is created
770  * using xSemaphoreCreateMutex() then the required memory is automatically
771  * dynamically allocated inside the xSemaphoreCreateMutex() function. (see
772  * http://www.freertos.org/a00111.html). If a mutex is created using
773  * xSemaphoreCreateMutexStatic() then the application writer must provided the
774  * memory. xSemaphoreCreateMutexStatic() therefore allows a mutex to be created
775  * without using any dynamic memory allocation.
776  *
777  * Mutexes created using this function can be accessed using the xSemaphoreTake()
778  * and xSemaphoreGive() macros. The xSemaphoreTakeRecursive() and
779  * xSemaphoreGiveRecursive() macros must not be used.
780  *
781  * This type of semaphore uses a priority inheritance mechanism so a task
782  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
783  * semaphore it is no longer required.
784  *
785  * Mutex type semaphores cannot be used from within interrupt service routines.
786  *
787  * See xSemaphoreCreateBinary() for an alternative implementation that can be
788  * used for pure synchronisation (where one task or interrupt always 'gives' the
789  * semaphore and another always 'takes' the semaphore) and from within interrupt
790  * service routines.
791  *
792  * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
793  * which will be used to hold the mutex's data structure, removing the need for
794  * the memory to be allocated dynamically.
795  *
796  * @return If the mutex was successfully created then a handle to the created
797  * mutex is returned. If pxMutexBuffer was NULL then NULL is returned.
798  *
799  * Example usage:
800  <pre>
801  SemaphoreHandle_t xSemaphore;
802  StaticSemaphore_t xMutexBuffer;
803 
804  void vATask( void * pvParameters )
805  {
806  // A mutex cannot be used before it has been created. xMutexBuffer is
807  // into xSemaphoreCreateMutexStatic() so no dynamic memory allocation is
808  // attempted.
809  xSemaphore = xSemaphoreCreateMutexStatic( &xMutexBuffer );
810 
811  // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
812  // so there is no need to check it.
813  }
814  </pre>
815  * \defgroup xSemaphoreCreateMutexStatic xSemaphoreCreateMutexStatic
816  * \ingroup Semaphores
817  */
818  #if( configSUPPORT_STATIC_ALLOCATION == 1 )
819  #define xSemaphoreCreateMutexStatic( pxMutexBuffer ) xQueueCreateMutexStatic( queueQUEUE_TYPE_MUTEX, ( pxMutexBuffer ) )
820 #endif /* configSUPPORT_STATIC_ALLOCATION */
821 
822 
823 /**
824  * semphr. h
825  * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutex( void )</pre>
826  *
827  * Creates a new recursive mutex type semaphore instance, and returns a handle
828  * by which the new recursive mutex can be referenced.
829  *
830  * Internally, within the FreeRTOS implementation, recursive mutexs use a block
831  * of memory, in which the mutex structure is stored. If a recursive mutex is
832  * created using xSemaphoreCreateRecursiveMutex() then the required memory is
833  * automatically dynamically allocated inside the
834  * xSemaphoreCreateRecursiveMutex() function. (see
835  * http://www.freertos.org/a00111.html). If a recursive mutex is created using
836  * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
837  * provide the memory that will get used by the mutex.
838  * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
839  * be created without using any dynamic memory allocation.
840  *
841  * Mutexes created using this macro can be accessed using the
842  * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
843  * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
844  *
845  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
846  * doesn't become available again until the owner has called
847  * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
848  * if a task successfully 'takes' the same mutex 5 times then the mutex will
849  * not be available to any other task until it has also 'given' the mutex back
850  * exactly five times.
851  *
852  * This type of semaphore uses a priority inheritance mechanism so a task
853  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
854  * semaphore it is no longer required.
855  *
856  * Mutex type semaphores cannot be used from within interrupt service routines.
857  *
858  * See xSemaphoreCreateBinary() for an alternative implementation that can be
859  * used for pure synchronisation (where one task or interrupt always 'gives' the
860  * semaphore and another always 'takes' the semaphore) and from within interrupt
861  * service routines.
862  *
863  * @return xSemaphore Handle to the created mutex semaphore. Should be of type
864  * SemaphoreHandle_t.
865  *
866  * Example usage:
867  <pre>
868  SemaphoreHandle_t xSemaphore;
869 
870  void vATask( void * pvParameters )
871  {
872  // Semaphore cannot be used before a call to xSemaphoreCreateMutex().
873  // This is a macro so pass the variable in directly.
874  xSemaphore = xSemaphoreCreateRecursiveMutex();
875 
876  if( xSemaphore != NULL )
877  {
878  // The semaphore was created successfully.
879  // The semaphore can now be used.
880  }
881  }
882  </pre>
883  * \defgroup xSemaphoreCreateRecursiveMutex xSemaphoreCreateRecursiveMutex
884  * \ingroup Semaphores
885  */
886 #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
887  #define xSemaphoreCreateRecursiveMutex() xQueueCreateMutex( queueQUEUE_TYPE_RECURSIVE_MUTEX )
888 #endif
889 
890 /**
891  * semphr. h
892  * <pre>SemaphoreHandle_t xSemaphoreCreateRecursiveMutexStatic( StaticSemaphore_t *pxMutexBuffer )</pre>
893  *
894  * Creates a new recursive mutex type semaphore instance, and returns a handle
895  * by which the new recursive mutex can be referenced.
896  *
897  * Internally, within the FreeRTOS implementation, recursive mutexs use a block
898  * of memory, in which the mutex structure is stored. If a recursive mutex is
899  * created using xSemaphoreCreateRecursiveMutex() then the required memory is
900  * automatically dynamically allocated inside the
901  * xSemaphoreCreateRecursiveMutex() function. (see
902  * http://www.freertos.org/a00111.html). If a recursive mutex is created using
903  * xSemaphoreCreateRecursiveMutexStatic() then the application writer must
904  * provide the memory that will get used by the mutex.
905  * xSemaphoreCreateRecursiveMutexStatic() therefore allows a recursive mutex to
906  * be created without using any dynamic memory allocation.
907  *
908  * Mutexes created using this macro can be accessed using the
909  * xSemaphoreTakeRecursive() and xSemaphoreGiveRecursive() macros. The
910  * xSemaphoreTake() and xSemaphoreGive() macros must not be used.
911  *
912  * A mutex used recursively can be 'taken' repeatedly by the owner. The mutex
913  * doesn't become available again until the owner has called
914  * xSemaphoreGiveRecursive() for each successful 'take' request. For example,
915  * if a task successfully 'takes' the same mutex 5 times then the mutex will
916  * not be available to any other task until it has also 'given' the mutex back
917  * exactly five times.
918  *
919  * This type of semaphore uses a priority inheritance mechanism so a task
920  * 'taking' a semaphore MUST ALWAYS 'give' the semaphore back once the
921  * semaphore it is no longer required.
922  *
923  * Mutex type semaphores cannot be used from within interrupt service routines.
924  *
925  * See xSemaphoreCreateBinary() for an alternative implementation that can be
926  * used for pure synchronisation (where one task or interrupt always 'gives' the
927  * semaphore and another always 'takes' the semaphore) and from within interrupt
928  * service routines.
929  *
930  * @param pxMutexBuffer Must point to a variable of type StaticSemaphore_t,
931  * which will then be used to hold the recursive mutex's data structure,
932  * removing the need for the memory to be allocated dynamically.
933  *
934  * @return If the recursive mutex was successfully created then a handle to the
935  * created recursive mutex is returned. If pxMutexBuffer was NULL then NULL is
936  * returned.
937  *
938  * Example usage:
939  <pre>
940  SemaphoreHandle_t xSemaphore;
941  StaticSemaphore_t xMutexBuffer;
942 
943  void vATask( void * pvParameters )
944  {
945  // A recursive semaphore cannot be used before it is created. Here a
946  // recursive mutex is created using xSemaphoreCreateRecursiveMutexStatic().
947  // The address of xMutexBuffer is passed into the function, and will hold
948  // the mutexes data structures - so no dynamic memory allocation will be
949  // attempted.
950  xSemaphore = xSemaphoreCreateRecursiveMutexStatic( &xMutexBuffer );
951 
952  // As no dynamic memory allocation was performed, xSemaphore cannot be NULL,
953  // so there is no need to check it.
954  }
955  </pre>
956  * \defgroup xSemaphoreCreateRecursiveMutexStatic xSemaphoreCreateRecursiveMutexStatic
957  * \ingroup Semaphores
958  */
959 #if( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configUSE_RECURSIVE_MUTEXES == 1 ) )
960  #define xSemaphoreCreateRecursiveMutexStatic( pxStaticSemaphore ) xQueueCreateMutexStatic( queueQUEUE_TYPE_RECURSIVE_MUTEX, pxStaticSemaphore )
961 #endif /* configSUPPORT_STATIC_ALLOCATION */
962 
963 /**
964  * semphr. h
965  * <pre>SemaphoreHandle_t xSemaphoreCreateCounting( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount )</pre>
966  *
967  * Creates a new counting semaphore instance, and returns a handle by which the
968  * new counting semaphore can be referenced.
969  *
970  * In many usage scenarios it is faster and more memory efficient to use a
971  * direct to task notification in place of a counting semaphore!
972  * http://www.freertos.org/RTOS-task-notifications.html
973  *
974  * Internally, within the FreeRTOS implementation, counting semaphores use a
975  * block of memory, in which the counting semaphore structure is stored. If a
976  * counting semaphore is created using xSemaphoreCreateCounting() then the
977  * required memory is automatically dynamically allocated inside the
978  * xSemaphoreCreateCounting() function. (see
979  * http://www.freertos.org/a00111.html). If a counting semaphore is created
980  * using xSemaphoreCreateCountingStatic() then the application writer can
981  * instead optionally provide the memory that will get used by the counting
982  * semaphore. xSemaphoreCreateCountingStatic() therefore allows a counting
983  * semaphore to be created without using any dynamic memory allocation.
984  *
985  * Counting semaphores are typically used for two things:
986  *
987  * 1) Counting events.
988  *
989  * In this usage scenario an event handler will 'give' a semaphore each time
990  * an event occurs (incrementing the semaphore count value), and a handler
991  * task will 'take' a semaphore each time it processes an event
992  * (decrementing the semaphore count value). The count value is therefore
993  * the difference between the number of events that have occurred and the
994  * number that have been processed. In this case it is desirable for the
995  * initial count value to be zero.
996  *
997  * 2) Resource management.
998  *
999  * In this usage scenario the count value indicates the number of resources
1000  * available. To obtain control of a resource a task must first obtain a
1001  * semaphore - decrementing the semaphore count value. When the count value
1002  * reaches zero there are no free resources. When a task finishes with the
1003  * resource it 'gives' the semaphore back - incrementing the semaphore count
1004  * value. In this case it is desirable for the initial count value to be
1005  * equal to the maximum count value, indicating that all resources are free.
1006  *
1007  * @param uxMaxCount The maximum count value that can be reached. When the
1008  * semaphore reaches this value it can no longer be 'given'.
1009  *
1010  * @param uxInitialCount The count value assigned to the semaphore when it is
1011  * created.
1012  *
1013  * @return Handle to the created semaphore. Null if the semaphore could not be
1014  * created.
1015  *
1016  * Example usage:
1017  <pre>
1018  SemaphoreHandle_t xSemaphore;
1019 
1020  void vATask( void * pvParameters )
1021  {
1022  SemaphoreHandle_t xSemaphore = NULL;
1023 
1024  // Semaphore cannot be used before a call to xSemaphoreCreateCounting().
1025  // The max value to which the semaphore can count should be 10, and the
1026  // initial value assigned to the count should be 0.
1027  xSemaphore = xSemaphoreCreateCounting( 10, 0 );
1028 
1029  if( xSemaphore != NULL )
1030  {
1031  // The semaphore was created successfully.
1032  // The semaphore can now be used.
1033  }
1034  }
1035  </pre>
1036  * \defgroup xSemaphoreCreateCounting xSemaphoreCreateCounting
1037  * \ingroup Semaphores
1038  */
1039 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1040  #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
1041 #endif
1042 
1043 /**
1044  * semphr. h
1045  * <pre>SemaphoreHandle_t xSemaphoreCreateCountingStatic( UBaseType_t uxMaxCount, UBaseType_t uxInitialCount, StaticSemaphore_t *pxSemaphoreBuffer )</pre>
1046  *
1047  * Creates a new counting semaphore instance, and returns a handle by which the
1048  * new counting semaphore can be referenced.
1049  *
1050  * In many usage scenarios it is faster and more memory efficient to use a
1051  * direct to task notification in place of a counting semaphore!
1052  * http://www.freertos.org/RTOS-task-notifications.html
1053  *
1054  * Internally, within the FreeRTOS implementation, counting semaphores use a
1055  * block of memory, in which the counting semaphore structure is stored. If a
1056  * counting semaphore is created using xSemaphoreCreateCounting() then the
1057  * required memory is automatically dynamically allocated inside the
1058  * xSemaphoreCreateCounting() function. (see
1059  * http://www.freertos.org/a00111.html). If a counting semaphore is created
1060  * using xSemaphoreCreateCountingStatic() then the application writer must
1061  * provide the memory. xSemaphoreCreateCountingStatic() therefore allows a
1062  * counting semaphore to be created without using any dynamic memory allocation.
1063  *
1064  * Counting semaphores are typically used for two things:
1065  *
1066  * 1) Counting events.
1067  *
1068  * In this usage scenario an event handler will 'give' a semaphore each time
1069  * an event occurs (incrementing the semaphore count value), and a handler
1070  * task will 'take' a semaphore each time it processes an event
1071  * (decrementing the semaphore count value). The count value is therefore
1072  * the difference between the number of events that have occurred and the
1073  * number that have been processed. In this case it is desirable for the
1074  * initial count value to be zero.
1075  *
1076  * 2) Resource management.
1077  *
1078  * In this usage scenario the count value indicates the number of resources
1079  * available. To obtain control of a resource a task must first obtain a
1080  * semaphore - decrementing the semaphore count value. When the count value
1081  * reaches zero there are no free resources. When a task finishes with the
1082  * resource it 'gives' the semaphore back - incrementing the semaphore count
1083  * value. In this case it is desirable for the initial count value to be
1084  * equal to the maximum count value, indicating that all resources are free.
1085  *
1086  * @param uxMaxCount The maximum count value that can be reached. When the
1087  * semaphore reaches this value it can no longer be 'given'.
1088  *
1089  * @param uxInitialCount The count value assigned to the semaphore when it is
1090  * created.
1091  *
1092  * @param pxSemaphoreBuffer Must point to a variable of type StaticSemaphore_t,
1093  * which will then be used to hold the semaphore's data structure, removing the
1094  * need for the memory to be allocated dynamically.
1095  *
1096  * @return If the counting semaphore was successfully created then a handle to
1097  * the created counting semaphore is returned. If pxSemaphoreBuffer was NULL
1098  * then NULL is returned.
1099  *
1100  * Example usage:
1101  <pre>
1102  SemaphoreHandle_t xSemaphore;
1103  StaticSemaphore_t xSemaphoreBuffer;
1104 
1105  void vATask( void * pvParameters )
1106  {
1107  SemaphoreHandle_t xSemaphore = NULL;
1108 
1109  // Counting semaphore cannot be used before they have been created. Create
1110  // a counting semaphore using xSemaphoreCreateCountingStatic(). The max
1111  // value to which the semaphore can count is 10, and the initial value
1112  // assigned to the count will be 0. The address of xSemaphoreBuffer is
1113  // passed in and will be used to hold the semaphore structure, so no dynamic
1114  // memory allocation will be used.
1115  xSemaphore = xSemaphoreCreateCounting( 10, 0, &xSemaphoreBuffer );
1116 
1117  // No memory allocation was attempted so xSemaphore cannot be NULL, so there
1118  // is no need to check its value.
1119  }
1120  </pre>
1121  * \defgroup xSemaphoreCreateCountingStatic xSemaphoreCreateCountingStatic
1122  * \ingroup Semaphores
1123  */
1124 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
1125  #define xSemaphoreCreateCountingStatic( uxMaxCount, uxInitialCount, pxSemaphoreBuffer ) xQueueCreateCountingSemaphoreStatic( ( uxMaxCount ), ( uxInitialCount ), ( pxSemaphoreBuffer ) )
1126 #endif /* configSUPPORT_STATIC_ALLOCATION */
1127 
1128 /**
1129  * semphr. h
1130  * <pre>void vSemaphoreDelete( SemaphoreHandle_t xSemaphore );</pre>
1131  *
1132  * Delete a semaphore. This function must be used with care. For example,
1133  * do not delete a mutex type semaphore if the mutex is held by a task.
1134  *
1135  * @param xSemaphore A handle to the semaphore to be deleted.
1136  *
1137  * \defgroup vSemaphoreDelete vSemaphoreDelete
1138  * \ingroup Semaphores
1139  */
1140 #define vSemaphoreDelete( xSemaphore ) vQueueDelete( ( QueueHandle_t ) ( xSemaphore ) )
1141 
1142 /**
1143  * semphr.h
1144  * <pre>TaskHandle_t xSemaphoreGetMutexHolder( SemaphoreHandle_t xMutex );</pre>
1145  *
1146  * If xMutex is indeed a mutex type semaphore, return the current mutex holder.
1147  * If xMutex is not a mutex type semaphore, or the mutex is available (not held
1148  * by a task), return NULL.
1149  *
1150  * Note: This is a good way of determining if the calling task is the mutex
1151  * holder, but not a good way of determining the identity of the mutex holder as
1152  * the holder may change between the function exiting and the returned value
1153  * being tested.
1154  */
1155 #define xSemaphoreGetMutexHolder( xSemaphore ) xQueueGetMutexHolder( ( xSemaphore ) )
1156 
1157 /**
1158  * semphr.h
1159  * <pre>UBaseType_t uxSemaphoreGetCount( SemaphoreHandle_t xSemaphore );</pre>
1160  *
1161  * If the semaphore is a counting semaphore then uxSemaphoreGetCount() returns
1162  * its current count value. If the semaphore is a binary semaphore then
1163  * uxSemaphoreGetCount() returns 1 if the semaphore is available, and 0 if the
1164  * semaphore is not available.
1165  *
1166  */
1167 #define uxSemaphoreGetCount( xSemaphore ) uxQueueMessagesWaiting( ( QueueHandle_t ) ( xSemaphore ) )
1168 
1169 #endif /* SEMAPHORE_H */
queue.h
SemaphoreHandle_t
QueueHandle_t SemaphoreHandle_t
Definition: semphr.h:79
QueueHandle_t
void * QueueHandle_t
Definition: queue.h:88