i2s.c 48 KB

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