i2s.c 52 KB

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