i2s_std.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * This file is specified for I2S standard communication mode
  8. * Features:
  9. * - Philip/MSB/PCM are supported in standard mode
  10. * - Fixed to 2 slots
  11. */
  12. #pragma once
  13. #include "hal/i2s_types.h"
  14. #ifdef __cplusplus
  15. extern "C" {
  16. #endif
  17. #if SOC_I2S_HW_VERSION_1 // For esp32/esp32-s2
  18. /**
  19. * @brief Philip format in 2 slots
  20. * @param bits_per_sample i2s data bit width
  21. * @param mono_or_stereo I2S_SLOT_MODE_MONO or I2S_SLOT_MODE_STEREO
  22. */
  23. #define I2S_STD_PHILIP_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) { \
  24. .mode = I2S_COMM_MODE_STD, \
  25. .data_bit_width = bits_per_sample, \
  26. .slot_bit_width = I2S_SLOT_BIT_WIDTH_DEFAULT, \
  27. .slot_mode = mono_or_stereo, \
  28. .ws_width = bits_per_sample, \
  29. .ws_pol = false, \
  30. .bit_shift = true, \
  31. .msb_right = false, \
  32. }
  33. /**
  34. * @brief PCM(short) format in 2 slots
  35. * @note PCM(long) is sample as philip in 2 slots
  36. * @param bits_per_sample i2s data bit width
  37. * @param mono_or_stereo I2S_SLOT_MODE_MONO or I2S_SLOT_MODE_STEREO
  38. */
  39. #define I2S_STD_PCM_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) { \
  40. .mode = I2S_COMM_MODE_STD, \
  41. .data_bit_width = bits_per_sample, \
  42. .slot_bit_width = I2S_SLOT_BIT_WIDTH_DEFAULT, \
  43. .slot_mode = mono_or_stereo, \
  44. .ws_width = 1, \
  45. .ws_pol = true, \
  46. .bit_shift = true, \
  47. .msb_right = false, \
  48. }
  49. /**
  50. * @brief MSB format in 2 slots
  51. * @param bits_per_sample i2s data bit width
  52. * @param mono_or_stereo I2S_SLOT_MODE_MONO or I2S_SLOT_MODE_STEREO
  53. */
  54. #define I2S_STD_MSB_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) { \
  55. .mode = I2S_COMM_MODE_STD, \
  56. .data_bit_width = bits_per_sample, \
  57. .slot_bit_width = I2S_SLOT_BIT_WIDTH_DEFAULT, \
  58. .slot_mode = mono_or_stereo, \
  59. .ws_width = bits_per_sample, \
  60. .ws_pol = false, \
  61. .bit_shift = false, \
  62. .msb_right = false, \
  63. }
  64. #else
  65. /**
  66. * @brief Philip format in 2 slots
  67. * @param bits_per_sample i2s data bit width
  68. * @param mono_or_stereo I2S_SLOT_MODE_MONO or I2S_SLOT_MODE_STEREO
  69. */
  70. #define I2S_STD_PHILIP_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) { \
  71. .mode = I2S_COMM_MODE_STD, \
  72. .data_bit_width = bits_per_sample, \
  73. .slot_bit_width = I2S_SLOT_BIT_WIDTH_DEFAULT, \
  74. .slot_mode = mono_or_stereo, \
  75. .ws_width = bits_per_sample, \
  76. .ws_pol = false, \
  77. .bit_shift = true, \
  78. .left_align = false, \
  79. .big_endian = false, \
  80. .bit_order_lsb = false \
  81. }
  82. /**
  83. * @brief PCM(short) format in 2 slots
  84. * @note PCM(long) is sample as philip in 2 slots
  85. * @param bits_per_sample i2s data bit width
  86. * @param mono_or_stereo I2S_SLOT_MODE_MONO or I2S_SLOT_MODE_STEREO
  87. */
  88. #define I2S_STD_PCM_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) { \
  89. .mode = I2S_COMM_MODE_STD, \
  90. .data_bit_width = bits_per_sample, \
  91. .slot_bit_width = I2S_SLOT_BIT_WIDTH_DEFAULT, \
  92. .slot_mode = mono_or_stereo, \
  93. .ws_width = 1, \
  94. .ws_pol = true, \
  95. .bit_shift = true, \
  96. .left_align = false, \
  97. .big_endian = false, \
  98. .bit_order_lsb = false \
  99. }
  100. /**
  101. * @brief MSB format in 2 slots
  102. * @param bits_per_sample i2s data bit width
  103. * @param mono_or_stereo I2S_SLOT_MODE_MONO or I2S_SLOT_MODE_STEREO
  104. */
  105. #define I2S_STD_MSB_SLOT_DEFAULT_CONFIG(bits_per_sample, mono_or_stereo) { \
  106. .mode = I2S_COMM_MODE_STD, \
  107. .data_bit_width = bits_per_sample, \
  108. .slot_bit_width = I2S_SLOT_BIT_WIDTH_DEFAULT, \
  109. .slot_mode = mono_or_stereo, \
  110. .ws_width = bits_per_sample, \
  111. .ws_pol = false, \
  112. .bit_shift = false, \
  113. .left_align = false, \
  114. .big_endian = false, \
  115. .bit_order_lsb = false \
  116. }
  117. #endif
  118. /**
  119. * @brief i2s default standard clock configuration
  120. * @note Please set the mclk_multiple to I2S_MCLK_MULTIPLE_384 while using 24 bits data width
  121. * Otherwise the sample rate might be imprecise since the bclk division is not a integer
  122. * @param rate sample rate
  123. */
  124. #define I2S_STD_CLK_DEFAULT_CONFIG(rate) { \
  125. .sample_rate_hz = rate, \
  126. .clk_src = I2S_CLK_D2CLK, \
  127. .mclk_multiple = I2S_MCLK_MULTIPLE_256, \
  128. }
  129. /**
  130. * @breif I2S slot configuration for standard mode
  131. */
  132. typedef struct {
  133. /* General fields */
  134. i2s_comm_mode_t mode; /*!< I2S communication mode, this field is for identification (MUST match the communication mode in 'i2s_chan_config_t') */
  135. i2s_data_bit_width_t data_bit_width; /*!< I2S sample data bit width (valid data bits per sample) */
  136. i2s_slot_bit_width_t slot_bit_width; /*!< I2S slot bit width (total bits per slot) */
  137. i2s_slot_mode_t slot_mode; /*!< Set mono or stereo mode with I2S_SLOT_MODE_MONO or I2S_SLOT_MODE_STEREO */
  138. /* Particular fields */
  139. uint32_t ws_width; /*!< WS signal width (i.e. the number of bclk ticks that ws signal is high) */
  140. bool ws_pol; /*!< WS signal polarity, set true to enable high lever first */
  141. bool bit_shift; /*!< Set to enbale bit shift in Philip mode */
  142. #if SOC_I2S_HW_VERSION_1 // For esp32/esp32-s2
  143. bool msb_right; /*!< Set to place right channel data at the MSB in the FIFO */
  144. #else
  145. bool left_align; /*!< Set to enable left alignment */
  146. bool big_endian; /*!< Set to enable big endian */
  147. bool bit_order_lsb; /*!< Set to enable lsb first */
  148. #endif
  149. } i2s_std_slot_config_t;
  150. /**
  151. * @breif I2S clock configuration for standard mode
  152. */
  153. typedef struct {
  154. /* General fields */
  155. uint32_t sample_rate_hz; /*!< I2S sample rate */
  156. i2s_clock_src_t clk_src; /*!< Choose clock source */
  157. i2s_mclk_multiple_t mclk_multiple; /*!< The multiple of mclk to the sample rate */
  158. } i2s_std_clk_config_t;
  159. #ifdef __cplusplus
  160. }
  161. #endif