SemaphoreP.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Copyright (c) 2015-2017, Texas Instruments Incorporated
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of Texas Instruments Incorporated nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /** ============================================================================
  33. * @file SemaphoreP.h
  34. *
  35. * @brief Semaphore module for the RTOS Porting Interface
  36. *
  37. * Semaphores can be counting semaphores or binary semaphores. Counting
  38. * semaphores keep track of the number of times the semaphore has been posted
  39. * with post functions. This is useful, for example, if you have a group of
  40. * resources that are shared between tasks. Such tasks might call pend() to see
  41. * if a resource is available before using one. A count of zero for a counting
  42. * semaphore denotes that it is not available. A positive count denotes
  43. * how many times a SemaphoreP_pend can be called before it is blocked (or
  44. * returns SemaphoreP_TIMEOUT).
  45. *
  46. * Binary semaphores can have only two states: available (count = 1) and
  47. * unavailable (count = 0). They can be used to share a single resource
  48. * between tasks. They can also be used for a basic signalling mechanism, where
  49. * the semaphore can be posted multiple times. Binary semaphores do not keep
  50. * track of the count; they simply track whether the semaphore has been posted
  51. * or not.
  52. *
  53. * ============================================================================
  54. */
  55. #ifndef ti_osal_SemaphoreP__include
  56. #define ti_osal_SemaphoreP__include
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. #include <stdint.h>
  61. #include <stdbool.h>
  62. #include <stddef.h>
  63. /*!
  64. * @brief Status codes for SemaphoreP APIs
  65. */
  66. typedef enum SemaphoreP_Status_e {
  67. /*! API completed successfully */
  68. SemaphoreP_OK = 0,
  69. /*! API failed */
  70. SemaphoreP_FAILURE = (-(int32_t)1),
  71. /*! API failed because of a timeout */
  72. SemaphoreP_TIMEOUT = (-(int32_t)2),
  73. /*! API failed because of not supported */
  74. SemaphoreP_UNSUPPORTED =(-(int32_t)3)
  75. } SemaphoreP_Status;
  76. /*!
  77. * @brief Wait forever define
  78. */
  79. #define SemaphoreP_WAIT_FOREVER (~((uint32_t)0U))
  80. /*!
  81. * @brief No wait define
  82. */
  83. #define SemaphoreP_NO_WAIT ((uint32_t)0U)
  84. /*!
  85. * @brief Opaque client reference to an instance of a SemaphoreP
  86. *
  87. * A SemaphoreP_Handle returned from the ::SemaphoreP_create represents that
  88. * instance and is used in the other instance based functions (e.g.
  89. * ::SemaphoreP_post or ::SemaphoreP_pend, etc.).
  90. */
  91. typedef void *SemaphoreP_Handle;
  92. /*!
  93. * @brief Mode of the semaphore
  94. */
  95. typedef enum SemaphoreP_Mode_e {
  96. SemaphoreP_Mode_COUNTING = 0x0,
  97. SemaphoreP_Mode_BINARY = 0x1
  98. } SemaphoreP_Mode;
  99. /*!
  100. * @brief Basic SemaphoreP Parameters
  101. *
  102. * Structure that contains the parameters are passed into ::SemaphoreP_create
  103. * when creating a SemaphoreP instance. The ::SemaphoreP_Params_init function
  104. * should be used to initialize the fields to default values before the
  105. * application sets the fields manually. The SemaphoreP default parameters are
  106. * noted in SemaphoreP_Params_init.
  107. */
  108. typedef struct SemaphoreP_Params_s {
  109. char *name; /*!< Name of the semaphore instance. Memory must
  110. persist for the life of the semaphore instance */
  111. SemaphoreP_Mode mode; /*!< Mode for the semaphore */
  112. uint32_t maxCount; /*!< The max count allowed for counting semaphore */
  113. } SemaphoreP_Params;
  114. /*!
  115. * @brief Function to create a semaphore.
  116. *
  117. * @param count Initial count of the semaphore. For binary semaphores,
  118. * only values of 0 or 1 are valid.
  119. *
  120. * @param params Pointer to the instance configuration parameters. NULL
  121. * denotes to use the default parameters (SemaphoreP default
  122. * parameters as noted in ::SemaphoreP_Params_init.
  123. *
  124. * @return A SemaphoreP_Handle on success or a NULL on an error
  125. */
  126. extern SemaphoreP_Handle SemaphoreP_create(uint32_t count,
  127. SemaphoreP_Params *params);
  128. /*!
  129. * @brief Function to delete a semaphore.
  130. *
  131. * @param handle A SemaphoreP_Handle returned from ::SemaphoreP_create
  132. *
  133. * @return Status of the functions
  134. * - SemaphoreP_OK: Deleted the semaphore instance
  135. * - SemaphoreP_FAILED: Failed to delete the semaphore instance
  136. */
  137. extern SemaphoreP_Status SemaphoreP_delete(SemaphoreP_Handle handle);
  138. /*!
  139. * @brief Initialize params structure to default values.
  140. *
  141. * The default parameters are:
  142. * - mode: SemaphoreP_Mode_COUNTING
  143. * - name: NULL
  144. *
  145. * @param params Pointer to the instance configuration parameters.
  146. */
  147. extern void SemaphoreP_Params_init(SemaphoreP_Params *params);
  148. /*!
  149. * @brief Function to pend (wait) on a semaphore.
  150. *
  151. * @param handle A SemaphoreP_Handle returned from ::SemaphoreP_create
  152. *
  153. * @param timeout Timeout (in milliseconds) to wait for the semaphore to
  154. * be posted (signalled).
  155. *
  156. * @return Status of the functions
  157. * - SemaphoreP_OK: Obtain the semaphore
  158. * - SemaphoreP_TIMEOUT: Timed out. Semaphore was not obtained.
  159. * - SemaphoreP_FAILED: Non-time out failure.
  160. */
  161. extern SemaphoreP_Status SemaphoreP_pend(SemaphoreP_Handle handle,
  162. uint32_t timeout);
  163. /*!
  164. * @brief Function to post (signal) a semaphore.
  165. *
  166. * @param handle A SemaphoreP_Handle returned from ::SemaphoreP_create
  167. *
  168. * @return Status of the functions
  169. * - SemaphoreP_OK: Released the semaphore
  170. * - SemaphoreP_FAILED: Failed to post the semaphore
  171. */
  172. extern SemaphoreP_Status SemaphoreP_post(SemaphoreP_Handle handle);
  173. /*!
  174. * @brief Function to post (signal) a semaphore from an ClockP function.
  175. *
  176. * @param handle A SemaphoreP_Handle returned from ::SemaphoreP_create
  177. *
  178. * @return Status of the functions
  179. * - SemaphoreP_OK: Released the semaphore
  180. * - SemaphoreP_FAILED: Failed to post the semaphore
  181. */
  182. extern SemaphoreP_Status SemaphoreP_postFromClock(SemaphoreP_Handle handle);
  183. /*!
  184. * @brief Function to post (signal) a semaphore from an ISR.
  185. *
  186. * @param handle A SemaphoreP_Handle returned from ::SemaphoreP_create
  187. *
  188. * @return Status of the functions
  189. * - SemaphoreP_OK: Released the semaphore
  190. * - SemaphoreP_FAILED: Failed to post the semaphore
  191. */
  192. extern SemaphoreP_Status SemaphoreP_postFromISR(SemaphoreP_Handle handle);
  193. #ifdef __cplusplus
  194. }
  195. #endif
  196. #endif /* ti_osal_SemaphoreP__include */