i2s.c 57 KB

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