hpm_dao_drv.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*
  2. * Copyright (c) 2021 HPMicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_DAO_DRV_H
  8. #define HPM_DAO_DRV_H
  9. #include "hpm_common.h"
  10. #include "hpm_dao_regs.h"
  11. #include "hpm_i2s_common.h"
  12. /**
  13. * @brief DAO driver APIs
  14. * @defgroup dao_interface DAO driver APIs
  15. * @ingroup io_interfaces
  16. * @{
  17. */
  18. /**
  19. * @brief DAO channel selection
  20. */
  21. #define DAO_CHANNEL_LEFT_ONLY DAO_CTRL_LEFT_EN_MASK
  22. #define DAO_CHANNEL_RIGHT_ONLY DAO_CTRL_RIGHT_EN_MASK
  23. #define DAO_CHANNEL_BOTH \
  24. (DAO_CTRL_RIGHT_EN_MASK | DAO_CTRL_LEFT_EN_MASK)
  25. /**
  26. * @brief DAO default output
  27. */
  28. #define DAO_DEFAULT_OUTPUT_ALL_LOW (0U)
  29. #define DAO_DEFAULT_OUTPUT_ALL_HIGH (1U)
  30. #define DAO_DEFAULT_OUTPUT_P_HIGH_N_LOW (2U)
  31. #define DAO_DEFAULT_OUTPUT_DISABLED (3U)
  32. /**
  33. * @brief DAO config
  34. */
  35. typedef struct dao_config {
  36. bool enable_mono_output;
  37. uint8_t default_output_level;
  38. uint8_t channel_count;
  39. } dao_config_t;
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * @brief config high pass filter
  45. *
  46. * @param [in] ptr DAO base address
  47. * @param [in] hpf_coef_ma high pass filter a coefficient's complement
  48. * @param [in] hpf_coef_b high pass filter b coefficient
  49. * @param [in] enable
  50. * @arg true: enable
  51. * @arg false: disable
  52. */
  53. static inline void dao_config_hpf(DAO_Type *ptr,
  54. uint32_t hpf_coef_ma,
  55. uint32_t hpf_coef_b,
  56. bool enable)
  57. {
  58. ptr->HPF_MA = DAO_HPF_MA_COEF_SET(hpf_coef_ma);
  59. ptr->HPF_B = DAO_HPF_B_COEF_SET(hpf_coef_b);
  60. ptr->CTRL = (ptr->CTRL & ~DAO_CTRL_HPF_EN_MASK)
  61. | (enable ? DAO_CTRL_HPF_EN_MASK : 0);
  62. }
  63. /**
  64. * @brief enable high pass filter
  65. *
  66. * @param [in] ptr DAO base address
  67. */
  68. static inline void dao_enable_hpf(DAO_Type *ptr)
  69. {
  70. ptr->CTRL |= DAO_CTRL_HPF_EN_MASK;
  71. }
  72. /**
  73. * @brief disable high pass filter
  74. *
  75. * @param [in] ptr DAO base address
  76. */
  77. static inline void dao_disable_hpf(DAO_Type *ptr)
  78. {
  79. ptr->CTRL &= ~DAO_CTRL_HPF_EN_MASK;
  80. }
  81. /**
  82. * @brief enable channel
  83. *
  84. * @param [in] ptr DAO base address
  85. * @param [in] ch channel number
  86. */
  87. static inline void dao_enable_channel(DAO_Type *ptr, uint32_t ch)
  88. {
  89. ptr->CTRL |= ch;
  90. }
  91. /**
  92. * @brief disable channel
  93. *
  94. * @param [in] ptr DAO base address
  95. * @param [in] ch channel number
  96. */
  97. static inline void dao_disable_channel(DAO_Type *ptr, uint32_t ch)
  98. {
  99. ptr->CTRL &= ~ch;
  100. }
  101. /**
  102. * @brief enable mono output
  103. *
  104. * @param [in] ptr DAO base address
  105. */
  106. static inline void dao_enable_mono_output(DAO_Type *ptr)
  107. {
  108. ptr->CTRL |= DAO_CTRL_MONO_MASK;
  109. }
  110. /**
  111. * @brief disable mono output
  112. *
  113. * @param [in] ptr DAO base address
  114. */
  115. static inline void dao_disable_mono_output(DAO_Type *ptr)
  116. {
  117. ptr->CTRL &= ~DAO_CTRL_MONO_MASK;
  118. }
  119. /**
  120. * @brief enable remap
  121. *
  122. * @param [in] ptr DAO base address
  123. */
  124. static inline void dao_enable_remap(DAO_Type *ptr)
  125. {
  126. ptr->CTRL |= DAO_CTRL_REMAP_MASK;
  127. }
  128. /**
  129. * @brief disable remap
  130. *
  131. * @param [in] ptr DAO base address
  132. */
  133. static inline void dao_disable_remap(DAO_Type *ptr)
  134. {
  135. ptr->CTRL &= ~DAO_CTRL_REMAP_MASK;
  136. }
  137. /**
  138. * @brief invert output
  139. *
  140. * @param [in] ptr DAO base address
  141. * @param [in] invert
  142. * @arg true: invert output
  143. * @arg false: not invert output
  144. */
  145. static inline void dao_invert_output(DAO_Type *ptr, bool invert)
  146. {
  147. ptr->CTRL = (ptr->CTRL & DAO_CTRL_INVERT_MASK)
  148. | DAO_CTRL_INVERT_SET(invert);
  149. }
  150. /**
  151. * @brief force pads output with certain level
  152. *
  153. * @param [in] ptr DAO base address
  154. * @param [in] output output level
  155. */
  156. static inline void dao_force_output(DAO_Type *ptr, uint8_t output)
  157. {
  158. ptr->CTRL = (ptr->CTRL & DAO_CTRL_FALSE_LEVEL_MASK)
  159. | DAO_CTRL_FALSE_LEVEL_SET(output);
  160. }
  161. /**
  162. * @brief enable false run
  163. * when false run mode is enabled, the module continues to consume data, no actual output on pads.
  164. * @param [in] ptr DAO base address
  165. * @param [in] enable
  166. * @arg true: enable
  167. * @arg false: disable
  168. */
  169. static inline void dao_enable_false_run(DAO_Type *ptr, bool enable)
  170. {
  171. ptr->CTRL = (ptr->CTRL & DAO_CTRL_FALSE_RUN_MASK)
  172. | DAO_CTRL_FALSE_RUN_SET(enable);
  173. }
  174. /**
  175. * @brief software reset
  176. *
  177. * @param [in] ptr DAO base address
  178. */
  179. static inline void dao_software_reset(DAO_Type *ptr)
  180. {
  181. ptr->CMD |= DAO_CMD_SFTRST_MASK;
  182. ptr->CMD &= ~DAO_CMD_SFTRST_MASK;
  183. }
  184. /**
  185. * @brief check whether DAO is running
  186. *
  187. * @param [in] ptr DAO base address
  188. * @retval true if dao is running
  189. */
  190. static inline bool dao_is_running(DAO_Type *ptr)
  191. {
  192. return ptr->CMD & DAO_CMD_RUN_MASK;
  193. }
  194. /**
  195. * @brief start
  196. *
  197. * @param [in] ptr DAO base address
  198. */
  199. static inline void dao_start(DAO_Type *ptr)
  200. {
  201. ptr->CMD |= DAO_CMD_RUN_MASK;
  202. }
  203. /**
  204. * @brief stop
  205. *
  206. * @param [in] ptr DAO base address
  207. */
  208. static inline void dao_stop(DAO_Type *ptr)
  209. {
  210. ptr->CMD &= ~DAO_CMD_RUN_MASK;
  211. }
  212. /**
  213. * @brief initlization
  214. *
  215. * @param [in] ptr DAO base address
  216. * @param [in] config dao_config_t
  217. * @retval hpm_stat_t status_invalid_argument or status_success
  218. */
  219. hpm_stat_t dao_init(DAO_Type *ptr, dao_config_t *config);
  220. /**
  221. * @brief get default config
  222. *
  223. * @param [in] ptr DAO base address
  224. * @param [out] config dao_config_t
  225. */
  226. void dao_get_default_config(DAO_Type *ptr, dao_config_t *config);
  227. /**
  228. * @}
  229. */
  230. #ifdef __cplusplus
  231. }
  232. #endif
  233. #endif /* HPM_DAO_DRV_H */