hal_icu.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file hal_icu.h
  15. * @brief ICU Driver macros and structures.
  16. *
  17. * @addtogroup ICU
  18. * @{
  19. */
  20. #ifndef HAL_ICU_H
  21. #define HAL_ICU_H
  22. #if (HAL_USE_ICU == TRUE) || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /*===========================================================================*/
  27. /* Driver pre-compile time settings. */
  28. /*===========================================================================*/
  29. /*===========================================================================*/
  30. /* Derived constants and error checks. */
  31. /*===========================================================================*/
  32. /*===========================================================================*/
  33. /* Driver data structures and types. */
  34. /*===========================================================================*/
  35. /**
  36. * @brief Driver state machine possible states.
  37. */
  38. typedef enum {
  39. ICU_UNINIT = 0, /**< Not initialized. */
  40. ICU_STOP = 1, /**< Stopped. */
  41. ICU_READY = 2, /**< Ready. */
  42. ICU_WAITING = 3, /**< Waiting for first front. */
  43. ICU_ACTIVE = 4 /**< First front detected. */
  44. } icustate_t;
  45. /**
  46. * @brief Type of a structure representing an ICU driver.
  47. */
  48. typedef struct ICUDriver ICUDriver;
  49. /**
  50. * @brief ICU notification callback type.
  51. *
  52. * @param[in] icup pointer to a @p ICUDriver object
  53. */
  54. typedef void (*icucallback_t)(ICUDriver *icup);
  55. #include "hal_icu_lld.h"
  56. /*===========================================================================*/
  57. /* Driver macros. */
  58. /*===========================================================================*/
  59. /**
  60. * @name Macro Functions
  61. * @{
  62. */
  63. /**
  64. * @brief Starts the input capture.
  65. *
  66. * @param[in] icup pointer to the @p ICUDriver object
  67. *
  68. * @iclass
  69. */
  70. #define icuStartCaptureI(icup) do { \
  71. icu_lld_start_capture(icup); \
  72. (icup)->state = ICU_WAITING; \
  73. } while (false)
  74. /**
  75. * @brief Stops the input capture.
  76. *
  77. * @param[in] icup pointer to the @p ICUDriver object
  78. *
  79. * @iclass
  80. */
  81. #define icuStopCaptureI(icup) do { \
  82. icu_lld_stop_capture(icup); \
  83. (icup)->state = ICU_READY; \
  84. } while (false)
  85. /**
  86. * @brief Enables notifications.
  87. * @pre The ICU unit must have been activated using @p icuStart() and the
  88. * capture started using @p icuStartCapture().
  89. * @note If the notification is already enabled then the call has no effect.
  90. *
  91. * @param[in] icup pointer to the @p ICUDriver object
  92. *
  93. * @iclass
  94. */
  95. #define icuEnableNotificationsI(icup) icu_lld_enable_notifications(icup)
  96. /**
  97. * @brief Disables notifications.
  98. * @pre The ICU unit must have been activated using @p icuStart() and the
  99. * capture started using @p icuStartCapture().
  100. * @note If the notification is already disabled then the call has no effect.
  101. *
  102. * @param[in] icup pointer to the @p ICUDriver object
  103. *
  104. * @iclass
  105. */
  106. #define icuDisableNotificationsI(icup) icu_lld_disable_notifications(icup)
  107. /**
  108. * @brief Check on notifications status.
  109. *
  110. * @param[in] icup pointer to the @p ICUDriver object
  111. * @return The notifications status.
  112. * @retval false if notifications are not enabled.
  113. * @retval true if notifications are enabled.
  114. *
  115. * @notapi
  116. */
  117. #define icuAreNotificationsEnabledX(icup) \
  118. icu_lld_are_notifications_enabled(icup)
  119. /**
  120. * @brief Returns the width of the latest pulse.
  121. * @details The pulse width is defined as number of ticks between the start
  122. * edge and the stop edge.
  123. * @note This function is meant to be invoked from the width capture
  124. * callback.
  125. *
  126. * @param[in] icup pointer to the @p ICUDriver object
  127. * @return The number of ticks.
  128. *
  129. * @xclass
  130. */
  131. #define icuGetWidthX(icup) icu_lld_get_width(icup)
  132. /**
  133. * @brief Returns the width of the latest cycle.
  134. * @details The cycle width is defined as number of ticks between a start
  135. * edge and the next start edge.
  136. * @note This function is meant to be invoked from the width capture
  137. * callback.
  138. *
  139. * @param[in] icup pointer to the @p ICUDriver object
  140. * @return The number of ticks.
  141. *
  142. * @xclass
  143. */
  144. #define icuGetPeriodX(icup) icu_lld_get_period(icup)
  145. /** @} */
  146. /**
  147. * @name Low level driver helper macros
  148. * @{
  149. */
  150. /**
  151. * @brief Common ISR code, ICU width event.
  152. *
  153. * @param[in] icup pointer to the @p ICUDriver object
  154. *
  155. * @notapi
  156. */
  157. #define _icu_isr_invoke_width_cb(icup) do { \
  158. if (((icup)->state == ICU_ACTIVE) && \
  159. ((icup)->config->width_cb != NULL)) \
  160. (icup)->config->width_cb(icup); \
  161. } while (0)
  162. /**
  163. * @brief Common ISR code, ICU period event.
  164. * @note A period event brings the driver into the @p ICU_ACTIVE state.
  165. *
  166. * @param[in] icup pointer to the @p ICUDriver object
  167. *
  168. * @notapi
  169. */
  170. #define _icu_isr_invoke_period_cb(icup) do { \
  171. if (((icup)->state == ICU_ACTIVE) && \
  172. ((icup)->config->period_cb != NULL)) \
  173. (icup)->config->period_cb(icup); \
  174. (icup)->state = ICU_ACTIVE; \
  175. } while (0)
  176. /**
  177. * @brief Common ISR code, ICU timer overflow event.
  178. * @note An overflow always brings the driver back to the @p ICU_WAITING
  179. * state.
  180. *
  181. * @param[in] icup pointer to the @p ICUDriver object
  182. *
  183. * @notapi
  184. */
  185. #define _icu_isr_invoke_overflow_cb(icup) do { \
  186. (icup)->config->overflow_cb(icup); \
  187. (icup)->state = ICU_WAITING; \
  188. } while (0)
  189. /** @} */
  190. /*===========================================================================*/
  191. /* External declarations. */
  192. /*===========================================================================*/
  193. #ifdef __cplusplus
  194. extern "C" {
  195. #endif
  196. void icuInit(void);
  197. void icuObjectInit(ICUDriver *icup);
  198. void icuStart(ICUDriver *icup, const ICUConfig *config);
  199. void icuStop(ICUDriver *icup);
  200. void icuStartCapture(ICUDriver *icup);
  201. bool icuWaitCapture(ICUDriver *icup);
  202. void icuStopCapture(ICUDriver *icup);
  203. void icuEnableNotifications(ICUDriver *icup);
  204. void icuDisableNotifications(ICUDriver *icup);
  205. #ifdef __cplusplus
  206. }
  207. #endif
  208. #endif /* HAL_USE_ICU == TRUE */
  209. #endif /* HAL_ICU_H */
  210. /** @} */