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. #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 "soc/chip_revision.h"
  37. #include "hal/efuse_hal.h"
  38. #include "esp_rom_gpio.h"
  39. #include "sdkconfig.h"
  40. static const char* I2S_TAG = "I2S";
  41. #define I2S_CHECK(a, str, ret) if (!(a)) { \
  42. ESP_LOGE(I2S_TAG,"%s(%d): %s", __FUNCTION__, __LINE__, str); \
  43. return (ret); \
  44. }
  45. #define I2S_ENTER_CRITICAL_ISR() portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  46. #define I2S_EXIT_CRITICAL_ISR() portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  47. #define I2S_ENTER_CRITICAL() portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  48. #define I2S_EXIT_CRITICAL() portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  49. #define I2S_FULL_DUPLEX_SLAVE_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_SLAVE)
  50. #define I2S_FULL_DUPLEX_MASTER_MODE_MASK (I2S_MODE_TX | I2S_MODE_RX | I2S_MODE_MASTER)
  51. //TODO: Refactor to put this logic into LL
  52. #define I2S_AD_BCK_FACTOR (2)
  53. #define I2S_PDM_BCK_FACTOR (64)
  54. #define I2S_BASE_CLK (2*APB_CLK_FREQ)
  55. #define APLL_I2S_MIN_RATE (10675) //in Hz, I2S Clock rate limited by hardware
  56. /**
  57. * @brief DMA buffer object
  58. *
  59. */
  60. typedef struct {
  61. char **buf;
  62. int buf_size;
  63. int rw_pos;
  64. void *curr_ptr;
  65. SemaphoreHandle_t mux;
  66. xQueueHandle queue;
  67. lldesc_t **desc;
  68. } i2s_dma_t;
  69. /**
  70. * @brief I2S object instance
  71. *
  72. */
  73. typedef struct {
  74. i2s_port_t i2s_num; /*!< I2S port number*/
  75. int queue_size; /*!< I2S event queue size*/
  76. QueueHandle_t i2s_queue; /*!< I2S queue handler*/
  77. int dma_buf_count; /*!< DMA buffer count, number of buffer*/
  78. int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
  79. i2s_dma_t *rx; /*!< DMA Tx buffer*/
  80. i2s_dma_t *tx; /*!< DMA Rx buffer*/
  81. i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  82. int channel_num; /*!< Number of channels*/
  83. int bytes_per_sample; /*!< Bytes per sample*/
  84. int bits_per_sample; /*!< Bits per sample*/
  85. i2s_mode_t mode; /*!< I2S Working mode*/
  86. uint32_t sample_rate; /*!< I2S sample rate */
  87. bool use_apll; /*!< I2S use APLL clock */
  88. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
  89. int fixed_mclk; /*!< I2S fixed MLCK clock */
  90. double real_rate;
  91. #ifdef CONFIG_PM_ENABLE
  92. esp_pm_lock_handle_t pm_lock;
  93. #endif
  94. i2s_hal_context_t hal; /*!< I2S hal context*/
  95. } i2s_obj_t;
  96. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  97. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX];
  98. #if SOC_I2S_SUPPORTS_ADC_DAC
  99. static int _i2s_adc_unit = -1;
  100. static int _i2s_adc_channel = -1;
  101. #endif
  102. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
  103. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
  104. static inline void gpio_matrix_out_check(int gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  105. {
  106. //if pin = -1, do not need to configure
  107. if (gpio != -1) {
  108. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  109. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  110. esp_rom_gpio_connect_out_signal(gpio, signal_idx, out_inv, oen_inv);
  111. }
  112. }
  113. static inline void gpio_matrix_in_check(int gpio, uint32_t signal_idx, bool inv)
  114. {
  115. if (gpio != -1) {
  116. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  117. //Set direction, for some GPIOs, the input function are not enabled as default.
  118. gpio_set_direction(gpio, GPIO_MODE_INPUT);
  119. esp_rom_gpio_connect_in_signal(gpio, signal_idx, inv);
  120. }
  121. }
  122. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  123. {
  124. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  125. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
  126. return ESP_OK;
  127. }
  128. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  129. {
  130. I2S_ENTER_CRITICAL();
  131. i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  132. I2S_EXIT_CRITICAL();
  133. return ESP_OK;
  134. }
  135. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  136. {
  137. I2S_ENTER_CRITICAL();
  138. i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  139. I2S_EXIT_CRITICAL();
  140. return ESP_OK;
  141. }
  142. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  143. {
  144. I2S_ENTER_CRITICAL();
  145. i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  146. I2S_EXIT_CRITICAL();
  147. return ESP_OK;
  148. }
  149. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  150. {
  151. I2S_ENTER_CRITICAL();
  152. i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  153. I2S_EXIT_CRITICAL();
  154. return ESP_OK;
  155. }
  156. float i2s_get_clk(i2s_port_t i2s_num)
  157. {
  158. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  159. return p_i2s_obj[i2s_num]->real_rate;
  160. }
  161. 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)
  162. {
  163. return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
  164. }
  165. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  166. {
  167. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  168. #if CONFIG_IDF_TARGET_ESP32
  169. /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
  170. if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 100)) {
  171. sdm0 = 0;
  172. sdm1 = 0;
  173. }
  174. #endif
  175. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  176. if (fout < SOC_I2S_APLL_MIN_FREQ || fout > SOC_I2S_APLL_MAX_FREQ) {
  177. return SOC_I2S_APLL_MAX_FREQ;
  178. }
  179. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  180. return fpll/2;
  181. }
  182. /**
  183. * @brief APLL calculate function, was described by following:
  184. * APLL Output frequency is given by the formula:
  185. *
  186. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  187. * apll_freq = fout / ((o_div + 2) * 2)
  188. *
  189. * The dividend in this expression should be in the range of 240 - 600 MHz.
  190. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  191. * * sdm0 frequency adjustment parameter, 0..255
  192. * * sdm1 frequency adjustment parameter, 0..255
  193. * * sdm2 frequency adjustment parameter, 0..63
  194. * * o_div frequency divider, 0..31
  195. *
  196. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  197. * then apply the above formula, finding the closest frequency to the desired one.
  198. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  199. * 1. We will choose the parameters with the highest level of change,
  200. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  201. * Take average frequency close to the desired frequency, and select sdm2
  202. * 2. Next, we look for sequences of less influential and more detailed parameters,
  203. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  204. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  205. *
  206. * @param[in] rate The I2S Frequency (MCLK)
  207. * @param[in] bits_per_sample The bits per sample
  208. * @param[out] sdm0 The sdm 0
  209. * @param[out] sdm1 The sdm 1
  210. * @param[out] sdm2 The sdm 2
  211. * @param[out] odir The odir
  212. *
  213. * @return ESP_ERR_INVALID_ARG or ESP_OK
  214. */
  215. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  216. {
  217. int _odir, _sdm0, _sdm1, _sdm2;
  218. float r = rtc_clk_xtal_freq_get() * 1000000. / (rate * 2 * 2);
  219. int _sdm2_max;
  220. uint32_t prec = -1;
  221. int o, s1, s0;
  222. if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
  223. return ESP_ERR_INVALID_ARG;
  224. }
  225. *sdm0 = 0;
  226. *sdm1 = 0;
  227. *sdm2 = 0;
  228. *odir = 0;
  229. _sdm2 = 1/r * 2 - 4;
  230. if (_sdm2 < 4) _sdm2 = 4;
  231. _sdm2_max = ceil(1/r * (31 + 2) - (255/256 + 255/65536 + 4));
  232. if (_sdm2_max > 8) _sdm2_max = 8;
  233. // explore up to 5 sdm2 values
  234. for (; _sdm2 < _sdm2_max; _sdm2++) {
  235. _odir = r * (_sdm2 + 4) - 2;
  236. if (_odir < 0) _odir = 0;
  237. else if (_odir > 31) _odir = 31;
  238. for (o = 0; o < 2 && _odir + o < 32; o++) {
  239. _sdm1 = 256*((_odir + o + 2) / r - (_sdm2 + 4));
  240. if (_sdm1 < 0) _sdm1 = 0;
  241. else if (_sdm1 > 255) _sdm1 = 255;
  242. for (s1 = 0; s1 < 2 && _sdm1 + s1 < 256; s1++) {
  243. _sdm0 = 65536*((_odir + o + 2) / r - (_sdm2 + (float) (_sdm1 + s1)/256 + 4));
  244. if (_sdm0 < 0) _sdm1 = 0;
  245. else if (_sdm0 > 255) _sdm0 = 255;
  246. for (s0 = 0; s0 < 2 && _sdm2 + s0 < 256; s0++) {
  247. int _fi2s = i2s_apll_get_fi2s(bits_per_sample, _sdm0 + s0, _sdm1 + s1, _sdm2, _odir + o);
  248. if (abs(_fi2s - rate) < prec) {
  249. prec = abs(_fi2s - rate);
  250. *sdm0 = _sdm0 + s0;
  251. *sdm1 = _sdm1 + s1;
  252. *sdm2 = _sdm2;
  253. *odir = _odir + o;
  254. }
  255. }
  256. }
  257. }
  258. }
  259. if (*sdm2 + *sdm0 + *sdm0 + *odir) return ESP_OK;
  260. else return ESP_ERR_INVALID_ARG;
  261. }
  262. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  263. {
  264. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  265. int clkmInteger, clkmDecimals, bck = 0;
  266. double denom = (double)1 / 64;
  267. int channel = 2;
  268. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  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. }