i2s.c 47 KB

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