i2s.c 54 KB

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