i2s.rst 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. .. highlight:: c
  13. ::
  14. #include "driver/i2s.h"
  15. #include "freertos/queue.h"
  16. static const int i2s_num = 0; // i2s port number
  17. static const i2s_config_t i2s_config = {
  18. .mode = I2S_MODE_MASTER | I2S_MODE_TX,
  19. .sample_rate = 44100,
  20. .bits_per_sample = 16,
  21. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  22. .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
  23. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
  24. .dma_buf_count = 8,
  25. .dma_buf_len = 64
  26. };
  27. static const i2s_pin_config_t pin_config = {
  28. .bck_io_num = 26,
  29. .ws_io_num = 25,
  30. .data_out_num = 22,
  31. .data_in_num = I2S_PIN_NO_CHANGE
  32. };
  33. ...
  34. i2s_driver_install(i2s_num, &i2s_config, 0, NULL); //install and start i2s driver
  35. i2s_set_pin(i2s_num, &pin_config);
  36. i2s_set_sample_rates(i2s_num, 22050); //set sample rates
  37. i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver
  38. Short example configuring I2S to use internal DAC for analog output::
  39. #include "driver/i2s.h"
  40. #include "freertos/queue.h"
  41. static const int i2s_num = 0; // i2s port number
  42. static const i2s_config_t i2s_config = {
  43. .mode = I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN,
  44. .sample_rate = 44100,
  45. .bits_per_sample = 16, /* the DAC module will only take the 8bits from MSB */
  46. .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
  47. .communication_format = I2S_COMM_FORMAT_I2S_MSB,
  48. .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
  49. .dma_buf_count = 8,
  50. .dma_buf_len = 64
  51. };
  52. ...
  53. i2s_driver_install(i2s_num, &i2s_config, 0, NULL); //install and start i2s driver
  54. i2s_set_pin(i2s_num, NULL); //for internal DAC, this will enable both of the internal channels
  55. //You can call i2s_set_dac_mode to set built-in DAC output mode.
  56. //i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  57. i2s_set_sample_rates(i2s_num, 22050); //set sample rates
  58. i2s_driver_uninstall(i2s_num); //stop & destroy i2s driver
  59. API Reference
  60. -------------
  61. .. include:: /_build/inc/i2s.inc