hpm_dao_drv.h 5.7 KB

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