i2s.c 52 KB

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