i2s.c 48 KB

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