i2s_hal.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 hal/include/hal/readme.md
  18. ******************************************************************************/
  19. // The HAL layer for I2S.
  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 "soc/i2s_periph.h"
  23. #include "soc/soc_caps.h"
  24. #include "hal/i2s_ll.h"
  25. #include "hal/i2s_types.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. i2s_dev_t *dev;
  34. uint32_t version;
  35. } i2s_hal_context_t;
  36. /**
  37. * @brief Get I2S interrupt status
  38. *
  39. * @param hal Context of the HAL layer
  40. * @param status interrupt status
  41. */
  42. #define i2s_hal_get_intr_status(hal, status) i2s_ll_get_intr_status((hal)->dev, status)
  43. /**
  44. * @brief Clear I2S interrupt status
  45. *
  46. * @param hal Context of the HAL layer
  47. * @param mask interrupt status mask
  48. */
  49. #define i2s_hal_clear_intr_status(hal, mask) i2s_ll_clear_intr_status((hal)->dev, mask)
  50. /**
  51. * @brief Get I2S out eof des address
  52. *
  53. * @param hal Context of the HAL layer
  54. * @param addr out eof des address
  55. */
  56. #define i2s_hal_get_out_eof_des_addr(hal, addr) i2s_ll_get_out_eof_des_addr((hal)->dev, addr)
  57. /**
  58. * @brief Get I2S in eof des address
  59. *
  60. * @param hal Context of the HAL layer
  61. * @param addr in eof des address
  62. */
  63. #define i2s_hal_get_in_eof_des_addr(hal, addr) i2s_ll_get_in_eof_des_addr((hal)->dev, addr)
  64. /**
  65. * @brief Enable I2S rx interrupt
  66. *
  67. * @param hal Context of the HAL layer
  68. */
  69. #define i2s_hal_enable_rx_intr(hal) i2s_ll_enable_rx_intr((hal)->dev)
  70. /**
  71. * @brief Disable I2S rx interrupt
  72. *
  73. * @param hal Context of the HAL layer
  74. */
  75. #define i2s_hal_disable_rx_intr(hal) i2s_ll_disable_rx_intr((hal)->dev)
  76. /**
  77. * @brief Disable I2S tx interrupt
  78. *
  79. * @param hal Context of the HAL layer
  80. */
  81. #define i2s_hal_disable_tx_intr(hal) i2s_ll_disable_tx_intr((hal)->dev)
  82. /**
  83. * @brief Enable I2S tx interrupt
  84. *
  85. * @param hal Context of the HAL layer
  86. */
  87. #define i2s_hal_enable_tx_intr(hal) i2s_ll_enable_tx_intr((hal)->dev)
  88. /**
  89. * @brief Set I2S tx mode
  90. *
  91. * @param hal Context of the HAL layer
  92. * @param ch i2s channel
  93. * @param bits bits per sample
  94. */
  95. void i2s_hal_set_tx_mode(i2s_hal_context_t *hal, i2s_channel_t ch, i2s_bits_per_sample_t bits);
  96. /**
  97. * @brief Set I2S rx mode
  98. *
  99. * @param hal Context of the HAL layer
  100. * @param ch i2s channel
  101. * @param bits bits per sample
  102. */
  103. void i2s_hal_set_rx_mode(i2s_hal_context_t *hal, i2s_channel_t ch, i2s_bits_per_sample_t bits);
  104. /**
  105. * @brief Set I2S out link address
  106. *
  107. * @param hal Context of the HAL layer
  108. * @param addr out link address
  109. */
  110. #define i2s_hal_set_out_link_addr(hal, addr) i2s_ll_set_out_link_addr((hal)->dev, addr)
  111. /**
  112. * @brief Set I2S out link address
  113. *
  114. * @param hal Context of the HAL layer
  115. * @param addr out link address
  116. */
  117. #define i2s_hal_set_out_link_addr(hal, addr) i2s_ll_set_out_link_addr((hal)->dev, addr)
  118. /**
  119. * @brief Set I2S out link address
  120. *
  121. * @param hal Context of the HAL layer
  122. * @param addr out link address
  123. */
  124. #define i2s_hal_set_out_link_addr(hal, addr) i2s_ll_set_out_link_addr((hal)->dev, addr)
  125. /**
  126. * @brief Set I2S in link
  127. *
  128. * @param hal Context of the HAL layer
  129. * @param rx_eof_num in link eof num
  130. * @param addr in link address
  131. */
  132. void i2s_hal_set_in_link(i2s_hal_context_t *hal, uint32_t rx_eof_num, uint32_t addr);
  133. /**
  134. * @brief Set I2S clk div
  135. *
  136. * @param hal Context of the HAL layer
  137. * @param div_num i2s clkm div num
  138. * @param div_a i2s clkm div a
  139. * @param div_b i2s clkm div b
  140. * @param tx_bck_div tx bck div num
  141. * @param rx_bck_div rx bck div num
  142. */
  143. void i2s_hal_set_clk_div(i2s_hal_context_t *hal, int div_num, int div_a, int div_b, int tx_bck_div, int rx_bck_div);
  144. /**
  145. * @brief Set I2S clock sel
  146. *
  147. * @param hal Context of the HAL layer
  148. * @param sel clock sel
  149. */
  150. #define i2s_hal_set_clock_sel(hal, sel) i2s_ll_set_clk_sel((hal)->dev, sel)
  151. /**
  152. * @brief Set I2S tx bits mod
  153. *
  154. * @param hal Context of the HAL layer
  155. * @param bits bit width per sample.
  156. */
  157. void i2s_hal_set_tx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits);
  158. /**
  159. * @brief Set I2S rx bits mod
  160. *
  161. * @param hal Context of the HAL layer
  162. * @param bits bit width per sample.
  163. */
  164. void i2s_hal_set_rx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits);
  165. /**
  166. * @brief Reset I2S TX & RX module, including DMA and FIFO
  167. *
  168. * @param hal Context of the HAL layer
  169. */
  170. void i2s_hal_reset(i2s_hal_context_t *hal);
  171. /**
  172. * @brief Start I2S tx
  173. *
  174. * @param hal Context of the HAL layer
  175. */
  176. void i2s_hal_start_tx(i2s_hal_context_t *hal);
  177. /**
  178. * @brief Start I2S rx
  179. *
  180. * @param hal Context of the HAL layer
  181. */
  182. void i2s_hal_start_rx(i2s_hal_context_t *hal);
  183. /**
  184. * @brief Stop I2S tx
  185. *
  186. * @param hal Context of the HAL layer
  187. */
  188. void i2s_hal_stop_tx(i2s_hal_context_t *hal);
  189. /**
  190. * @brief Stop I2S rx
  191. *
  192. * @param hal Context of the HAL layer
  193. */
  194. void i2s_hal_stop_rx(i2s_hal_context_t *hal);
  195. /**
  196. * @brief Config I2S param
  197. *
  198. * @param hal Context of the HAL layer
  199. * @param i2s_config I2S configurations - see i2s_config_t struct
  200. */
  201. void i2s_hal_config_param(i2s_hal_context_t *hal, const i2s_config_t *i2s_config);
  202. /**
  203. * @brief Enable I2S sig loopback
  204. *
  205. * @param hal Context of the HAL layer
  206. */
  207. #define i2s_hal_enable_sig_loopback(hal) i2s_ll_set_sig_loopback((hal)->dev, 1)
  208. /**
  209. * @brief Enable I2S master mode
  210. *
  211. * @param hal Context of the HAL layer
  212. */
  213. void i2s_hal_enable_master_mode(i2s_hal_context_t *hal);
  214. /**
  215. * @brief Enable I2S slave mode
  216. *
  217. * @param hal Context of the HAL layer
  218. */
  219. void i2s_hal_enable_slave_mode(i2s_hal_context_t *hal);
  220. /**
  221. * @brief Init the I2S hal and set the I2S to the default configuration. This function should be called first before other hal layer function is called
  222. *
  223. * @param hal Context of the HAL layer
  224. * @param i2s_num The uart port number, the max port number is (I2S_NUM_MAX -1)
  225. */
  226. void i2s_hal_init(i2s_hal_context_t *hal, int i2s_num);
  227. #if SOC_I2S_SUPPORTS_PDM
  228. /**
  229. * @brief Set I2S tx pdm
  230. *
  231. * @param hal Context of the HAL layer
  232. * @param fp tx pdm fp
  233. * @param fs tx pdm fs
  234. */
  235. void i2s_hal_tx_pdm_cfg(i2s_hal_context_t *hal, uint32_t fp, uint32_t fs);
  236. /**
  237. * @brief Get I2S tx pdm
  238. *
  239. * @param hal Context of the HAL layer
  240. * @param dsr rx pdm dsr
  241. */
  242. void i2s_hal_rx_pdm_cfg(i2s_hal_context_t *hal, uint32_t dsr);
  243. /**
  244. * @brief Get I2S tx pdm configuration
  245. *
  246. * @param hal Context of the HAL layer
  247. * @param fp Pointer to receive tx PDM fp configuration
  248. * @param fs Pointer to receive tx PDM fs configuration
  249. */
  250. void i2s_hal_get_tx_pdm(i2s_hal_context_t *hal, uint32_t *fp, uint32_t *fs);
  251. /**
  252. * @brief Get I2S rx pdm configuration
  253. *
  254. * @param hal Context of the HAL layer
  255. * @param dsr rx pdm dsr
  256. */
  257. void i2s_hal_get_rx_pdm(i2s_hal_context_t *hal, uint32_t *dsr);
  258. #endif
  259. #ifdef __cplusplus
  260. }
  261. #endif