i2s.c 49 KB

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