i2s.rst 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 = 8, /* must be 8 for built-in DAC */
  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
  53. i2s_set_sample_rates(i2s_num, 22050); //set sample rates
  54. i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver
  55. API Reference
  56. -------------
  57. Header Files
  58. ^^^^^^^^^^^^
  59. * `components/driver/include/driver/i2s.h`
  60. Data Structures
  61. ^^^^^^^^^^^^^^^
  62. .. doxygenstruct:: i2s_config_t
  63. :members:
  64. .. doxygenstruct:: i2s_event_t
  65. :members:
  66. .. doxygenstruct:: i2s_pin_config_t
  67. :members:
  68. Macros
  69. ^^^^^^
  70. .. doxygendefine:: I2S_PIN_NO_CHANGE
  71. Enumerations
  72. ^^^^^^^^^^^^
  73. .. doxygenenum:: i2s_bits_per_sample_t
  74. .. doxygenenum:: i2s_comm_format_t
  75. .. doxygenenum:: i2s_channel_fmt_t
  76. .. doxygenenum:: pdm_sample_rate_ratio_t
  77. .. doxygenenum:: pdm_pcm_conv_t
  78. .. doxygenenum:: i2s_port_t
  79. .. doxygenenum:: i2s_mode_t
  80. .. doxygenenum:: i2s_event_type_t
  81. Functions
  82. ^^^^^^^^^
  83. .. doxygenfunction:: i2s_set_pin
  84. .. doxygenfunction:: i2s_driver_install
  85. .. doxygenfunction:: i2s_driver_uninstall
  86. .. doxygenfunction:: i2s_write_bytes
  87. .. doxygenfunction:: i2s_read_bytes
  88. .. doxygenfunction:: i2s_push_sample
  89. .. doxygenfunction:: i2s_pop_sample
  90. .. doxygenfunction:: i2s_set_sample_rates
  91. .. doxygenfunction:: i2s_start
  92. .. doxygenfunction:: i2s_stop
  93. .. doxygenfunction:: i2s_zero_dma_buffer