i2s_hal.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // The HAL layer for I2S (common part)
  7. #include "soc/soc.h"
  8. #include "soc/soc_caps.h"
  9. #include "hal/i2s_hal.h"
  10. /**
  11. * @brief Calculate the closest sample rate clock configuration.
  12. * clock relationship:
  13. * Fmclk = bck_div*fbck = fsclk/(mclk_div+b/a)
  14. *
  15. * @param clk_cfg I2S clock configuration(input)
  16. * @param cal Point to `i2s_ll_mclk_div_t` structure(output).
  17. */
  18. static void i2s_hal_mclk_div_decimal_cal(i2s_hal_clock_cfg_t *clk_cfg, i2s_ll_mclk_div_t *cal)
  19. {
  20. int ma = 0;
  21. int mb = 0;
  22. cal->mclk_div = clk_cfg->mclk_div;
  23. cal->a = 1;
  24. cal->b = 0;
  25. uint32_t freq_diff = abs(clk_cfg->sclk - clk_cfg->mclk * cal->mclk_div);
  26. if (!freq_diff) {
  27. return;
  28. }
  29. float decimal = freq_diff / (float)clk_cfg->mclk;
  30. // Carry bit if the decimal is greater than 1.0 - 1.0 / (63.0 * 2) = 125.0 / 126.0
  31. if (decimal > 125.0 / 126.0) {
  32. cal->mclk_div++;
  33. return;
  34. }
  35. uint32_t min = ~0;
  36. for (int a = 2; a <= I2S_LL_MCLK_DIVIDER_MAX; a++) {
  37. // Calculate the closest 'b' in this loop, no need to loop 'b' to seek the closest value
  38. int b = (int)(a * (freq_diff / (double)clk_cfg->mclk) + 0.5);
  39. ma = freq_diff * a;
  40. mb = clk_cfg->mclk * b;
  41. if (ma == mb) {
  42. cal->a = a;
  43. cal->b = b;
  44. return;
  45. }
  46. if (abs((mb - ma)) < min) {
  47. cal->a = a;
  48. cal->b = b;
  49. min = abs(mb - ma);
  50. }
  51. }
  52. }
  53. void i2s_hal_set_clock_src(i2s_hal_context_t *hal, i2s_clock_src_t sel)
  54. {
  55. i2s_ll_tx_clk_set_src(hal->dev, sel);
  56. i2s_ll_rx_clk_set_src(hal->dev, sel);
  57. }
  58. void i2s_hal_tx_clock_config(i2s_hal_context_t *hal, i2s_hal_clock_cfg_t *clk_cfg)
  59. {
  60. i2s_ll_mclk_div_t mclk_set;
  61. i2s_hal_mclk_div_decimal_cal(clk_cfg, &mclk_set);
  62. i2s_ll_tx_set_clk(hal->dev, &mclk_set);
  63. i2s_ll_tx_set_bck_div_num(hal->dev, clk_cfg->bclk_div);
  64. }
  65. void i2s_hal_rx_clock_config(i2s_hal_context_t *hal, i2s_hal_clock_cfg_t *clk_cfg)
  66. {
  67. i2s_ll_mclk_div_t mclk_set;
  68. i2s_hal_mclk_div_decimal_cal(clk_cfg, &mclk_set);
  69. i2s_ll_rx_set_clk(hal->dev, &mclk_set);
  70. i2s_ll_rx_set_bck_div_num(hal->dev, clk_cfg->bclk_div);
  71. }
  72. void i2s_hal_enable_master_fd_mode(i2s_hal_context_t *hal)
  73. {
  74. i2s_ll_tx_set_slave_mod(hal->dev, false); //TX master
  75. i2s_ll_rx_set_slave_mod(hal->dev, true); //RX Slave
  76. }
  77. void i2s_hal_enable_slave_fd_mode(i2s_hal_context_t *hal)
  78. {
  79. i2s_ll_tx_set_slave_mod(hal->dev, true); //TX Slave
  80. i2s_ll_rx_set_slave_mod(hal->dev, true); //RX Slave
  81. }
  82. void i2s_hal_init(i2s_hal_context_t *hal, int i2s_num)
  83. {
  84. /* Get hardware instance */
  85. hal->dev = I2S_LL_GET_HW(i2s_num);
  86. }
  87. #if SOC_I2S_SUPPORTS_PDM_TX
  88. void i2s_hal_tx_set_pdm_mode_default(i2s_hal_context_t *hal, uint32_t sample_rate)
  89. {
  90. /* enable pdm tx mode */
  91. i2s_ll_tx_enable_pdm(hal->dev, true);
  92. #if SOC_I2S_SUPPORTS_TDM
  93. i2s_ll_tx_enable_clock(hal->dev);
  94. i2s_ll_tx_clk_set_src(hal->dev, I2S_CLK_D2CLK); // Set I2S_CLK_D2CLK as default
  95. i2s_ll_mclk_use_tx_clk(hal->dev);
  96. /* Still need to enable the first 2 TDM channel mask to get the correct number of frame */
  97. i2s_ll_tx_set_active_chan_mask(hal->dev, I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1);
  98. #else
  99. i2s_ll_tx_force_enable_fifo_mod(hal->dev, true);
  100. #endif
  101. /* set pdm tx default presacle */
  102. i2s_ll_tx_set_pdm_prescale(hal->dev, 0);
  103. /* set pdm tx default sacle of high pass filter */
  104. i2s_ll_tx_set_pdm_hp_scale(hal->dev, I2S_PDM_SIG_SCALING_MUL_1);
  105. /* set pdm tx default sacle of low pass filter */
  106. i2s_ll_tx_set_pdm_lp_scale(hal->dev, I2S_PDM_SIG_SCALING_MUL_1);
  107. /* set pdm tx default sacle of sinc filter */
  108. i2s_ll_tx_set_pdm_sinc_scale(hal->dev, I2S_PDM_SIG_SCALING_MUL_1);
  109. /* set pdm tx default sacle of sigma-delta filter */
  110. i2s_ll_tx_set_pdm_sd_scale(hal->dev, I2S_PDM_SIG_SCALING_MUL_1);
  111. /* set pdm tx sample rate */
  112. i2s_ll_tx_set_pdm_fpfs(hal->dev, 960, sample_rate / 100);
  113. #if SOC_I2S_SUPPORTS_PDM_CODEC
  114. /* enable pdm high pass filter */
  115. i2s_ll_tx_enable_pdm_hp_filter(hal->dev, true);
  116. /* set pdm tx high pass filter parameters */
  117. i2s_ll_tx_set_pdm_hp_filter_param0(hal->dev, 6);
  118. i2s_ll_tx_set_pdm_hp_filter_param5(hal->dev, 7);
  119. /* enable pdm sigma-delta codec */
  120. i2s_ll_tx_enable_pdm_sd_codec(hal->dev, true);
  121. /* set pdm tx sigma-delta codec dither */
  122. i2s_ll_tx_set_pdm_sd_dither(hal->dev, 0);
  123. i2s_ll_tx_set_pdm_sd_dither2(hal->dev, 0);
  124. #endif // SOC_I2S_SUPPORTS_PDM_CODEC
  125. }
  126. #endif // SOC_I2S_SUPPORTS_PDM_TX
  127. #if SOC_I2S_SUPPORTS_PDM_RX
  128. void i2s_hal_rx_set_pdm_mode_default(i2s_hal_context_t *hal)
  129. {
  130. /* enable pdm rx mode */
  131. i2s_ll_rx_enable_pdm(hal->dev, true);
  132. /* set pdm rx downsample number */
  133. i2s_ll_rx_set_pdm_dsr(hal->dev, I2S_PDM_DSR_8S);
  134. #if !SOC_I2S_SUPPORTS_TDM
  135. i2s_ll_rx_force_enable_fifo_mod(hal->dev, true);
  136. #endif
  137. #if SOC_I2S_SUPPORTS_TDM
  138. i2s_ll_rx_enable_clock(hal->dev);
  139. i2s_ll_rx_clk_set_src(hal->dev, I2S_CLK_D2CLK); // Set I2S_CLK_D2CLK as default
  140. i2s_ll_mclk_use_rx_clk(hal->dev);
  141. /* Still need to enable the first 2 TDM channel mask to get the correct number of frame */
  142. i2s_ll_rx_set_active_chan_mask(hal->dev, I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1);
  143. #else
  144. i2s_ll_rx_force_enable_fifo_mod(hal->dev, true);
  145. #endif
  146. }
  147. #endif // SOC_I2S_SUPPORTS_PDM_RX
  148. void i2s_hal_tx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg)
  149. {
  150. /* Disable PDM tx mode and enable TDM mode (if support) */
  151. i2s_ll_tx_enable_pdm(hal->dev, false);
  152. #if SOC_I2S_SUPPORTS_TDM
  153. i2s_ll_tx_enable_clock(hal->dev);
  154. i2s_ll_tx_clk_set_src(hal->dev, I2S_CLK_D2CLK); // Set I2S_CLK_D2CLK as default
  155. i2s_ll_mclk_use_tx_clk(hal->dev);
  156. i2s_ll_tx_set_active_chan_mask(hal->dev, hal_cfg->chan_mask);
  157. // In TDM mode(more than 2 channels), the ws polarity should be high first.
  158. if (hal_cfg->total_chan > 2) {
  159. i2s_ll_tx_set_ws_idle_pol(hal->dev, true);
  160. }
  161. i2s_ll_tx_enable_left_align(hal->dev, hal_cfg->left_align);
  162. i2s_ll_tx_enable_big_endian(hal->dev, hal_cfg->big_edin);
  163. i2s_ll_tx_set_bit_order(hal->dev, hal_cfg->bit_order_msb);
  164. i2s_ll_tx_set_skip_mask(hal->dev, hal_cfg->skip_msk);
  165. #else
  166. i2s_ll_tx_enable_msb_right(hal->dev, false);
  167. i2s_ll_tx_enable_right_first(hal->dev, false);
  168. i2s_ll_tx_force_enable_fifo_mod(hal->dev, true);
  169. #endif
  170. }
  171. void i2s_hal_rx_set_common_mode(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg)
  172. {
  173. /* Disable PDM rx mode and enable TDM rx mode (if support)*/
  174. i2s_ll_rx_enable_pdm(hal->dev, false);
  175. #if SOC_I2S_SUPPORTS_TDM
  176. i2s_ll_rx_enable_clock(hal->dev);
  177. i2s_ll_rx_clk_set_src(hal->dev, I2S_CLK_D2CLK); // Set I2S_CLK_D2CLK as default
  178. i2s_ll_mclk_use_rx_clk(hal->dev);
  179. i2s_ll_rx_set_active_chan_mask(hal->dev, hal_cfg->chan_mask);
  180. // In TDM mode(more than 2 channels), the ws polarity should be high first.
  181. if (hal_cfg->total_chan > 2) {
  182. i2s_ll_rx_set_ws_idle_pol(hal->dev, true);
  183. }
  184. i2s_ll_rx_enable_left_align(hal->dev, hal_cfg->left_align);
  185. i2s_ll_rx_enable_big_endian(hal->dev, hal_cfg->big_edin);
  186. i2s_ll_rx_set_bit_order(hal->dev, hal_cfg->bit_order_msb);
  187. #else
  188. i2s_ll_rx_enable_msb_right(hal->dev, false);
  189. i2s_ll_rx_enable_right_first(hal->dev, false);
  190. i2s_ll_rx_force_enable_fifo_mod(hal->dev, true);
  191. #endif
  192. }
  193. static uint32_t i2s_hal_get_ws_bit(i2s_comm_format_t fmt, uint32_t chan_num, uint32_t chan_bits)
  194. {
  195. switch (fmt) {
  196. case I2S_COMM_FORMAT_STAND_MSB:
  197. return chan_num * chan_bits / 2;
  198. case I2S_COMM_FORMAT_STAND_PCM_SHORT:
  199. return 1;
  200. case I2S_COMM_FORMAT_STAND_PCM_LONG:
  201. return chan_bits;
  202. default: //I2S_COMM_FORMAT_STAND_I2S
  203. return chan_num * chan_bits / 2;
  204. }
  205. }
  206. void i2s_hal_tx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg)
  207. {
  208. uint32_t chan_num = 2;
  209. uint32_t chan_bits = hal_cfg->chan_bits;
  210. uint32_t data_bits = hal_cfg->sample_bits;
  211. bool is_mono = (hal_cfg->chan_fmt == I2S_CHANNEL_FMT_ONLY_RIGHT) ||
  212. (hal_cfg->chan_fmt == I2S_CHANNEL_FMT_ONLY_LEFT);
  213. /* Set channel number and valid data bits */
  214. #if SOC_I2S_SUPPORTS_TDM
  215. chan_num = hal_cfg->total_chan;
  216. i2s_ll_tx_set_chan_num(hal->dev, chan_num);
  217. #endif
  218. i2s_ll_tx_set_sample_bit(hal->dev, chan_bits, data_bits);
  219. i2s_ll_tx_enable_mono_mode(hal->dev, is_mono);
  220. /* Set communication format */
  221. bool shift_en = hal_cfg->comm_fmt == I2S_COMM_FORMAT_STAND_I2S ? true : false;
  222. uint32_t ws_width = i2s_hal_get_ws_bit(hal_cfg->comm_fmt, chan_num, chan_bits);
  223. i2s_ll_tx_enable_msb_shift(hal->dev, shift_en);
  224. i2s_ll_tx_set_ws_width(hal->dev, ws_width);
  225. #if SOC_I2S_SUPPORTS_TDM
  226. i2s_ll_tx_set_half_sample_bit(hal->dev, chan_num * chan_bits / 2);
  227. #endif
  228. }
  229. void i2s_hal_rx_set_channel_style(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg)
  230. {
  231. uint32_t chan_num = 2;
  232. uint32_t chan_bits = hal_cfg->chan_bits;
  233. uint32_t data_bits = hal_cfg->sample_bits;
  234. bool is_mono = (hal_cfg->chan_fmt == I2S_CHANNEL_FMT_ONLY_RIGHT) ||
  235. (hal_cfg->chan_fmt == I2S_CHANNEL_FMT_ONLY_LEFT);
  236. #if SOC_I2S_SUPPORTS_TDM
  237. chan_num = hal_cfg->total_chan;
  238. i2s_ll_rx_set_chan_num(hal->dev, chan_num);
  239. #endif
  240. i2s_ll_rx_set_sample_bit(hal->dev, chan_bits, data_bits);
  241. i2s_ll_rx_enable_mono_mode(hal->dev, is_mono);
  242. /* Set communication format */
  243. bool shift_en = hal_cfg->comm_fmt == I2S_COMM_FORMAT_STAND_I2S ? true : false;
  244. uint32_t ws_width = i2s_hal_get_ws_bit(hal_cfg->comm_fmt, chan_num, chan_bits);
  245. i2s_ll_rx_enable_msb_shift(hal->dev, shift_en);
  246. i2s_ll_rx_set_ws_width(hal->dev, ws_width);
  247. #if SOC_I2S_SUPPORTS_TDM
  248. i2s_ll_rx_set_half_sample_bit(hal->dev, chan_num * chan_bits / 2);
  249. #endif
  250. }
  251. void i2s_hal_config_param(i2s_hal_context_t *hal, const i2s_hal_config_t *hal_cfg)
  252. {
  253. #if SOC_I2S_SUPPORTS_ADC
  254. if (hal_cfg->mode & I2S_MODE_ADC_BUILT_IN) {
  255. /* In ADC built-in mode, we need to call i2s_set_adc_mode to initialize the specific ADC channel.
  256. * In the current stage, we only support ADC1 and single channel mode.
  257. * In default data mode, the ADC data is in 12-bit resolution mode.
  258. */
  259. i2s_ll_enable_builtin_adc(hal->dev, true);
  260. return;
  261. }
  262. i2s_ll_enable_builtin_adc(hal->dev, false);
  263. #endif
  264. #if SOC_I2S_SUPPORTS_DAC
  265. if (hal_cfg->mode & I2S_MODE_DAC_BUILT_IN) {
  266. i2s_ll_enable_builtin_dac(hal->dev, true);
  267. return;
  268. }
  269. i2s_ll_enable_builtin_dac(hal->dev, false);
  270. #endif
  271. /* Set configurations for TX mode */
  272. if (hal_cfg->mode & I2S_MODE_TX) {
  273. i2s_ll_tx_stop(hal->dev);
  274. i2s_ll_tx_reset(hal->dev);
  275. i2s_ll_tx_set_slave_mod(hal->dev, (hal_cfg->mode & I2S_MODE_SLAVE) != 0); //TX Slave
  276. #if SOC_I2S_SUPPORTS_PDM_TX
  277. if (hal_cfg->mode & I2S_MODE_PDM) {
  278. /* Set tx pdm mode */
  279. i2s_hal_tx_set_pdm_mode_default(hal, hal_cfg->sample_rate);
  280. } else
  281. #endif
  282. {
  283. /* Set tx common mode */
  284. i2s_hal_tx_set_common_mode(hal, hal_cfg);
  285. }
  286. i2s_hal_tx_set_channel_style(hal, hal_cfg);
  287. }
  288. /* Set configurations for RX mode */
  289. if (hal_cfg->mode & I2S_MODE_RX) {
  290. i2s_ll_rx_stop(hal->dev);
  291. i2s_ll_rx_reset(hal->dev);
  292. i2s_ll_rx_set_slave_mod(hal->dev, (hal_cfg->mode & I2S_MODE_SLAVE) != 0); //RX Slave
  293. #if SOC_I2S_SUPPORTS_PDM_RX
  294. if (hal_cfg->mode & I2S_MODE_PDM) {
  295. /* Set rx pdm mode */
  296. i2s_hal_rx_set_pdm_mode_default(hal);
  297. } else
  298. #endif
  299. {
  300. /* Set rx common mode */
  301. i2s_hal_rx_set_common_mode(hal, hal_cfg);
  302. }
  303. i2s_hal_rx_set_channel_style(hal, hal_cfg);
  304. }
  305. /* Set configurations for full-duplex mode */
  306. if ((hal_cfg->mode & I2S_MODE_RX) && (hal_cfg->mode & I2S_MODE_TX)) {
  307. i2s_ll_share_bck_ws(hal->dev, true);
  308. if (hal_cfg->mode & I2S_MODE_MASTER) {
  309. i2s_hal_enable_master_fd_mode(hal);
  310. } else {
  311. i2s_hal_enable_slave_fd_mode(hal);
  312. }
  313. }
  314. }
  315. void i2s_hal_start_tx(i2s_hal_context_t *hal)
  316. {
  317. #if SOC_I2S_SUPPORTS_TDM
  318. i2s_ll_tx_enable_clock(hal->dev);
  319. #endif
  320. i2s_ll_tx_start(hal->dev);
  321. }
  322. void i2s_hal_start_rx(i2s_hal_context_t *hal)
  323. {
  324. #if SOC_I2S_SUPPORTS_TDM
  325. i2s_ll_rx_enable_clock(hal->dev);
  326. #endif
  327. i2s_ll_rx_start(hal->dev);
  328. }
  329. void i2s_hal_stop_tx(i2s_hal_context_t *hal)
  330. {
  331. i2s_ll_tx_stop(hal->dev);
  332. #if SOC_I2S_SUPPORTS_TDM
  333. i2s_ll_tx_disable_clock(hal->dev);
  334. #endif
  335. }
  336. void i2s_hal_stop_rx(i2s_hal_context_t *hal)
  337. {
  338. i2s_ll_rx_stop(hal->dev);
  339. #if SOC_I2S_SUPPORTS_TDM
  340. i2s_ll_rx_disable_clock(hal->dev);
  341. #endif
  342. }