i2s.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include <math.h>
  9. #include <esp_types.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/semphr.h"
  13. #include "soc/lldesc.h"
  14. #include "driver/gpio.h"
  15. #include "driver/i2s.h"
  16. #include "hal/gpio_hal.h"
  17. #if SOC_I2S_SUPPORTS_ADC_DAC
  18. #include "driver/dac.h"
  19. #include "hal/i2s_hal.h"
  20. #include "adc1_private.h"
  21. #endif
  22. #include "soc/rtc.h"
  23. #include "esp_intr_alloc.h"
  24. #include "esp_err.h"
  25. #include "esp_attr.h"
  26. #include "esp_log.h"
  27. #include "esp_pm.h"
  28. #include "esp_efuse.h"
  29. #include "esp_rom_gpio.h"
  30. #include "sdkconfig.h"
  31. static const char* I2S_TAG = "I2S";
  32. #define I2S_CHECK(a, str, ret) if (!(a)) { \
  33. ESP_LOGE(I2S_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  34. return (ret); \
  35. }
  36. #define I2S_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  37. #define I2S_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  38. #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  39. #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  40. #define I2S_FULL_DUPLEX_SLAVE_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_SLAVE)
  41. #define I2S_FULL_DUPLEX_MASTER_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_MASTER)
  42. //TODO: Refactor to put this logic into LL
  43. #define I2S_AD_BCK_FACTOR (2)
  44. #define I2S_PDM_BCK_FACTOR (64)
  45. #define I2S_BASE_CLK (2*APB_CLK_FREQ)
  46. /**
  47. * @brief DMA buffer object
  48. *
  49. */
  50. typedef struct {
  51. char **buf;
  52. int buf_size;
  53. int rw_pos;
  54. void *curr_ptr;
  55. SemaphoreHandle_t mux;
  56. xQueueHandle queue;
  57. lldesc_t **desc;
  58. } i2s_dma_t;
  59. /**
  60. * @brief I2S object instance
  61. *
  62. */
  63. typedef struct {
  64. i2s_port_t i2s_num; /*!< I2S port number*/
  65. int queue_size; /*!< I2S event queue size*/
  66. QueueHandle_t i2s_queue; /*!< I2S queue handler*/
  67. int dma_buf_count; /*!< DMA buffer count, number of buffer*/
  68. int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
  69. i2s_dma_t *rx; /*!< DMA Tx buffer*/
  70. i2s_dma_t *tx; /*!< DMA Rx buffer*/
  71. i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  72. int channel_num; /*!< Number of channels*/
  73. int bytes_per_sample; /*!< Bytes per sample*/
  74. int bits_per_sample; /*!< Bits per sample*/
  75. i2s_mode_t mode; /*!< I2S Working mode*/
  76. uint32_t sample_rate; /*!< I2S sample rate */
  77. bool use_apll; /*!< I2S use APLL clock */
  78. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
  79. int fixed_mclk; /*!< I2S fixed MLCK clock */
  80. double real_rate;
  81. #ifdef CONFIG_PM_ENABLE
  82. esp_pm_lock_handle_t pm_lock;
  83. #endif
  84. i2s_hal_context_t hal; /*!< I2S hal context*/
  85. } i2s_obj_t;
  86. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  87. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX];
  88. #if SOC_I2S_SUPPORTS_ADC_DAC
  89. static int _i2s_adc_unit = -1;
  90. static int _i2s_adc_channel = -1;
  91. #endif
  92. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
  93. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
  94. static inline void gpio_matrix_out_check(int gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  95. {
  96. //if pin = -1, do not need to configure
  97. if (gpio != -1) {
  98. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  99. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  100. esp_rom_gpio_connect_out_signal(gpio, signal_idx, out_inv, oen_inv);
  101. }
  102. }
  103. static inline void gpio_matrix_in_check(int gpio, uint32_t signal_idx, bool inv)
  104. {
  105. if (gpio != -1) {
  106. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  107. //Set direction, for some GPIOs, the input function are not enabled as default.
  108. gpio_set_direction(gpio, GPIO_MODE_INPUT);
  109. esp_rom_gpio_connect_in_signal(gpio, signal_idx, inv);
  110. }
  111. }
  112. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  113. {
  114. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  115. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
  116. return ESP_OK;
  117. }
  118. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  119. {
  120. I2S_ENTER_CRITICAL();
  121. i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  122. I2S_EXIT_CRITICAL();
  123. return ESP_OK;
  124. }
  125. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  126. {
  127. I2S_ENTER_CRITICAL();
  128. i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  129. I2S_EXIT_CRITICAL();
  130. return ESP_OK;
  131. }
  132. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  133. {
  134. I2S_ENTER_CRITICAL();
  135. i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  136. I2S_EXIT_CRITICAL();
  137. return ESP_OK;
  138. }
  139. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  140. {
  141. I2S_ENTER_CRITICAL();
  142. i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  143. I2S_EXIT_CRITICAL();
  144. return ESP_OK;
  145. }
  146. float i2s_get_clk(i2s_port_t i2s_num)
  147. {
  148. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  149. return p_i2s_obj[i2s_num]->real_rate;
  150. }
  151. static esp_err_t i2s_isr_register(i2s_port_t i2s_num, int intr_alloc_flags, void (*fn)(void*), void * arg, i2s_isr_handle_t *handle)
  152. {
  153. return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
  154. }
  155. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  156. {
  157. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  158. #if CONFIG_IDF_TARGET_ESP32
  159. /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
  160. if (esp_efuse_get_chip_ver() == 0) {
  161. sdm0 = 0;
  162. sdm1 = 0;
  163. }
  164. #endif
  165. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  166. if (fout < SOC_I2S_APLL_MIN_FREQ || fout > SOC_I2S_APLL_MAX_FREQ) {
  167. return SOC_I2S_APLL_MAX_FREQ;
  168. }
  169. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  170. return fpll/2;
  171. }
  172. /**
  173. * @brief APLL calculate function, was described by following:
  174. * APLL Output frequency is given by the formula:
  175. *
  176. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  177. * apll_freq = fout / ((o_div + 2) * 2)
  178. *
  179. * The dividend in this expression should be in the range of 240 - 600 MHz.
  180. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  181. * * sdm0 frequency adjustment parameter, 0..255
  182. * * sdm1 frequency adjustment parameter, 0..255
  183. * * sdm2 frequency adjustment parameter, 0..63
  184. * * o_div frequency divider, 0..31
  185. *
  186. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  187. * then apply the above formula, finding the closest frequency to the desired one.
  188. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  189. * 1. We will choose the parameters with the highest level of change,
  190. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  191. * Take average frequency close to the desired frequency, and select sdm2
  192. * 2. Next, we look for sequences of less influential and more detailed parameters,
  193. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  194. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  195. *
  196. * @param[in] rate The I2S Frequency (MCLK)
  197. * @param[in] bits_per_sample The bits per sample
  198. * @param[out] sdm0 The sdm 0
  199. * @param[out] sdm1 The sdm 1
  200. * @param[out] sdm2 The sdm 2
  201. * @param[out] odir The odir
  202. *
  203. * @return ESP_ERR_INVALID_ARG or ESP_OK
  204. */
  205. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  206. {
  207. int _odir, _sdm0, _sdm1, _sdm2;
  208. float avg;
  209. float min_rate, max_rate, min_diff;
  210. if (rate/bits_per_sample/2/8 < SOC_I2S_APLL_MIN_RATE) {
  211. return ESP_ERR_INVALID_ARG;
  212. }
  213. *sdm0 = 0;
  214. *sdm1 = 0;
  215. *sdm2 = 0;
  216. *odir = 0;
  217. min_diff = SOC_I2S_APLL_MAX_FREQ;
  218. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  219. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, 0);
  220. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, 31);
  221. avg = (max_rate + min_rate)/2;
  222. if (abs(avg - rate) < min_diff) {
  223. min_diff = abs(avg - rate);
  224. *sdm2 = _sdm2;
  225. }
  226. }
  227. min_diff = SOC_I2S_APLL_MAX_FREQ;
  228. for (_odir = 0; _odir < 32; _odir ++) {
  229. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, *sdm2, _odir);
  230. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, *sdm2, _odir);
  231. avg = (max_rate + min_rate)/2;
  232. if (abs(avg - rate) < min_diff) {
  233. min_diff = abs(avg - rate);
  234. *odir = _odir;
  235. }
  236. }
  237. min_diff = SOC_I2S_APLL_MAX_FREQ;
  238. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  239. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, *odir);
  240. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, *odir);
  241. avg = (max_rate + min_rate)/2;
  242. if (abs(avg - rate) < min_diff) {
  243. min_diff = abs(avg - rate);
  244. *sdm2 = _sdm2;
  245. }
  246. }
  247. min_diff = SOC_I2S_APLL_MAX_FREQ;
  248. for (_sdm1 = 0; _sdm1 < 256; _sdm1 ++) {
  249. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, _sdm1, *sdm2, *odir);
  250. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, _sdm1, *sdm2, *odir);
  251. avg = (max_rate + min_rate)/2;
  252. if (abs(avg - rate) < min_diff) {
  253. min_diff = abs(avg - rate);
  254. *sdm1 = _sdm1;
  255. }
  256. }
  257. min_diff = SOC_I2S_APLL_MAX_FREQ;
  258. for (_sdm0 = 0; _sdm0 < 256; _sdm0 ++) {
  259. avg = i2s_apll_get_fi2s(bits_per_sample, _sdm0, *sdm1, *sdm2, *odir);
  260. if (abs(avg - rate) < min_diff) {
  261. min_diff = abs(avg - rate);
  262. *sdm0 = _sdm0;
  263. }
  264. }
  265. return ESP_OK;
  266. }
  267. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  268. {
  269. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  270. int clkmInteger, clkmDecimals, bck = 0;
  271. double denom = (double)1 / 64;
  272. int channel = 2;
  273. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  274. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  275. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  276. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  277. return ESP_ERR_INVALID_ARG;
  278. }
  279. if (p_i2s_obj[i2s_num] == NULL) {
  280. ESP_LOGE(I2S_TAG, "Not initialized yet");
  281. return ESP_ERR_INVALID_ARG;
  282. }
  283. p_i2s_obj[i2s_num]->sample_rate = rate;
  284. double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
  285. if (clkmdiv > 256) {
  286. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  287. return ESP_ERR_INVALID_ARG;
  288. }
  289. // wait all on-going writing finish
  290. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  291. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  292. }
  293. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  294. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  295. }
  296. i2s_stop(i2s_num);
  297. #if SOC_I2S_SUPPORTS_ADC_DAC
  298. /* I2S-ADC only support single channel format. */
  299. if (!(p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN)) {
  300. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  301. }
  302. #else
  303. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  304. #endif
  305. i2s_hal_set_tx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  306. if (p_i2s_obj[i2s_num]->channel_num != (int)ch) {
  307. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  308. }
  309. if ((int)bits != p_i2s_obj[i2s_num]->bits_per_sample) {
  310. p_i2s_obj[i2s_num]->bits_per_sample = bits;
  311. // Round bytes_per_sample up to next multiple of 16 bits
  312. int halfwords_per_sample = (bits + 15) / 16;
  313. p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
  314. // Because limited of DMA buffer is 4092 bytes
  315. if (p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num > 4092) {
  316. p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
  317. }
  318. // Re-create TX DMA buffer
  319. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  320. save_tx = p_i2s_obj[i2s_num]->tx;
  321. p_i2s_obj[i2s_num]->tx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
  322. if (p_i2s_obj[i2s_num]->tx == NULL) {
  323. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  324. i2s_driver_uninstall(i2s_num);
  325. return ESP_ERR_NO_MEM;
  326. }
  327. i2s_hal_set_out_link_addr(&(p_i2s_obj[i2s_num]->hal), (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0]);
  328. //destroy old tx dma if exist
  329. if (save_tx) {
  330. i2s_destroy_dma_queue(i2s_num, save_tx);
  331. }
  332. }
  333. // Re-create RX DMA buffer
  334. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  335. save_rx = p_i2s_obj[i2s_num]->rx;
  336. p_i2s_obj[i2s_num]->rx = i2s_create_dma_queue(i2s_num, p_i2s_obj[i2s_num]->dma_buf_count, p_i2s_obj[i2s_num]->dma_buf_len);
  337. if (p_i2s_obj[i2s_num]->rx == NULL){
  338. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  339. i2s_driver_uninstall(i2s_num);
  340. return ESP_ERR_NO_MEM;
  341. }
  342. i2s_hal_set_in_link(&(p_i2s_obj[i2s_num]->hal), p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->channel_num * p_i2s_obj[i2s_num]->bytes_per_sample, (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0]);
  343. //destroy old rx dma if exist
  344. if (save_rx) {
  345. i2s_destroy_dma_queue(i2s_num, save_rx);
  346. }
  347. }
  348. }
  349. double mclk;
  350. int sdm0, sdm1, sdm2, odir, m_scale = 8;
  351. int fi2s_clk = rate*channel*bits*m_scale;
  352. #if SOC_I2S_SUPPORTS_ADC_DAC
  353. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  354. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  355. //Rate as given to this function is the intended sample rate;
  356. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  357. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  358. fi2s_clk /= I2S_AD_BCK_FACTOR;
  359. int factor2 = 60;
  360. mclk = b_clk * factor2;
  361. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  362. clkmInteger = clkmdiv;
  363. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  364. bck = mclk / b_clk;
  365. #endif
  366. #if SOC_I2S_SUPPORTS_PDM
  367. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  368. uint32_t b_clk = 0;
  369. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  370. uint32_t fp, fs;
  371. i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
  372. // Recommended set `fp = 960, fs = sample_rate / 100`
  373. fs = rate / 100;
  374. i2s_hal_tx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), fp, fs);
  375. b_clk = rate * I2S_PDM_BCK_FACTOR * fp / fs;
  376. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  377. uint32_t dsr;
  378. i2s_hal_get_rx_pdm(&(p_i2s_obj[i2s_num]->hal), &dsr);
  379. b_clk = rate * I2S_PDM_BCK_FACTOR * (dsr ? 2 : 1);
  380. }
  381. fi2s_clk = b_clk * m_scale;
  382. int factor2 = 5 ;
  383. mclk = b_clk * factor2;
  384. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  385. clkmInteger = clkmdiv;
  386. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  387. bck = mclk / b_clk;
  388. } else
  389. #endif
  390. {
  391. clkmInteger = clkmdiv;
  392. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  393. mclk = clkmInteger + denom * clkmDecimals;
  394. bck = factor/(bits * channel);
  395. }
  396. if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
  397. fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
  398. m_scale = fi2s_clk/bits/rate/channel;
  399. }
  400. if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
  401. ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
  402. rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
  403. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), 1, 1, 0, m_scale, m_scale);
  404. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_APLL);
  405. double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
  406. p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
  407. ESP_LOGI(I2S_TAG, "APLL: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK_M: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
  408. rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
  409. } else {
  410. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  411. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), clkmInteger, 63, clkmDecimals, bck, bck);
  412. double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
  413. p_i2s_obj[i2s_num]->real_rate = real_rate;
  414. ESP_LOGI(I2S_TAG, "PLL_D2: Req RATE: %d, real rate: %0.3f, BITS: %u, CLKM: %u, BCK: %u, MCLK: %0.3f, SCLK: %f, diva: %d, divb: %d",
  415. rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
  416. }
  417. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  418. p_i2s_obj[i2s_num]->tx->curr_ptr = NULL;
  419. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  420. }
  421. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  422. p_i2s_obj[i2s_num]->rx->curr_ptr = NULL;
  423. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  424. }
  425. i2s_hal_set_tx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  426. i2s_hal_set_rx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  427. // wait all writing on-going finish
  428. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  429. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  430. }
  431. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  432. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  433. }
  434. i2s_start(i2s_num);
  435. return ESP_OK;
  436. }
  437. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  438. {
  439. i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
  440. uint32_t status;
  441. i2s_hal_get_intr_status(&(p_i2s->hal), &status);
  442. if(status == 0) {
  443. //Avoid spurious interrupt
  444. return;
  445. }
  446. i2s_event_t i2s_event;
  447. int dummy;
  448. portBASE_TYPE high_priority_task_awoken = 0;
  449. lldesc_t *finish_desc = NULL;
  450. if ((status & I2S_INTR_OUT_DSCR_ERR) || (status & I2S_INTR_IN_DSCR_ERR)) {
  451. ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", status);
  452. if (p_i2s->i2s_queue) {
  453. i2s_event.type = I2S_EVENT_DMA_ERROR;
  454. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  455. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  456. }
  457. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  458. }
  459. }
  460. if ((status & I2S_INTR_OUT_EOF) && p_i2s->tx) {
  461. i2s_hal_get_out_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  462. // All buffers are empty. This means we have an underflow on our hands.
  463. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  464. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
  465. // See if tx descriptor needs to be auto cleared:
  466. // This will avoid any kind of noise that may get introduced due to transmission
  467. // of previous data from tx descriptor on I2S line.
  468. if (p_i2s->tx_desc_auto_clear == true) {
  469. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  470. }
  471. }
  472. xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  473. if (p_i2s->i2s_queue) {
  474. i2s_event.type = I2S_EVENT_TX_DONE;
  475. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  476. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  477. }
  478. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  479. }
  480. }
  481. if ((status & I2S_INTR_IN_SUC_EOF) && p_i2s->rx) {
  482. // All buffers are full. This means we have an overflow.
  483. i2s_hal_get_in_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  484. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  485. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
  486. }
  487. xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  488. if (p_i2s->i2s_queue) {
  489. i2s_event.type = I2S_EVENT_RX_DONE;
  490. if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  491. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  492. }
  493. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  494. }
  495. }
  496. i2s_hal_clear_intr_status(&(p_i2s->hal), status);
  497. if (high_priority_task_awoken == pdTRUE) {
  498. portYIELD_FROM_ISR();
  499. }
  500. }
  501. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
  502. {
  503. int bux_idx;
  504. if (p_i2s_obj[i2s_num] == NULL) {
  505. ESP_LOGE(I2S_TAG, "Not initialized yet");
  506. return ESP_ERR_INVALID_ARG;
  507. }
  508. if (dma == NULL) {
  509. ESP_LOGE(I2S_TAG, "dma is NULL");
  510. return ESP_ERR_INVALID_ARG;
  511. }
  512. for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
  513. if (dma->desc && dma->desc[bux_idx]) {
  514. free(dma->desc[bux_idx]);
  515. }
  516. if (dma->buf && dma->buf[bux_idx]) {
  517. free(dma->buf[bux_idx]);
  518. }
  519. }
  520. if (dma->buf) {
  521. free(dma->buf);
  522. }
  523. if (dma->desc) {
  524. free(dma->desc);
  525. }
  526. vQueueDelete(dma->queue);
  527. vSemaphoreDelete(dma->mux);
  528. free(dma);
  529. return ESP_OK;
  530. }
  531. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
  532. {
  533. int bux_idx;
  534. int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  535. i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
  536. if (dma == NULL) {
  537. ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
  538. return NULL;
  539. }
  540. memset(dma, 0, sizeof(i2s_dma_t));
  541. dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
  542. if (dma->buf == NULL) {
  543. ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
  544. free(dma);
  545. return NULL;
  546. }
  547. memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
  548. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  549. dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
  550. if (dma->buf[bux_idx] == NULL) {
  551. ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
  552. i2s_destroy_dma_queue(i2s_num, dma);
  553. return NULL;
  554. }
  555. ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
  556. }
  557. dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
  558. if (dma->desc == NULL) {
  559. ESP_LOGE(I2S_TAG, "Error malloc dma description");
  560. i2s_destroy_dma_queue(i2s_num, dma);
  561. return NULL;
  562. }
  563. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  564. dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  565. if (dma->desc[bux_idx] == NULL) {
  566. ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
  567. i2s_destroy_dma_queue(i2s_num, dma);
  568. return NULL;
  569. }
  570. }
  571. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  572. dma->desc[bux_idx]->owner = 1;
  573. dma->desc[bux_idx]->eof = 1;
  574. dma->desc[bux_idx]->sosf = 0;
  575. dma->desc[bux_idx]->length = dma_buf_len * sample_size;
  576. dma->desc[bux_idx]->size = dma_buf_len * sample_size;
  577. dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
  578. dma->desc[bux_idx]->offset = 0;
  579. dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
  580. }
  581. dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
  582. dma->mux = xSemaphoreCreateMutex();
  583. dma->buf_size = dma_buf_len * sample_size;
  584. ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
  585. return dma;
  586. }
  587. esp_err_t i2s_start(i2s_port_t i2s_num)
  588. {
  589. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  590. //start DMA link
  591. I2S_ENTER_CRITICAL();
  592. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  593. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  594. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), I2S_INTR_MAX);
  595. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  596. i2s_enable_tx_intr(i2s_num);
  597. i2s_hal_start_tx(&(p_i2s_obj[i2s_num]->hal));
  598. }
  599. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  600. i2s_enable_rx_intr(i2s_num);
  601. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  602. }
  603. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  604. I2S_EXIT_CRITICAL();
  605. return ESP_OK;
  606. }
  607. esp_err_t i2s_stop(i2s_port_t i2s_num)
  608. {
  609. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  610. I2S_ENTER_CRITICAL();
  611. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  612. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  613. i2s_hal_stop_tx(&(p_i2s_obj[i2s_num]->hal));
  614. i2s_disable_tx_intr(i2s_num);
  615. }
  616. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  617. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  618. i2s_disable_rx_intr(i2s_num);
  619. }
  620. uint32_t mask;
  621. i2s_hal_get_intr_status(&(p_i2s_obj[i2s_num]->hal), &mask);
  622. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), mask);
  623. I2S_EXIT_CRITICAL();
  624. return ESP_OK;
  625. }
  626. #if SOC_I2S_SUPPORTS_ADC_DAC
  627. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  628. {
  629. I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
  630. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  631. dac_output_disable(DAC_CHANNEL_1);
  632. dac_output_disable(DAC_CHANNEL_2);
  633. dac_i2s_disable();
  634. } else {
  635. dac_i2s_enable();
  636. }
  637. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  638. //DAC1, right channel
  639. dac_output_enable(DAC_CHANNEL_1);
  640. }
  641. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  642. //DAC2, left channel
  643. dac_output_enable(DAC_CHANNEL_2);
  644. }
  645. return ESP_OK;
  646. }
  647. static esp_err_t _i2s_adc_mode_recover(void)
  648. {
  649. I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
  650. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  651. }
  652. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  653. {
  654. I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
  655. // For now, we only support SAR ADC1.
  656. _i2s_adc_unit = adc_unit;
  657. _i2s_adc_channel = adc_channel;
  658. return adc_i2s_mode_init(adc_unit, adc_channel);
  659. }
  660. #endif
  661. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  662. {
  663. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  664. if (pin == NULL) {
  665. #if SOC_I2S_SUPPORTS_ADC_DAC
  666. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  667. #else
  668. return ESP_ERR_INVALID_ARG;
  669. #endif
  670. }
  671. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  672. ESP_LOGE(I2S_TAG, "bck_io_num error");
  673. return ESP_FAIL;
  674. }
  675. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  676. ESP_LOGE(I2S_TAG, "ws_io_num error");
  677. return ESP_FAIL;
  678. }
  679. if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
  680. ESP_LOGE(I2S_TAG, "data_out_num error");
  681. return ESP_FAIL;
  682. }
  683. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  684. ESP_LOGE(I2S_TAG, "data_in_num error");
  685. return ESP_FAIL;
  686. }
  687. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  688. //Each IIS hw module has a RX and TX unit.
  689. //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
  690. //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
  691. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  692. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  693. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  694. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  695. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  696. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  697. bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
  698. ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
  699. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  700. }
  701. }
  702. //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
  703. //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
  704. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  705. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  706. bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
  707. ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
  708. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  709. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  710. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  711. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  712. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  713. }
  714. }
  715. //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
  716. //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
  717. if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
  718. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  719. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  720. } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
  721. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  722. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  723. }
  724. gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
  725. gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
  726. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  727. gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
  728. gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
  729. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  730. gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
  731. gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
  732. }
  733. ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
  734. return ESP_OK;
  735. }
  736. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  737. {
  738. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  739. I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
  740. return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  741. }
  742. #if SOC_I2S_SUPPORTS_PDM
  743. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
  744. {
  745. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  746. i2s_hal_rx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), dsr);
  747. return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  748. }
  749. #endif
  750. static esp_err_t i2s_check_cfg_static(i2s_port_t i2s_num, const i2s_config_t *cfg)
  751. {
  752. #if SOC_I2S_SUPPORTS_ADC_DAC
  753. //We only check if the I2S number is invalid when set to build in ADC and DAC mode.
  754. I2S_CHECK(!((cfg->mode & I2S_MODE_ADC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S ADC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
  755. I2S_CHECK(!((cfg->mode & I2S_MODE_DAC_BUILT_IN) && (i2s_num != I2S_NUM_0)), "I2S DAC built-in only support on I2S0", ESP_ERR_INVALID_ARG);
  756. return ESP_OK;
  757. #endif
  758. #if SOC_I2S_SUPPORTS_PDM
  759. //We only check if the I2S number is invalid when set to PDM mode.
  760. I2S_CHECK(!((cfg->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
  761. return ESP_OK;
  762. #endif
  763. I2S_CHECK(cfg->communication_format && (cfg->communication_format < I2S_COMM_FORMAT_STAND_MAX), "invalid communication formats", ESP_ERR_INVALID_ARG);
  764. I2S_CHECK(!((cfg->communication_format & I2S_COMM_FORMAT_STAND_MSB) && (cfg->communication_format & I2S_COMM_FORMAT_STAND_PCM_LONG)), "multiple communication formats specified", ESP_ERR_INVALID_ARG);
  765. return ESP_OK;
  766. }
  767. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  768. {
  769. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  770. I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
  771. I2S_CHECK((i2s_check_cfg_static(i2s_num, i2s_config) == ESP_OK), "param check error", ESP_ERR_INVALID_ARG);
  772. #if SOC_I2S_SUPPORTS_ADC_DAC
  773. if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  774. //in ADC built-in mode, we need to call i2s_set_adc_mode to
  775. //initialize the specific ADC channel.
  776. //in the current stage, we only support ADC1 and single channel mode.
  777. //In default data mode, the ADC data is in 12-bit resolution mode.
  778. adc_power_acquire();
  779. }
  780. #endif
  781. // configure I2S data port interface.
  782. i2s_hal_config_param(&(p_i2s_obj[i2s_num]->hal), i2s_config);
  783. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
  784. i2s_hal_enable_sig_loopback(&(p_i2s_obj[i2s_num]->hal));
  785. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  786. i2s_hal_enable_master_mode(&(p_i2s_obj[i2s_num]->hal));
  787. } else {
  788. i2s_hal_enable_slave_mode(&(p_i2s_obj[i2s_num]->hal));
  789. }
  790. }
  791. p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
  792. p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
  793. p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
  794. return ESP_OK;
  795. }
  796. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  797. {
  798. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  799. if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
  800. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  801. memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
  802. }
  803. }
  804. if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
  805. int bytes_left = 0;
  806. bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
  807. if (bytes_left) {
  808. size_t zero_bytes = 0, bytes_written;
  809. i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
  810. }
  811. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  812. memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
  813. }
  814. }
  815. return ESP_OK;
  816. }
  817. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
  818. {
  819. esp_err_t err;
  820. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  821. I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
  822. I2S_CHECK((i2s_config->dma_buf_count >= 2 && i2s_config->dma_buf_count <= 128), "I2S buffer count less than 128 and more than 2", ESP_ERR_INVALID_ARG);
  823. I2S_CHECK((i2s_config->dma_buf_len >= 8 && i2s_config->dma_buf_len <= 1024), "I2S buffer length at most 1024 and more than 8", ESP_ERR_INVALID_ARG);
  824. if (p_i2s_obj[i2s_num] == NULL) {
  825. p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
  826. if (p_i2s_obj[i2s_num] == NULL) {
  827. ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
  828. return ESP_ERR_NO_MEM;
  829. }
  830. memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
  831. portMUX_TYPE i2s_spinlock_unlocked[1] = {portMUX_INITIALIZER_UNLOCKED};
  832. for (int x = 0; x < I2S_NUM_MAX; x++) {
  833. i2s_spinlock[x] = i2s_spinlock_unlocked[0];
  834. }
  835. //To make sure hardware is enabled before any hardware register operations.
  836. periph_module_enable(i2s_periph_signal[i2s_num].module);
  837. i2s_hal_init(&(p_i2s_obj[i2s_num]->hal), i2s_num);
  838. p_i2s_obj[i2s_num]->i2s_num = i2s_num;
  839. p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
  840. p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
  841. p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
  842. p_i2s_obj[i2s_num]->mode = i2s_config->mode;
  843. p_i2s_obj[i2s_num]->bits_per_sample = 0;
  844. p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
  845. p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  846. #ifdef CONFIG_PM_ENABLE
  847. if (i2s_config->use_apll) {
  848. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  849. } else {
  850. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  851. }
  852. if (err != ESP_OK) {
  853. free(p_i2s_obj[i2s_num]);
  854. p_i2s_obj[i2s_num] = NULL;
  855. ESP_LOGE(I2S_TAG, "I2S pm lock error");
  856. return err;
  857. }
  858. #endif //CONFIG_PM_ENABLE
  859. //initial interrupt
  860. err = i2s_isr_register(i2s_num, i2s_config->intr_alloc_flags, i2s_intr_handler_default, p_i2s_obj[i2s_num], &p_i2s_obj[i2s_num]->i2s_isr_handle);
  861. if (err != ESP_OK) {
  862. #ifdef CONFIG_PM_ENABLE
  863. if (p_i2s_obj[i2s_num]->pm_lock) {
  864. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  865. }
  866. #endif
  867. free(p_i2s_obj[i2s_num]);
  868. p_i2s_obj[i2s_num] = NULL;
  869. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  870. return err;
  871. }
  872. i2s_stop(i2s_num);
  873. err = i2s_param_config(i2s_num, i2s_config);
  874. if (err != ESP_OK) {
  875. i2s_driver_uninstall(i2s_num);
  876. ESP_LOGE(I2S_TAG, "I2S param configure error");
  877. return err;
  878. }
  879. if (i2s_queue) {
  880. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  881. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  882. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  883. } else {
  884. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  885. }
  886. //set clock and start
  887. return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  888. }
  889. ESP_LOGW(I2S_TAG, "I2S driver already installed");
  890. return ESP_OK;
  891. }
  892. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  893. {
  894. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  895. if (p_i2s_obj[i2s_num] == NULL) {
  896. ESP_LOGI(I2S_TAG, "already uninstalled");
  897. return ESP_OK;
  898. }
  899. i2s_stop(i2s_num);
  900. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  901. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  902. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  903. p_i2s_obj[i2s_num]->tx = NULL;
  904. }
  905. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  906. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  907. p_i2s_obj[i2s_num]->rx = NULL;
  908. }
  909. if (p_i2s_obj[i2s_num]->i2s_queue) {
  910. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  911. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  912. }
  913. if(p_i2s_obj[i2s_num]->use_apll) {
  914. // switch back to PLL clock source
  915. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  916. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  917. }
  918. #ifdef CONFIG_PM_ENABLE
  919. if (p_i2s_obj[i2s_num]->pm_lock) {
  920. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  921. }
  922. #endif
  923. free(p_i2s_obj[i2s_num]);
  924. p_i2s_obj[i2s_num] = NULL;
  925. periph_module_disable(i2s_periph_signal[i2s_num].module);
  926. return ESP_OK;
  927. }
  928. esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait)
  929. {
  930. char *data_ptr, *src_byte;
  931. size_t bytes_can_write;
  932. *bytes_written = 0;
  933. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  934. I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  935. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  936. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  937. #ifdef CONFIG_PM_ENABLE
  938. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  939. #endif
  940. src_byte = (char *)src;
  941. while (size > 0) {
  942. if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
  943. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  944. break;
  945. }
  946. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  947. }
  948. ESP_LOGD(I2S_TAG, "size: %d, rw_pos: %d, buf_size: %d, curr_ptr: %d", size, p_i2s_obj[i2s_num]->tx->rw_pos, p_i2s_obj[i2s_num]->tx->buf_size, (int)p_i2s_obj[i2s_num]->tx->curr_ptr);
  949. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  950. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  951. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  952. if (bytes_can_write > size) {
  953. bytes_can_write = size;
  954. }
  955. memcpy(data_ptr, src_byte, bytes_can_write);
  956. size -= bytes_can_write;
  957. src_byte += bytes_can_write;
  958. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  959. (*bytes_written) += bytes_can_write;
  960. }
  961. #ifdef CONFIG_PM_ENABLE
  962. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  963. #endif
  964. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  965. return ESP_OK;
  966. }
  967. #if SOC_I2S_SUPPORTS_ADC_DAC
  968. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  969. {
  970. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  971. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  972. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  973. adc1_dma_mode_acquire();
  974. _i2s_adc_mode_recover();
  975. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  976. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  977. return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  978. }
  979. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  980. {
  981. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  982. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  983. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  984. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  985. adc1_lock_release();
  986. return ESP_OK;
  987. }
  988. #endif
  989. esp_err_t i2s_write_expand(i2s_port_t i2s_num, const void *src, size_t size, size_t src_bits, size_t aim_bits, size_t *bytes_written, TickType_t ticks_to_wait)
  990. {
  991. char *data_ptr;
  992. int bytes_can_write, tail;
  993. int src_bytes, aim_bytes, zero_bytes;
  994. *bytes_written = 0;
  995. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  996. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  997. I2S_CHECK((aim_bits * size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  998. I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
  999. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  1000. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  1001. ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
  1002. return ESP_ERR_INVALID_ARG;
  1003. }
  1004. if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  1005. ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  1006. return ESP_ERR_INVALID_ARG;
  1007. }
  1008. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  1009. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  1010. return ESP_ERR_INVALID_ARG;
  1011. }
  1012. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  1013. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  1014. return ESP_ERR_INVALID_ARG;
  1015. }
  1016. src_bytes = src_bits / 8;
  1017. aim_bytes = aim_bits / 8;
  1018. zero_bytes = aim_bytes - src_bytes;
  1019. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1020. size = size * aim_bytes / src_bytes;
  1021. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  1022. while (size > 0) {
  1023. if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
  1024. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1025. break;
  1026. }
  1027. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1028. }
  1029. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1030. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1031. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1032. if (bytes_can_write > (int)size) {
  1033. bytes_can_write = size;
  1034. }
  1035. tail = bytes_can_write % aim_bytes;
  1036. bytes_can_write = bytes_can_write - tail;
  1037. memset(data_ptr, 0, bytes_can_write);
  1038. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1039. j += zero_bytes;
  1040. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1041. (*bytes_written) += (aim_bytes - zero_bytes);
  1042. }
  1043. size -= bytes_can_write;
  1044. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1045. }
  1046. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1047. return ESP_OK;
  1048. }
  1049. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1050. {
  1051. char *data_ptr, *dest_byte;
  1052. int bytes_can_read;
  1053. *bytes_read = 0;
  1054. dest_byte = (char *)dest;
  1055. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1056. I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1057. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1058. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1059. #ifdef CONFIG_PM_ENABLE
  1060. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1061. #endif
  1062. while (size > 0) {
  1063. if (p_i2s_obj[i2s_num]->rx->rw_pos == p_i2s_obj[i2s_num]->rx->buf_size || p_i2s_obj[i2s_num]->rx->curr_ptr == NULL) {
  1064. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1065. break;
  1066. }
  1067. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1068. }
  1069. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1070. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1071. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1072. if (bytes_can_read > (int)size) {
  1073. bytes_can_read = size;
  1074. }
  1075. memcpy(dest_byte, data_ptr, bytes_can_read);
  1076. size -= bytes_can_read;
  1077. dest_byte += bytes_can_read;
  1078. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1079. (*bytes_read) += bytes_can_read;
  1080. }
  1081. #ifdef CONFIG_PM_ENABLE
  1082. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1083. #endif
  1084. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1085. return ESP_OK;
  1086. }