i2s.c 55 KB

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