i2s.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "esp_types.h"
  8. #include "esp_err.h"
  9. #include "freertos/FreeRTOS.h"
  10. #include "freertos/semphr.h"
  11. #include "soc/i2s_periph.h"
  12. #include "soc/rtc_periph.h"
  13. #include "soc/soc_caps.h"
  14. #include "hal/i2s_types.h"
  15. #include "esp_intr_alloc.h"
  16. #if SOC_I2S_SUPPORTS_ADC
  17. #include "driver/adc.h"
  18. #endif
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #define I2S_PIN_NO_CHANGE (-1) /*!< Use in i2s_pin_config_t for pins which should not be changed */
  23. /**
  24. * @brief I2S port number, the max port number is (I2S_NUM_MAX -1).
  25. */
  26. typedef enum {
  27. I2S_NUM_0 = 0, /*!< I2S port 0 */
  28. #if SOC_I2S_NUM > 1
  29. I2S_NUM_1 = 1, /*!< I2S port 1 */
  30. #endif
  31. I2S_NUM_MAX, /*!< I2S port max */
  32. } i2s_port_t;
  33. #if SOC_I2S_SUPPORTS_PCM
  34. /**
  35. * @brief I2S PCM configuration
  36. *
  37. */
  38. typedef struct {
  39. i2s_pcm_compress_t pcm_type; /*!< I2S PCM a/u-law decompress or compress type */
  40. } i2s_pcm_cfg_t;
  41. #endif
  42. #if SOC_I2S_SUPPORTS_PDM_TX
  43. /**
  44. * @brief Default I2S PDM Up-Sampling Rate configuration
  45. */
  46. #define I2S_PDM_DEFAULT_UPSAMPLE_CONFIG(rate) { \
  47. .sample_rate = rate, \
  48. .fp = 960, \
  49. .fs = (rate) / 100, \
  50. }
  51. /**
  52. * @brief I2S PDM up-sample rate configuration
  53. * @note TX PDM can only be set to the following two upsampling rate configurations:
  54. * 1: fp = 960, fs = sample_rate / 100, in this case, Fpdm = 128*48000
  55. * 2: fp = 960, fs = 480, in this case, Fpdm = 128*Fpcm = 128*sample_rate
  56. * If the pdm receiver do not care the pdm serial clock, it's recommended set Fpdm = 128*48000.
  57. * Otherwise, the second configuration should be applied.
  58. */
  59. typedef struct {
  60. int sample_rate; /*!< I2S PDM sample rate */
  61. int fp; /*!< I2S PDM TX upsampling paramater. Normally it should be set to 960 */
  62. int fs; /*!< I2S PDM TX upsampling paramater. When it is set to 480, the pdm clock frequency Fpdm = 128 * sample_rate, when it is set to sample_rate / 100, Fpdm will be fixed to 128*48000 */
  63. } i2s_pdm_tx_upsample_cfg_t;
  64. #endif
  65. /**
  66. * @brief I2S pin number for i2s_set_pin
  67. *
  68. */
  69. typedef struct {
  70. int mck_io_num; /*!< MCK in out pin. Note that ESP32 supports setting MCK on GPIO0/GPIO1/GPIO3 only*/
  71. int bck_io_num; /*!< BCK in out pin*/
  72. int ws_io_num; /*!< WS in out pin*/
  73. int data_out_num; /*!< DATA out pin*/
  74. int data_in_num; /*!< DATA in pin*/
  75. } i2s_pin_config_t;
  76. /**
  77. * @brief I2S driver configuration parameters
  78. *
  79. */
  80. typedef struct {
  81. i2s_mode_t mode; /*!< I2S work mode */
  82. uint32_t sample_rate; /*!< I2S sample rate */
  83. i2s_bits_per_sample_t bits_per_sample; /*!< I2S sample bits in one channel */
  84. i2s_channel_fmt_t channel_format; /*!< I2S channel format.*/
  85. i2s_comm_format_t communication_format; /*!< I2S communication format */
  86. int intr_alloc_flags; /*!< Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info */
  87. union {
  88. int dma_desc_num; /*!< The total number of descriptors used by I2S DMA to receive/transmit data */
  89. int dma_buf_count __attribute__((deprecated)); /*!< This is an alias to 'dma_desc_num' for backward compatibility */
  90. };
  91. union {
  92. int dma_frame_num; /*!< Frame number for one-time sampling. Frame here means the total data from all the channels in a WS cycle */
  93. int dma_buf_len __attribute__((deprecated)); /*!< This is an alias to 'dma_frame_num' for backward compatibility */
  94. };
  95. bool use_apll; /*!< I2S using APLL as main I2S clock, enable it to get accurate clock */
  96. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor if there is underflow condition (helps in avoiding noise in case of data unavailability) */
  97. int fixed_mclk; /*!< I2S using fixed MCLK output. If use_apll = true and fixed_mclk > 0, then the clock output for i2s is fixed and equal to the fixed_mclk value. If fixed_mclk set, mclk_multiple won't take effect */
  98. i2s_mclk_multiple_t mclk_multiple; /*!< The multiple of I2S master clock(MCLK) to sample rate */
  99. i2s_bits_per_chan_t bits_per_chan; /*!< I2S total bits in one channel, only take effect when larger than 'bits_per_sample', default '0' means equal to 'bits_per_sample' */
  100. #if SOC_I2S_SUPPORTS_TDM
  101. i2s_channel_t chan_mask; /*!< I2S 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). */
  102. uint32_t total_chan; /*!< I2S Total number of channels. If it is smaller than the biggest active channel number, it will be set to this number automatically. */
  103. bool left_align; /*!< Set to enable left alignment */
  104. bool big_edin; /*!< Set to enable big edin */
  105. bool bit_order_msb; /*!< Set to enable msb order */
  106. 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 */
  107. #endif // SOC_I2S_SUPPORTS_TDM
  108. } i2s_driver_config_t;
  109. typedef i2s_driver_config_t i2s_config_t; // for backward compatible
  110. typedef intr_handle_t i2s_isr_handle_t; // for backward compatible
  111. /**
  112. * @brief I2S event queue types
  113. *
  114. */
  115. typedef enum {
  116. I2S_EVENT_DMA_ERROR,
  117. I2S_EVENT_TX_DONE, /*!< I2S DMA finish sent 1 buffer*/
  118. I2S_EVENT_RX_DONE, /*!< I2S DMA finish received 1 buffer*/
  119. I2S_EVENT_TX_Q_OVF, /*!< I2S DMA sent queue overflow*/
  120. I2S_EVENT_RX_Q_OVF, /*!< I2S DMA receive queue overflow*/
  121. I2S_EVENT_MAX, /*!< I2S event max index*/
  122. } i2s_event_type_t;
  123. /**
  124. * @brief Event structure used in I2S event queue
  125. *
  126. */
  127. typedef struct {
  128. i2s_event_type_t type; /*!< I2S event type */
  129. size_t size; /*!< I2S data size for I2S_DATA event*/
  130. } i2s_event_t;
  131. /**
  132. * @brief Set I2S pin number
  133. *
  134. * @note
  135. * The I2S peripheral output signals can be connected to multiple GPIO pads.
  136. * However, the I2S peripheral input signal can only be connected to one GPIO pad.
  137. *
  138. * @param i2s_num I2S port number
  139. *
  140. * @param pin I2S Pin structure, or NULL to set 2-channel 8-bit internal DAC pin configuration (GPIO25 & GPIO26)
  141. *
  142. * Inside the pin configuration structure, set I2S_PIN_NO_CHANGE for any pin where
  143. * the current configuration should not be changed.
  144. *
  145. * @note if *pin is set as NULL, this function will initialize both of the built-in DAC channels by default.
  146. * if you don't want this to happen and you want to initialize only one of the DAC channels, you can call i2s_set_dac_mode instead.
  147. *
  148. * @return
  149. * - ESP_OK Success
  150. * - ESP_ERR_INVALID_ARG Parameter error
  151. * - ESP_FAIL IO error
  152. */
  153. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin);
  154. #if SOC_I2S_SUPPORTS_PDM_RX
  155. /**
  156. * @brief Set PDM mode down-sample rate
  157. * In PDM RX mode, there would be 2 rounds of downsample process in hardware.
  158. * In the first downsample process, the sampling number can be 16 or 8.
  159. * In the second downsample process, the sampling number is fixed as 8.
  160. * So the clock frequency in PDM RX mode would be (fpcm * 64) or (fpcm * 128) accordingly.
  161. * @param i2s_num I2S port number
  162. * @param downsample i2s RX down sample rate for PDM mode.
  163. *
  164. * @note After calling this function, it would call i2s_set_clk inside to update the clock frequency.
  165. * Please call this function after I2S driver has been initialized.
  166. *
  167. * @return
  168. * - ESP_OK Success
  169. * - ESP_ERR_INVALID_ARG Parameter error
  170. * - ESP_ERR_NO_MEM Out of memory
  171. */
  172. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t downsample);
  173. #endif
  174. #if SOC_I2S_SUPPORTS_PDM_TX
  175. /**
  176. * @brief Set TX PDM mode up-sample rate
  177. * @note If you have set PDM mode while calling 'i2s_driver_install',
  178. * default PDM TX upsample parameters have already been set,
  179. * no need to call this function again if you don't have to change the default configuration
  180. *
  181. * @param i2s_num I2S port number
  182. * @param upsample_cfg Set I2S PDM up-sample rate configuration
  183. *
  184. * @return
  185. * - ESP_OK Success
  186. * - ESP_ERR_INVALID_ARG Parameter error
  187. * - ESP_ERR_NO_MEM Out of memory
  188. */
  189. esp_err_t i2s_set_pdm_tx_up_sample(i2s_port_t i2s_num, const i2s_pdm_tx_upsample_cfg_t *upsample_cfg);
  190. #endif
  191. /**
  192. * @brief Install and start I2S driver.
  193. *
  194. * @param i2s_num I2S port number
  195. *
  196. * @param i2s_config I2S configurations - see i2s_config_t struct
  197. *
  198. * @param queue_size I2S event queue size/depth.
  199. *
  200. * @param i2s_queue I2S event queue handle, if set NULL, driver will not use an event queue.
  201. *
  202. * This function must be called before any I2S driver read/write operations.
  203. *
  204. * @return
  205. * - ESP_OK Success
  206. * - ESP_ERR_INVALID_ARG Parameter error
  207. * - ESP_ERR_NO_MEM Out of memory
  208. * - ESP_ERR_INVALID_STATE Current I2S port is in use
  209. */
  210. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void *i2s_queue);
  211. /**
  212. * @brief Uninstall I2S driver.
  213. *
  214. * @param i2s_num I2S port number
  215. *
  216. * @return
  217. * - ESP_OK Success
  218. * - ESP_ERR_INVALID_ARG Parameter error
  219. * - ESP_ERR_INVALID_STATE I2S port has been uninstalled by others (e.g. LCD i80)
  220. */
  221. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num);
  222. /**
  223. * @brief Write data to I2S DMA transmit buffer.
  224. *
  225. * @param i2s_num I2S port number
  226. *
  227. * @param src Source address to write from
  228. *
  229. * @param size Size of data in bytes
  230. *
  231. * @param[out] bytes_written Number of bytes written, if timeout, the result will be less than the size passed in.
  232. *
  233. * @param ticks_to_wait TX buffer wait timeout in RTOS ticks. If this
  234. * many ticks pass without space becoming available in the DMA
  235. * transmit buffer, then the function will return (note that if the
  236. * data is written to the DMA buffer in pieces, the overall operation
  237. * may still take longer than this timeout.) Pass portMAX_DELAY for no
  238. * timeout.
  239. *
  240. * @return
  241. * - ESP_OK Success
  242. * - ESP_ERR_INVALID_ARG Parameter error
  243. */
  244. esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait);
  245. /**
  246. * @brief Write data to I2S DMA transmit buffer while expanding the number of bits per sample. For example, expanding 16-bit PCM to 32-bit PCM.
  247. *
  248. * @param i2s_num I2S port number
  249. *
  250. * @param src Source address to write from
  251. *
  252. * @param size Size of data in bytes
  253. *
  254. * @param src_bits Source audio bit
  255. *
  256. * @param aim_bits Bit wanted, no more than 32, and must be greater than src_bits
  257. *
  258. * @param[out] bytes_written Number of bytes written, if timeout, the result will be less than the size passed in.
  259. *
  260. * @param ticks_to_wait TX buffer wait timeout in RTOS ticks. If this
  261. * many ticks pass without space becoming available in the DMA
  262. * transmit buffer, then the function will return (note that if the
  263. * data is written to the DMA buffer in pieces, the overall operation
  264. * may still take longer than this timeout.) Pass portMAX_DELAY for no
  265. * timeout.
  266. *
  267. * Format of the data in source buffer is determined by the I2S
  268. * configuration (see i2s_config_t).
  269. *
  270. * @return
  271. * - ESP_OK Success
  272. * - ESP_ERR_INVALID_ARG Parameter error
  273. */
  274. esp_err_t i2s_write_expand(i2s_port_t i2s_num, const void *src, size_t size, size_t src_bits, size_t aim_bits, size_t *bytes_written, TickType_t ticks_to_wait);
  275. /**
  276. * @brief Read data from I2S DMA receive buffer
  277. *
  278. * @param i2s_num I2S port number
  279. *
  280. * @param dest Destination address to read into
  281. *
  282. * @param size Size of data in bytes
  283. *
  284. * @param[out] bytes_read Number of bytes read, if timeout, bytes read will be less than the size passed in.
  285. *
  286. * @param ticks_to_wait RX buffer wait timeout in RTOS ticks. If this many ticks pass without bytes becoming available in the DMA receive buffer, then the function will return (note that if data is read from the DMA buffer in pieces, the overall operation may still take longer than this timeout.) Pass portMAX_DELAY for no timeout.
  287. *
  288. * @note If the built-in ADC mode is enabled, we should call i2s_adc_enable and i2s_adc_disable around the whole reading process,
  289. * to prevent the data getting corrupted.
  290. *
  291. * @return
  292. * - ESP_OK Success
  293. * - ESP_ERR_INVALID_ARG Parameter error
  294. */
  295. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait);
  296. /**
  297. * @brief Set sample rate used for I2S RX and TX.
  298. *
  299. * The bit clock rate is determined by the sample rate and i2s_config_t configuration parameters (number of channels, bits_per_sample).
  300. *
  301. * `bit_clock = rate * (number of channels) * bits_per_sample`
  302. *
  303. * @param i2s_num I2S port number
  304. *
  305. * @param rate I2S sample rate (ex: 8000, 44100...)
  306. *
  307. * @return
  308. * - ESP_OK Success
  309. * - ESP_ERR_INVALID_ARG Parameter error
  310. * - ESP_ERR_NO_MEM Out of memory
  311. */
  312. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate);
  313. /**
  314. * @brief Stop I2S driver
  315. *
  316. * There is no need to call i2s_stop() before calling i2s_driver_uninstall().
  317. *
  318. * Disables I2S TX/RX, until i2s_start() is called.
  319. *
  320. * @param i2s_num I2S port number
  321. *
  322. * @return
  323. * - ESP_OK Success
  324. * - ESP_ERR_INVALID_ARG Parameter error
  325. */
  326. esp_err_t i2s_stop(i2s_port_t i2s_num);
  327. /**
  328. * @brief Start I2S driver
  329. *
  330. * It is not necessary to call this function after i2s_driver_install() (it is started automatically), however it is necessary to call it after i2s_stop().
  331. *
  332. *
  333. * @param i2s_num I2S port number
  334. *
  335. * @return
  336. * - ESP_OK Success
  337. * - ESP_ERR_INVALID_ARG Parameter error
  338. */
  339. esp_err_t i2s_start(i2s_port_t i2s_num);
  340. /**
  341. * @brief Zero the contents of the TX DMA buffer.
  342. *
  343. * Pushes zero-byte samples into the TX DMA buffer, until it is full.
  344. *
  345. * @param i2s_num I2S port number
  346. *
  347. * @return
  348. * - ESP_OK Success
  349. * - ESP_ERR_INVALID_ARG Parameter error
  350. */
  351. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num);
  352. #if SOC_I2S_SUPPORTS_PCM
  353. /**
  354. * @brief Configure I2S a/u-law decompress or compress
  355. *
  356. * @note This function should be called after i2s driver installed
  357. * Only take effecttive when the i2s 'communication_format' is set to 'I2S_COMM_FORMAT_STAND_PCM_SHORT' or 'I2S_COMM_FORMAT_STAND_PCM_LONG'
  358. *
  359. * @param i2s_num I2S port number
  360. *
  361. * @param pcm_cfg including mode selection and a/u-law decompress or compress configuration paramater
  362. *
  363. * @return
  364. * - ESP_OK Success
  365. * - ESP_ERR_INVALID_ARG Parameter error
  366. */
  367. esp_err_t i2s_pcm_config(i2s_port_t i2s_num, const i2s_pcm_cfg_t *pcm_cfg);
  368. #endif
  369. /**
  370. * @brief Set clock & bit width used for I2S RX and TX.
  371. *
  372. * Similar to i2s_set_sample_rates(), but also sets bit width.
  373. *
  374. * 1. stop i2s;
  375. * 2. calculate mclk, bck, bck_factor
  376. * 3. malloc dma buffer;
  377. * 4. start i2s
  378. *
  379. * @param i2s_num I2S port number
  380. *
  381. * @param rate I2S sample rate (ex: 8000, 44100...)
  382. *
  383. * @param bits_cfg I2S bits configuration
  384. * the low 16 bits is for data bits per sample in one channel (see 'i2s_bits_per_sample_t')
  385. * the high 16 bits is for total bits in one channel (see 'i2s_bits_per_chan_t')
  386. * high 16bits =0 means same as the bits per sample.
  387. *
  388. * @param ch I2S channel, (I2S_CHANNEL_MONO, I2S_CHANNEL_STEREO or specific channel in TDM mode)
  389. *
  390. * @return
  391. * - ESP_OK Success
  392. * - ESP_ERR_INVALID_ARG Parameter error
  393. * - ESP_ERR_NO_MEM Out of memory
  394. */
  395. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, uint32_t bits_cfg, i2s_channel_t ch);
  396. /**
  397. * @brief get clock set on particular port number.
  398. *
  399. * @param i2s_num I2S port number
  400. *
  401. * @return
  402. * - actual clock set by i2s driver
  403. */
  404. float i2s_get_clk(i2s_port_t i2s_num);
  405. #if SOC_I2S_SUPPORTS_ADC
  406. /**
  407. * @brief Set built-in ADC mode for I2S DMA, this function will initialize ADC pad,
  408. * and set ADC parameters.
  409. * @note In this mode, the ADC maximum sampling rate is 150KHz. Set the sampling rate through ``i2s_config_t``.
  410. * @param adc_unit SAR ADC unit index
  411. * @param adc_channel ADC channel index
  412. * @return
  413. * - ESP_OK Success
  414. * - ESP_ERR_INVALID_ARG Parameter error
  415. */
  416. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel);
  417. /**
  418. * @brief Start to use I2S built-in ADC mode
  419. * @note This function would acquire the lock of ADC to prevent the data getting corrupted
  420. * during the I2S peripheral is being used to do fully continuous ADC sampling.
  421. *
  422. * @param i2s_num i2s port index
  423. * @return
  424. * - ESP_OK Success
  425. * - ESP_ERR_INVALID_ARG Parameter error
  426. * - ESP_ERR_INVALID_STATE Driver state error
  427. */
  428. esp_err_t i2s_adc_enable(i2s_port_t i2s_num);
  429. /**
  430. * @brief Stop to use I2S built-in ADC mode
  431. * @param i2s_num i2s port index
  432. * @note This function would release the lock of ADC so that other tasks can use ADC.
  433. * @return
  434. * - ESP_OK Success
  435. * - ESP_ERR_INVALID_ARG Parameter error
  436. * - ESP_ERR_INVALID_STATE Driver state error
  437. */
  438. esp_err_t i2s_adc_disable(i2s_port_t i2s_num);
  439. #endif // SOC_I2S_SUPPORTS_ADC
  440. #if SOC_I2S_SUPPORTS_DAC
  441. /**
  442. * @brief Set I2S dac mode, I2S built-in DAC is disabled by default
  443. *
  444. * @param dac_mode DAC mode configurations - see i2s_dac_mode_t
  445. *
  446. * @note Built-in DAC functions are only supported on I2S0 for current ESP32 chip.
  447. * If either of the built-in DAC channel are enabled, the other one can not
  448. * be used as RTC DAC function at the same time.
  449. *
  450. * @return
  451. * - ESP_OK Success
  452. * - ESP_ERR_INVALID_ARG Parameter error
  453. */
  454. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode);
  455. #endif //SOC_I2S_SUPPORTS_DAC
  456. #ifdef __cplusplus
  457. }
  458. #endif