i2s.c 48 KB

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