semphr.h 48 KB

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