i2s.c 53 KB

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