pcnt_hal.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*******************************************************************************
  15. * NOTICE
  16. * The hal is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for PCNT.
  20. // There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters.
  21. #pragma once
  22. #include <stdio.h>
  23. #include "soc/pcnt_periph.h"
  24. #include "hal/pcnt_types.h"
  25. #include "hal/pcnt_ll.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. /**
  30. * Context that should be maintained by both the driver and the HAL
  31. */
  32. typedef struct {
  33. pcnt_dev_t *dev;
  34. } pcnt_hal_context_t;
  35. /**
  36. * @brief Set PCNT counter mode
  37. *
  38. * @param hal Context of the HAL layer
  39. * @param unit PCNT unit number
  40. * @param channel PCNT channel number
  41. * @param pos_mode Counter mode when detecting positive edge
  42. * @param neg_mode Counter mode when detecting negative edge
  43. * @param hctrl_mode Counter mode when control signal is high level
  44. * @param lctrl_mode Counter mode when control signal is low level
  45. */
  46. #define pcnt_hal_set_mode(hal, unit, channel, pos_mode, neg_mode, hctrl_mode, lctrl_mode) pcnt_ll_set_mode((hal)->dev, unit, channel, pos_mode, neg_mode, hctrl_mode, lctrl_mode)
  47. /**
  48. * @brief Get pulse counter value
  49. *
  50. * @param hal Context of the HAL layer
  51. * @param unit Pulse Counter unit number
  52. * @param count Pointer to accept counter value
  53. */
  54. #define pcnt_hal_get_counter_value(hal, unit, count) pcnt_ll_get_counter_value((hal)->dev, unit, count)
  55. /**
  56. * @brief Pause PCNT counter of PCNT unit
  57. *
  58. * @param hal Context of the HAL layer
  59. * @param unit PCNT unit number
  60. */
  61. #define pcnt_hal_counter_pause(hal, unit) pcnt_ll_counter_pause((hal)->dev, unit)
  62. /**
  63. * @brief Resume counting for PCNT counter
  64. *
  65. * @param hal Context of the HAL layer
  66. * @param unit PCNT unit number, select from unit_t
  67. */
  68. #define pcnt_hal_counter_resume(hal, unit) pcnt_ll_counter_resume((hal)->dev, unit)
  69. /**
  70. * @brief Clear and reset PCNT counter value to zero
  71. *
  72. * @param hal Context of the HAL layer
  73. * @param unit PCNT unit number, select from unit_t
  74. */
  75. #define pcnt_hal_counter_clear(hal, unit) pcnt_ll_counter_clear((hal)->dev, unit)
  76. /**
  77. * @brief Enable PCNT interrupt for PCNT unit
  78. * @note
  79. * Each Pulse counter unit has five watch point events that share the same interrupt.
  80. * Configure events with pcnt_event_enable() and pcnt_event_disable()
  81. *
  82. * @param hal Context of the HAL layer
  83. * @param unit PCNT unit number
  84. */
  85. #define pcnt_hal_intr_enable(hal, unit) pcnt_ll_intr_enable((hal)->dev, unit)
  86. /**
  87. * @brief Disable PCNT interrupt for PCNT unit
  88. *
  89. * @param hal Context of the HAL layer
  90. * @param unit PCNT unit number
  91. */
  92. #define pcnt_hal_intr_disable(hal, unit) pcnt_ll_intr_disable((hal)->dev, unit)
  93. /**
  94. * @brief Get PCNT interrupt status
  95. *
  96. * @param hal Context of the HAL layer
  97. * @param mask The interrupt status mask to be cleared. Pointer to accept value interrupt status mask.
  98. */
  99. #define pcnt_hal_get_intr_status(hal, mask) pcnt_ll_get_intr_status((hal)->dev, mask)
  100. /**
  101. * @brief Clear PCNT interrupt status
  102. *
  103. * @param hal Context of the HAL layer
  104. * @param mask The interrupt status mask to be cleared.
  105. */
  106. #define pcnt_hal_clear_intr_status(hal, mask) pcnt_ll_clear_intr_status((hal)->dev, mask)
  107. /**
  108. * @brief Enable PCNT event of PCNT unit
  109. *
  110. * @param hal Context of the HAL layer
  111. * @param unit PCNT unit number
  112. * @param evt_type Watch point event type.
  113. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  114. */
  115. #define pcnt_hal_event_enable(hal, unit, evt_type) pcnt_ll_event_enable((hal)->dev, unit, evt_type)
  116. /**
  117. * @brief Disable PCNT event of PCNT unit
  118. *
  119. * @param hal Context of the HAL layer
  120. * @param unit PCNT unit number
  121. * @param evt_type Watch point event type.
  122. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  123. */
  124. #define pcnt_hal_event_disable(hal, unit, evt_type) pcnt_ll_event_disable((hal)->dev, unit, evt_type)
  125. /**
  126. * @brief Set PCNT event value of PCNT unit
  127. *
  128. * @param hal Context of the HAL layer
  129. * @param unit PCNT unit number
  130. * @param evt_type Watch point event type.
  131. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  132. *
  133. * @param value Counter value for PCNT event
  134. */
  135. #define pcnt_hal_set_event_value(hal, unit, evt_type, value) pcnt_ll_set_event_value((hal)->dev, unit, evt_type, value)
  136. /**
  137. * @brief Get PCNT event value of PCNT unit
  138. *
  139. * @param hal Context of the HAL layer
  140. * @param unit PCNT unit number
  141. * @param evt_type Watch point event type.
  142. * All enabled events share the same interrupt (one interrupt per pulse counter unit).
  143. * @param value Pointer to accept counter value for PCNT event
  144. */
  145. #define pcnt_hal_get_event_value(hal, unit, evt_type, value) pcnt_ll_get_event_value((hal)->dev, unit, evt_type, value)
  146. /**
  147. * @brief Set PCNT filter value
  148. *
  149. * @param hal Context of the HAL layer
  150. * @param unit PCNT unit number
  151. * @param filter_val PCNT signal filter value, counter in APB_CLK cycles.
  152. * Any pulses lasting shorter than this will be ignored when the filter is enabled.
  153. * @note
  154. * filter_val is a 10-bit value, so the maximum filter_val should be limited to 1023.
  155. */
  156. #define pcnt_hal_set_filter_value(hal, unit, filter_val) pcnt_ll_set_filter_value((hal)->dev, unit, filter_val)
  157. /**
  158. * @brief Get PCNT filter value
  159. *
  160. * @param hal Context of the HAL layer
  161. * @param unit PCNT unit number
  162. * @param filter_val Pointer to accept PCNT filter value.
  163. */
  164. #define pcnt_hal_get_filter_value(hal, unit, filter_val) pcnt_ll_get_filter_value((hal)->dev, unit, filter_val)
  165. /**
  166. * @brief Enable PCNT input filter
  167. *
  168. * @param hal Context of the HAL layer
  169. * @param unit PCNT unit number
  170. */
  171. #define pcnt_hal_filter_enable(hal, unit) pcnt_ll_filter_enable((hal)->dev, unit)
  172. /**
  173. * @brief Disable PCNT input filter
  174. *
  175. * @param hal Context of the HAL layer
  176. * @param unit PCNT unit number
  177. */
  178. #define pcnt_hal_filter_disable(hal, unit) pcnt_ll_filter_disable((hal)->dev, unit)
  179. /**
  180. * @brief Init the PCNT hal and set the PCNT to the default configuration. This function should be called first before other hal layer function is called
  181. *
  182. * @param hal Context of the HAL layer
  183. * @param pcnt_num The uart port number, the max port number is (PCNT_NUM_MAX -1)
  184. */
  185. void pcnt_hal_init(pcnt_hal_context_t *hal, int pcnt_num);
  186. #ifdef __cplusplus
  187. }
  188. #endif