i2s.rst 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. I2S
  2. ===
  3. .. only:: esp32c3
  4. .. warning::
  5. This document is not updated for ESP32-C3 yet.
  6. Overview
  7. --------
  8. I2S (Inter-IC Sound) is a serial, synchronous communication protocol that is usually used for transmitting audio data between two digital audio devices.
  9. .. only:: esp32
  10. {IDF_TARGET_NAME} contains two I2S peripherals. These peripherals can be configured to input and output sample data via the I2S driver.
  11. .. only:: esp32s2
  12. {IDF_TARGET_NAME} contains one I2S peripheral. These peripherals can be configured to input and output sample data via the I2S driver.
  13. An I2S bus consists of the following lines:
  14. - Bit clock line
  15. - Channel select line
  16. - Serial data line
  17. Each I2S controller has the following features that can be configured using the I2S driver:
  18. - Operation as system master or slave
  19. - Capable of acting as transmitter or receiver
  20. - Dedicated DMA controller that allows for streaming sample data without requiring the CPU to copy each data sample
  21. Each controller can operate in half-duplex communication mode. Thus, the two controllers can be combined to establish full-duplex communication.
  22. I2S0 output can be routed directly to the digital-to-analog converter's (DAC) output channels (GPIO 25 & GPIO 26) to produce direct analog output without involving any external I2S codecs. I2S0 can also be used for transmitting PDM (Pulse-density modulation) signals.
  23. The I2S peripherals also support LCD mode for communicating data over a parallel bus, as used by some LCD displays and camera modules. LCD mode has the following operational modes:
  24. - LCD master transmitting mode
  25. - Camera slave receiving mode
  26. - ADC/DAC mode
  27. .. only:: esp32
  28. For more information, see *{IDF_TARGET_NAME} Technical Reference Manual* > *I2S Controller (I2S)* > LCD Mode [`PDF <{IDF_TARGET_TRM_EN_URL}#camlcdctrl>`__].
  29. .. note::
  30. For high accuracy clock applications, use the APLL_CLK clock source, which has the frequency range of 16 ~ 128 MHz. You can enable the APLL_CLK clock source by setting :cpp:member:`i2s_config_t::use_apll` to ``TRUE``.
  31. If :cpp:member:`i2s_config_t::use_apll` = ``TRUE`` and :cpp:member:`i2s_config_t::fixed_mclk` > ``0``, then the master clock output frequency for I2S will be equal to the value of :cpp:member:`i2s_config_t::fixed_mclk`, which means that the mclk frequency is provided by the user, instead of being calculated by the driver.
  32. The clock rate of the word select line, which is called audio left-right clock rate (LRCK) here, is always the divisor of the master clock output frequency and for which the following is always true: 0 < MCLK/LRCK/channels/bits_per_sample < 64.
  33. Functional Overview
  34. -------------------
  35. Installing the Driver
  36. ^^^^^^^^^^^^^^^^^^^^^
  37. Install the I2S driver by calling the function :cpp:func`i2s_driver_install` and passing the following arguments:
  38. - Port number
  39. - The structure :cpp:type:`i2s_config_t` with defined communication parameters
  40. - Event queue size and handle
  41. Configuration example:
  42. .. code-block:: c
  43. static const int i2s_num = 0; // i2s port number
  44. static const i2s_config_t i2s_config = {
  45. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  46. .sample_rate = 44100,
  47. .bits_per_sample = 16,
  48. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  49. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  50. .intr_alloc_flags = 0, // default interrupt priority
  51. .dma_buf_count = 8,
  52. .dma_buf_len = 64,
  53. .use_apll = false
  54. };
  55. i2s_driver_install(I2S_NUM, &i2s_config, 0, NULL);
  56. Setting Communication Pins
  57. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  58. Once the driver is installed, configure physical GPIO pins to which signals will be routed. For this, call the function :cpp:func`i2s_set_pin` and pass the following arguments to it:
  59. - Port number
  60. - The structure :cpp:type:`i2s_pin_config_t` defining the GPIO pin numbers to which the driver should route the BCK, WS, DATA out, and DATA in signals. If you want to keep a currently allocated pin number for a specific signal, or if this signal is unused, then pass the macro :c:macro:`I2S_PIN_NO_CHANGE`. See the example below.
  61. .. code-block:: c
  62. static const i2s_pin_config_t pin_config = {
  63. .bck_io_num = 26,
  64. .ws_io_num = 25,
  65. .data_out_num = 22,
  66. .data_in_num = I2S_PIN_NO_CHANGE
  67. };
  68. i2s_set_pin(i2s_num, &pin_config);
  69. Running I2S Communication
  70. ^^^^^^^^^^^^^^^^^^^^^^^^^
  71. To perform a transmission:
  72. - Prepare the data for sending
  73. - Call the function :cpp:func:`i2s_write` and pass the data buffer address and data length to it
  74. The function will write the data to the I2S DMA Tx buffer, and then the data will be transmitted automatically.
  75. .. code-block:: c
  76. i2s_write(I2S_NUM, samples_data, ((bits+8)/16)*SAMPLE_PER_CYCLE*4, &i2s_bytes_write, 100);
  77. To retrieve received data, use the function :cpp:func:`i2s_read`. It will retrieve the data from the I2S DMA Rx buffer, once the data is received by the I2S controller.
  78. You can temporarily stop the I2S driver by calling the function :cpp:func:`i2s_stop`, which will disable the I2S Tx/Rx units until the function :cpp:func:`i2s_start` is called. If the function :cpp:func`i2s_driver_install` is used, the driver will start up automatically eliminating the need to call :cpp:func:`i2s_start`.
  79. Deleting the Driver
  80. ^^^^^^^^^^^^^^^^^^^
  81. If the established communication is no longer required, the driver can be removed to free allocated resources by calling :cpp:func:`i2s_driver_uninstall`.
  82. Application Example
  83. -------------------
  84. A code example for the I2S driver can be found in the directory :example:`peripherals/i2s`.
  85. In addition, there are two short configuration examples for the I2S driver.
  86. I2S configuration
  87. ^^^^^^^^^^^^^^^^^
  88. .. code-block:: c
  89. #include "driver/i2s.h"
  90. #include "freertos/queue.h"
  91. static const int i2s_num = 0; // i2s port number
  92. static const i2s_config_t i2s_config = {
  93. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  94. .sample_rate = 44100,
  95. .bits_per_sample = 16,
  96. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  97. .communication_format = I2S_COMM_FORMAT_STAND_I2S,
  98. .intr_alloc_flags = 0, // default interrupt priority
  99. .dma_buf_count = 8,
  100. .dma_buf_len = 64,
  101. .use_apll = false
  102. };
  103. static const i2s_pin_config_t pin_config = {
  104. .bck_io_num = 26,
  105. .ws_io_num = 25,
  106. .data_out_num = 22,
  107. .data_in_num = I2S_PIN_NO_CHANGE
  108. };
  109. ...
  110. i2s_driver_install(i2s_num, &i2s_config, 0, NULL); //install and start i2s driver
  111. i2s_set_pin(i2s_num, &pin_config);
  112. i2s_set_sample_rates(i2s_num, 22050); //set sample rates
  113. i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver
  114. Configuring I2S to use internal DAC for analog output
  115. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  116. .. code-block:: c
  117. #include "driver/i2s.h"
  118. #include "freertos/queue.h"
  119. static const int i2s_num = 0; // i2s port number
  120. static const i2s_config_t i2s_config = {
  121. .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
  122. .sample_rate = 44100,
  123. .bits_per_sample = 16, /* the DAC module will only take the 8bits from MSB */
  124. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  125. .intr_alloc_flags = 0, // default interrupt priority
  126. .dma_buf_count = 8,
  127. .dma_buf_len = 64,
  128. .use_apll = false
  129. };
  130. ...
  131. i2s_driver_install(i2s_num, &i2s_config, 0, NULL); //install and start i2s driver
  132. i2s_set_pin(i2s_num, NULL); //for internal DAC, this will enable both of the internal channels
  133. //You can call i2s_set_dac_mode to set built-in DAC output mode.
  134. //i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  135. i2s_set_sample_rates(i2s_num, 22050); //set sample rates
  136. i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver
  137. API Reference
  138. -------------
  139. .. include-build-file:: inc/i2s.inc
  140. .. include-build-file:: inc/i2s_types.inc