i2s_hal.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 soc/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/i2s_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. #if SOC_I2S_SUPPORTS_PDM
  134. /**
  135. * @brief Get I2S tx pdm
  136. *
  137. * @param hal Context of the HAL layer
  138. * @param fp tx pdm fp
  139. * @param fs tx pdm fs
  140. */
  141. void i2s_hal_get_tx_pdm(i2s_hal_context_t *hal, int *fp, int *fs);
  142. #endif
  143. /**
  144. * @brief Get I2S rx sinc dsr 16 en
  145. *
  146. * @param hal Context of the HAL layer
  147. * @param en 0: disable, 1: enable
  148. */
  149. #define i2s_hal_get_rx_sinc_dsr_16_en(hal, en) i2s_ll_get_rx_sinc_dsr_16_en((hal)->dev, en)
  150. /**
  151. * @brief Set I2S clk div
  152. *
  153. * @param hal Context of the HAL layer
  154. * @param div_num i2s clkm div num
  155. * @param div_a i2s clkm div a
  156. * @param div_b i2s clkm div b
  157. * @param tx_bck_div tx bck div num
  158. * @param rx_bck_div rx bck div num
  159. */
  160. 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);
  161. /**
  162. * @brief Set I2S clock sel
  163. *
  164. * @param hal Context of the HAL layer
  165. * @param sel clock sel
  166. */
  167. #define i2s_hal_set_clock_sel(hal, sel) i2s_ll_set_clk_sel((hal)->dev, sel)
  168. /**
  169. * @brief Set I2S tx bits mod
  170. *
  171. * @param hal Context of the HAL layer
  172. * @param bits bit width per sample.
  173. */
  174. void i2s_hal_set_tx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits);
  175. /**
  176. * @brief Set I2S rx bits mod
  177. *
  178. * @param hal Context of the HAL layer
  179. * @param bits bit width per sample.
  180. */
  181. void i2s_hal_set_rx_bits_mod(i2s_hal_context_t *hal, i2s_bits_per_sample_t bits);
  182. /**
  183. * @brief Reset I2S TX & RX module, including DMA and FIFO
  184. *
  185. * @param hal Context of the HAL layer
  186. */
  187. void i2s_hal_reset(i2s_hal_context_t *hal);
  188. /**
  189. * @brief Start I2S tx
  190. *
  191. * @param hal Context of the HAL layer
  192. */
  193. void i2s_hal_start_tx(i2s_hal_context_t *hal);
  194. /**
  195. * @brief Start I2S rx
  196. *
  197. * @param hal Context of the HAL layer
  198. */
  199. void i2s_hal_start_rx(i2s_hal_context_t *hal);
  200. /**
  201. * @brief Stop I2S tx
  202. *
  203. * @param hal Context of the HAL layer
  204. */
  205. void i2s_hal_stop_tx(i2s_hal_context_t *hal);
  206. /**
  207. * @brief Stop I2S rx
  208. *
  209. * @param hal Context of the HAL layer
  210. */
  211. void i2s_hal_stop_rx(i2s_hal_context_t *hal);
  212. #if SOC_I2S_SUPPORTS_PDM
  213. /**
  214. * @brief Set I2S pdm rx down sample
  215. *
  216. * @param hal Context of the HAL layer
  217. * @param dsr 0:disable, 1: enable
  218. */
  219. #define i2s_hal_set_pdm_rx_down_sample(hal, dsr) i2s_ll_set_rx_sinc_dsr_16_en((hal)->dev, dsr)
  220. #endif
  221. /**
  222. * @brief Config I2S param
  223. *
  224. * @param hal Context of the HAL layer
  225. * @param i2s_config I2S configurations - see i2s_config_t struct
  226. */
  227. void i2s_hal_config_param(i2s_hal_context_t *hal, const i2s_config_t *i2s_config);
  228. /**
  229. * @brief Enable I2S sig loopback
  230. *
  231. * @param hal Context of the HAL layer
  232. */
  233. #define i2s_hal_enable_sig_loopback(hal) i2s_ll_set_sig_loopback((hal)->dev, 1)
  234. /**
  235. * @brief Enable I2S master mode
  236. *
  237. * @param hal Context of the HAL layer
  238. */
  239. void i2s_hal_enable_master_mode(i2s_hal_context_t *hal);
  240. /**
  241. * @brief Enable I2S slave mode
  242. *
  243. * @param hal Context of the HAL layer
  244. */
  245. void i2s_hal_enable_slave_mode(i2s_hal_context_t *hal);
  246. /**
  247. * @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
  248. *
  249. * @param hal Context of the HAL layer
  250. * @param i2s_num The uart port number, the max port number is (I2S_NUM_MAX -1)
  251. */
  252. void i2s_hal_init(i2s_hal_context_t *hal, int i2s_num);
  253. #ifdef __cplusplus
  254. }
  255. #endif