i2s.c 56 KB

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