i2s.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  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/xtensa_api.h"
  21. #include "freertos/semphr.h"
  22. #include "esp32/rom/lldesc.h"
  23. #include "driver/gpio.h"
  24. #include "driver/i2s.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 "esp_intr_alloc.h"
  31. #include "esp_err.h"
  32. #include "esp_attr.h"
  33. #include "esp_log.h"
  34. #include "esp_pm.h"
  35. #include "esp_efuse.h"
  36. static const char* I2S_TAG = "I2S";
  37. #define I2S_CHECK(a, str, ret) if (!(a)) { \
  38. ESP_LOGE(I2S_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  39. return (ret); \
  40. }
  41. #define I2S_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  42. #define I2S_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  43. #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  44. #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  45. #define I2S_FULL_DUPLEX_SLAVE_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_SLAVE)
  46. #define I2S_FULL_DUPLEX_MASTER_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_MASTER)
  47. /**
  48. * @brief DMA buffer object
  49. *
  50. */
  51. typedef struct {
  52. char **buf;
  53. int buf_size;
  54. int rw_pos;
  55. void *curr_ptr;
  56. SemaphoreHandle_t mux;
  57. xQueueHandle queue;
  58. lldesc_t **desc;
  59. } i2s_dma_t;
  60. /**
  61. * @brief I2S object instance
  62. *
  63. */
  64. typedef struct {
  65. i2s_port_t i2s_num; /*!< I2S port number*/
  66. int queue_size; /*!< I2S event queue size*/
  67. QueueHandle_t i2s_queue; /*!< I2S queue handler*/
  68. int dma_buf_count; /*!< DMA buffer count, number of buffer*/
  69. int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
  70. i2s_dma_t *rx; /*!< DMA Tx buffer*/
  71. i2s_dma_t *tx; /*!< DMA Rx buffer*/
  72. i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  73. int channel_num; /*!< Number of channels*/
  74. int bytes_per_sample; /*!< Bytes per sample*/
  75. int bits_per_sample; /*!< Bits per sample*/
  76. i2s_mode_t mode; /*!< I2S Working mode*/
  77. uint32_t sample_rate; /*!< I2S sample rate */
  78. bool use_apll; /*!< I2S use APLL clock */
  79. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
  80. int fixed_mclk; /*!< I2S fixed MLCK clock */
  81. double real_rate;
  82. #ifdef CONFIG_PM_ENABLE
  83. esp_pm_lock_handle_t pm_lock;
  84. #endif
  85. i2s_hal_context_t hal; /*!< I2S hal context*/
  86. } i2s_obj_t;
  87. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  88. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX];
  89. #if SOC_I2S_SUPPORTS_ADC_DAC
  90. static int _i2s_adc_unit = -1;
  91. static int _i2s_adc_channel = -1;
  92. #endif
  93. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
  94. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
  95. static inline void gpio_matrix_out_check(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  96. {
  97. //if pin = -1, do not need to configure
  98. if (gpio != -1) {
  99. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  100. gpio_set_direction(gpio, GPIO_MODE_DEF_OUTPUT);
  101. gpio_matrix_out(gpio, signal_idx, out_inv, oen_inv);
  102. }
  103. }
  104. static inline void gpio_matrix_in_check(uint32_t gpio, uint32_t signal_idx, bool inv)
  105. {
  106. if (gpio != -1) {
  107. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  108. //Set direction, for some GPIOs, the input function are not enabled as default.
  109. gpio_set_direction(gpio, GPIO_MODE_DEF_INPUT);
  110. gpio_matrix_in(gpio, signal_idx, inv);
  111. }
  112. }
  113. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  114. {
  115. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  116. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
  117. return ESP_OK;
  118. }
  119. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  120. {
  121. I2S_ENTER_CRITICAL();
  122. i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  123. I2S_EXIT_CRITICAL();
  124. return ESP_OK;
  125. }
  126. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  127. {
  128. I2S_ENTER_CRITICAL();
  129. i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  130. I2S_EXIT_CRITICAL();
  131. return ESP_OK;
  132. }
  133. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  134. {
  135. I2S_ENTER_CRITICAL();
  136. i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  137. I2S_EXIT_CRITICAL();
  138. return ESP_OK;
  139. }
  140. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  141. {
  142. I2S_ENTER_CRITICAL();
  143. i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  144. I2S_EXIT_CRITICAL();
  145. return ESP_OK;
  146. }
  147. float i2s_get_clk(i2s_port_t i2s_num)
  148. {
  149. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  150. return p_i2s_obj[i2s_num]->real_rate;
  151. }
  152. 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)
  153. {
  154. return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
  155. }
  156. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  157. {
  158. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  159. #if CONFIG_IDF_TARGET_ESP32
  160. /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
  161. if (esp_efuse_get_chip_ver() == 0) {
  162. sdm0 = 0;
  163. sdm1 = 0;
  164. }
  165. #endif
  166. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  167. if (fout < APLL_MIN_FREQ || fout > APLL_MAX_FREQ) {
  168. return APLL_MAX_FREQ;
  169. }
  170. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  171. return fpll/2;
  172. }
  173. /**
  174. * @brief APLL calculate function, was described by following:
  175. * APLL Output frequency is given by the formula:
  176. *
  177. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  178. * apll_freq = fout / ((o_div + 2) * 2)
  179. *
  180. * The dividend in this expression should be in the range of 240 - 600 MHz.
  181. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  182. * * sdm0 frequency adjustment parameter, 0..255
  183. * * sdm1 frequency adjustment parameter, 0..255
  184. * * sdm2 frequency adjustment parameter, 0..63
  185. * * o_div frequency divider, 0..31
  186. *
  187. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  188. * then apply the above formula, finding the closest frequency to the desired one.
  189. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  190. * 1. We will choose the parameters with the highest level of change,
  191. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  192. * Take average frequency close to the desired frequency, and select sdm2
  193. * 2. Next, we look for sequences of less influential and more detailed parameters,
  194. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  195. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  196. *
  197. * @param[in] rate The I2S Frequency (MCLK)
  198. * @param[in] bits_per_sample The bits per sample
  199. * @param[out] sdm0 The sdm 0
  200. * @param[out] sdm1 The sdm 1
  201. * @param[out] sdm2 The sdm 2
  202. * @param[out] odir The odir
  203. *
  204. * @return ESP_ERR_INVALID_ARG or ESP_OK
  205. */
  206. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  207. {
  208. int _odir, _sdm0, _sdm1, _sdm2;
  209. float avg;
  210. float min_rate, max_rate, min_diff;
  211. if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
  212. return ESP_ERR_INVALID_ARG;
  213. }
  214. *sdm0 = 0;
  215. *sdm1 = 0;
  216. *sdm2 = 0;
  217. *odir = 0;
  218. min_diff = APLL_MAX_FREQ;
  219. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  220. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, 0);
  221. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, 31);
  222. avg = (max_rate + min_rate)/2;
  223. if (abs(avg - rate) < min_diff) {
  224. min_diff = abs(avg - rate);
  225. *sdm2 = _sdm2;
  226. }
  227. }
  228. min_diff = APLL_MAX_FREQ;
  229. for (_odir = 0; _odir < 32; _odir ++) {
  230. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, *sdm2, _odir);
  231. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, *sdm2, _odir);
  232. avg = (max_rate + min_rate)/2;
  233. if (abs(avg - rate) < min_diff) {
  234. min_diff = abs(avg - rate);
  235. *odir = _odir;
  236. }
  237. }
  238. min_diff = APLL_MAX_FREQ;
  239. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  240. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, *odir);
  241. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, *odir);
  242. avg = (max_rate + min_rate)/2;
  243. if (abs(avg - rate) < min_diff) {
  244. min_diff = abs(avg - rate);
  245. *sdm2 = _sdm2;
  246. }
  247. }
  248. min_diff = APLL_MAX_FREQ;
  249. for (_sdm1 = 0; _sdm1 < 256; _sdm1 ++) {
  250. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, _sdm1, *sdm2, *odir);
  251. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, _sdm1, *sdm2, *odir);
  252. avg = (max_rate + min_rate)/2;
  253. if (abs(avg - rate) < min_diff) {
  254. min_diff = abs(avg - rate);
  255. *sdm1 = _sdm1;
  256. }
  257. }
  258. min_diff = APLL_MAX_FREQ;
  259. for (_sdm0 = 0; _sdm0 < 256; _sdm0 ++) {
  260. avg = i2s_apll_get_fi2s(bits_per_sample, _sdm0, *sdm1, *sdm2, *odir);
  261. if (abs(avg - rate) < min_diff) {
  262. min_diff = abs(avg - rate);
  263. *sdm0 = _sdm0;
  264. }
  265. }
  266. return ESP_OK;
  267. }
  268. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  269. {
  270. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  271. int clkmInteger, clkmDecimals, bck = 0;
  272. double denom = (double)1 / 64;
  273. int channel = 2;
  274. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  275. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  276. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  277. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  278. return ESP_ERR_INVALID_ARG;
  279. }
  280. if (p_i2s_obj[i2s_num] == NULL) {
  281. ESP_LOGE(I2S_TAG, "Not initialized yet");
  282. return ESP_ERR_INVALID_ARG;
  283. }
  284. p_i2s_obj[i2s_num]->sample_rate = rate;
  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 != ch) {
  308. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  309. }
  310. if (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 fi2s_clk = rate*channel*bits*m_scale;
  353. #if SOC_I2S_SUPPORTS_ADC_DAC
  354. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  355. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  356. //Rate as given to this function is the intended sample rate;
  357. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  358. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  359. fi2s_clk /= I2S_AD_BCK_FACTOR;
  360. int factor2 = 60;
  361. mclk = b_clk * factor2;
  362. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  363. clkmInteger = clkmdiv;
  364. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  365. bck = mclk / b_clk;
  366. #endif
  367. #if SOC_I2S_SUPPORTS_PDM
  368. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  369. uint32_t b_clk = 0;
  370. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  371. int fp;
  372. int fs;
  373. i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
  374. b_clk = rate * I2S_PDM_BCK_FACTOR * (fp / fs);
  375. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (fp / fs));
  376. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  377. bool en;
  378. i2s_hal_get_rx_sinc_dsr_16_en(&(p_i2s_obj[i2s_num]->hal), &en);
  379. b_clk = rate * I2S_PDM_BCK_FACTOR * (en ? 2 : 1);
  380. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (en ? 2 : 1));
  381. }
  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;
  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_set_pdm_rx_down_sample(&(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. periph_module_enable(i2s_periph_signal[i2s_num].module);
  773. #if SOC_I2S_SUPPORTS_ADC_DAC
  774. if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  775. //in ADC built-in mode, we need to call i2s_set_adc_mode to
  776. //initialize the specific ADC channel.
  777. //in the current stage, we only support ADC1 and single channel mode.
  778. //In default data mode, the ADC data is in 12-bit resolution mode.
  779. adc_power_acquire();
  780. }
  781. #endif
  782. // configure I2S data port interface.
  783. i2s_hal_config_param(&(p_i2s_obj[i2s_num]->hal), i2s_config);
  784. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
  785. i2s_hal_enable_sig_loopback(&(p_i2s_obj[i2s_num]->hal));
  786. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  787. i2s_hal_enable_master_mode(&(p_i2s_obj[i2s_num]->hal));
  788. } else {
  789. i2s_hal_enable_slave_mode(&(p_i2s_obj[i2s_num]->hal));
  790. }
  791. }
  792. p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
  793. p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
  794. p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
  795. return ESP_OK;
  796. }
  797. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  798. {
  799. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  800. if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
  801. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  802. memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
  803. }
  804. }
  805. if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
  806. int bytes_left = 0;
  807. bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
  808. if (bytes_left) {
  809. size_t zero_bytes = 0, bytes_written;
  810. i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
  811. }
  812. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  813. memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
  814. }
  815. }
  816. return ESP_OK;
  817. }
  818. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
  819. {
  820. esp_err_t err;
  821. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  822. I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
  823. 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);
  824. 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);
  825. if (p_i2s_obj[i2s_num] == NULL) {
  826. p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
  827. if (p_i2s_obj[i2s_num] == NULL) {
  828. ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
  829. return ESP_ERR_NO_MEM;
  830. }
  831. memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
  832. portMUX_TYPE i2s_spinlock_unlocked[1] = {portMUX_INITIALIZER_UNLOCKED};
  833. for (int x = 0; x < I2S_NUM_MAX; x++) {
  834. i2s_spinlock[x] = i2s_spinlock_unlocked[0];
  835. }
  836. //To make sure hardware is enabled before any hardware register operations.
  837. periph_module_enable(i2s_periph_signal[i2s_num].module);
  838. i2s_hal_init(&(p_i2s_obj[i2s_num]->hal), i2s_num);
  839. p_i2s_obj[i2s_num]->i2s_num = i2s_num;
  840. p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
  841. p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
  842. p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
  843. p_i2s_obj[i2s_num]->mode = i2s_config->mode;
  844. p_i2s_obj[i2s_num]->bits_per_sample = 0;
  845. p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
  846. p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  847. #ifdef CONFIG_PM_ENABLE
  848. if (i2s_config->use_apll) {
  849. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  850. } else {
  851. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  852. }
  853. if (err != ESP_OK) {
  854. free(p_i2s_obj[i2s_num]);
  855. p_i2s_obj[i2s_num] = NULL;
  856. ESP_LOGE(I2S_TAG, "I2S pm lock error");
  857. return err;
  858. }
  859. #endif //CONFIG_PM_ENABLE
  860. //initial interrupt
  861. 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);
  862. if (err != ESP_OK) {
  863. #ifdef CONFIG_PM_ENABLE
  864. if (p_i2s_obj[i2s_num]->pm_lock) {
  865. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  866. }
  867. #endif
  868. free(p_i2s_obj[i2s_num]);
  869. p_i2s_obj[i2s_num] = NULL;
  870. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  871. return err;
  872. }
  873. i2s_stop(i2s_num);
  874. err = i2s_param_config(i2s_num, i2s_config);
  875. if (err != ESP_OK) {
  876. i2s_driver_uninstall(i2s_num);
  877. ESP_LOGE(I2S_TAG, "I2S param configure error");
  878. return err;
  879. }
  880. if (i2s_queue) {
  881. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  882. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  883. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  884. } else {
  885. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  886. }
  887. //set clock and start
  888. return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  889. }
  890. ESP_LOGW(I2S_TAG, "I2S driver already installed");
  891. return ESP_OK;
  892. }
  893. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  894. {
  895. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  896. if (p_i2s_obj[i2s_num] == NULL) {
  897. ESP_LOGI(I2S_TAG, "already uninstalled");
  898. return ESP_OK;
  899. }
  900. i2s_stop(i2s_num);
  901. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  902. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  903. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  904. p_i2s_obj[i2s_num]->tx = NULL;
  905. }
  906. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  907. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  908. p_i2s_obj[i2s_num]->rx = NULL;
  909. }
  910. if (p_i2s_obj[i2s_num]->i2s_queue) {
  911. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  912. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  913. }
  914. if(p_i2s_obj[i2s_num]->use_apll) {
  915. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  916. }
  917. #ifdef CONFIG_PM_ENABLE
  918. if (p_i2s_obj[i2s_num]->pm_lock) {
  919. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  920. }
  921. #endif
  922. free(p_i2s_obj[i2s_num]);
  923. p_i2s_obj[i2s_num] = NULL;
  924. periph_module_disable(i2s_periph_signal[i2s_num].module);
  925. return ESP_OK;
  926. }
  927. 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)
  928. {
  929. char *data_ptr, *src_byte;
  930. int bytes_can_write;
  931. *bytes_written = 0;
  932. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  933. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  934. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  935. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  936. #ifdef CONFIG_PM_ENABLE
  937. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  938. #endif
  939. src_byte = (char *)src;
  940. while (size > 0) {
  941. 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) {
  942. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  943. break;
  944. }
  945. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  946. }
  947. 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);
  948. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  949. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  950. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  951. if (bytes_can_write > size) {
  952. bytes_can_write = size;
  953. }
  954. memcpy(data_ptr, src_byte, bytes_can_write);
  955. size -= bytes_can_write;
  956. src_byte += bytes_can_write;
  957. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  958. (*bytes_written) += bytes_can_write;
  959. }
  960. #ifdef CONFIG_PM_ENABLE
  961. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  962. #endif
  963. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  964. return ESP_OK;
  965. }
  966. #if SOC_I2S_SUPPORTS_ADC_DAC
  967. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  968. {
  969. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  970. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  971. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  972. adc1_dma_mode_acquire();
  973. _i2s_adc_mode_recover();
  974. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  975. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  976. 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);
  977. }
  978. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  979. {
  980. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  981. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  982. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  983. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  984. adc1_lock_release();
  985. return ESP_OK;
  986. }
  987. #endif
  988. 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)
  989. {
  990. char *data_ptr;
  991. int bytes_can_write, tail;
  992. int src_bytes, aim_bytes, zero_bytes;
  993. *bytes_written = 0;
  994. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  995. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  996. I2S_CHECK((aim_bits * size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  997. I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
  998. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  999. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  1000. ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
  1001. return ESP_ERR_INVALID_ARG;
  1002. }
  1003. if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  1004. ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  1005. return ESP_ERR_INVALID_ARG;
  1006. }
  1007. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  1008. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  1009. return ESP_ERR_INVALID_ARG;
  1010. }
  1011. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  1012. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  1013. return ESP_ERR_INVALID_ARG;
  1014. }
  1015. src_bytes = src_bits / 8;
  1016. aim_bytes = aim_bits / 8;
  1017. zero_bytes = aim_bytes - src_bytes;
  1018. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1019. size = size * aim_bytes / src_bytes;
  1020. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  1021. while (size > 0) {
  1022. 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) {
  1023. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1024. break;
  1025. }
  1026. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1027. }
  1028. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1029. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1030. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1031. if (bytes_can_write > size) {
  1032. bytes_can_write = size;
  1033. }
  1034. tail = bytes_can_write % aim_bytes;
  1035. bytes_can_write = bytes_can_write - tail;
  1036. memset(data_ptr, 0, bytes_can_write);
  1037. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1038. j += zero_bytes;
  1039. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1040. (*bytes_written) += (aim_bytes - zero_bytes);
  1041. }
  1042. size -= bytes_can_write;
  1043. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1044. }
  1045. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1046. return ESP_OK;
  1047. }
  1048. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1049. {
  1050. char *data_ptr, *dest_byte;
  1051. int bytes_can_read;
  1052. *bytes_read = 0;
  1053. dest_byte = (char *)dest;
  1054. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1055. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1056. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1057. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1058. #ifdef CONFIG_PM_ENABLE
  1059. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1060. #endif
  1061. while (size > 0) {
  1062. 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) {
  1063. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1064. break;
  1065. }
  1066. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1067. }
  1068. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1069. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1070. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1071. if (bytes_can_read > size) {
  1072. bytes_can_read = size;
  1073. }
  1074. memcpy(dest_byte, data_ptr, bytes_can_read);
  1075. size -= bytes_can_read;
  1076. dest_byte += bytes_can_read;
  1077. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1078. (*bytes_read) += bytes_can_read;
  1079. }
  1080. #ifdef CONFIG_PM_ENABLE
  1081. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1082. #endif
  1083. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1084. return ESP_OK;
  1085. }