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. ESP_LOGW(I2S_TAG, "THIS IS US IN I2S !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  269. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  270. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  271. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  272. return ESP_ERR_INVALID_ARG;
  273. }
  274. if (p_i2s_obj[i2s_num] == NULL) {
  275. ESP_LOGE(I2S_TAG, "Not initialized yet");
  276. return ESP_ERR_INVALID_ARG;
  277. }
  278. p_i2s_obj[i2s_num]->sample_rate = rate;
  279. /**
  280. * Due to hardware issue, bck division on ESP32/ESP32-S2 should be greater than 8 in slave mode
  281. * So the factor need to be an appropriate value
  282. */
  283. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) && !p_i2s_obj[i2s_num]->use_apll) {
  284. factor = 64 * bits;
  285. }
  286. double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
  287. if (clkmdiv > 256) {
  288. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  289. return ESP_ERR_INVALID_ARG;
  290. }
  291. // wait all on-going writing finish
  292. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  293. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  294. }
  295. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  296. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  297. }
  298. i2s_stop(i2s_num);
  299. #if SOC_I2S_SUPPORTS_ADC_DAC
  300. /* I2S-ADC only support single channel format. */
  301. if (!(p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN)) {
  302. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  303. }
  304. #else
  305. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  306. #endif
  307. i2s_hal_set_tx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  308. if (p_i2s_obj[i2s_num]->channel_num != (int)ch) {
  309. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  310. }
  311. if ((int)bits != p_i2s_obj[i2s_num]->bits_per_sample) {
  312. p_i2s_obj[i2s_num]->bits_per_sample = bits;
  313. // Round bytes_per_sample up to next multiple of 16 bits
  314. int halfwords_per_sample = (bits + 15) / 16;
  315. p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
  316. // Because limited of DMA buffer is 4092 bytes
  317. 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) {
  318. p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
  319. }
  320. // Re-create TX DMA buffer
  321. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  322. save_tx = p_i2s_obj[i2s_num]->tx;
  323. 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);
  324. if (p_i2s_obj[i2s_num]->tx == NULL) {
  325. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  326. i2s_driver_uninstall(i2s_num);
  327. return ESP_ERR_NO_MEM;
  328. }
  329. i2s_hal_set_out_link_addr(&(p_i2s_obj[i2s_num]->hal), (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0]);
  330. //destroy old tx dma if exist
  331. if (save_tx) {
  332. i2s_destroy_dma_queue(i2s_num, save_tx);
  333. }
  334. }
  335. // Re-create RX DMA buffer
  336. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  337. save_rx = p_i2s_obj[i2s_num]->rx;
  338. 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);
  339. if (p_i2s_obj[i2s_num]->rx == NULL){
  340. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  341. i2s_driver_uninstall(i2s_num);
  342. return ESP_ERR_NO_MEM;
  343. }
  344. 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]);
  345. //destroy old rx dma if exist
  346. if (save_rx) {
  347. i2s_destroy_dma_queue(i2s_num, save_rx);
  348. }
  349. }
  350. }
  351. double mclk;
  352. //int sdm0, sdm1, sdm2, odir, m_scale = 8;
  353. int sdm0, sdm1, sdm2, odir, m_scale = (rate > 96000 && bits > 16) ? 4 : 8;
  354. int fi2s_clk = rate*channel*bits*m_scale;
  355. #if SOC_I2S_SUPPORTS_ADC_DAC
  356. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  357. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  358. //Rate as given to this function is the intended sample rate;
  359. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  360. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  361. fi2s_clk /= I2S_AD_BCK_FACTOR;
  362. int factor2 = 60;
  363. mclk = b_clk * factor2;
  364. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  365. clkmInteger = clkmdiv;
  366. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  367. bck = mclk / b_clk;
  368. #endif
  369. #if SOC_I2S_SUPPORTS_PDM
  370. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  371. uint32_t b_clk = 0;
  372. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  373. uint32_t fp, fs;
  374. i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
  375. // Recommended set `fp = 960, fs = sample_rate / 100`
  376. fs = rate / 100;
  377. i2s_hal_tx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), fp, fs);
  378. b_clk = rate * I2S_PDM_BCK_FACTOR * fp / fs;
  379. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  380. uint32_t dsr;
  381. i2s_hal_get_rx_pdm(&(p_i2s_obj[i2s_num]->hal), &dsr);
  382. b_clk = rate * I2S_PDM_BCK_FACTOR * (dsr ? 2 : 1);
  383. }
  384. fi2s_clk = b_clk * m_scale;
  385. int factor2 = 5 ;
  386. mclk = b_clk * factor2;
  387. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  388. clkmInteger = clkmdiv;
  389. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  390. bck = mclk / b_clk;
  391. } else
  392. #endif
  393. {
  394. clkmInteger = clkmdiv;
  395. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  396. mclk = clkmInteger + denom * clkmDecimals;
  397. bck = factor/(bits * channel);
  398. }
  399. if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
  400. fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
  401. m_scale = fi2s_clk/bits/rate/channel;
  402. }
  403. if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
  404. ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
  405. rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
  406. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), 1, 1, 0, m_scale, m_scale);
  407. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_APLL);
  408. double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
  409. p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
  410. 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",
  411. rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
  412. } else {
  413. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  414. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), clkmInteger, 63, clkmDecimals, bck, bck);
  415. double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
  416. p_i2s_obj[i2s_num]->real_rate = real_rate;
  417. 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",
  418. rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
  419. }
  420. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  421. p_i2s_obj[i2s_num]->tx->curr_ptr = NULL;
  422. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  423. }
  424. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  425. p_i2s_obj[i2s_num]->rx->curr_ptr = NULL;
  426. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  427. }
  428. i2s_hal_set_tx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  429. i2s_hal_set_rx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  430. // wait all writing on-going finish
  431. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  432. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  433. }
  434. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  435. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  436. }
  437. i2s_start(i2s_num);
  438. return ESP_OK;
  439. }
  440. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  441. {
  442. i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
  443. uint32_t status;
  444. i2s_hal_get_intr_status(&(p_i2s->hal), &status);
  445. if(status == 0) {
  446. //Avoid spurious interrupt
  447. return;
  448. }
  449. i2s_event_t i2s_event;
  450. int dummy;
  451. portBASE_TYPE high_priority_task_awoken = 0;
  452. lldesc_t *finish_desc = NULL;
  453. if ((status & I2S_INTR_OUT_DSCR_ERR) || (status & I2S_INTR_IN_DSCR_ERR)) {
  454. ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", status);
  455. if (p_i2s->i2s_queue) {
  456. i2s_event.type = I2S_EVENT_DMA_ERROR;
  457. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  458. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  459. }
  460. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  461. }
  462. }
  463. if ((status & I2S_INTR_OUT_EOF) && p_i2s->tx) {
  464. i2s_hal_get_out_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  465. // All buffers are empty. This means we have an underflow on our hands.
  466. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  467. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
  468. // See if tx descriptor needs to be auto cleared:
  469. // This will avoid any kind of noise that may get introduced due to transmission
  470. // of previous data from tx descriptor on I2S line.
  471. if (p_i2s->tx_desc_auto_clear == true) {
  472. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  473. }
  474. }
  475. xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  476. if (p_i2s->i2s_queue) {
  477. i2s_event.type = I2S_EVENT_TX_DONE;
  478. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  479. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  480. }
  481. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  482. }
  483. }
  484. if ((status & I2S_INTR_IN_SUC_EOF) && p_i2s->rx) {
  485. // All buffers are full. This means we have an overflow.
  486. i2s_hal_get_in_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  487. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  488. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
  489. }
  490. xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  491. if (p_i2s->i2s_queue) {
  492. i2s_event.type = I2S_EVENT_RX_DONE;
  493. if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  494. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  495. }
  496. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  497. }
  498. }
  499. i2s_hal_clear_intr_status(&(p_i2s->hal), status);
  500. if (high_priority_task_awoken == pdTRUE) {
  501. portYIELD_FROM_ISR();
  502. }
  503. }
  504. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
  505. {
  506. int bux_idx;
  507. if (p_i2s_obj[i2s_num] == NULL) {
  508. ESP_LOGE(I2S_TAG, "Not initialized yet");
  509. return ESP_ERR_INVALID_ARG;
  510. }
  511. if (dma == NULL) {
  512. ESP_LOGE(I2S_TAG, "dma is NULL");
  513. return ESP_ERR_INVALID_ARG;
  514. }
  515. for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
  516. if (dma->desc && dma->desc[bux_idx]) {
  517. free(dma->desc[bux_idx]);
  518. }
  519. if (dma->buf && dma->buf[bux_idx]) {
  520. free(dma->buf[bux_idx]);
  521. }
  522. }
  523. if (dma->buf) {
  524. free(dma->buf);
  525. }
  526. if (dma->desc) {
  527. free(dma->desc);
  528. }
  529. vQueueDelete(dma->queue);
  530. vSemaphoreDelete(dma->mux);
  531. free(dma);
  532. return ESP_OK;
  533. }
  534. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
  535. {
  536. int bux_idx;
  537. int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  538. i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
  539. if (dma == NULL) {
  540. ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
  541. return NULL;
  542. }
  543. memset(dma, 0, sizeof(i2s_dma_t));
  544. dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
  545. if (dma->buf == NULL) {
  546. ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
  547. free(dma);
  548. return NULL;
  549. }
  550. memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
  551. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  552. dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
  553. if (dma->buf[bux_idx] == NULL) {
  554. ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
  555. i2s_destroy_dma_queue(i2s_num, dma);
  556. return NULL;
  557. }
  558. ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
  559. }
  560. dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
  561. if (dma->desc == NULL) {
  562. ESP_LOGE(I2S_TAG, "Error malloc dma description");
  563. i2s_destroy_dma_queue(i2s_num, dma);
  564. return NULL;
  565. }
  566. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  567. dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  568. if (dma->desc[bux_idx] == NULL) {
  569. ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
  570. i2s_destroy_dma_queue(i2s_num, dma);
  571. return NULL;
  572. }
  573. }
  574. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  575. dma->desc[bux_idx]->owner = 1;
  576. dma->desc[bux_idx]->eof = 1;
  577. dma->desc[bux_idx]->sosf = 0;
  578. dma->desc[bux_idx]->length = dma_buf_len * sample_size;
  579. dma->desc[bux_idx]->size = dma_buf_len * sample_size;
  580. dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
  581. dma->desc[bux_idx]->offset = 0;
  582. dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
  583. }
  584. dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
  585. dma->mux = xSemaphoreCreateMutex();
  586. dma->buf_size = dma_buf_len * sample_size;
  587. ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
  588. return dma;
  589. }
  590. esp_err_t i2s_start(i2s_port_t i2s_num)
  591. {
  592. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  593. //start DMA link
  594. I2S_ENTER_CRITICAL();
  595. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  596. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  597. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), I2S_INTR_MAX);
  598. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  599. i2s_enable_tx_intr(i2s_num);
  600. i2s_hal_start_tx(&(p_i2s_obj[i2s_num]->hal));
  601. }
  602. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  603. i2s_enable_rx_intr(i2s_num);
  604. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  605. }
  606. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  607. I2S_EXIT_CRITICAL();
  608. return ESP_OK;
  609. }
  610. esp_err_t i2s_stop(i2s_port_t i2s_num)
  611. {
  612. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  613. I2S_ENTER_CRITICAL();
  614. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  615. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  616. i2s_hal_stop_tx(&(p_i2s_obj[i2s_num]->hal));
  617. i2s_disable_tx_intr(i2s_num);
  618. }
  619. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  620. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  621. i2s_disable_rx_intr(i2s_num);
  622. }
  623. uint32_t mask;
  624. i2s_hal_get_intr_status(&(p_i2s_obj[i2s_num]->hal), &mask);
  625. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), mask);
  626. I2S_EXIT_CRITICAL();
  627. return ESP_OK;
  628. }
  629. #if SOC_I2S_SUPPORTS_ADC_DAC
  630. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  631. {
  632. I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
  633. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  634. dac_output_disable(DAC_CHANNEL_1);
  635. dac_output_disable(DAC_CHANNEL_2);
  636. dac_i2s_disable();
  637. } else {
  638. dac_i2s_enable();
  639. }
  640. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  641. //DAC1, right channel
  642. dac_output_enable(DAC_CHANNEL_1);
  643. }
  644. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  645. //DAC2, left channel
  646. dac_output_enable(DAC_CHANNEL_2);
  647. }
  648. return ESP_OK;
  649. }
  650. static esp_err_t _i2s_adc_mode_recover(void)
  651. {
  652. I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
  653. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  654. }
  655. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  656. {
  657. I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
  658. // For now, we only support SAR ADC1.
  659. _i2s_adc_unit = adc_unit;
  660. _i2s_adc_channel = adc_channel;
  661. return adc_i2s_mode_init(adc_unit, adc_channel);
  662. }
  663. #endif
  664. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  665. {
  666. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  667. if (pin == NULL) {
  668. #if SOC_I2S_SUPPORTS_ADC_DAC
  669. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  670. #else
  671. return ESP_ERR_INVALID_ARG;
  672. #endif
  673. }
  674. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  675. ESP_LOGE(I2S_TAG, "bck_io_num error");
  676. return ESP_FAIL;
  677. }
  678. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  679. ESP_LOGE(I2S_TAG, "ws_io_num error");
  680. return ESP_FAIL;
  681. }
  682. if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
  683. ESP_LOGE(I2S_TAG, "data_out_num error");
  684. return ESP_FAIL;
  685. }
  686. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  687. ESP_LOGE(I2S_TAG, "data_in_num error");
  688. return ESP_FAIL;
  689. }
  690. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  691. //Each IIS hw module has a RX and TX unit.
  692. //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
  693. //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
  694. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  695. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  696. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  697. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  698. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  699. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  700. bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
  701. ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
  702. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  703. }
  704. }
  705. //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
  706. //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
  707. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  708. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  709. bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
  710. ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
  711. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  712. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  713. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  714. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  715. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  716. }
  717. }
  718. //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
  719. //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
  720. if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
  721. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  722. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  723. } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
  724. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  725. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  726. }
  727. gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
  728. gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
  729. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  730. gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
  731. gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
  732. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  733. gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
  734. gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
  735. }
  736. ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
  737. return ESP_OK;
  738. }
  739. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  740. {
  741. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  742. I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
  743. return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  744. }
  745. #if SOC_I2S_SUPPORTS_PDM
  746. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
  747. {
  748. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  749. i2s_hal_rx_pdm_cfg(&(p_i2s_obj[i2s_num]->hal), dsr);
  750. 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);
  751. }
  752. #endif
  753. static esp_err_t i2s_check_cfg_static(i2s_port_t i2s_num, const i2s_config_t *cfg)
  754. {
  755. #if SOC_I2S_SUPPORTS_ADC_DAC
  756. //We only check if the I2S number is invalid when set to build in ADC and DAC mode.
  757. 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);
  758. 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);
  759. return ESP_OK;
  760. #endif
  761. #if SOC_I2S_SUPPORTS_PDM
  762. //We only check if the I2S number is invalid when set to PDM mode.
  763. I2S_CHECK(!((cfg->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
  764. return ESP_OK;
  765. #endif
  766. I2S_CHECK(cfg->communication_format && (cfg->communication_format < I2S_COMM_FORMAT_STAND_MAX), "invalid communication formats", ESP_ERR_INVALID_ARG);
  767. 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);
  768. return ESP_OK;
  769. }
  770. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  771. {
  772. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  773. I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
  774. I2S_CHECK((i2s_check_cfg_static(i2s_num, i2s_config) == ESP_OK), "param check error", ESP_ERR_INVALID_ARG);
  775. #if SOC_I2S_SUPPORTS_ADC_DAC
  776. if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  777. //in ADC built-in mode, we need to call i2s_set_adc_mode to
  778. //initialize the specific ADC channel.
  779. //in the current stage, we only support ADC1 and single channel mode.
  780. //In default data mode, the ADC data is in 12-bit resolution mode.
  781. adc_power_acquire();
  782. }
  783. #endif
  784. // configure I2S data port interface.
  785. i2s_hal_config_param(&(p_i2s_obj[i2s_num]->hal), i2s_config);
  786. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
  787. i2s_hal_enable_sig_loopback(&(p_i2s_obj[i2s_num]->hal));
  788. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  789. i2s_hal_enable_master_mode(&(p_i2s_obj[i2s_num]->hal));
  790. } else {
  791. i2s_hal_enable_slave_mode(&(p_i2s_obj[i2s_num]->hal));
  792. }
  793. }
  794. p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
  795. p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
  796. p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
  797. return ESP_OK;
  798. }
  799. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  800. {
  801. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  802. if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
  803. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  804. memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
  805. }
  806. }
  807. if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
  808. int bytes_left = 0;
  809. bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
  810. if (bytes_left) {
  811. size_t zero_bytes = 0, bytes_written;
  812. i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
  813. }
  814. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  815. memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
  816. }
  817. }
  818. return ESP_OK;
  819. }
  820. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
  821. {
  822. esp_err_t err;
  823. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  824. I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
  825. 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);
  826. 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);
  827. if (p_i2s_obj[i2s_num] == NULL) {
  828. p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
  829. if (p_i2s_obj[i2s_num] == NULL) {
  830. ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
  831. return ESP_ERR_NO_MEM;
  832. }
  833. memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
  834. portMUX_TYPE i2s_spinlock_unlocked[1] = {portMUX_INITIALIZER_UNLOCKED};
  835. for (int x = 0; x < I2S_NUM_MAX; x++) {
  836. i2s_spinlock[x] = i2s_spinlock_unlocked[0];
  837. }
  838. //To make sure hardware is enabled before any hardware register operations.
  839. periph_module_enable(i2s_periph_signal[i2s_num].module);
  840. i2s_hal_init(&(p_i2s_obj[i2s_num]->hal), i2s_num);
  841. p_i2s_obj[i2s_num]->i2s_num = i2s_num;
  842. p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
  843. p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
  844. p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
  845. p_i2s_obj[i2s_num]->mode = i2s_config->mode;
  846. p_i2s_obj[i2s_num]->bits_per_sample = 0;
  847. p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
  848. p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  849. #ifdef CONFIG_PM_ENABLE
  850. if (i2s_config->use_apll) {
  851. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  852. } else {
  853. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  854. }
  855. if (err != ESP_OK) {
  856. free(p_i2s_obj[i2s_num]);
  857. p_i2s_obj[i2s_num] = NULL;
  858. ESP_LOGE(I2S_TAG, "I2S pm lock error");
  859. return err;
  860. }
  861. #endif //CONFIG_PM_ENABLE
  862. //initial interrupt
  863. 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);
  864. if (err != ESP_OK) {
  865. #ifdef CONFIG_PM_ENABLE
  866. if (p_i2s_obj[i2s_num]->pm_lock) {
  867. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  868. }
  869. #endif
  870. free(p_i2s_obj[i2s_num]);
  871. p_i2s_obj[i2s_num] = NULL;
  872. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  873. return err;
  874. }
  875. i2s_stop(i2s_num);
  876. err = i2s_param_config(i2s_num, i2s_config);
  877. if (err != ESP_OK) {
  878. i2s_driver_uninstall(i2s_num);
  879. ESP_LOGE(I2S_TAG, "I2S param configure error");
  880. return err;
  881. }
  882. if (i2s_queue) {
  883. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  884. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  885. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  886. } else {
  887. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  888. }
  889. //set clock and start
  890. return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  891. }
  892. ESP_LOGW(I2S_TAG, "I2S driver already installed");
  893. return ESP_OK;
  894. }
  895. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  896. {
  897. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  898. if (p_i2s_obj[i2s_num] == NULL) {
  899. ESP_LOGI(I2S_TAG, "already uninstalled");
  900. return ESP_OK;
  901. }
  902. i2s_stop(i2s_num);
  903. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  904. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  905. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  906. p_i2s_obj[i2s_num]->tx = NULL;
  907. }
  908. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  909. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  910. p_i2s_obj[i2s_num]->rx = NULL;
  911. }
  912. if (p_i2s_obj[i2s_num]->i2s_queue) {
  913. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  914. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  915. }
  916. if(p_i2s_obj[i2s_num]->use_apll) {
  917. // switch back to PLL clock source
  918. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  919. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  920. }
  921. #ifdef CONFIG_PM_ENABLE
  922. if (p_i2s_obj[i2s_num]->pm_lock) {
  923. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  924. }
  925. #endif
  926. free(p_i2s_obj[i2s_num]);
  927. p_i2s_obj[i2s_num] = NULL;
  928. periph_module_disable(i2s_periph_signal[i2s_num].module);
  929. return ESP_OK;
  930. }
  931. 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)
  932. {
  933. char *data_ptr, *src_byte;
  934. size_t bytes_can_write;
  935. *bytes_written = 0;
  936. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  937. I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  938. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  939. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  940. #ifdef CONFIG_PM_ENABLE
  941. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  942. #endif
  943. src_byte = (char *)src;
  944. while (size > 0) {
  945. 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) {
  946. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  947. break;
  948. }
  949. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  950. }
  951. 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);
  952. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  953. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  954. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  955. if (bytes_can_write > size) {
  956. bytes_can_write = size;
  957. }
  958. memcpy(data_ptr, src_byte, bytes_can_write);
  959. size -= bytes_can_write;
  960. src_byte += bytes_can_write;
  961. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  962. (*bytes_written) += bytes_can_write;
  963. }
  964. #ifdef CONFIG_PM_ENABLE
  965. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  966. #endif
  967. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  968. return ESP_OK;
  969. }
  970. #if SOC_I2S_SUPPORTS_ADC_DAC
  971. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  972. {
  973. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  974. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  975. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  976. adc1_dma_mode_acquire();
  977. _i2s_adc_mode_recover();
  978. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  979. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  980. 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);
  981. }
  982. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  983. {
  984. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  985. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  986. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  987. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  988. adc1_lock_release();
  989. return ESP_OK;
  990. }
  991. #endif
  992. 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)
  993. {
  994. char *data_ptr;
  995. int bytes_can_write, tail;
  996. int src_bytes, aim_bytes, zero_bytes;
  997. *bytes_written = 0;
  998. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  999. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  1000. I2S_CHECK((aim_bits * size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1001. I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
  1002. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  1003. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  1004. ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, 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_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  1008. ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  1009. return ESP_ERR_INVALID_ARG;
  1010. }
  1011. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  1012. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  1013. return ESP_ERR_INVALID_ARG;
  1014. }
  1015. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  1016. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  1017. return ESP_ERR_INVALID_ARG;
  1018. }
  1019. src_bytes = src_bits / 8;
  1020. aim_bytes = aim_bits / 8;
  1021. zero_bytes = aim_bytes - src_bytes;
  1022. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1023. size = size * aim_bytes / src_bytes;
  1024. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  1025. while (size > 0) {
  1026. 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) {
  1027. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1028. break;
  1029. }
  1030. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1031. }
  1032. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1033. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1034. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1035. if (bytes_can_write > (int)size) {
  1036. bytes_can_write = size;
  1037. }
  1038. tail = bytes_can_write % aim_bytes;
  1039. bytes_can_write = bytes_can_write - tail;
  1040. memset(data_ptr, 0, bytes_can_write);
  1041. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1042. j += zero_bytes;
  1043. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1044. (*bytes_written) += (aim_bytes - zero_bytes);
  1045. }
  1046. size -= bytes_can_write;
  1047. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1048. }
  1049. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1050. return ESP_OK;
  1051. }
  1052. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1053. {
  1054. char *data_ptr, *dest_byte;
  1055. int bytes_can_read;
  1056. *bytes_read = 0;
  1057. dest_byte = (char *)dest;
  1058. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1059. I2S_CHECK((size < SOC_I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1060. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1061. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1062. #ifdef CONFIG_PM_ENABLE
  1063. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1064. #endif
  1065. while (size > 0) {
  1066. 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) {
  1067. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1068. break;
  1069. }
  1070. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1071. }
  1072. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1073. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1074. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1075. if (bytes_can_read > (int)size) {
  1076. bytes_can_read = size;
  1077. }
  1078. memcpy(dest_byte, data_ptr, bytes_can_read);
  1079. size -= bytes_can_read;
  1080. dest_byte += bytes_can_read;
  1081. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1082. (*bytes_read) += bytes_can_read;
  1083. }
  1084. #ifdef CONFIG_PM_ENABLE
  1085. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1086. #endif
  1087. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1088. return ESP_OK;
  1089. }