i2s.c 53 KB

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