i2s.c 48 KB

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