i2s.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. // Copyright 2015-2019 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/i2s_periph.h"
  20. #include "soc/rtc_periph.h"
  21. #include "soc/rtc.h"
  22. #include "soc/efuse_periph.h"
  23. #include "esp32/rom/lldesc.h"
  24. #include "driver/gpio.h"
  25. #include "driver/i2s.h"
  26. #include "driver/rtc_io.h"
  27. #include "driver/dac.h"
  28. #include "adc1_i2s_private.h"
  29. #include "esp_intr_alloc.h"
  30. #include "esp_err.h"
  31. #include "esp_log.h"
  32. #include "esp_pm.h"
  33. #include "sdkconfig.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. #ifdef CONFIG_PM_ENABLE
  88. esp_pm_lock_handle_t pm_lock;
  89. #endif
  90. } i2s_obj_t;
  91. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  92. #ifdef CONFIG_IDF_TARGET_ESP32
  93. static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0, &I2S1};
  94. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED,portMUX_INITIALIZER_UNLOCKED};
  95. #elif defined CONFIG_IDF_TARGET_ESP32S2BETA
  96. static i2s_dev_t* I2S[I2S_NUM_MAX] = {&I2S0};
  97. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX] = {portMUX_INITIALIZER_UNLOCKED};
  98. #endif
  99. static int _i2s_adc_unit = -1;
  100. static int _i2s_adc_channel = -1;
  101. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
  102. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
  103. static esp_err_t i2s_reset_fifo(i2s_port_t i2s_num)
  104. {
  105. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  106. I2S_ENTER_CRITICAL();
  107. I2S[i2s_num]->conf.rx_fifo_reset = 1;
  108. I2S[i2s_num]->conf.rx_fifo_reset = 0;
  109. I2S[i2s_num]->conf.tx_fifo_reset = 1;
  110. I2S[i2s_num]->conf.tx_fifo_reset = 0;
  111. I2S_EXIT_CRITICAL();
  112. return ESP_OK;
  113. }
  114. inline static void gpio_matrix_out_check(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  115. {
  116. //if pin = -1, do not need to configure
  117. if (gpio != -1) {
  118. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  119. gpio_set_direction(gpio, GPIO_MODE_DEF_OUTPUT);
  120. gpio_matrix_out(gpio, signal_idx, out_inv, oen_inv);
  121. }
  122. }
  123. inline static void gpio_matrix_in_check(uint32_t gpio, uint32_t signal_idx, bool inv)
  124. {
  125. if (gpio != -1) {
  126. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  127. //Set direction, for some GPIOs, the input function are not enabled as default.
  128. gpio_set_direction(gpio, GPIO_MODE_DEF_INPUT);
  129. gpio_matrix_in(gpio, signal_idx, inv);
  130. }
  131. }
  132. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  133. {
  134. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  135. I2S[i2s_num]->int_clr.val = clr_mask;
  136. return ESP_OK;
  137. }
  138. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  139. {
  140. I2S_ENTER_CRITICAL();
  141. I2S[i2s_num]->int_ena.in_suc_eof = 1;
  142. I2S[i2s_num]->int_ena.in_dscr_err = 1;
  143. I2S_EXIT_CRITICAL();
  144. return ESP_OK;
  145. }
  146. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  147. {
  148. I2S_ENTER_CRITICAL();
  149. I2S[i2s_num]->int_ena.in_suc_eof = 0;
  150. I2S[i2s_num]->int_ena.in_dscr_err = 0;
  151. I2S_EXIT_CRITICAL();
  152. return ESP_OK;
  153. }
  154. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  155. {
  156. I2S_ENTER_CRITICAL();
  157. I2S[i2s_num]->int_ena.out_eof = 0;
  158. I2S[i2s_num]->int_ena.out_dscr_err = 0;
  159. I2S_EXIT_CRITICAL();
  160. return ESP_OK;
  161. }
  162. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  163. {
  164. I2S_ENTER_CRITICAL();
  165. I2S[i2s_num]->int_ena.out_eof = 1;
  166. I2S[i2s_num]->int_ena.out_dscr_err = 1;
  167. I2S_EXIT_CRITICAL();
  168. return ESP_OK;
  169. }
  170. float i2s_get_clk(i2s_port_t i2s_num)
  171. {
  172. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  173. return p_i2s_obj[i2s_num]->real_rate;
  174. }
  175. 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)
  176. {
  177. return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
  178. }
  179. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  180. {
  181. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  182. #ifdef CONFIG_IDF_TARGET_ESP32
  183. uint32_t is_rev0 = (GET_PERI_REG_BITS2(EFUSE_BLK0_RDATA3_REG, 1, 15) == 0);
  184. if (is_rev0) {
  185. sdm0 = 0;
  186. sdm1 = 0;
  187. }
  188. #elif defined CONFIG_IDF_TARGET_ESP32S2BETA
  189. sdm0 = 0;
  190. sdm1 = 0;
  191. #endif
  192. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  193. if (fout < APLL_MIN_FREQ || fout > APLL_MAX_FREQ) {
  194. return APLL_MAX_FREQ;
  195. }
  196. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  197. return fpll/2;
  198. }
  199. /**
  200. * @brief APLL calculate function, was described by following:
  201. * APLL Output frequency is given by the formula:
  202. *
  203. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  204. * apll_freq = fout / ((o_div + 2) * 2)
  205. *
  206. * The dividend in this expression should be in the range of 240 - 600 MHz.
  207. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  208. * * sdm0 frequency adjustment parameter, 0..255
  209. * * sdm1 frequency adjustment parameter, 0..255
  210. * * sdm2 frequency adjustment parameter, 0..63
  211. * * o_div frequency divider, 0..31
  212. *
  213. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  214. * then apply the above formula, finding the closest frequency to the desired one.
  215. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  216. * 1. We will choose the parameters with the highest level of change,
  217. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  218. * Take average frequency close to the desired frequency, and select sdm2
  219. * 2. Next, we look for sequences of less influential and more detailed parameters,
  220. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  221. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  222. *
  223. * @param[in] rate The I2S Frequency (MCLK)
  224. * @param[in] bits_per_sample The bits per sample
  225. * @param[out] sdm0 The sdm 0
  226. * @param[out] sdm1 The sdm 1
  227. * @param[out] sdm2 The sdm 2
  228. * @param[out] odir The odir
  229. *
  230. * @return ESP_ERR_INVALID_ARG or ESP_OK
  231. */
  232. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  233. {
  234. int _odir, _sdm0, _sdm1, _sdm2;
  235. float avg;
  236. float min_rate, max_rate, min_diff;
  237. if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
  238. return ESP_ERR_INVALID_ARG;
  239. }
  240. *sdm0 = 0;
  241. *sdm1 = 0;
  242. *sdm2 = 0;
  243. *odir = 0;
  244. min_diff = APLL_MAX_FREQ;
  245. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  246. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, 0);
  247. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, 31);
  248. avg = (max_rate + min_rate)/2;
  249. if (abs(avg - rate) < min_diff) {
  250. min_diff = abs(avg - rate);
  251. *sdm2 = _sdm2;
  252. }
  253. }
  254. min_diff = APLL_MAX_FREQ;
  255. for (_odir = 0; _odir < 32; _odir ++) {
  256. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, *sdm2, _odir);
  257. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, *sdm2, _odir);
  258. avg = (max_rate + min_rate)/2;
  259. if (abs(avg - rate) < min_diff) {
  260. min_diff = abs(avg - rate);
  261. *odir = _odir;
  262. }
  263. }
  264. min_diff = APLL_MAX_FREQ;
  265. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  266. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, *odir);
  267. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, *odir);
  268. avg = (max_rate + min_rate)/2;
  269. if (abs(avg - rate) < min_diff) {
  270. min_diff = abs(avg - rate);
  271. *sdm2 = _sdm2;
  272. }
  273. }
  274. min_diff = APLL_MAX_FREQ;
  275. for (_sdm1 = 0; _sdm1 < 256; _sdm1 ++) {
  276. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, _sdm1, *sdm2, *odir);
  277. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, _sdm1, *sdm2, *odir);
  278. avg = (max_rate + min_rate)/2;
  279. if (abs(avg - rate) < min_diff) {
  280. min_diff = abs(avg - rate);
  281. *sdm1 = _sdm1;
  282. }
  283. }
  284. min_diff = APLL_MAX_FREQ;
  285. for (_sdm0 = 0; _sdm0 < 256; _sdm0 ++) {
  286. avg = i2s_apll_get_fi2s(bits_per_sample, _sdm0, *sdm1, *sdm2, *odir);
  287. if (abs(avg - rate) < min_diff) {
  288. min_diff = abs(avg - rate);
  289. *sdm0 = _sdm0;
  290. }
  291. }
  292. return ESP_OK;
  293. }
  294. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  295. {
  296. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  297. int clkmInteger, clkmDecimals, bck = 0;
  298. double denom = (double)1 / 64;
  299. int channel = 2;
  300. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  301. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  302. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  303. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  304. return ESP_ERR_INVALID_ARG;
  305. }
  306. if (p_i2s_obj[i2s_num] == NULL) {
  307. ESP_LOGE(I2S_TAG, "Not initialized yet");
  308. return ESP_ERR_INVALID_ARG;
  309. }
  310. p_i2s_obj[i2s_num]->sample_rate = rate;
  311. double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
  312. if (clkmdiv > 256) {
  313. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  314. return ESP_ERR_INVALID_ARG;
  315. }
  316. // wait all on-going writing finish
  317. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  318. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  319. }
  320. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  321. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  322. }
  323. i2s_stop(i2s_num);
  324. uint32_t cur_mode = 0;
  325. if (p_i2s_obj[i2s_num]->channel_num != ch) {
  326. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  327. cur_mode = I2S[i2s_num]->fifo_conf.tx_fifo_mod;
  328. I2S[i2s_num]->fifo_conf.tx_fifo_mod = (ch == 2) ? cur_mode - 1 : cur_mode + 1;
  329. cur_mode = I2S[i2s_num]->fifo_conf.rx_fifo_mod;
  330. I2S[i2s_num]->fifo_conf.rx_fifo_mod = (ch == 2) ? cur_mode -1 : cur_mode + 1;
  331. I2S[i2s_num]->conf_chan.tx_chan_mod = (ch == 2) ? 0 : 1;
  332. I2S[i2s_num]->conf_chan.rx_chan_mod = (ch == 2) ? 0 : 1;
  333. }
  334. if (bits != p_i2s_obj[i2s_num]->bits_per_sample) {
  335. //change fifo mode
  336. if (p_i2s_obj[i2s_num]->bits_per_sample <= 16 && bits > 16) {
  337. I2S[i2s_num]->fifo_conf.tx_fifo_mod += 2;
  338. I2S[i2s_num]->fifo_conf.rx_fifo_mod += 2;
  339. } else if (p_i2s_obj[i2s_num]->bits_per_sample > 16 && bits <= 16) {
  340. I2S[i2s_num]->fifo_conf.tx_fifo_mod -= 2;
  341. I2S[i2s_num]->fifo_conf.rx_fifo_mod -= 2;
  342. }
  343. p_i2s_obj[i2s_num]->bits_per_sample = bits;
  344. p_i2s_obj[i2s_num]->bytes_per_sample = p_i2s_obj[i2s_num]->bits_per_sample / 8;
  345. // Round bytes_per_sample up to next multiple of 16 bits
  346. int halfwords_per_sample = (p_i2s_obj[i2s_num]->bits_per_sample + 15) / 16;
  347. p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
  348. // Because limited of DMA buffer is 4092 bytes
  349. 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) {
  350. p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
  351. }
  352. // Re-create TX DMA buffer
  353. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  354. save_tx = p_i2s_obj[i2s_num]->tx;
  355. 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);
  356. if (p_i2s_obj[i2s_num]->tx == NULL) {
  357. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  358. i2s_driver_uninstall(i2s_num);
  359. return ESP_ERR_NO_MEM;
  360. }
  361. I2S[i2s_num]->out_link.addr = (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0];
  362. //destroy old tx dma if exist
  363. if (save_tx) {
  364. i2s_destroy_dma_queue(i2s_num, save_tx);
  365. }
  366. }
  367. // Re-create RX DMA buffer
  368. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  369. save_rx = p_i2s_obj[i2s_num]->rx;
  370. 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);
  371. if (p_i2s_obj[i2s_num]->rx == NULL){
  372. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  373. i2s_driver_uninstall(i2s_num);
  374. return ESP_ERR_NO_MEM;
  375. }
  376. #ifdef CONFIG_IDF_TARGET_ESP32
  377. //On ESP32S2, the eof_num count in words.
  378. 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;
  379. #elif defined CONFIG_IDF_TARGET_ESP32S2BETA
  380. //On ESP32S2, the eof_num count in bytes.
  381. 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);
  382. #endif
  383. I2S[i2s_num]->in_link.addr = (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0];
  384. //destroy old rx dma if exist
  385. if (save_rx) {
  386. i2s_destroy_dma_queue(i2s_num, save_rx);
  387. }
  388. }
  389. }
  390. double mclk;
  391. int sdm0, sdm1, sdm2, odir, m_scale = 8;
  392. int fi2s_clk = rate*channel*bits*m_scale;
  393. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  394. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  395. //Rate as given to this function is the intended sample rate;
  396. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  397. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  398. fi2s_clk /= I2S_AD_BCK_FACTOR;
  399. int factor2 = 60;
  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. #if SOC_I2S_SUPPORT_PDM
  406. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  407. uint32_t b_clk = 0;
  408. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  409. int fp = I2S[i2s_num]->pdm_freq_conf.tx_pdm_fp;
  410. int fs = I2S[i2s_num]->pdm_freq_conf.tx_pdm_fs;
  411. b_clk = rate * I2S_PDM_BCK_FACTOR * (fp / fs);
  412. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (fp / fs));
  413. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  414. b_clk = rate * I2S_PDM_BCK_FACTOR * (I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en + 1);
  415. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en + 1));
  416. }
  417. int factor2 = 5 ;
  418. mclk = b_clk * factor2;
  419. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  420. clkmInteger = clkmdiv;
  421. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  422. bck = mclk / b_clk;
  423. #endif
  424. } else {
  425. clkmInteger = clkmdiv;
  426. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  427. mclk = clkmInteger + denom * clkmDecimals;
  428. bck = factor/(bits * channel);
  429. }
  430. if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
  431. fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
  432. m_scale = fi2s_clk/bits/rate/channel;
  433. }
  434. if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
  435. ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
  436. rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
  437. I2S[i2s_num]->clkm_conf.clkm_div_num = 1;
  438. I2S[i2s_num]->clkm_conf.clkm_div_b = 0;
  439. I2S[i2s_num]->clkm_conf.clkm_div_a = 1;
  440. I2S[i2s_num]->sample_rate_conf.tx_bck_div_num = m_scale;
  441. I2S[i2s_num]->sample_rate_conf.rx_bck_div_num = m_scale;
  442. #ifdef CONFIG_IDF_TARGET_ESP32
  443. I2S[i2s_num]->clkm_conf.clka_en = 1;
  444. #elif defined CONFIG_IDF_TARGET_ESP32S2BETA
  445. I2S[i2s_num]->clkm_conf.clk_sel = 1;
  446. #endif
  447. double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
  448. p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
  449. 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",
  450. rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
  451. } else {
  452. #ifdef CONFIG_IDF_TARGET_ESP32
  453. I2S[i2s_num]->clkm_conf.clka_en = 0;
  454. #elif defined CONFIG_IDF_TARGET_ESP32S2BETA
  455. I2S[i2s_num]->clkm_conf.clk_sel = 2;
  456. #endif
  457. I2S[i2s_num]->clkm_conf.clkm_div_a = 63;
  458. I2S[i2s_num]->clkm_conf.clkm_div_b = clkmDecimals;
  459. I2S[i2s_num]->clkm_conf.clkm_div_num = clkmInteger;
  460. I2S[i2s_num]->sample_rate_conf.tx_bck_div_num = bck;
  461. I2S[i2s_num]->sample_rate_conf.rx_bck_div_num = bck;
  462. double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
  463. p_i2s_obj[i2s_num]->real_rate = real_rate;
  464. 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",
  465. rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
  466. }
  467. I2S[i2s_num]->sample_rate_conf.tx_bits_mod = bits;
  468. I2S[i2s_num]->sample_rate_conf.rx_bits_mod = bits;
  469. // wait all writing on-going finish
  470. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  471. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  472. }
  473. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  474. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  475. }
  476. i2s_start(i2s_num);
  477. return ESP_OK;
  478. }
  479. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  480. {
  481. i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
  482. uint8_t i2s_num = p_i2s->i2s_num;
  483. i2s_dev_t* i2s_reg = I2S[i2s_num];
  484. typeof(i2s_reg->int_st) int_st_val = i2s_reg->int_st;
  485. if(int_st_val.val == 0) {
  486. //Avoid spurious interrupt
  487. return;
  488. }
  489. i2s_event_t i2s_event;
  490. int dummy;
  491. portBASE_TYPE high_priority_task_awoken = 0;
  492. lldesc_t *finish_desc;
  493. if (int_st_val.out_dscr_err || int_st_val.in_dscr_err) {
  494. ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", int_st_val.val);
  495. if (p_i2s->i2s_queue) {
  496. i2s_event.type = I2S_EVENT_DMA_ERROR;
  497. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  498. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  499. }
  500. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  501. }
  502. }
  503. if (int_st_val.out_eof && p_i2s->tx) {
  504. finish_desc = (lldesc_t*) i2s_reg->out_eof_des_addr;
  505. // All buffers are empty. This means we have an underflow on our hands.
  506. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  507. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
  508. // See if tx descriptor needs to be auto cleared:
  509. // This will avoid any kind of noise that may get introduced due to transmission
  510. // of previous data from tx descriptor on I2S line.
  511. if (p_i2s->tx_desc_auto_clear == true) {
  512. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  513. }
  514. }
  515. xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  516. if (p_i2s->i2s_queue) {
  517. i2s_event.type = I2S_EVENT_TX_DONE;
  518. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  519. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  520. }
  521. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  522. }
  523. }
  524. if (int_st_val.in_suc_eof && p_i2s->rx) {
  525. // All buffers are full. This means we have an overflow.
  526. finish_desc = (lldesc_t*) i2s_reg->in_eof_des_addr;
  527. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  528. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
  529. }
  530. xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  531. if (p_i2s->i2s_queue) {
  532. i2s_event.type = I2S_EVENT_RX_DONE;
  533. if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  534. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  535. }
  536. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  537. }
  538. }
  539. i2s_reg->int_clr.val = int_st_val.val;
  540. if (high_priority_task_awoken == pdTRUE) {
  541. portYIELD_FROM_ISR();
  542. }
  543. }
  544. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
  545. {
  546. int bux_idx;
  547. if (p_i2s_obj[i2s_num] == NULL) {
  548. ESP_LOGE(I2S_TAG, "Not initialized yet");
  549. return ESP_ERR_INVALID_ARG;
  550. }
  551. if (dma == NULL) {
  552. ESP_LOGE(I2S_TAG, "dma is NULL");
  553. return ESP_ERR_INVALID_ARG;
  554. }
  555. for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
  556. if (dma->desc && dma->desc[bux_idx]) {
  557. free(dma->desc[bux_idx]);
  558. }
  559. if (dma->buf && dma->buf[bux_idx]) {
  560. free(dma->buf[bux_idx]);
  561. }
  562. }
  563. if (dma->buf) {
  564. free(dma->buf);
  565. }
  566. if (dma->desc) {
  567. free(dma->desc);
  568. }
  569. vQueueDelete(dma->queue);
  570. vSemaphoreDelete(dma->mux);
  571. free(dma);
  572. return ESP_OK;
  573. }
  574. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
  575. {
  576. int bux_idx;
  577. int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  578. i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
  579. if (dma == NULL) {
  580. ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
  581. return NULL;
  582. }
  583. memset(dma, 0, sizeof(i2s_dma_t));
  584. dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
  585. if (dma->buf == NULL) {
  586. ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
  587. free(dma);
  588. return NULL;
  589. }
  590. memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
  591. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  592. dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
  593. if (dma->buf[bux_idx] == NULL) {
  594. ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
  595. i2s_destroy_dma_queue(i2s_num, dma);
  596. return NULL;
  597. }
  598. ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
  599. }
  600. dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
  601. if (dma->desc == NULL) {
  602. ESP_LOGE(I2S_TAG, "Error malloc dma description");
  603. i2s_destroy_dma_queue(i2s_num, dma);
  604. return NULL;
  605. }
  606. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  607. dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  608. if (dma->desc[bux_idx] == NULL) {
  609. ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
  610. i2s_destroy_dma_queue(i2s_num, dma);
  611. return NULL;
  612. }
  613. }
  614. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  615. dma->desc[bux_idx]->owner = 1;
  616. dma->desc[bux_idx]->eof = 1;
  617. dma->desc[bux_idx]->sosf = 0;
  618. dma->desc[bux_idx]->length = dma_buf_len * sample_size;
  619. dma->desc[bux_idx]->size = dma_buf_len * sample_size;
  620. dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
  621. dma->desc[bux_idx]->offset = 0;
  622. dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
  623. }
  624. dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
  625. dma->mux = xSemaphoreCreateMutex();
  626. dma->rw_pos = 0;
  627. dma->buf_size = dma_buf_len * sample_size;
  628. dma->curr_ptr = NULL;
  629. ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
  630. return dma;
  631. }
  632. esp_err_t i2s_start(i2s_port_t i2s_num)
  633. {
  634. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  635. //start DMA link
  636. I2S_ENTER_CRITICAL();
  637. i2s_reset_fifo(i2s_num);
  638. //reset dma
  639. I2S[i2s_num]->lc_conf.in_rst = 1;
  640. I2S[i2s_num]->lc_conf.in_rst = 0;
  641. I2S[i2s_num]->lc_conf.out_rst = 1;
  642. I2S[i2s_num]->lc_conf.out_rst = 0;
  643. I2S[i2s_num]->conf.tx_reset = 1;
  644. I2S[i2s_num]->conf.tx_reset = 0;
  645. I2S[i2s_num]->conf.rx_reset = 1;
  646. I2S[i2s_num]->conf.rx_reset = 0;
  647. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  648. I2S[i2s_num]->int_clr.val = 0xFFFFFFFF;
  649. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  650. i2s_enable_tx_intr(i2s_num);
  651. I2S[i2s_num]->out_link.start = 1;
  652. I2S[i2s_num]->conf.tx_start = 1;
  653. }
  654. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  655. i2s_enable_rx_intr(i2s_num);
  656. I2S[i2s_num]->in_link.start = 1;
  657. I2S[i2s_num]->conf.rx_start = 1;
  658. }
  659. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  660. I2S_EXIT_CRITICAL();
  661. return ESP_OK;
  662. }
  663. esp_err_t i2s_stop(i2s_port_t i2s_num)
  664. {
  665. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  666. I2S_ENTER_CRITICAL();
  667. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  668. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  669. I2S[i2s_num]->out_link.stop = 1;
  670. I2S[i2s_num]->conf.tx_start = 0;
  671. i2s_disable_tx_intr(i2s_num);
  672. }
  673. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  674. I2S[i2s_num]->in_link.stop = 1;
  675. I2S[i2s_num]->conf.rx_start = 0;
  676. i2s_disable_rx_intr(i2s_num);
  677. }
  678. I2S[i2s_num]->int_clr.val = I2S[i2s_num]->int_st.val; //clear pending interrupt
  679. I2S_EXIT_CRITICAL();
  680. return ESP_OK;
  681. }
  682. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  683. {
  684. I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
  685. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  686. dac_output_disable(DAC_CHANNEL_1);
  687. dac_output_disable(DAC_CHANNEL_2);
  688. dac_i2s_disable();
  689. } else {
  690. dac_i2s_enable();
  691. }
  692. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  693. //DAC1, right channel, GPIO25
  694. dac_output_enable(DAC_CHANNEL_1);
  695. }
  696. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  697. //DAC2, left channel, GPIO26
  698. dac_output_enable(DAC_CHANNEL_2);
  699. }
  700. return ESP_OK;
  701. }
  702. static esp_err_t _i2s_adc_mode_recover(void)
  703. {
  704. I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
  705. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  706. }
  707. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  708. {
  709. I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
  710. // For now, we only support SAR ADC1.
  711. _i2s_adc_unit = adc_unit;
  712. _i2s_adc_channel = adc_channel;
  713. return adc_i2s_mode_init(adc_unit, adc_channel);
  714. }
  715. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  716. {
  717. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  718. if (pin == NULL) {
  719. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  720. }
  721. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  722. ESP_LOGE(I2S_TAG, "bck_io_num error");
  723. return ESP_FAIL;
  724. }
  725. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  726. ESP_LOGE(I2S_TAG, "ws_io_num error");
  727. return ESP_FAIL;
  728. }
  729. if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
  730. ESP_LOGE(I2S_TAG, "data_out_num error");
  731. return ESP_FAIL;
  732. }
  733. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  734. ESP_LOGE(I2S_TAG, "data_in_num error");
  735. return ESP_FAIL;
  736. }
  737. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  738. //Each IIS hw module has a RX and TX unit.
  739. //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
  740. //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
  741. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  742. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  743. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  744. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  745. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  746. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  747. bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
  748. ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
  749. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  750. }
  751. }
  752. //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
  753. //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
  754. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  755. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  756. bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
  757. ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
  758. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  759. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  760. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  761. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  762. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  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. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  769. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  770. } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
  771. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  772. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  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. #if SOC_I2S_SUPPORT_PDM
  793. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
  794. {
  795. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  796. I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en = dsr;
  797. 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);
  798. }
  799. #endif
  800. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  801. {
  802. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  803. I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
  804. 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);
  805. 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);
  806. #if SOC_I2S_SUPPORT_PDM
  807. I2S_CHECK(!((i2s_config->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
  808. #endif
  809. periph_module_enable(i2s_periph_signal[i2s_num].module);
  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 = 1;
  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 = 1;
  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 SOC_I2S_SUPPORT_PDM
  876. if (i2s_config->mode & I2S_MODE_PDM) {
  877. I2S[i2s_num]->fifo_conf.rx_fifo_mod_force_en = 1;
  878. I2S[i2s_num]->fifo_conf.tx_fifo_mod_force_en = 1;
  879. I2S[i2s_num]->pdm_freq_conf.tx_pdm_fp = 960;
  880. I2S[i2s_num]->pdm_freq_conf.tx_pdm_fs = i2s_config->sample_rate / 1000 * 10;
  881. 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;
  882. I2S[i2s_num]->pdm_conf.rx_sinc_dsr_16_en = 0;
  883. I2S[i2s_num]->pdm_conf.rx_pdm_en = 1;
  884. I2S[i2s_num]->pdm_conf.tx_pdm_en = 1;
  885. I2S[i2s_num]->pdm_conf.pcm2pdm_conv_en = 1;
  886. I2S[i2s_num]->pdm_conf.pdm2pcm_conv_en = 1;
  887. } else {
  888. I2S[i2s_num]->pdm_conf.rx_pdm_en = 0;
  889. I2S[i2s_num]->pdm_conf.tx_pdm_en = 0;
  890. }
  891. #else
  892. I2S[i2s_num]->pdm_conf.rx_pdm_en = 0;
  893. I2S[i2s_num]->pdm_conf.tx_pdm_en = 0;
  894. #endif
  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. periph_module_enable(i2s_periph_signal[i2s_num].module);
  996. //initial interrupt
  997. 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);
  998. if (err != ESP_OK) {
  999. #ifdef CONFIG_PM_ENABLE
  1000. if (p_i2s_obj[i2s_num]->pm_lock) {
  1001. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  1002. }
  1003. #endif
  1004. free(p_i2s_obj[i2s_num]);
  1005. p_i2s_obj[i2s_num] = NULL;
  1006. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  1007. return err;
  1008. }
  1009. i2s_stop(i2s_num);
  1010. err = i2s_param_config(i2s_num, i2s_config);
  1011. if (err != ESP_OK) {
  1012. i2s_driver_uninstall(i2s_num);
  1013. ESP_LOGE(I2S_TAG, "I2S param configure error");
  1014. return err;
  1015. }
  1016. if (i2s_queue) {
  1017. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  1018. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  1019. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  1020. } else {
  1021. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  1022. }
  1023. //set clock and start
  1024. return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  1025. }
  1026. ESP_LOGW(I2S_TAG, "I2S driver already installed");
  1027. return ESP_OK;
  1028. }
  1029. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  1030. {
  1031. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1032. if (p_i2s_obj[i2s_num] == NULL) {
  1033. ESP_LOGI(I2S_TAG, "already uninstalled");
  1034. return ESP_OK;
  1035. }
  1036. i2s_stop(i2s_num);
  1037. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  1038. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  1039. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  1040. p_i2s_obj[i2s_num]->tx = NULL;
  1041. }
  1042. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  1043. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  1044. p_i2s_obj[i2s_num]->rx = NULL;
  1045. }
  1046. if (p_i2s_obj[i2s_num]->i2s_queue) {
  1047. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  1048. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  1049. }
  1050. if(p_i2s_obj[i2s_num]->use_apll) {
  1051. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  1052. }
  1053. #ifdef CONFIG_PM_ENABLE
  1054. if (p_i2s_obj[i2s_num]->pm_lock) {
  1055. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  1056. }
  1057. #endif
  1058. free(p_i2s_obj[i2s_num]);
  1059. p_i2s_obj[i2s_num] = NULL;
  1060. periph_module_disable(i2s_periph_signal[i2s_num].module);
  1061. return ESP_OK;
  1062. }
  1063. esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *bytes_written, TickType_t ticks_to_wait)
  1064. {
  1065. char *data_ptr, *src_byte;
  1066. int bytes_can_write;
  1067. *bytes_written = 0;
  1068. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1069. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1070. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  1071. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1072. #ifdef CONFIG_PM_ENABLE
  1073. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1074. #endif
  1075. src_byte = (char *)src;
  1076. while (size > 0) {
  1077. if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
  1078. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1079. break;
  1080. }
  1081. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1082. }
  1083. ESP_LOGD(I2S_TAG, "size: %d, rw_pos: %d, buf_size: %d, curr_ptr: %d", size, p_i2s_obj[i2s_num]->tx->rw_pos, p_i2s_obj[i2s_num]->tx->buf_size, (int)p_i2s_obj[i2s_num]->tx->curr_ptr);
  1084. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1085. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1086. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1087. if (bytes_can_write > size) {
  1088. bytes_can_write = size;
  1089. }
  1090. memcpy(data_ptr, src_byte, bytes_can_write);
  1091. size -= bytes_can_write;
  1092. src_byte += bytes_can_write;
  1093. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1094. (*bytes_written) += bytes_can_write;
  1095. }
  1096. #ifdef CONFIG_PM_ENABLE
  1097. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1098. #endif
  1099. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1100. return ESP_OK;
  1101. }
  1102. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  1103. {
  1104. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1105. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  1106. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  1107. adc1_i2s_mode_acquire();
  1108. _i2s_adc_mode_recover();
  1109. return i2s_set_clk(i2s_num, p_i2s_obj[i2s_num]->sample_rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  1110. }
  1111. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  1112. {
  1113. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1114. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  1115. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  1116. adc1_lock_release();
  1117. return ESP_OK;
  1118. }
  1119. esp_err_t i2s_write_expand(i2s_port_t i2s_num, const void *src, size_t size, size_t src_bits, size_t aim_bits, size_t *bytes_written, TickType_t ticks_to_wait)
  1120. {
  1121. char *data_ptr;
  1122. int bytes_can_write, tail;
  1123. int src_bytes, aim_bytes, zero_bytes;
  1124. *bytes_written = 0;
  1125. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1126. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  1127. I2S_CHECK((aim_bits * size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1128. I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
  1129. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  1130. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  1131. ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
  1132. return ESP_ERR_INVALID_ARG;
  1133. }
  1134. if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  1135. ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  1136. return ESP_ERR_INVALID_ARG;
  1137. }
  1138. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  1139. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  1140. return ESP_ERR_INVALID_ARG;
  1141. }
  1142. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  1143. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  1144. return ESP_ERR_INVALID_ARG;
  1145. }
  1146. src_bytes = src_bits / 8;
  1147. aim_bytes = aim_bits / 8;
  1148. zero_bytes = aim_bytes - src_bytes;
  1149. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  1150. size = size * aim_bytes / src_bytes;
  1151. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  1152. while (size > 0) {
  1153. if (p_i2s_obj[i2s_num]->tx->rw_pos == p_i2s_obj[i2s_num]->tx->buf_size || p_i2s_obj[i2s_num]->tx->curr_ptr == NULL) {
  1154. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1155. break;
  1156. }
  1157. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1158. }
  1159. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1160. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1161. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1162. if (bytes_can_write > size) {
  1163. bytes_can_write = size;
  1164. }
  1165. tail = bytes_can_write % aim_bytes;
  1166. bytes_can_write = bytes_can_write - tail;
  1167. memset(data_ptr, 0, bytes_can_write);
  1168. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1169. j += zero_bytes;
  1170. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1171. (*bytes_written) += (aim_bytes - zero_bytes);
  1172. }
  1173. size -= bytes_can_write;
  1174. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1175. }
  1176. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1177. return ESP_OK;
  1178. }
  1179. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1180. {
  1181. char *data_ptr, *dest_byte;
  1182. int bytes_can_read;
  1183. *bytes_read = 0;
  1184. dest_byte = (char *)dest;
  1185. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1186. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1187. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1188. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1189. #ifdef CONFIG_PM_ENABLE
  1190. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1191. #endif
  1192. while (size > 0) {
  1193. 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) {
  1194. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1195. break;
  1196. }
  1197. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1198. }
  1199. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1200. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1201. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1202. if (bytes_can_read > size) {
  1203. bytes_can_read = size;
  1204. }
  1205. memcpy(dest_byte, data_ptr, bytes_can_read);
  1206. size -= bytes_can_read;
  1207. dest_byte += bytes_can_read;
  1208. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1209. (*bytes_read) += bytes_can_read;
  1210. }
  1211. #ifdef CONFIG_PM_ENABLE
  1212. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1213. #endif
  1214. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1215. return ESP_OK;
  1216. }