hpm_pdm_drv.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_PDM_DRV_H
  8. #define HPM_PDM_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_pdm_regs.h"
  11. /**
  12. * @brief PDM driver APIs
  13. * @defgroup pdm_interface PDM driver APIs
  14. * @ingroup io_interfaces
  15. * @{
  16. */
  17. /**
  18. * @brief PDM event
  19. */
  20. #define PDM_EVENT_FILT_CRX_ERROR (PDM_ST_FILT_CRX_ERR_MASK)
  21. #define PDM_EVENT_OFIFO_OVERFLOW_ERROR (PDM_ST_OFIFO_OVFL_ERR_MASK)
  22. #define PDM_EVENT_CIC_OVERLOAD_ERROR (PDM_ST_CIC_OVLD_ERR_MASK)
  23. #define PDM_EVENT_CIC_SAT_ERROR (PDM_ST_CIC_SAT_ERR_MASK)
  24. /**
  25. * @brief PDM CIC sidma-delta filter order
  26. */
  27. #define PDM_CIC_SIGMA_DELTA_ORDER_5 (2U)
  28. #define PDM_CIC_SIGMA_DELTA_ORDER_6 (1U)
  29. #define PDM_CIC_SIGMA_DELTA_ORDER_7 (0U)
  30. /**
  31. * @brief PDM config
  32. */
  33. typedef struct pdm_config {
  34. bool sof_at_ref_clk_falling_edge;
  35. bool bypass_pdm_clk_div;
  36. bool enable_pdm_clk_out;
  37. bool enable_hpf;
  38. uint8_t pdm_clk_div;
  39. uint8_t capture_delay;
  40. uint8_t dec_after_cic;
  41. uint8_t post_scale;
  42. uint8_t sigma_delta_order;
  43. uint8_t cic_dec_ratio;
  44. } pdm_config_t;
  45. #ifdef __cplusplus
  46. extern "C" {
  47. #endif
  48. /**
  49. * @brief config high pass filter
  50. *
  51. * @param [in] ptr PDM base address
  52. * @param [in] hpf_coef_ma high pass filter a coefficient's complement
  53. * @param [in] hpf_coef_b high pass filter b coefficient
  54. */
  55. static inline void pdm_config_hpf(PDM_Type *ptr,
  56. uint32_t hpf_coef_ma,
  57. uint32_t hpf_coef_b)
  58. {
  59. ptr->HPF_MA = PDM_HPF_MA_COEF_SET(hpf_coef_ma);
  60. ptr->HPF_B = PDM_HPF_B_COEF_SET(hpf_coef_b);
  61. }
  62. /**
  63. * @brief enable high pass filter
  64. *
  65. * @param [in] ptr PDM base address
  66. */
  67. static inline void pdm_enable_hpf(PDM_Type *ptr)
  68. {
  69. ptr->CTRL |= PDM_CTRL_HPF_EN_MASK;
  70. }
  71. /**
  72. * @brief disable high pass filter
  73. *
  74. * @param [in] ptr PDM base address
  75. */
  76. static inline void pdm_disable_hpf(PDM_Type *ptr)
  77. {
  78. ptr->CTRL &= ~PDM_CTRL_HPF_EN_MASK;
  79. }
  80. /**
  81. * @brief check whether PDM is running
  82. *
  83. * @param [in] ptr PDM base address
  84. * @retval true in PDM is running
  85. */
  86. static inline bool pdm_is_running(PDM_Type *ptr)
  87. {
  88. return ptr->RUN & PDM_RUN_PDM_EN_MASK;
  89. }
  90. /**
  91. * @brief stop pdm
  92. *
  93. * @param [in] ptr PDM base address
  94. */
  95. static inline void pdm_stop(PDM_Type *ptr)
  96. {
  97. ptr->RUN &= ~PDM_RUN_PDM_EN_MASK;
  98. }
  99. /**
  100. * @brief start pdm
  101. *
  102. * @param [in] ptr PDM base address
  103. */
  104. static inline void pdm_start(PDM_Type *ptr)
  105. {
  106. ptr->RUN |= PDM_RUN_PDM_EN_MASK;
  107. }
  108. /**
  109. * @brief disable channel
  110. *
  111. * @param [in] ptr PDM base address
  112. * @param [in] channel_disable_mask channel mask
  113. */
  114. static inline void pdm_disable_channel(PDM_Type *ptr,
  115. uint16_t channel_disable_mask)
  116. {
  117. ptr->CH_CTRL &= ~PDM_CH_CTRL_CH_EN_SET(channel_disable_mask);
  118. }
  119. /**
  120. * @brief enable channel
  121. *
  122. * @param [in] ptr PDM base address
  123. * @param [in] capture_high_level_mask capture when PDM_CLK is high
  124. * @param [in] channel_enable_mask channel mask
  125. */
  126. static inline void pdm_enable_channel(PDM_Type *ptr,
  127. uint16_t capture_high_level_mask,
  128. uint16_t channel_enable_mask)
  129. {
  130. ptr->CH_CTRL |= PDM_CH_CTRL_CH_POL_SET(capture_high_level_mask)
  131. | PDM_CH_CTRL_CH_EN_SET(channel_enable_mask);
  132. }
  133. /**
  134. * @brief disable pdm clock out
  135. *
  136. * @param [in] ptr PDM base address
  137. */
  138. static inline void pdm_disable_pdm_clock_out(PDM_Type *ptr)
  139. {
  140. ptr->CTRL &= ~PDM_CTRL_PDM_CLK_OE_MASK;
  141. }
  142. /**
  143. * @brief enable pdm clock out
  144. *
  145. * @param [in] ptr PDM base address
  146. */
  147. static inline void pdm_enable_pdm_clock_out(PDM_Type *ptr)
  148. {
  149. ptr->CTRL |= PDM_CTRL_PDM_CLK_OE_MASK;
  150. }
  151. /**
  152. * @brief pdm config cic
  153. *
  154. * @param [in] ptr PDM base address
  155. * @param [in] sigma_delta_order sidma-delta filter order
  156. * @param [in] div Rate of down sampling
  157. * @param [in] post_scale output value post scale
  158. */
  159. static inline void pdm_config_cic(PDM_Type *ptr,
  160. uint8_t sigma_delta_order,
  161. uint8_t div,
  162. uint8_t post_scale)
  163. {
  164. ptr->CIC_CFG = PDM_CIC_CFG_POST_SCALE_SET(post_scale)
  165. | PDM_CIC_CFG_SGD_SET(sigma_delta_order)
  166. | PDM_CIC_CFG_CIC_DEC_RATIO_SET(div);
  167. }
  168. /**
  169. * @brief pdm software reset
  170. *
  171. * @param [in] ptr PDM base address
  172. */
  173. static inline void pdm_software_reset(PDM_Type *ptr)
  174. {
  175. ptr->CTRL |= PDM_CTRL_SFTRST_MASK;
  176. ptr->CTRL &= ~PDM_CTRL_SFTRST_MASK;
  177. }
  178. /**
  179. * @brief pdm enable irq
  180. *
  181. * @param [in] ptr PDM base address
  182. * @param [in] mask pdm irq mask in ST register
  183. */
  184. static inline void pdm_enable_irq(PDM_Type *ptr, uint8_t mask)
  185. {
  186. ptr->CTRL |= mask << (PDM_CTRL_CIC_SAT_ERR_IE_SHIFT - PDM_ST_CIC_SAT_ERR_SHIFT);
  187. }
  188. /**
  189. * @brief pdm disable irq
  190. *
  191. * @param [in] ptr PDM base address
  192. * @param [in] mask pdm irq mask in ST register
  193. */
  194. static inline void pdm_disable_irq(PDM_Type *ptr, uint8_t mask)
  195. {
  196. ptr->CTRL &= ~(mask << (PDM_CTRL_CIC_SAT_ERR_IE_SHIFT - PDM_ST_CIC_SAT_ERR_SHIFT));
  197. }
  198. /**
  199. * @brief pdm initialization
  200. *
  201. * @param [in] ptr PDM base address
  202. * @param [in] config pdm_config_t
  203. * @retval hpm_stat_t status_invalid_argument or status_success
  204. */
  205. hpm_stat_t pdm_init(PDM_Type *ptr, pdm_config_t *config);
  206. /**
  207. * @brief pdm get default config
  208. *
  209. * @param [in] ptr PDM base address
  210. * @param [out] config pdm_config_t
  211. */
  212. void pdm_get_default_config(PDM_Type *ptr, pdm_config_t *config);
  213. /**
  214. * @}
  215. */
  216. #ifdef __cplusplus
  217. }
  218. #endif
  219. #endif /* HPM_PDM_DRV_H */