hpm_dao_drv.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 error irq
  83. *
  84. * @param [in] ptr DAO base address
  85. */
  86. static inline void dao_enable_error_irq(DAO_Type *ptr)
  87. {
  88. ptr->CTRL |= DAO_CTRL_SAT_ERR_IE_MASK;
  89. }
  90. /**
  91. * @brief disable error irq
  92. *
  93. * @param [in] ptr DAO base address
  94. */
  95. static inline void dao_disable_error_irq(DAO_Type *ptr)
  96. {
  97. ptr->CTRL &= ~DAO_CTRL_SAT_ERR_IE_MASK;
  98. }
  99. /**
  100. * @brief enable channel
  101. *
  102. * @param [in] ptr DAO base address
  103. * @param [in] ch channel number
  104. */
  105. static inline void dao_enable_channel(DAO_Type *ptr, uint32_t ch)
  106. {
  107. ptr->CTRL |= ch;
  108. }
  109. /**
  110. * @brief disable channel
  111. *
  112. * @param [in] ptr DAO base address
  113. * @param [in] ch channel number
  114. */
  115. static inline void dao_disable_channel(DAO_Type *ptr, uint32_t ch)
  116. {
  117. ptr->CTRL &= ~ch;
  118. }
  119. /**
  120. * @brief enable mono output
  121. *
  122. * @param [in] ptr DAO base address
  123. */
  124. static inline void dao_enable_mono_output(DAO_Type *ptr)
  125. {
  126. ptr->CTRL |= DAO_CTRL_MONO_MASK;
  127. }
  128. /**
  129. * @brief disable mono output
  130. *
  131. * @param [in] ptr DAO base address
  132. */
  133. static inline void dao_disable_mono_output(DAO_Type *ptr)
  134. {
  135. ptr->CTRL &= ~DAO_CTRL_MONO_MASK;
  136. }
  137. /**
  138. * @brief enable remap
  139. *
  140. * @param [in] ptr DAO base address
  141. */
  142. static inline void dao_enable_remap(DAO_Type *ptr)
  143. {
  144. ptr->CTRL |= DAO_CTRL_REMAP_MASK;
  145. }
  146. /**
  147. * @brief disable remap
  148. *
  149. * @param [in] ptr DAO base address
  150. */
  151. static inline void dao_disable_remap(DAO_Type *ptr)
  152. {
  153. ptr->CTRL &= ~DAO_CTRL_REMAP_MASK;
  154. }
  155. /**
  156. * @brief invert output
  157. *
  158. * @param [in] ptr DAO base address
  159. * @param [in] invert
  160. * @arg true: invert output
  161. * @arg false: not invert output
  162. */
  163. static inline void dao_invert_output(DAO_Type *ptr, bool invert)
  164. {
  165. ptr->CTRL = (ptr->CTRL & DAO_CTRL_INVERT_MASK)
  166. | DAO_CTRL_INVERT_SET(invert);
  167. }
  168. /**
  169. * @brief force pads output with certain level
  170. *
  171. * @param [in] ptr DAO base address
  172. * @param [in] output output level
  173. */
  174. static inline void dao_force_output(DAO_Type *ptr, uint8_t output)
  175. {
  176. ptr->CTRL = (ptr->CTRL & DAO_CTRL_FALSE_LEVEL_MASK)
  177. | DAO_CTRL_FALSE_LEVEL_SET(output);
  178. }
  179. /**
  180. * @brief enable false run
  181. * when false run mode is enabled, the module continues to consume data, no actual output on pads.
  182. * @param [in] ptr DAO base address
  183. * @param [in] enable
  184. * @arg true: enable
  185. * @arg false: disable
  186. */
  187. static inline void dao_enable_false_run(DAO_Type *ptr, bool enable)
  188. {
  189. ptr->CTRL = (ptr->CTRL & DAO_CTRL_FALSE_RUN_MASK)
  190. | DAO_CTRL_FALSE_RUN_SET(enable);
  191. }
  192. /**
  193. * @brief software reset
  194. *
  195. * @param [in] ptr DAO base address
  196. */
  197. static inline void dao_software_reset(DAO_Type *ptr)
  198. {
  199. ptr->CMD |= DAO_CMD_SFTRST_MASK;
  200. ptr->CMD &= ~DAO_CMD_SFTRST_MASK;
  201. }
  202. /**
  203. * @brief check whether DAO is running
  204. *
  205. * @param [in] ptr DAO base address
  206. * @retval true if dao is running
  207. */
  208. static inline bool dao_is_running(DAO_Type *ptr)
  209. {
  210. return ptr->CMD & DAO_CMD_RUN_MASK;
  211. }
  212. /**
  213. * @brief start
  214. *
  215. * @param [in] ptr DAO base address
  216. */
  217. static inline void dao_start(DAO_Type *ptr)
  218. {
  219. ptr->CMD |= DAO_CMD_RUN_MASK;
  220. }
  221. /**
  222. * @brief stop
  223. *
  224. * @param [in] ptr DAO base address
  225. */
  226. static inline void dao_stop(DAO_Type *ptr)
  227. {
  228. ptr->CMD &= ~DAO_CMD_RUN_MASK;
  229. }
  230. /**
  231. * @brief initlization
  232. *
  233. * @param [in] ptr DAO base address
  234. * @param [in] config dao_config_t
  235. * @retval hpm_stat_t status_invalid_argument or status_success
  236. */
  237. hpm_stat_t dao_init(DAO_Type *ptr, dao_config_t *config);
  238. /**
  239. * @brief get default config
  240. *
  241. * @param [in] ptr DAO base address
  242. * @param [out] config dao_config_t
  243. */
  244. void dao_get_default_config(DAO_Type *ptr, dao_config_t *config);
  245. /**
  246. * @}
  247. */
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #endif /* HPM_DAO_DRV_H */