i2s.c 48 KB

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