i2s.c 53 KB

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