i2s_hal.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * SPDX-FileCopyrightText: 2020-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /*******************************************************************************
  7. * NOTICE
  8. * The hal is not public api, don't use in application code.
  9. * See readme.md in hal/include/hal/readme.md
  10. ******************************************************************************/
  11. // The HAL layer for I2S.
  12. // There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters.
  13. #pragma once
  14. #include "soc/i2s_periph.h"
  15. #include "soc/soc_caps.h"
  16. #include "hal/i2s_types.h"
  17. #include "hal/i2s_ll.h"
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @brief I2S clock configuration
  23. */
  24. typedef struct {
  25. uint32_t sclk; /*!< I2S module clock */
  26. uint32_t mclk; /*!< I2S master clock */
  27. uint32_t bclk; /*!< I2S bit clock */
  28. uint16_t mclk_div; /*!< I2S master clock division */
  29. uint16_t bclk_div; /*!< I2S bit clock division*/
  30. } i2s_hal_clock_cfg_t;
  31. /**
  32. * @brief I2S HAL configurations
  33. */
  34. typedef struct {
  35. i2s_mode_t mode; /*!< I2S work mode, using ored mask of `i2s_mode_t`*/
  36. uint32_t sample_rate; /*!< I2S sample rate*/
  37. i2s_comm_format_t comm_fmt; /*!< I2S communication format */
  38. i2s_channel_fmt_t chan_fmt; /*!< I2S channel format, there are total 16 channels in TDM mode.*/
  39. uint32_t sample_bits; /*!< I2S sample bits in one channel */
  40. uint32_t chan_bits; /*!< I2S total bits in one channel. Should not be smaller than 'sample_bits', default '0' means equal to 'sample_bits' */
  41. uint32_t active_chan; /*!< I2S active channel number */
  42. uint32_t total_chan; /*!< Total number of I2S channels */
  43. #if SOC_I2S_SUPPORTS_TDM
  44. uint32_t chan_mask; /*!< Active channel bit mask, set value in `i2s_channel_t` to enable specific channel, the bit map of active channel can not exceed (0x1<<total_chan_num). */
  45. bool left_align; /*!< Set to enable left aligment */
  46. bool big_edin; /*!< Set to enable big edin */
  47. bool bit_order_msb; /*!< Set to enable msb order */
  48. bool skip_msk; /*!< Set to enable skip mask. If it is enabled, only the data of the enabled channels will be sent, otherwise all data stored in DMA TX buffer will be sent */
  49. #endif
  50. } i2s_hal_config_t;
  51. /**
  52. * Context that should be maintained by both the driver and the HAL
  53. */
  54. typedef struct {
  55. i2s_dev_t *dev;
  56. uint32_t version;
  57. } i2s_hal_context_t;
  58. /**
  59. * @brief Enable I2S module clock
  60. *
  61. * @param hal Context of the HAL layer
  62. */
  63. #define i2s_hal_enable_module_clock(hal) i2s_ll_enable_clock((hal)->dev);
  64. /**
  65. * @brief Disable I2S module clock
  66. *
  67. * @param hal Context of the HAL layer
  68. */
  69. #define i2s_hal_disable_module_clock(hal) i2s_ll_disable_clock((hal)->dev);
  70. /**
  71. * @brief Reset I2S TX channel
  72. *
  73. * @param hal Context of the HAL layer
  74. */
  75. #define i2s_hal_reset_tx(hal) i2s_ll_tx_reset((hal)->dev)
  76. /**
  77. * @brief Reset I2S TX fifo
  78. *
  79. * @param hal Context of the HAL layer
  80. */
  81. #define i2s_hal_reset_tx_fifo(hal) i2s_ll_tx_reset_fifo((hal)->dev)
  82. /**
  83. * @brief Reset I2S RX channel
  84. *
  85. * @param hal Context of the HAL layer
  86. */
  87. #define i2s_hal_reset_rx(hal) i2s_ll_rx_reset((hal)->dev)
  88. /**
  89. * @brief Reset I2S RX fifo
  90. *
  91. * @param hal Context of the HAL layer
  92. */
  93. #define i2s_hal_reset_rx_fifo(hal) i2s_ll_rx_reset_fifo((hal)->dev)
  94. /**
  95. * @brief Get I2S hardware instance and enable I2S module clock
  96. * @note This function should be called first before other hal layer function is called
  97. *
  98. * @param hal Context of the HAL layer
  99. * @param i2s_num The uart port number, the max port number is (I2S_NUM_MAX -1)
  100. */
  101. void i2s_hal_init(i2s_hal_context_t *hal, int i2s_num);
  102. /**
  103. * @brief Configure I2S source clock
  104. *
  105. * @param hal Context of the HAL layer
  106. * @param sel The source clock index
  107. */
  108. void i2s_hal_set_clock_src(i2s_hal_context_t *hal, i2s_clock_src_t sel);
  109. /**
  110. * @brief Set Tx channel style
  111. *
  112. * @param hal Context of the HAL layer
  113. * @param hal_cfg I2S hal configuration structer, refer to `i2s_hal_config_t`
  114. */
  115. void i2s_hal_tx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg);
  116. /**
  117. * @brief Set Rx channel style
  118. *
  119. * @param hal Context of the HAL layer
  120. * @param hal_cfg I2S hal configuration structer, refer to `i2s_hal_config_t`
  121. */
  122. void i2s_hal_rx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg);
  123. /**
  124. * @brief Initialize I2S hardware
  125. *
  126. * @param hal Context of the HAL layer
  127. * @param hal_cfg I2S hal configuration structer, refer to `i2s_hal_config_t`
  128. */
  129. void i2s_hal_config_param(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg);
  130. /**
  131. * @brief Enable I2S master full-duplex mode
  132. *
  133. * @param hal Context of the HAL layer
  134. */
  135. void i2s_hal_enable_master_fd_mode(i2s_hal_context_t *hal);
  136. /**
  137. * @brief Enable I2S slave full-duplex mode
  138. *
  139. * @param hal Context of the HAL layer
  140. */
  141. void i2s_hal_enable_slave_fd_mode(i2s_hal_context_t *hal);
  142. /**
  143. * @brief Start I2S tx
  144. *
  145. * @param hal Context of the HAL layer
  146. */
  147. void i2s_hal_start_tx(i2s_hal_context_t *hal);
  148. /**
  149. * @brief Start I2S rx
  150. *
  151. * @param hal Context of the HAL layer
  152. */
  153. void i2s_hal_start_rx(i2s_hal_context_t *hal);
  154. /**
  155. * @brief Stop I2S tx
  156. *
  157. * @param hal Context of the HAL layer
  158. */
  159. void i2s_hal_stop_tx(i2s_hal_context_t *hal);
  160. /**
  161. * @brief Stop I2S rx
  162. *
  163. * @param hal Context of the HAL layer
  164. */
  165. void i2s_hal_stop_rx(i2s_hal_context_t *hal);
  166. /**
  167. * @brief Set the received data length to trigger `in_suc_eof` interrupt.
  168. *
  169. * @param hal Context of the HAL layer
  170. * @param eof_byte The byte length that trigger in_suc_eof interrupt.
  171. */
  172. #define i2s_hal_set_rx_eof_num(hal, eof_byte) i2s_ll_rx_set_eof_num((hal)->dev, eof_byte)
  173. /**
  174. * @brief Set I2S TX sample bit
  175. *
  176. * @param hal Context of the HAL layer
  177. * @param chan_bit I2S TX chan bit
  178. * @param data_bit The sample data bit length.
  179. */
  180. #define i2s_hal_set_tx_sample_bit(hal, chan_bit, data_bit) i2s_ll_tx_set_sample_bit((hal)->dev, chan_bit, data_bit)
  181. /**
  182. * @brief Set I2S RX sample bit
  183. *
  184. * @param hal Context of the HAL layer
  185. * @param chan_bit I2S RX chan bit
  186. * @param data_bit The sample data bit length.
  187. */
  188. #define i2s_hal_set_rx_sample_bit(hal, chan_bit, data_bit) i2s_ll_rx_set_sample_bit((hal)->dev, chan_bit, data_bit)
  189. /**
  190. * @brief Configure I2S TX module clock devider
  191. *
  192. * @param hal Context of the HAL layer
  193. * @param clk_cfg I2S clock configuration
  194. */
  195. void i2s_hal_tx_clock_config(i2s_hal_context_t *hal, i2s_hal_clock_cfg_t *clk_cfg);
  196. /**
  197. * @brief Configure I2S RX module clock devider
  198. *
  199. * @param hal Context of the HAL layer
  200. * @param clk_cfg I2S clock configuration
  201. */
  202. void i2s_hal_rx_clock_config(i2s_hal_context_t *hal, i2s_hal_clock_cfg_t *clk_cfg);
  203. /**
  204. * @brief Set I2S tx clock source
  205. *
  206. * @param hal Context of the HAL layer
  207. * @param clk_src i2s tx clock source (see 'i2s_clock_src_t')
  208. */
  209. #define i2s_hal_tx_set_clock_source(hal, clk_src) i2s_ll_tx_clk_set_src((hal)->dev, clk_src)
  210. /**
  211. * @brief Set I2S rx clock source
  212. *
  213. * @param hal Context of the HAL layer
  214. * @param clk_src i2s rx clock source (see 'i2s_clock_src_t')
  215. */
  216. #define i2s_hal_rx_set_clock_source(hal, clk_src) i2s_ll_rx_clk_set_src((hal)->dev, clk_src)
  217. /**
  218. * @brief Enable I2S tx slave mode
  219. *
  220. * @param hal Context of the HAL layer
  221. * @param enable set 'true' to enable tx slave mode
  222. */
  223. #define i2s_hal_tx_enable_slave_mode(hal, enable) i2s_ll_tx_set_slave_mod((hal)->dev, enable)
  224. /**
  225. * @brief Enable I2S rx slave mode
  226. *
  227. * @param hal Context of the HAL layer
  228. * @param enable set 'true' to enable rx slave mode
  229. */
  230. #define i2s_hal_rx_enable_slave_mode(hal, enable) i2s_ll_rx_set_slave_mod((hal)->dev, enable)
  231. /**
  232. * @brief Enable loopback mode
  233. *
  234. * @param hal Context of the HAL layer
  235. */
  236. #define i2s_hal_enable_sig_loopback(hal) i2s_ll_share_bck_ws((hal)->dev, true)
  237. /**
  238. * @brief Set I2S configuration for common TX mode
  239. * @note Common mode is for non-PDM mode like philip/MSB/PCM
  240. *
  241. * @param hal Context of the HAL layer
  242. * @param hal_cfg hal configuration structure
  243. */
  244. void i2s_hal_tx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg);
  245. /**
  246. * @brief Set I2S configuration for common RX mode
  247. * @note Common mode is for non-PDM mode like philip/MSB/PCM
  248. *
  249. * @param hal Context of the HAL layer
  250. * @param hal_cfg hal configuration structure
  251. */
  252. void i2s_hal_rx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg);
  253. #if SOC_I2S_SUPPORTS_PCM
  254. /**
  255. * @brief Configure I2S TX PCM encoder or decoder.
  256. *
  257. * @param hal Context of the HAL layer
  258. * @param cfg PCM configure paramater, refer to `i2s_pcm_compress_t`
  259. */
  260. #define i2s_hal_tx_pcm_cfg(hal, cfg) i2s_ll_tx_set_pcm_type((hal)->dev, cfg)
  261. /**
  262. * @brief Configure I2S RX PCM encoder or decoder.
  263. *
  264. * @param hal Context of the HAL layer
  265. * @param cfg PCM configure paramater, refer to `i2s_pcm_compress_t`
  266. */
  267. #define i2s_hal_rx_pcm_cfg(hal, cfg) i2s_ll_rx_set_pcm_type((hal)->dev, cfg)
  268. #endif
  269. #if SOC_I2S_SUPPORTS_PDM_TX
  270. /**
  271. * @brief Configure I2S TX PDM sample rate
  272. * Fpdm = 64*Fpcm*fp/fs
  273. *
  274. * @param hal Context of the HAL layer
  275. * @param fp TX PDM fp paramater configuration
  276. * @param fs TX PDM fs paramater configuration
  277. */
  278. #define i2s_hal_set_tx_pdm_fpfs(hal, fp, fs) i2s_ll_tx_set_pdm_fpfs((hal)->dev, fp, fs)
  279. /**
  280. * @brief Get I2S TX PDM fp
  281. *
  282. * @param hal Context of the HAL layer
  283. * @return
  284. * - fp configuration paramater
  285. */
  286. #define i2s_hal_get_tx_pdm_fp(hal) i2s_ll_tx_get_pdm_fp((hal)->dev)
  287. /**
  288. * @brief Get I2S TX PDM fs
  289. *
  290. * @param hal Context of the HAL layer
  291. * @return
  292. * - fs configuration paramater
  293. */
  294. #define i2s_hal_get_tx_pdm_fs(hal) i2s_ll_tx_get_pdm_fs((hal)->dev)
  295. /**
  296. * @brief Set I2S default configuration for PDM TX mode
  297. *
  298. * @param hal Context of the HAL layer
  299. * @param sample_rate PDM sample rate
  300. */
  301. void i2s_hal_tx_set_pdm_mode_default(i2s_hal_context_t *hal, uint32_t sample_rate);
  302. #endif
  303. #if SOC_I2S_SUPPORTS_PDM_RX
  304. /**
  305. * @brief Configure RX PDM downsample
  306. *
  307. * @param hal Context of the HAL layer
  308. * @param dsr PDM downsample configuration paramater
  309. */
  310. #define i2s_hal_set_rx_pdm_dsr(hal, dsr) i2s_ll_rx_set_pdm_dsr((hal)->dev, dsr)
  311. /**
  312. * @brief Get RX PDM downsample configuration
  313. *
  314. * @param hal Context of the HAL layer
  315. * @param dsr Pointer to accept PDM downsample configuration
  316. */
  317. #define i2s_hal_get_rx_pdm_dsr(hal, dsr) i2s_ll_rx_get_pdm_dsr((hal)->dev, dsr)
  318. /**
  319. * @brief Set I2S default configuration for PDM R mode
  320. *
  321. * @param hal Context of the HAL layer
  322. */
  323. void i2s_hal_rx_set_pdm_mode_default(i2s_hal_context_t *hal);
  324. #endif
  325. #if !SOC_GDMA_SUPPORTED
  326. /**
  327. * @brief Enable I2S TX DMA
  328. *
  329. * @param hal Context of the HAL layer
  330. */
  331. #define i2s_hal_enable_tx_dma(hal) i2s_ll_enable_dma((hal)->dev,true)
  332. /**
  333. * @brief Enable I2S RX DMA
  334. *
  335. * @param hal Context of the HAL layer
  336. */
  337. #define i2s_hal_enable_rx_dma(hal) i2s_ll_enable_dma((hal)->dev,true)
  338. /**
  339. * @brief Disable I2S TX DMA
  340. *
  341. * @param hal Context of the HAL layer
  342. */
  343. #define i2s_hal_disable_tx_dma(hal) i2s_ll_enable_dma((hal)->dev,false)
  344. /**
  345. * @brief Disable I2S RX DMA
  346. *
  347. * @param hal Context of the HAL layer
  348. */
  349. #define i2s_hal_disable_rx_dma(hal) i2s_ll_enable_dma((hal)->dev,false)
  350. /**
  351. * @brief Get I2S interrupt status
  352. *
  353. * @param hal Context of the HAL layer
  354. * @return
  355. * - module interrupt status
  356. */
  357. #define i2s_hal_get_intr_status(hal) i2s_ll_get_intr_status((hal)->dev)
  358. /**
  359. * @brief Get I2S interrupt status
  360. *
  361. * @param hal Context of the HAL layer
  362. * @param mask Interrupt mask to be cleared.
  363. */
  364. #define i2s_hal_clear_intr_status(hal, mask) i2s_ll_clear_intr_status((hal)->dev, mask)
  365. /**
  366. * @brief Enable I2S RX interrupt
  367. *
  368. * @param hal Context of the HAL layer
  369. */
  370. #define i2s_hal_enable_rx_intr(hal) i2s_ll_rx_enable_intr((hal)->dev)
  371. /**
  372. * @brief Disable I2S RX interrupt
  373. *
  374. * @param hal Context of the HAL layer
  375. */
  376. #define i2s_hal_disable_rx_intr(hal) i2s_ll_rx_disable_intr((hal)->dev)
  377. /**
  378. * @brief Disable I2S TX interrupt
  379. *
  380. * @param hal Context of the HAL layer
  381. */
  382. #define i2s_hal_disable_tx_intr(hal) i2s_ll_tx_disable_intr((hal)->dev)
  383. /**
  384. * @brief Enable I2S TX interrupt
  385. *
  386. * @param hal Context of the HAL layer
  387. */
  388. #define i2s_hal_enable_tx_intr(hal) i2s_ll_tx_enable_intr((hal)->dev)
  389. /**
  390. * @brief Configure TX DMA descriptor address and start TX DMA
  391. *
  392. * @param hal Context of the HAL layer
  393. * @param link_addr DMA descriptor link address.
  394. */
  395. #define i2s_hal_start_tx_link(hal, link_addr) i2s_ll_tx_start_link((hal)->dev, link_addr)
  396. /**
  397. * @brief Configure RX DMA descriptor address and start RX DMA
  398. *
  399. * @param hal Context of the HAL layer
  400. * @param link_addr DMA descriptor link address.
  401. */
  402. #define i2s_hal_start_rx_link(hal, link_addr) i2s_ll_rx_start_link((hal)->dev, link_addr)
  403. /**
  404. * @brief Stop TX DMA link
  405. *
  406. * @param hal Context of the HAL layer
  407. */
  408. #define i2s_hal_stop_tx_link(hal) i2s_ll_tx_stop_link((hal)->dev)
  409. /**
  410. * @brief Stop RX DMA link
  411. *
  412. * @param hal Context of the HAL layer
  413. */
  414. #define i2s_hal_stop_rx_link(hal) i2s_ll_rx_stop_link((hal)->dev)
  415. /**
  416. * @brief Reset RX DMA
  417. *
  418. * @param hal Context of the HAL layer
  419. */
  420. #define i2s_hal_reset_rxdma(hal) i2s_ll_rx_reset_dma((hal)->dev)
  421. /**
  422. * @brief Reset TX DMA
  423. *
  424. * @param hal Context of the HAL layer
  425. */
  426. #define i2s_hal_reset_txdma(hal) i2s_ll_tx_reset_dma((hal)->dev)
  427. /**
  428. * @brief Get I2S out eof descriptor address
  429. *
  430. * @param hal Context of the HAL layer
  431. * @param addr Pointer to accept out eof des address
  432. */
  433. #define i2s_hal_get_out_eof_des_addr(hal, addr) i2s_ll_tx_get_eof_des_addr((hal)->dev, addr)
  434. /**
  435. * @brief Get I2S in suc eof descriptor address
  436. *
  437. * @param hal Context of the HAL layer
  438. * @param addr Pointer to accept in suc eof des address
  439. */
  440. #define i2s_hal_get_in_eof_des_addr(hal, addr) i2s_ll_rx_get_eof_des_addr((hal)->dev, addr)
  441. #endif
  442. #if SOC_I2S_SUPPORTS_ADC
  443. /**
  444. * @brief Enable Builtin DAC
  445. *
  446. * @param hal Context of the HAL layer
  447. */
  448. #define i2s_hal_enable_builtin_dac(hal) i2s_ll_enable_builtin_dac((hal)->dev, true);
  449. /**
  450. * @brief Enable Builtin ADC
  451. *
  452. * @param hal Context of the HAL layer
  453. */
  454. #define i2s_hal_enable_builtin_adc(hal) i2s_ll_enable_builtin_adc((hal)->dev, true);
  455. /**
  456. * @brief Disable Builtin ADC
  457. *
  458. * @param hal Context of the HAL layer
  459. */
  460. #define i2s_hal_disable_builtin_adc(hal) i2s_ll_enable_builtin_adc((hal)->dev, false);
  461. #endif
  462. #if SOC_I2S_SUPPORTS_DAC
  463. /**
  464. * @brief Disable Builtin DAC
  465. *
  466. * @param hal Context of the HAL layer
  467. */
  468. #define i2s_hal_disable_builtin_dac(hal) i2s_ll_enable_builtin_dac((hal)->dev, false);
  469. #endif
  470. #ifdef __cplusplus
  471. }
  472. #endif