i2s.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #ifndef _DRIVER_I2S_H_
  14. #define _DRIVER_I2S_H_
  15. #include "esp_err.h"
  16. #include <esp_types.h>
  17. #include "soc/soc.h"
  18. #include "soc/gpio_periph.h"
  19. #include "soc/i2s_periph.h"
  20. #include "soc/rtc_periph.h"
  21. #include "esp32/rom/gpio.h"
  22. #include "esp_attr.h"
  23. #include "esp_intr_alloc.h"
  24. #include "driver/periph_ctrl.h"
  25. #include "driver/adc.h"
  26. #include "freertos/semphr.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /**
  31. * @brief I2S bit width per sample.
  32. *
  33. */
  34. typedef enum {
  35. I2S_BITS_PER_SAMPLE_8BIT = 8, /*!< I2S bits per sample: 8-bits*/
  36. I2S_BITS_PER_SAMPLE_16BIT = 16, /*!< I2S bits per sample: 16-bits*/
  37. I2S_BITS_PER_SAMPLE_24BIT = 24, /*!< I2S bits per sample: 24-bits*/
  38. I2S_BITS_PER_SAMPLE_32BIT = 32, /*!< I2S bits per sample: 32-bits*/
  39. } i2s_bits_per_sample_t;
  40. /**
  41. * @brief I2S channel.
  42. *
  43. */
  44. typedef enum {
  45. I2S_CHANNEL_MONO = 1, /*!< I2S 1 channel (mono)*/
  46. I2S_CHANNEL_STEREO = 2 /*!< I2S 2 channel (stereo)*/
  47. } i2s_channel_t;
  48. /**
  49. * @brief I2S communication standard format
  50. *
  51. */
  52. typedef enum {
  53. I2S_COMM_FORMAT_I2S = 0x01, /*!< I2S communication format I2S*/
  54. I2S_COMM_FORMAT_I2S_MSB = 0x02, /*!< I2S format MSB*/
  55. I2S_COMM_FORMAT_I2S_LSB = 0x04, /*!< I2S format LSB*/
  56. I2S_COMM_FORMAT_PCM = 0x08, /*!< I2S communication format PCM*/
  57. I2S_COMM_FORMAT_PCM_SHORT = 0x10, /*!< PCM Short*/
  58. I2S_COMM_FORMAT_PCM_LONG = 0x20, /*!< PCM Long*/
  59. } i2s_comm_format_t;
  60. /**
  61. * @brief I2S channel format type
  62. */
  63. typedef enum {
  64. I2S_CHANNEL_FMT_RIGHT_LEFT = 0x00,
  65. I2S_CHANNEL_FMT_ALL_RIGHT,
  66. I2S_CHANNEL_FMT_ALL_LEFT,
  67. I2S_CHANNEL_FMT_ONLY_RIGHT,
  68. I2S_CHANNEL_FMT_ONLY_LEFT,
  69. } i2s_channel_fmt_t;
  70. /**
  71. * @brief PDM sample rate ratio, measured in Hz.
  72. *
  73. */
  74. typedef enum {
  75. PDM_SAMPLE_RATE_RATIO_64,
  76. PDM_SAMPLE_RATE_RATIO_128,
  77. } pdm_sample_rate_ratio_t;
  78. /**
  79. * @brief PDM PCM convter enable/disable.
  80. *
  81. */
  82. typedef enum {
  83. PDM_PCM_CONV_ENABLE,
  84. PDM_PCM_CONV_DISABLE,
  85. } pdm_pcm_conv_t;
  86. /**
  87. * @brief I2S Peripheral, 0 & 1.
  88. *
  89. */
  90. typedef enum {
  91. I2S_NUM_0 = 0x0, /*!< I2S 0*/
  92. I2S_NUM_1 = 0x1, /*!< I2S 1*/
  93. I2S_NUM_MAX,
  94. } i2s_port_t;
  95. /**
  96. * @brief I2S Mode, defaut is I2S_MODE_MASTER | I2S_MODE_TX
  97. *
  98. * @note PDM and built-in DAC functions are only supported on I2S0 for current ESP32 chip.
  99. *
  100. */
  101. typedef enum {
  102. I2S_MODE_MASTER = 1,
  103. I2S_MODE_SLAVE = 2,
  104. I2S_MODE_TX = 4,
  105. I2S_MODE_RX = 8,
  106. I2S_MODE_DAC_BUILT_IN = 16, /*!< Output I2S data to built-in DAC, no matter the data format is 16bit or 32 bit, the DAC module will only take the 8bits from MSB*/
  107. I2S_MODE_ADC_BUILT_IN = 32, /*!< Input I2S data from built-in ADC, each data can be 12-bit width at most*/
  108. I2S_MODE_PDM = 64,
  109. } i2s_mode_t;
  110. /**
  111. * @brief I2S configuration parameters for i2s_param_config function
  112. *
  113. */
  114. typedef struct {
  115. i2s_mode_t mode; /*!< I2S work mode*/
  116. int sample_rate; /*!< I2S sample rate*/
  117. i2s_bits_per_sample_t bits_per_sample; /*!< I2S bits per sample*/
  118. i2s_channel_fmt_t channel_format; /*!< I2S channel format */
  119. i2s_comm_format_t communication_format; /*!< I2S communication format */
  120. 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 */
  121. int dma_buf_count; /*!< I2S DMA Buffer Count */
  122. int dma_buf_len; /*!< I2S DMA Buffer Length */
  123. bool use_apll; /*!< I2S using APLL as main I2S clock, enable it to get accurate clock */
  124. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor if there is underflow condition (helps in avoiding noise in case of data unavailability) */
  125. 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.*/
  126. } i2s_config_t;
  127. /**
  128. * @brief I2S event types
  129. *
  130. */
  131. typedef enum {
  132. I2S_EVENT_DMA_ERROR,
  133. I2S_EVENT_TX_DONE, /*!< I2S DMA finish sent 1 buffer*/
  134. I2S_EVENT_RX_DONE, /*!< I2S DMA finish received 1 buffer*/
  135. I2S_EVENT_MAX, /*!< I2S event max index*/
  136. } i2s_event_type_t;
  137. /**
  138. * @brief I2S DAC mode for i2s_set_dac_mode.
  139. *
  140. * @note PDM and built-in DAC functions are only supported on I2S0 for current ESP32 chip.
  141. */
  142. typedef enum {
  143. I2S_DAC_CHANNEL_DISABLE = 0, /*!< Disable I2S built-in DAC signals*/
  144. I2S_DAC_CHANNEL_RIGHT_EN = 1, /*!< Enable I2S built-in DAC right channel, maps to DAC channel 1 on GPIO25*/
  145. I2S_DAC_CHANNEL_LEFT_EN = 2, /*!< Enable I2S built-in DAC left channel, maps to DAC channel 2 on GPIO26*/
  146. I2S_DAC_CHANNEL_BOTH_EN = 0x3, /*!< Enable both of the I2S built-in DAC channels.*/
  147. I2S_DAC_CHANNEL_MAX = 0x4, /*!< I2S built-in DAC mode max index*/
  148. } i2s_dac_mode_t;
  149. /**
  150. * @brief Event structure used in I2S event queue
  151. *
  152. */
  153. typedef struct {
  154. i2s_event_type_t type; /*!< I2S event type */
  155. size_t size; /*!< I2S data size for I2S_DATA event*/
  156. } i2s_event_t;
  157. #define I2S_PIN_NO_CHANGE (-1) /*!< Use in i2s_pin_config_t for pins which should not be changed */
  158. /**
  159. * @brief I2S pin number for i2s_set_pin
  160. *
  161. */
  162. typedef struct {
  163. int bck_io_num; /*!< BCK in out pin*/
  164. int ws_io_num; /*!< WS in out pin*/
  165. int data_out_num; /*!< DATA out pin*/
  166. int data_in_num; /*!< DATA in pin*/
  167. } i2s_pin_config_t;
  168. /**
  169. * @brief I2S PDM RX downsample mode
  170. */
  171. typedef enum {
  172. I2S_PDM_DSR_8S = 0, /*!< downsampling number is 8 for PDM RX mode*/
  173. I2S_PDM_DSR_16S, /*!< downsampling number is 16 for PDM RX mode*/
  174. I2S_PDM_DSR_MAX,
  175. } i2s_pdm_dsr_t;
  176. typedef intr_handle_t i2s_isr_handle_t;
  177. /**
  178. * @brief Set I2S pin number
  179. *
  180. * @note
  181. * The I2S peripheral output signals can be connected to multiple GPIO pads.
  182. * However, the I2S peripheral input signal can only be connected to one GPIO pad.
  183. *
  184. * @param i2s_num I2S_NUM_0 or I2S_NUM_1
  185. *
  186. * @param pin I2S Pin structure, or NULL to set 2-channel 8-bit internal DAC pin configuration (GPIO25 & GPIO26)
  187. *
  188. * Inside the pin configuration structure, set I2S_PIN_NO_CHANGE for any pin where
  189. * the current configuration should not be changed.
  190. *
  191. * @note if *pin is set as NULL, this function will initialize both of the built-in DAC channels by default.
  192. * 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.
  193. *
  194. * @return
  195. * - ESP_OK Success
  196. * - ESP_ERR_INVALID_ARG Parameter error
  197. * - ESP_FAIL IO error
  198. */
  199. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin);
  200. /**
  201. * @brief Set PDM mode down-sample rate
  202. * In PDM RX mode, there would be 2 rounds of downsample process in hardware.
  203. * In the first downsample process, the sampling number can be 16 or 8.
  204. * In the second downsample process, the sampling number is fixed as 8.
  205. * So the clock frequency in PDM RX mode would be (fpcm * 64) or (fpcm * 128) accordingly.
  206. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  207. * @param dsr i2s RX down sample rate for PDM mode.
  208. *
  209. * @note After calling this function, it would call i2s_set_clk inside to update the clock frequency.
  210. * Please call this function after I2S driver has been initialized.
  211. *
  212. * @return
  213. * - ESP_OK Success
  214. * - ESP_ERR_INVALID_ARG Parameter error
  215. * - ESP_ERR_NO_MEM Out of memory
  216. */
  217. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr);
  218. /**
  219. * @brief Set I2S dac mode, I2S built-in DAC is disabled by default
  220. *
  221. * @param dac_mode DAC mode configurations - see i2s_dac_mode_t
  222. *
  223. * @note Built-in DAC functions are only supported on I2S0 for current ESP32 chip.
  224. * If either of the built-in DAC channel are enabled, the other one can not
  225. * be used as RTC DAC function at the same time.
  226. *
  227. * @return
  228. * - ESP_OK Success
  229. * - ESP_ERR_INVALID_ARG Parameter error
  230. */
  231. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode);
  232. /**
  233. * @brief Install and start I2S driver.
  234. *
  235. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  236. *
  237. * @param i2s_config I2S configurations - see i2s_config_t struct
  238. *
  239. * @param queue_size I2S event queue size/depth.
  240. *
  241. * @param i2s_queue I2S event queue handle, if set NULL, driver will not use an event queue.
  242. *
  243. * This function must be called before any I2S driver read/write operations.
  244. *
  245. * @return
  246. * - ESP_OK Success
  247. * - ESP_ERR_INVALID_ARG Parameter error
  248. * - ESP_ERR_NO_MEM Out of memory
  249. */
  250. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue);
  251. /**
  252. * @brief Uninstall I2S driver.
  253. *
  254. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  255. *
  256. * @return
  257. * - ESP_OK Success
  258. * - ESP_ERR_INVALID_ARG Parameter error
  259. */
  260. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num);
  261. /**
  262. * @brief Write data to I2S DMA transmit buffer.
  263. *
  264. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  265. *
  266. * @param src Source address to write from
  267. *
  268. * @param size Size of data in bytes
  269. *
  270. * @param[out] bytes_written Number of bytes written, if timeout, the result will be less than the size passed in.
  271. *
  272. * @param ticks_to_wait TX buffer wait timeout in RTOS ticks. If this
  273. * many ticks pass without space becoming available in the DMA
  274. * transmit buffer, then the function will return (note that if the
  275. * data is written to the DMA buffer in pieces, the overall operation
  276. * may still take longer than this timeout.) Pass portMAX_DELAY for no
  277. * timeout.
  278. *
  279. * @return
  280. * - ESP_OK Success
  281. * - ESP_ERR_INVALID_ARG Parameter error
  282. */
  283. 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);
  284. /**
  285. * @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.
  286. *
  287. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  288. *
  289. * @param src Source address to write from
  290. *
  291. * @param size Size of data in bytes
  292. *
  293. * @param src_bits Source audio bit
  294. *
  295. * @param aim_bits Bit wanted, no more than 32, and must be greater than src_bits
  296. *
  297. * @param[out] bytes_written Number of bytes written, if timeout, the result will be less than the size passed in.
  298. *
  299. * @param ticks_to_wait TX buffer wait timeout in RTOS ticks. If this
  300. * many ticks pass without space becoming available in the DMA
  301. * transmit buffer, then the function will return (note that if the
  302. * data is written to the DMA buffer in pieces, the overall operation
  303. * may still take longer than this timeout.) Pass portMAX_DELAY for no
  304. * timeout.
  305. *
  306. * Format of the data in source buffer is determined by the I2S
  307. * configuration (see i2s_config_t).
  308. *
  309. * @return
  310. * - ESP_OK Success
  311. * - ESP_ERR_INVALID_ARG Parameter error
  312. */
  313. 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);
  314. /**
  315. * @brief Read data from I2S DMA receive buffer
  316. *
  317. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  318. *
  319. * @param dest Destination address to read into
  320. *
  321. * @param size Size of data in bytes
  322. *
  323. * @param[out] bytes_read Number of bytes read, if timeout, bytes read will be less than the size passed in.
  324. *
  325. * @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.
  326. *
  327. * @note If the built-in ADC mode is enabled, we should call i2s_adc_start and i2s_adc_stop around the whole reading process,
  328. * to prevent the data getting corrupted.
  329. *
  330. * @return
  331. * - ESP_OK Success
  332. * - ESP_ERR_INVALID_ARG Parameter error
  333. */
  334. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait);
  335. /**
  336. * @brief Set sample rate used for I2S RX and TX.
  337. *
  338. * The bit clock rate is determined by the sample rate and i2s_config_t configuration parameters (number of channels, bits_per_sample).
  339. *
  340. * `bit_clock = rate * (number of channels) * bits_per_sample`
  341. *
  342. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  343. *
  344. * @param rate I2S sample rate (ex: 8000, 44100...)
  345. *
  346. * @return
  347. * - ESP_OK Success
  348. * - ESP_ERR_INVALID_ARG Parameter error
  349. * - ESP_ERR_NO_MEM Out of memory
  350. */
  351. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate);
  352. /**
  353. * @brief Stop I2S driver
  354. *
  355. * Disables I2S TX/RX, until i2s_start() is called.
  356. *
  357. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  358. *
  359. * @return
  360. * - ESP_OK Success
  361. * - ESP_ERR_INVALID_ARG Parameter error
  362. */
  363. esp_err_t i2s_stop(i2s_port_t i2s_num);
  364. /**
  365. * @brief Start I2S driver
  366. *
  367. * 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().
  368. *
  369. *
  370. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  371. *
  372. * @return
  373. * - ESP_OK Success
  374. * - ESP_ERR_INVALID_ARG Parameter error
  375. */
  376. esp_err_t i2s_start(i2s_port_t i2s_num);
  377. /**
  378. * @brief Zero the contents of the TX DMA buffer.
  379. *
  380. * Pushes zero-byte samples into the TX DMA buffer, until it is full.
  381. *
  382. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  383. *
  384. * @return
  385. * - ESP_OK Success
  386. * - ESP_ERR_INVALID_ARG Parameter error
  387. */
  388. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num);
  389. /**
  390. * @brief Set clock & bit width used for I2S RX and TX.
  391. *
  392. * Similar to i2s_set_sample_rates(), but also sets bit width.
  393. *
  394. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  395. *
  396. * @param rate I2S sample rate (ex: 8000, 44100...)
  397. *
  398. * @param bits I2S bit width (I2S_BITS_PER_SAMPLE_16BIT, I2S_BITS_PER_SAMPLE_24BIT, I2S_BITS_PER_SAMPLE_32BIT)
  399. *
  400. * @param ch I2S channel, (I2S_CHANNEL_MONO, I2S_CHANNEL_STEREO)
  401. *
  402. * @return
  403. * - ESP_OK Success
  404. * - ESP_ERR_INVALID_ARG Parameter error
  405. * - ESP_ERR_NO_MEM Out of memory
  406. */
  407. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch);
  408. /**
  409. * @brief get clock set on particular port number.
  410. *
  411. * @param i2s_num I2S_NUM_0, I2S_NUM_1
  412. *
  413. * @return
  414. * - actual clock set by i2s driver
  415. */
  416. float i2s_get_clk(i2s_port_t i2s_num);
  417. /**
  418. * @brief Set built-in ADC mode for I2S DMA, this function will initialize ADC pad,
  419. * and set ADC parameters.
  420. * @param adc_unit SAR ADC unit index
  421. * @param adc_channel ADC channel index
  422. * @return
  423. * - ESP_OK Success
  424. * - ESP_ERR_INVALID_ARG Parameter error
  425. */
  426. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel);
  427. /**
  428. * @brief Start to use I2S built-in ADC mode
  429. * @note This function would acquire the lock of ADC to prevent the data getting corrupted
  430. * during the I2S peripheral is being used to do fully continuous ADC sampling.
  431. *
  432. * @param i2s_num i2s port index
  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_enable(i2s_port_t i2s_num);
  439. /**
  440. * @brief Stop to use I2S built-in ADC mode
  441. * @param i2s_num i2s port index
  442. * @note This function would release the lock of ADC so that other tasks can use ADC.
  443. * @return
  444. * - ESP_OK Success
  445. * - ESP_ERR_INVALID_ARG Parameter error
  446. * - ESP_ERR_INVALID_STATE Driver state error
  447. */
  448. esp_err_t i2s_adc_disable(i2s_port_t i2s_num);
  449. #ifdef __cplusplus
  450. }
  451. #endif
  452. #endif /* _DRIVER_I2S_H_ */