osif.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2018 NXP
  4. * All rights reserved.
  5. *
  6. * THIS SOFTWARE IS PROVIDED BY NXP "AS IS" AND ANY EXPRESSED OR
  7. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  8. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  9. * IN NO EVENT SHALL NXP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  10. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  11. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  12. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  13. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  14. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  15. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  16. * THE POSSIBILITY OF SUCH DAMAGE.
  17. */
  18. #ifndef OSIF_H
  19. #define OSIF_H
  20. #include <stdint.h>
  21. /**
  22. * @page misra_violations MISRA-C:2012 violations
  23. *
  24. * @section [global]
  25. * Violates MISRA 2012 Advisory Rule 2.5, Global macro not referenced.
  26. * The macro defines a value that will be interpreted as an infinite timeout.
  27. *
  28. */
  29. /*! @file */
  30. /*!
  31. * @addtogroup osif
  32. * @{
  33. */
  34. /* */
  35. /* */
  36. /* */
  37. /*******************************************************************************
  38. * Definitions
  39. ******************************************************************************/
  40. /*! @cond DRIVER_INTERNAL_USE_ONLY */
  41. #ifdef USING_OS_FREERTOS
  42. /* FreeRTOS implementation */
  43. #include "FreeRTOS.h"
  44. #include "semphr.h"
  45. #if configSUPPORT_STATIC_ALLOCATION == 1
  46. typedef struct {
  47. SemaphoreHandle_t handle;
  48. StaticSemaphore_t buffer;
  49. } semaphore_t;
  50. typedef semaphore_t mutex_t;
  51. #else /* configSUPPORT_STATIC_ALLOCATION == 0, it's dynamic allocation */
  52. /*! @brief Type for a mutex. */
  53. typedef SemaphoreHandle_t mutex_t;
  54. /*! @brief Type for a semaphore. */
  55. typedef SemaphoreHandle_t semaphore_t;
  56. #endif /* configSUPPORT_STATIC_ALLOCATION == 1 */
  57. #else
  58. /* Bare-metal implementation */
  59. /*! @brief Type for a mutex. */
  60. typedef uint8_t mutex_t;
  61. /*! @brief Type for a semaphore. */
  62. typedef volatile uint8_t semaphore_t;
  63. #endif /* ifdef USING_OS_FREERTOS */
  64. /*! @endcond */
  65. #define OSIF_WAIT_FOREVER 0xFFFFFFFFu
  66. #include "status.h"
  67. /*******************************************************************************
  68. * API
  69. ******************************************************************************/
  70. #if defined (__cplusplus)
  71. extern "C" {
  72. #endif
  73. /*!
  74. * @brief Delays execution for a number of milliseconds.
  75. *
  76. * @param[in] delay Time delay in milliseconds.
  77. */
  78. void OSIF_TimeDelay(const uint32_t delay);
  79. /*!
  80. * @brief Returns the number of miliseconds elapsed since starting the internal timer
  81. * or starting the scheduler.
  82. *
  83. * @return the number of miliseconds elapsed
  84. */
  85. uint32_t OSIF_GetMilliseconds(void);
  86. /*!
  87. * @brief Waits for a mutex and locks it.
  88. *
  89. * @param[in] pMutex reference to the mutex object
  90. * @param[in] timeout time-out value in milliseconds
  91. * @return One of the possible status codes:
  92. * - STATUS_SUCCESS: mutex lock operation success
  93. * - STATUS_ERROR: mutex already owned by current thread
  94. * - STATUS_TIMEOUT: mutex lock operation timed out
  95. *
  96. */
  97. status_t OSIF_MutexLock(const mutex_t * const pMutex,
  98. const uint32_t timeout);
  99. /*!
  100. * @brief Unlocks a previously locked mutex.
  101. *
  102. * @param[in] pMutex reference to the mutex object
  103. * @return One of the possible status codes:
  104. * - STATUS_SUCCESS: mutex unlock operation success
  105. * - STATUS_ERROR: mutex unlock failed
  106. */
  107. status_t OSIF_MutexUnlock(const mutex_t * const pMutex);
  108. /*!
  109. * @brief Create an unlocked mutex.
  110. *
  111. * @param[in] pMutex reference to the mutex object
  112. * @return One of the possible status codes:
  113. * - STATUS_SUCCESS: mutex created
  114. * - STATUS_ERROR: mutex could not be created
  115. */
  116. status_t OSIF_MutexCreate(mutex_t * const pMutex);
  117. /*!
  118. * @brief Destroys a previously created mutex.
  119. *
  120. * @param[in] pMutex reference to the mutex object
  121. * @return One of the possible status codes:
  122. * - STATUS_SUCCESS: mutex destroyed
  123. */
  124. status_t OSIF_MutexDestroy(const mutex_t * const pMutex);
  125. /*!
  126. * @brief Decrement a semaphore with timeout.
  127. *
  128. * @param[in] pSem reference to the semaphore object
  129. * @param[in] timeout time-out value in milliseconds
  130. * @return One of the possible status codes:
  131. * - STATUS_SUCCESS: semaphore wait operation success
  132. * - STATUS_TIMEOUT: semaphore wait timed out
  133. */
  134. status_t OSIF_SemaWait(semaphore_t * const pSem,
  135. const uint32_t timeout);
  136. /*!
  137. * @brief Increment a semaphore
  138. *
  139. * @param[in] pSem reference to the semaphore object
  140. * @return One of the possible status codes:
  141. * - STATUS_SUCCESS: semaphore post operation success
  142. * - STATUS_ERROR: semaphore could not be incremented
  143. */
  144. status_t OSIF_SemaPost(semaphore_t * const pSem);
  145. /*!
  146. * @brief Creates a semaphore with a given value.
  147. *
  148. * @param[in] pSem reference to the semaphore object
  149. * @param[in] initValue initial value of the semaphore
  150. * @return One of the possible status codes:
  151. * - STATUS_SUCCESS: semaphore created
  152. * - STATUS_ERROR: semaphore could not be created
  153. */
  154. status_t OSIF_SemaCreate(semaphore_t * const pSem,
  155. const uint8_t initValue);
  156. /*!
  157. * @brief Destroys a previously created semaphore.
  158. *
  159. * @param[in] pSem reference to the semaphore object
  160. * @return One of the possible status codes:
  161. * - STATUS_SUCCESS: semaphore destroyed
  162. */
  163. status_t OSIF_SemaDestroy(const semaphore_t * const pSem);
  164. /*! @}*/
  165. #if defined (__cplusplus)
  166. }
  167. #endif
  168. /*! @}*/
  169. #endif /* OSIF_H */
  170. /*******************************************************************************
  171. * EOF
  172. ******************************************************************************/