i2s.rst 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. I2S
  2. ===
  3. Overview
  4. --------
  5. ESP32 contains two I2S peripherals. These peripherals can be configured to input and output sample data via the I2S driver.
  6. The I2S peripheral supports DMA meaning it can stream sample data without requiring each sample to be read or written by the CPU.
  7. I2S output can also be routed directly to the Digital/Analog Converter output channels (GPIO 25 & GPIO 26) to produce analog output directly, rather than via an external I2S codec.
  8. Application Example
  9. -------------------
  10. A full I2S example is available in esp-idf: :example:`peripherals/i2s`.
  11. Short example of I2S configuration::
  12. #include "driver/i2s.h"
  13. #include "freertos/queue.h"
  14. static const int i2s_num = 0; // i2s port number
  15. static const i2s_config_t i2s_config = {
  16. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  17. .sample_rate = 44100,
  18. .bits_per_sample = 16,
  19. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  20. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  21. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
  22. .dma_buf_count = 8,
  23. .dma_buf_len = 64
  24. };
  25. static const i2s_pin_config_t pin_config = {
  26. .bck_io_num = 26,
  27. .ws_io_num = 25,
  28. .data_out_num = 22,
  29. .data_in_num = I2S_PIN_NO_CHANGE
  30. };
  31. ...
  32. i2s_driver_install(i2s_num, &i2s_config, 0, NULL); //install and start i2s driver
  33. i2s_set_pin(i2s_num, &pin_config);
  34. i2s_set_sample_rates(i2s_num, 22050); //set sample rates
  35. i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver
  36. Short example configuring I2S to use internal DAC for analog output::
  37. #include "driver/i2s.h"
  38. #include "freertos/queue.h"
  39. static const int i2s_num = 0; // i2s port number
  40. static const i2s_config_t i2s_config = {
  41. .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
  42. .sample_rate = 44100,
  43. .bits_per_sample = 16, /* the DAC module will only take the 8bits from MSB */
  44. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  45. .communication_format = I2S_COMM_FORMAT_I2S_MSB,
  46. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
  47. .dma_buf_count = 8,
  48. .dma_buf_len = 64
  49. };
  50. ...
  51. i2s_driver_install(i2s_num, &i2s_config, 0, NULL); //install and start i2s driver
  52. i2s_set_pin(i2s_num, NULL); //for internal DAC, this will enable both of the internal channels
  53. //You can call i2s_set_dac_mode to set built-in DAC output mode.
  54. //i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  55. i2s_set_sample_rates(i2s_num, 22050); //set sample rates
  56. i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver
  57. API Reference
  58. -------------
  59. Header Files
  60. ^^^^^^^^^^^^
  61. * `components/driver/include/driver/i2s.h`
  62. Data Structures
  63. ^^^^^^^^^^^^^^^
  64. .. doxygenstruct:: i2s_config_t
  65. :members:
  66. .. doxygenstruct:: i2s_event_t
  67. :members:
  68. .. doxygenstruct:: i2s_pin_config_t
  69. :members:
  70. Macros
  71. ^^^^^^
  72. .. doxygendefine:: I2S_PIN_NO_CHANGE
  73. Enumerations
  74. ^^^^^^^^^^^^
  75. .. doxygenenum:: i2s_bits_per_sample_t
  76. .. doxygenenum:: i2s_comm_format_t
  77. .. doxygenenum:: i2s_channel_fmt_t
  78. .. doxygenenum:: pdm_sample_rate_ratio_t
  79. .. doxygenenum:: pdm_pcm_conv_t
  80. .. doxygenenum:: i2s_port_t
  81. .. doxygenenum:: i2s_mode_t
  82. .. doxygenenum:: i2s_event_type_t
  83. .. doxygenenum:: i2s_dac_mode_t
  84. Functions
  85. ^^^^^^^^^
  86. .. doxygenfunction:: i2s_set_pin
  87. .. doxygenfunction:: i2s_set_dac_mode
  88. .. doxygenfunction:: i2s_driver_install
  89. .. doxygenfunction:: i2s_driver_uninstall
  90. .. doxygenfunction:: i2s_write_bytes
  91. .. doxygenfunction:: i2s_read_bytes
  92. .. doxygenfunction:: i2s_push_sample
  93. .. doxygenfunction:: i2s_pop_sample
  94. .. doxygenfunction:: i2s_set_sample_rates
  95. .. doxygenfunction:: i2s_start
  96. .. doxygenfunction:: i2s_stop
  97. .. doxygenfunction:: i2s_zero_dma_buffer