i2s.c 54 KB

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