i2s.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <stdbool.h>
  16. #include <math.h>
  17. #include <esp_types.h>
  18. #include "freertos/FreeRTOS.h"
  19. #include "freertos/queue.h"
  20. #include "freertos/xtensa_api.h"
  21. #include "freertos/semphr.h"
  22. #include "esp32/rom/lldesc.h"
  23. #include "driver/gpio.h"
  24. #include "driver/i2s.h"
  25. #include "driver/dac.h"
  26. #include "hal/i2s_hal.h"
  27. #include "adc1_i2s_private.h"
  28. #include "esp_intr_alloc.h"
  29. #include "esp_err.h"
  30. #include "esp_attr.h"
  31. #include "esp_log.h"
  32. #include "esp_pm.h"
  33. #include "esp_efuse.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_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. /**
  46. * @brief DMA buffer object
  47. *
  48. */
  49. typedef struct {
  50. char **buf;
  51. int buf_size;
  52. int rw_pos;
  53. void *curr_ptr;
  54. SemaphoreHandle_t mux;
  55. xQueueHandle queue;
  56. lldesc_t **desc;
  57. } i2s_dma_t;
  58. /**
  59. * @brief I2S object instance
  60. *
  61. */
  62. typedef struct {
  63. i2s_port_t i2s_num; /*!< I2S port number*/
  64. int queue_size; /*!< I2S event queue size*/
  65. QueueHandle_t i2s_queue; /*!< I2S queue handler*/
  66. int dma_buf_count; /*!< DMA buffer count, number of buffer*/
  67. int dma_buf_len; /*!< DMA buffer length, length of each buffer*/
  68. i2s_dma_t *rx; /*!< DMA Tx buffer*/
  69. i2s_dma_t *tx; /*!< DMA Rx buffer*/
  70. i2s_isr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  71. int channel_num; /*!< Number of channels*/
  72. int bytes_per_sample; /*!< Bytes per sample*/
  73. int bits_per_sample; /*!< Bits per sample*/
  74. i2s_mode_t mode; /*!< I2S Working mode*/
  75. uint32_t sample_rate; /*!< I2S sample rate */
  76. bool use_apll; /*!< I2S use APLL clock */
  77. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
  78. int fixed_mclk; /*!< I2S fixed MLCK clock */
  79. double real_rate;
  80. #ifdef CONFIG_PM_ENABLE
  81. esp_pm_lock_handle_t pm_lock;
  82. #endif
  83. i2s_hal_context_t hal; /*!< I2S hal context*/
  84. } i2s_obj_t;
  85. static i2s_obj_t *p_i2s_obj[I2S_NUM_MAX] = {0};
  86. static portMUX_TYPE i2s_spinlock[I2S_NUM_MAX];
  87. static int _i2s_adc_unit = -1;
  88. static int _i2s_adc_channel = -1;
  89. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len);
  90. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma);
  91. inline static void gpio_matrix_out_check(uint32_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  92. {
  93. //if pin = -1, do not need to configure
  94. if (gpio != -1) {
  95. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  96. gpio_set_direction(gpio, GPIO_MODE_DEF_OUTPUT);
  97. gpio_matrix_out(gpio, signal_idx, out_inv, oen_inv);
  98. }
  99. }
  100. inline static void gpio_matrix_in_check(uint32_t gpio, uint32_t signal_idx, bool inv)
  101. {
  102. if (gpio != -1) {
  103. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  104. //Set direction, for some GPIOs, the input function are not enabled as default.
  105. gpio_set_direction(gpio, GPIO_MODE_DEF_INPUT);
  106. gpio_matrix_in(gpio, signal_idx, inv);
  107. }
  108. }
  109. esp_err_t i2s_clear_intr_status(i2s_port_t i2s_num, uint32_t clr_mask)
  110. {
  111. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  112. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), clr_mask);
  113. return ESP_OK;
  114. }
  115. esp_err_t i2s_enable_rx_intr(i2s_port_t i2s_num)
  116. {
  117. I2S_ENTER_CRITICAL();
  118. i2s_hal_enable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  119. I2S_EXIT_CRITICAL();
  120. return ESP_OK;
  121. }
  122. esp_err_t i2s_disable_rx_intr(i2s_port_t i2s_num)
  123. {
  124. I2S_ENTER_CRITICAL();
  125. i2s_hal_disable_rx_intr(&(p_i2s_obj[i2s_num]->hal));
  126. I2S_EXIT_CRITICAL();
  127. return ESP_OK;
  128. }
  129. esp_err_t i2s_disable_tx_intr(i2s_port_t i2s_num)
  130. {
  131. I2S_ENTER_CRITICAL();
  132. i2s_hal_disable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  133. I2S_EXIT_CRITICAL();
  134. return ESP_OK;
  135. }
  136. esp_err_t i2s_enable_tx_intr(i2s_port_t i2s_num)
  137. {
  138. I2S_ENTER_CRITICAL();
  139. i2s_hal_enable_tx_intr(&(p_i2s_obj[i2s_num]->hal));
  140. I2S_EXIT_CRITICAL();
  141. return ESP_OK;
  142. }
  143. float i2s_get_clk(i2s_port_t i2s_num)
  144. {
  145. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  146. return p_i2s_obj[i2s_num]->real_rate;
  147. }
  148. 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)
  149. {
  150. return esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_alloc_flags, fn, arg, handle);
  151. }
  152. static float i2s_apll_get_fi2s(int bits_per_sample, int sdm0, int sdm1, int sdm2, int odir)
  153. {
  154. int f_xtal = (int)rtc_clk_xtal_freq_get() * 1000000;
  155. #if CONFIG_IDF_TARGET_ESP32
  156. /* ESP32 rev0 silicon issue for APLL range/accuracy, please see ESP32 ECO document for more information on this */
  157. if (esp_efuse_get_chip_ver() == 0) {
  158. sdm0 = 0;
  159. sdm1 = 0;
  160. }
  161. #endif
  162. float fout = f_xtal * (sdm2 + sdm1 / 256.0f + sdm0 / 65536.0f + 4);
  163. if (fout < APLL_MIN_FREQ || fout > APLL_MAX_FREQ) {
  164. return APLL_MAX_FREQ;
  165. }
  166. float fpll = fout / (2 * (odir+2)); //== fi2s (N=1, b=0, a=1)
  167. return fpll/2;
  168. }
  169. /**
  170. * @brief APLL calculate function, was described by following:
  171. * APLL Output frequency is given by the formula:
  172. *
  173. * apll_freq = xtal_freq * (4 + sdm2 + sdm1/256 + sdm0/65536)/((o_div + 2) * 2)
  174. * apll_freq = fout / ((o_div + 2) * 2)
  175. *
  176. * The dividend in this expression should be in the range of 240 - 600 MHz.
  177. * In rev. 0 of ESP32, sdm0 and sdm1 are unused and always set to 0.
  178. * * sdm0 frequency adjustment parameter, 0..255
  179. * * sdm1 frequency adjustment parameter, 0..255
  180. * * sdm2 frequency adjustment parameter, 0..63
  181. * * o_div frequency divider, 0..31
  182. *
  183. * The most accurate way to find the sdm0..2 and odir parameters is to loop through them all,
  184. * then apply the above formula, finding the closest frequency to the desired one.
  185. * But 256*256*64*32 = 134.217.728 loops are too slow with ESP32
  186. * 1. We will choose the parameters with the highest level of change,
  187. * With 350MHz<fout<500MHz, we limit the sdm2 from 4 to 9,
  188. * Take average frequency close to the desired frequency, and select sdm2
  189. * 2. Next, we look for sequences of less influential and more detailed parameters,
  190. * also by taking the average of the largest and smallest frequencies closer to the desired frequency.
  191. * 3. And finally, loop through all the most detailed of the parameters, finding the best desired frequency
  192. *
  193. * @param[in] rate The I2S Frequency (MCLK)
  194. * @param[in] bits_per_sample The bits per sample
  195. * @param[out] sdm0 The sdm 0
  196. * @param[out] sdm1 The sdm 1
  197. * @param[out] sdm2 The sdm 2
  198. * @param[out] odir The odir
  199. *
  200. * @return ESP_ERR_INVALID_ARG or ESP_OK
  201. */
  202. static esp_err_t i2s_apll_calculate_fi2s(int rate, int bits_per_sample, int *sdm0, int *sdm1, int *sdm2, int *odir)
  203. {
  204. int _odir, _sdm0, _sdm1, _sdm2;
  205. float avg;
  206. float min_rate, max_rate, min_diff;
  207. if (rate/bits_per_sample/2/8 < APLL_I2S_MIN_RATE) {
  208. return ESP_ERR_INVALID_ARG;
  209. }
  210. *sdm0 = 0;
  211. *sdm1 = 0;
  212. *sdm2 = 0;
  213. *odir = 0;
  214. min_diff = APLL_MAX_FREQ;
  215. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  216. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, 0);
  217. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, 31);
  218. avg = (max_rate + min_rate)/2;
  219. if (abs(avg - rate) < min_diff) {
  220. min_diff = abs(avg - rate);
  221. *sdm2 = _sdm2;
  222. }
  223. }
  224. min_diff = APLL_MAX_FREQ;
  225. for (_odir = 0; _odir < 32; _odir ++) {
  226. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, *sdm2, _odir);
  227. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, *sdm2, _odir);
  228. avg = (max_rate + min_rate)/2;
  229. if (abs(avg - rate) < min_diff) {
  230. min_diff = abs(avg - rate);
  231. *odir = _odir;
  232. }
  233. }
  234. min_diff = APLL_MAX_FREQ;
  235. for (_sdm2 = 4; _sdm2 < 9; _sdm2 ++) {
  236. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, 255, _sdm2, *odir);
  237. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, 0, _sdm2, *odir);
  238. avg = (max_rate + min_rate)/2;
  239. if (abs(avg - rate) < min_diff) {
  240. min_diff = abs(avg - rate);
  241. *sdm2 = _sdm2;
  242. }
  243. }
  244. min_diff = APLL_MAX_FREQ;
  245. for (_sdm1 = 0; _sdm1 < 256; _sdm1 ++) {
  246. max_rate = i2s_apll_get_fi2s(bits_per_sample, 255, _sdm1, *sdm2, *odir);
  247. min_rate = i2s_apll_get_fi2s(bits_per_sample, 0, _sdm1, *sdm2, *odir);
  248. avg = (max_rate + min_rate)/2;
  249. if (abs(avg - rate) < min_diff) {
  250. min_diff = abs(avg - rate);
  251. *sdm1 = _sdm1;
  252. }
  253. }
  254. min_diff = APLL_MAX_FREQ;
  255. for (_sdm0 = 0; _sdm0 < 256; _sdm0 ++) {
  256. avg = i2s_apll_get_fi2s(bits_per_sample, _sdm0, *sdm1, *sdm2, *odir);
  257. if (abs(avg - rate) < min_diff) {
  258. min_diff = abs(avg - rate);
  259. *sdm0 = _sdm0;
  260. }
  261. }
  262. return ESP_OK;
  263. }
  264. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch)
  265. {
  266. int factor = (256%bits)? 384 : 256; // According to hardware codec requirement(supported 256fs or 384fs)
  267. int clkmInteger, clkmDecimals, bck = 0;
  268. double denom = (double)1 / 64;
  269. int channel = 2;
  270. i2s_dma_t *save_tx = NULL, *save_rx = NULL;
  271. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  272. if (bits % 8 != 0 || bits > I2S_BITS_PER_SAMPLE_32BIT || bits < I2S_BITS_PER_SAMPLE_16BIT) {
  273. ESP_LOGE(I2S_TAG, "Invalid bits per sample");
  274. return ESP_ERR_INVALID_ARG;
  275. }
  276. if (p_i2s_obj[i2s_num] == NULL) {
  277. ESP_LOGE(I2S_TAG, "Not initialized yet");
  278. return ESP_ERR_INVALID_ARG;
  279. }
  280. p_i2s_obj[i2s_num]->sample_rate = rate;
  281. /**
  282. * Due to hardware issue, bck division on ESP32/ESP32-S2 should be greater than 8 in slave mode
  283. * So the factor need to be an appropriate value
  284. */
  285. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  286. factor = 16 * bits;
  287. }
  288. double clkmdiv = (double)I2S_BASE_CLK / (rate * factor);
  289. if (clkmdiv > 256) {
  290. ESP_LOGE(I2S_TAG, "clkmdiv is too large\r\n");
  291. return ESP_ERR_INVALID_ARG;
  292. }
  293. // wait all on-going writing finish
  294. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  295. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  296. }
  297. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  298. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  299. }
  300. i2s_stop(i2s_num);
  301. i2s_hal_set_tx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  302. i2s_hal_set_rx_mode(&(p_i2s_obj[i2s_num]->hal), ch, bits);
  303. if (p_i2s_obj[i2s_num]->channel_num != ch) {
  304. p_i2s_obj[i2s_num]->channel_num = (ch == 2) ? 2 : 1;
  305. }
  306. if (bits != p_i2s_obj[i2s_num]->bits_per_sample) {
  307. p_i2s_obj[i2s_num]->bits_per_sample = bits;
  308. p_i2s_obj[i2s_num]->bytes_per_sample = p_i2s_obj[i2s_num]->bits_per_sample / 8;
  309. // Round bytes_per_sample up to next multiple of 16 bits
  310. int halfwords_per_sample = (p_i2s_obj[i2s_num]->bits_per_sample + 15) / 16;
  311. p_i2s_obj[i2s_num]->bytes_per_sample = halfwords_per_sample * 2;
  312. // Because limited of DMA buffer is 4092 bytes
  313. 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) {
  314. p_i2s_obj[i2s_num]->dma_buf_len = 4092 / p_i2s_obj[i2s_num]->bytes_per_sample / p_i2s_obj[i2s_num]->channel_num;
  315. }
  316. // Re-create TX DMA buffer
  317. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  318. save_tx = p_i2s_obj[i2s_num]->tx;
  319. 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);
  320. if (p_i2s_obj[i2s_num]->tx == NULL) {
  321. ESP_LOGE(I2S_TAG, "Failed to create tx dma buffer");
  322. i2s_driver_uninstall(i2s_num);
  323. return ESP_ERR_NO_MEM;
  324. }
  325. i2s_hal_set_out_link_addr(&(p_i2s_obj[i2s_num]->hal), (uint32_t) p_i2s_obj[i2s_num]->tx->desc[0]);
  326. //destroy old tx dma if exist
  327. if (save_tx) {
  328. i2s_destroy_dma_queue(i2s_num, save_tx);
  329. }
  330. }
  331. // Re-create RX DMA buffer
  332. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  333. save_rx = p_i2s_obj[i2s_num]->rx;
  334. 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);
  335. if (p_i2s_obj[i2s_num]->rx == NULL){
  336. ESP_LOGE(I2S_TAG, "Failed to create rx dma buffer");
  337. i2s_driver_uninstall(i2s_num);
  338. return ESP_ERR_NO_MEM;
  339. }
  340. i2s_hal_set_in_link(&(p_i2s_obj[i2s_num]->hal), p_i2s_obj[i2s_num]->dma_buf_len * p_i2s_obj[i2s_num]->channel_num * p_i2s_obj[i2s_num]->bytes_per_sample, (uint32_t) p_i2s_obj[i2s_num]->rx->desc[0]);
  341. //destroy old rx dma if exist
  342. if (save_rx) {
  343. i2s_destroy_dma_queue(i2s_num, save_rx);
  344. }
  345. }
  346. }
  347. double mclk;
  348. int sdm0, sdm1, sdm2, odir, m_scale = 8;
  349. int fi2s_clk = rate*channel*bits*m_scale;
  350. if (p_i2s_obj[i2s_num]->mode & (I2S_MODE_DAC_BUILT_IN | I2S_MODE_ADC_BUILT_IN)) {
  351. //DAC uses bclk as sample clock, not WS. WS can be something arbitrary.
  352. //Rate as given to this function is the intended sample rate;
  353. //According to the TRM, WS clk equals to the sample rate, and bclk is double the speed of WS
  354. uint32_t b_clk = rate * I2S_AD_BCK_FACTOR;
  355. fi2s_clk /= I2S_AD_BCK_FACTOR;
  356. int factor2 = 60;
  357. mclk = b_clk * factor2;
  358. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  359. clkmInteger = clkmdiv;
  360. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  361. bck = mclk / b_clk;
  362. #if SOC_I2S_SUPPORT_PDM
  363. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_PDM) {
  364. uint32_t b_clk = 0;
  365. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  366. int fp;
  367. int fs;
  368. i2s_hal_get_tx_pdm(&(p_i2s_obj[i2s_num]->hal), &fp, &fs);
  369. b_clk = rate * I2S_PDM_BCK_FACTOR * (fp / fs);
  370. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (fp / fs));
  371. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  372. bool en;
  373. i2s_hal_get_rx_sinc_dsr_16_en(&(p_i2s_obj[i2s_num]->hal), &en);
  374. b_clk = rate * I2S_PDM_BCK_FACTOR * (en ? 2 : 1);
  375. fi2s_clk /= (I2S_PDM_BCK_FACTOR * (en ? 2 : 1));
  376. }
  377. int factor2 = 5 ;
  378. mclk = b_clk * factor2;
  379. clkmdiv = ((double) I2S_BASE_CLK) / mclk;
  380. clkmInteger = clkmdiv;
  381. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  382. bck = mclk / b_clk;
  383. #endif
  384. } else {
  385. clkmInteger = clkmdiv;
  386. clkmDecimals = (clkmdiv - clkmInteger) / denom;
  387. mclk = clkmInteger + denom * clkmDecimals;
  388. bck = factor/(bits * channel);
  389. }
  390. if(p_i2s_obj[i2s_num]->use_apll && p_i2s_obj[i2s_num]->fixed_mclk) {
  391. fi2s_clk = p_i2s_obj[i2s_num]->fixed_mclk;
  392. m_scale = fi2s_clk/bits/rate/channel;
  393. }
  394. if(p_i2s_obj[i2s_num]->use_apll && i2s_apll_calculate_fi2s(fi2s_clk, bits, &sdm0, &sdm1, &sdm2, &odir) == ESP_OK) {
  395. ESP_LOGD(I2S_TAG, "sdm0=%d, sdm1=%d, sdm2=%d, odir=%d", sdm0, sdm1, sdm2, odir);
  396. rtc_clk_apll_enable(1, sdm0, sdm1, sdm2, odir);
  397. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), 1, 1, 0, m_scale, m_scale);
  398. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_APLL);
  399. double fi2s_rate = i2s_apll_get_fi2s(bits, sdm0, sdm1, sdm2, odir);
  400. p_i2s_obj[i2s_num]->real_rate = fi2s_rate/bits/channel/m_scale;
  401. 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",
  402. rate, fi2s_rate/bits/channel/m_scale, bits, 1, m_scale, fi2s_rate, fi2s_rate/8, 1, 0);
  403. } else {
  404. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  405. i2s_hal_set_clk_div(&(p_i2s_obj[i2s_num]->hal), clkmInteger, 63, clkmDecimals, bck, bck);
  406. double real_rate = (double) (I2S_BASE_CLK / (bck * bits * clkmInteger) / 2);
  407. p_i2s_obj[i2s_num]->real_rate = real_rate;
  408. 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",
  409. rate, real_rate, bits, clkmInteger, bck, (double)I2S_BASE_CLK / mclk, real_rate*bits*channel, 64, clkmDecimals);
  410. }
  411. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  412. p_i2s_obj[i2s_num]->tx->curr_ptr = NULL;
  413. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  414. }
  415. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  416. p_i2s_obj[i2s_num]->rx->curr_ptr = NULL;
  417. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  418. }
  419. i2s_hal_set_tx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  420. i2s_hal_set_rx_bits_mod(&(p_i2s_obj[i2s_num]->hal), bits);
  421. // wait all writing on-going finish
  422. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) && p_i2s_obj[i2s_num]->tx) {
  423. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  424. }
  425. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && p_i2s_obj[i2s_num]->rx) {
  426. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  427. }
  428. i2s_start(i2s_num);
  429. return ESP_OK;
  430. }
  431. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  432. {
  433. i2s_obj_t *p_i2s = (i2s_obj_t*) arg;
  434. uint32_t status;
  435. i2s_hal_get_intr_status(&(p_i2s->hal), &status);
  436. if(status == 0) {
  437. //Avoid spurious interrupt
  438. return;
  439. }
  440. i2s_event_t i2s_event;
  441. int dummy;
  442. portBASE_TYPE high_priority_task_awoken = 0;
  443. lldesc_t *finish_desc;
  444. if ((status & I2S_INTR_OUT_DSCR_ERR) || (status & I2S_INTR_IN_DSCR_ERR)) {
  445. ESP_EARLY_LOGE(I2S_TAG, "dma error, interrupt status: 0x%08x", status);
  446. if (p_i2s->i2s_queue) {
  447. i2s_event.type = I2S_EVENT_DMA_ERROR;
  448. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  449. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  450. }
  451. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  452. }
  453. }
  454. if ((status & I2S_INTR_OUT_EOF) && p_i2s->tx) {
  455. i2s_hal_get_out_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  456. // All buffers are empty. This means we have an underflow on our hands.
  457. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  458. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &high_priority_task_awoken);
  459. // See if tx descriptor needs to be auto cleared:
  460. // This will avoid any kind of noise that may get introduced due to transmission
  461. // of previous data from tx descriptor on I2S line.
  462. if (p_i2s->tx_desc_auto_clear == true) {
  463. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  464. }
  465. }
  466. xQueueSendFromISR(p_i2s->tx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  467. if (p_i2s->i2s_queue) {
  468. i2s_event.type = I2S_EVENT_TX_DONE;
  469. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  470. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  471. }
  472. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  473. }
  474. }
  475. if ((status & I2S_INTR_IN_SUC_EOF) && p_i2s->rx) {
  476. // All buffers are full. This means we have an overflow.
  477. i2s_hal_get_in_eof_des_addr(&(p_i2s->hal), (uint32_t *)&finish_desc);
  478. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  479. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &high_priority_task_awoken);
  480. }
  481. xQueueSendFromISR(p_i2s->rx->queue, (void*)(&finish_desc->buf), &high_priority_task_awoken);
  482. if (p_i2s->i2s_queue) {
  483. i2s_event.type = I2S_EVENT_RX_DONE;
  484. if (p_i2s->i2s_queue && xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  485. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &high_priority_task_awoken);
  486. }
  487. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &high_priority_task_awoken);
  488. }
  489. }
  490. i2s_hal_clear_intr_status(&(p_i2s->hal), status);
  491. if (high_priority_task_awoken == pdTRUE) {
  492. portYIELD_FROM_ISR();
  493. }
  494. }
  495. static esp_err_t i2s_destroy_dma_queue(i2s_port_t i2s_num, i2s_dma_t *dma)
  496. {
  497. int bux_idx;
  498. if (p_i2s_obj[i2s_num] == NULL) {
  499. ESP_LOGE(I2S_TAG, "Not initialized yet");
  500. return ESP_ERR_INVALID_ARG;
  501. }
  502. if (dma == NULL) {
  503. ESP_LOGE(I2S_TAG, "dma is NULL");
  504. return ESP_ERR_INVALID_ARG;
  505. }
  506. for (bux_idx = 0; bux_idx < p_i2s_obj[i2s_num]->dma_buf_count; bux_idx++) {
  507. if (dma->desc && dma->desc[bux_idx]) {
  508. free(dma->desc[bux_idx]);
  509. }
  510. if (dma->buf && dma->buf[bux_idx]) {
  511. free(dma->buf[bux_idx]);
  512. }
  513. }
  514. if (dma->buf) {
  515. free(dma->buf);
  516. }
  517. if (dma->desc) {
  518. free(dma->desc);
  519. }
  520. vQueueDelete(dma->queue);
  521. vSemaphoreDelete(dma->mux);
  522. free(dma);
  523. return ESP_OK;
  524. }
  525. static i2s_dma_t *i2s_create_dma_queue(i2s_port_t i2s_num, int dma_buf_count, int dma_buf_len)
  526. {
  527. int bux_idx;
  528. int sample_size = p_i2s_obj[i2s_num]->bytes_per_sample * p_i2s_obj[i2s_num]->channel_num;
  529. i2s_dma_t *dma = (i2s_dma_t*) malloc(sizeof(i2s_dma_t));
  530. if (dma == NULL) {
  531. ESP_LOGE(I2S_TAG, "Error malloc i2s_dma_t");
  532. return NULL;
  533. }
  534. memset(dma, 0, sizeof(i2s_dma_t));
  535. dma->buf = (char **)malloc(sizeof(char*) * dma_buf_count);
  536. if (dma->buf == NULL) {
  537. ESP_LOGE(I2S_TAG, "Error malloc dma buffer pointer");
  538. free(dma);
  539. return NULL;
  540. }
  541. memset(dma->buf, 0, sizeof(char*) * dma_buf_count);
  542. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  543. dma->buf[bux_idx] = (char*) heap_caps_calloc(1, dma_buf_len * sample_size, MALLOC_CAP_DMA);
  544. if (dma->buf[bux_idx] == NULL) {
  545. ESP_LOGE(I2S_TAG, "Error malloc dma buffer");
  546. i2s_destroy_dma_queue(i2s_num, dma);
  547. return NULL;
  548. }
  549. ESP_LOGD(I2S_TAG, "Addr[%d] = %d", bux_idx, (int)dma->buf[bux_idx]);
  550. }
  551. dma->desc = (lldesc_t**) malloc(sizeof(lldesc_t*) * dma_buf_count);
  552. if (dma->desc == NULL) {
  553. ESP_LOGE(I2S_TAG, "Error malloc dma description");
  554. i2s_destroy_dma_queue(i2s_num, dma);
  555. return NULL;
  556. }
  557. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  558. dma->desc[bux_idx] = (lldesc_t*) heap_caps_malloc(sizeof(lldesc_t), MALLOC_CAP_DMA);
  559. if (dma->desc[bux_idx] == NULL) {
  560. ESP_LOGE(I2S_TAG, "Error malloc dma description entry");
  561. i2s_destroy_dma_queue(i2s_num, dma);
  562. return NULL;
  563. }
  564. }
  565. for (bux_idx = 0; bux_idx < dma_buf_count; bux_idx++) {
  566. dma->desc[bux_idx]->owner = 1;
  567. dma->desc[bux_idx]->eof = 1;
  568. dma->desc[bux_idx]->sosf = 0;
  569. dma->desc[bux_idx]->length = dma_buf_len * sample_size;
  570. dma->desc[bux_idx]->size = dma_buf_len * sample_size;
  571. dma->desc[bux_idx]->buf = (uint8_t *) dma->buf[bux_idx];
  572. dma->desc[bux_idx]->offset = 0;
  573. dma->desc[bux_idx]->empty = (uint32_t)((bux_idx < (dma_buf_count - 1)) ? (dma->desc[bux_idx + 1]) : dma->desc[0]);
  574. }
  575. dma->queue = xQueueCreate(dma_buf_count - 1, sizeof(char*));
  576. dma->mux = xSemaphoreCreateMutex();
  577. dma->rw_pos = 0;
  578. dma->buf_size = dma_buf_len * sample_size;
  579. dma->curr_ptr = NULL;
  580. ESP_LOGI(I2S_TAG, "DMA Malloc info, datalen=blocksize=%d, dma_buf_count=%d", dma_buf_len * sample_size, dma_buf_count);
  581. return dma;
  582. }
  583. esp_err_t i2s_start(i2s_port_t i2s_num)
  584. {
  585. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  586. //start DMA link
  587. I2S_ENTER_CRITICAL();
  588. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  589. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  590. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), I2S_INTR_MAX);
  591. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  592. i2s_enable_tx_intr(i2s_num);
  593. i2s_hal_start_tx(&(p_i2s_obj[i2s_num]->hal));
  594. }
  595. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  596. i2s_enable_rx_intr(i2s_num);
  597. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  598. }
  599. esp_intr_enable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  600. I2S_EXIT_CRITICAL();
  601. return ESP_OK;
  602. }
  603. esp_err_t i2s_stop(i2s_port_t i2s_num)
  604. {
  605. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  606. I2S_ENTER_CRITICAL();
  607. esp_intr_disable(p_i2s_obj[i2s_num]->i2s_isr_handle);
  608. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  609. i2s_hal_stop_tx(&(p_i2s_obj[i2s_num]->hal));
  610. i2s_disable_tx_intr(i2s_num);
  611. }
  612. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  613. i2s_hal_stop_rx(&(p_i2s_obj[i2s_num]->hal));
  614. i2s_disable_rx_intr(i2s_num);
  615. }
  616. uint32_t mask;
  617. i2s_hal_get_intr_status(&(p_i2s_obj[i2s_num]->hal), &mask);
  618. i2s_hal_clear_intr_status(&(p_i2s_obj[i2s_num]->hal), mask);
  619. I2S_EXIT_CRITICAL();
  620. return ESP_OK;
  621. }
  622. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  623. {
  624. I2S_CHECK((dac_mode < I2S_DAC_CHANNEL_MAX), "i2s dac mode error", ESP_ERR_INVALID_ARG);
  625. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  626. dac_output_disable(DAC_CHANNEL_1);
  627. dac_output_disable(DAC_CHANNEL_2);
  628. dac_i2s_disable();
  629. } else {
  630. dac_i2s_enable();
  631. }
  632. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  633. //DAC1, right channel, GPIO25
  634. dac_output_enable(DAC_CHANNEL_1);
  635. }
  636. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  637. //DAC2, left channel, GPIO26
  638. dac_output_enable(DAC_CHANNEL_2);
  639. }
  640. return ESP_OK;
  641. }
  642. static esp_err_t _i2s_adc_mode_recover(void)
  643. {
  644. I2S_CHECK(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), "i2s ADC recover error, not initialized...", ESP_ERR_INVALID_ARG);
  645. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  646. }
  647. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  648. {
  649. I2S_CHECK((adc_unit < ADC_UNIT_2), "i2s ADC unit error, only support ADC1 for now", ESP_ERR_INVALID_ARG);
  650. // For now, we only support SAR ADC1.
  651. _i2s_adc_unit = adc_unit;
  652. _i2s_adc_channel = adc_channel;
  653. return adc_i2s_mode_init(adc_unit, adc_channel);
  654. }
  655. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  656. {
  657. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  658. if (pin == NULL) {
  659. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  660. }
  661. if (pin->bck_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->bck_io_num)) {
  662. ESP_LOGE(I2S_TAG, "bck_io_num error");
  663. return ESP_FAIL;
  664. }
  665. if (pin->ws_io_num != -1 && !GPIO_IS_VALID_GPIO(pin->ws_io_num)) {
  666. ESP_LOGE(I2S_TAG, "ws_io_num error");
  667. return ESP_FAIL;
  668. }
  669. if (pin->data_out_num != -1 && !GPIO_IS_VALID_OUTPUT_GPIO(pin->data_out_num)) {
  670. ESP_LOGE(I2S_TAG, "data_out_num error");
  671. return ESP_FAIL;
  672. }
  673. if (pin->data_in_num != -1 && !GPIO_IS_VALID_GPIO(pin->data_in_num)) {
  674. ESP_LOGE(I2S_TAG, "data_in_num error");
  675. return ESP_FAIL;
  676. }
  677. int bck_sig = -1, ws_sig = -1, data_out_sig = -1, data_in_sig = -1;
  678. //Each IIS hw module has a RX and TX unit.
  679. //For TX unit, the output signal index should be I2SnO_xxx_OUT_IDX
  680. //For TX unit, the input signal index should be I2SnO_xxx_IN_IDX
  681. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  682. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  683. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  684. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  685. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  686. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  687. bck_sig = i2s_periph_signal[i2s_num].o_bck_in_sig;
  688. ws_sig = i2s_periph_signal[i2s_num].o_ws_in_sig;
  689. data_out_sig = i2s_periph_signal[i2s_num].o_data_out_sig;
  690. }
  691. }
  692. //For RX unit, the output signal index should be I2SnI_xxx_OUT_IDX
  693. //For RX unit, the input signal index shuld be I2SnI_xxx_IN_IDX
  694. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  695. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  696. bck_sig = i2s_periph_signal[i2s_num].i_bck_out_sig;
  697. ws_sig = i2s_periph_signal[i2s_num].i_ws_out_sig;
  698. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  699. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  700. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  701. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  702. data_in_sig = i2s_periph_signal[i2s_num].i_data_in_sig;
  703. }
  704. }
  705. //For "full-duplex + slave" mode, we should select RX signal index for ws and bck.
  706. //For "full-duplex + master" mode, we should select TX signal index for ws and bck.
  707. if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_SLAVE_MODE_MASK) == I2S_FULL_DUPLEX_SLAVE_MODE_MASK) {
  708. bck_sig = i2s_periph_signal[i2s_num].i_bck_in_sig;
  709. ws_sig = i2s_periph_signal[i2s_num].i_ws_in_sig;
  710. } else if ((p_i2s_obj[i2s_num]->mode & I2S_FULL_DUPLEX_MASTER_MODE_MASK) == I2S_FULL_DUPLEX_MASTER_MODE_MASK) {
  711. bck_sig = i2s_periph_signal[i2s_num].o_bck_out_sig;
  712. ws_sig = i2s_periph_signal[i2s_num].o_ws_out_sig;
  713. }
  714. gpio_matrix_out_check(pin->data_out_num, data_out_sig, 0, 0);
  715. gpio_matrix_in_check(pin->data_in_num, data_in_sig, 0);
  716. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  717. gpio_matrix_out_check(pin->ws_io_num, ws_sig, 0, 0);
  718. gpio_matrix_out_check(pin->bck_io_num, bck_sig, 0, 0);
  719. } else if (p_i2s_obj[i2s_num]->mode & I2S_MODE_SLAVE) {
  720. gpio_matrix_in_check(pin->ws_io_num, ws_sig, 0);
  721. gpio_matrix_in_check(pin->bck_io_num, bck_sig, 0);
  722. }
  723. ESP_LOGD(I2S_TAG, "data: out %d, in: %d, ws: %d, bck: %d", data_out_sig, data_in_sig, ws_sig, bck_sig);
  724. return ESP_OK;
  725. }
  726. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  727. {
  728. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  729. I2S_CHECK((p_i2s_obj[i2s_num]->bytes_per_sample > 0), "bits_per_sample not set", ESP_ERR_INVALID_ARG);
  730. return i2s_set_clk(i2s_num, rate, p_i2s_obj[i2s_num]->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  731. }
  732. #if SOC_I2S_SUPPORT_PDM
  733. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr)
  734. {
  735. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  736. i2s_hal_set_pdm_rx_down_sample(&(p_i2s_obj[i2s_num]->hal), dsr);
  737. 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);
  738. }
  739. #endif
  740. static esp_err_t i2s_param_config(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  741. {
  742. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  743. I2S_CHECK((i2s_config), "param null", ESP_ERR_INVALID_ARG);
  744. 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);
  745. 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);
  746. #if SOC_I2S_SUPPORT_PDM
  747. I2S_CHECK(!((i2s_config->mode & I2S_MODE_PDM) && (i2s_num != I2S_NUM_0)), "I2S DAC PDM only support on I2S0", ESP_ERR_INVALID_ARG);
  748. #endif
  749. if(i2s_config->mode & I2S_MODE_ADC_BUILT_IN) {
  750. //in ADC built-in mode, we need to call i2s_set_adc_mode to
  751. //initialize the specific ADC channel.
  752. //in the current stage, we only support ADC1 and single channel mode.
  753. //In default data mode, the ADC data is in 12-bit resolution mode.
  754. adc_power_acquire();
  755. }
  756. // configure I2S data port interface.
  757. i2s_hal_config_param(&(p_i2s_obj[i2s_num]->hal), i2s_config);
  758. if ((p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) && (p_i2s_obj[i2s_num]->mode & I2S_MODE_TX)) {
  759. i2s_hal_enable_sig_loopback(&(p_i2s_obj[i2s_num]->hal));
  760. if (p_i2s_obj[i2s_num]->mode & I2S_MODE_MASTER) {
  761. i2s_hal_enable_master_mode(&(p_i2s_obj[i2s_num]->hal));
  762. } else {
  763. i2s_hal_enable_slave_mode(&(p_i2s_obj[i2s_num]->hal));
  764. }
  765. }
  766. p_i2s_obj[i2s_num]->use_apll = i2s_config->use_apll;
  767. p_i2s_obj[i2s_num]->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
  768. p_i2s_obj[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
  769. return ESP_OK;
  770. }
  771. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  772. {
  773. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  774. if (p_i2s_obj[i2s_num]->rx && p_i2s_obj[i2s_num]->rx->buf != NULL && p_i2s_obj[i2s_num]->rx->buf_size != 0) {
  775. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  776. memset(p_i2s_obj[i2s_num]->rx->buf[i], 0, p_i2s_obj[i2s_num]->rx->buf_size);
  777. }
  778. }
  779. if (p_i2s_obj[i2s_num]->tx && p_i2s_obj[i2s_num]->tx->buf != NULL && p_i2s_obj[i2s_num]->tx->buf_size != 0) {
  780. int bytes_left = 0;
  781. bytes_left = (p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos) % 4;
  782. if (bytes_left) {
  783. size_t zero_bytes = 0, bytes_written;
  784. i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
  785. }
  786. for (int i = 0; i < p_i2s_obj[i2s_num]->dma_buf_count; i++) {
  787. memset(p_i2s_obj[i2s_num]->tx->buf[i], 0, p_i2s_obj[i2s_num]->tx->buf_size);
  788. }
  789. }
  790. return ESP_OK;
  791. }
  792. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue)
  793. {
  794. esp_err_t err;
  795. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  796. I2S_CHECK((i2s_config != NULL), "I2S configuration must not NULL", ESP_ERR_INVALID_ARG);
  797. 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);
  798. 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);
  799. if (p_i2s_obj[i2s_num] == NULL) {
  800. p_i2s_obj[i2s_num] = (i2s_obj_t*) malloc(sizeof(i2s_obj_t));
  801. if (p_i2s_obj[i2s_num] == NULL) {
  802. ESP_LOGE(I2S_TAG, "Malloc I2S driver error");
  803. return ESP_ERR_NO_MEM;
  804. }
  805. memset(p_i2s_obj[i2s_num], 0, sizeof(i2s_obj_t));
  806. portMUX_TYPE i2s_spinlock_unlocked[1] = {portMUX_INITIALIZER_UNLOCKED};
  807. for (int x = 0; x < I2S_NUM_MAX; x++) {
  808. i2s_spinlock[x] = i2s_spinlock_unlocked[0];
  809. }
  810. //To make sure hardware is enabled before any hardware register operations.
  811. periph_module_enable(i2s_periph_signal[i2s_num].module);
  812. i2s_hal_init(&(p_i2s_obj[i2s_num]->hal), i2s_num);
  813. p_i2s_obj[i2s_num]->i2s_num = i2s_num;
  814. p_i2s_obj[i2s_num]->dma_buf_count = i2s_config->dma_buf_count;
  815. p_i2s_obj[i2s_num]->dma_buf_len = i2s_config->dma_buf_len;
  816. p_i2s_obj[i2s_num]->i2s_queue = i2s_queue;
  817. p_i2s_obj[i2s_num]->mode = i2s_config->mode;
  818. p_i2s_obj[i2s_num]->bits_per_sample = 0;
  819. p_i2s_obj[i2s_num]->bytes_per_sample = 0; // Not initialized yet
  820. p_i2s_obj[i2s_num]->channel_num = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ? 2 : 1;
  821. #ifdef CONFIG_PM_ENABLE
  822. if (i2s_config->use_apll) {
  823. err = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  824. } else {
  825. err = esp_pm_lock_create(ESP_PM_APB_FREQ_MAX, 0, "i2s_driver", &p_i2s_obj[i2s_num]->pm_lock);
  826. }
  827. if (err != ESP_OK) {
  828. free(p_i2s_obj[i2s_num]);
  829. p_i2s_obj[i2s_num] = NULL;
  830. ESP_LOGE(I2S_TAG, "I2S pm lock error");
  831. return err;
  832. }
  833. #endif //CONFIG_PM_ENABLE
  834. //initial interrupt
  835. 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);
  836. if (err != ESP_OK) {
  837. #ifdef CONFIG_PM_ENABLE
  838. if (p_i2s_obj[i2s_num]->pm_lock) {
  839. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  840. }
  841. #endif
  842. free(p_i2s_obj[i2s_num]);
  843. p_i2s_obj[i2s_num] = NULL;
  844. ESP_LOGE(I2S_TAG, "Register I2S Interrupt error");
  845. return err;
  846. }
  847. i2s_stop(i2s_num);
  848. err = i2s_param_config(i2s_num, i2s_config);
  849. if (err != ESP_OK) {
  850. i2s_driver_uninstall(i2s_num);
  851. ESP_LOGE(I2S_TAG, "I2S param configure error");
  852. return err;
  853. }
  854. if (i2s_queue) {
  855. p_i2s_obj[i2s_num]->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  856. *((QueueHandle_t*) i2s_queue) = p_i2s_obj[i2s_num]->i2s_queue;
  857. ESP_LOGI(I2S_TAG, "queue free spaces: %d", uxQueueSpacesAvailable(p_i2s_obj[i2s_num]->i2s_queue));
  858. } else {
  859. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  860. }
  861. //set clock and start
  862. return i2s_set_clk(i2s_num, i2s_config->sample_rate, i2s_config->bits_per_sample, p_i2s_obj[i2s_num]->channel_num);
  863. }
  864. ESP_LOGW(I2S_TAG, "I2S driver already installed");
  865. return ESP_OK;
  866. }
  867. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  868. {
  869. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  870. if (p_i2s_obj[i2s_num] == NULL) {
  871. ESP_LOGI(I2S_TAG, "already uninstalled");
  872. return ESP_OK;
  873. }
  874. i2s_stop(i2s_num);
  875. esp_intr_free(p_i2s_obj[i2s_num]->i2s_isr_handle);
  876. if (p_i2s_obj[i2s_num]->tx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_TX) {
  877. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->tx);
  878. p_i2s_obj[i2s_num]->tx = NULL;
  879. }
  880. if (p_i2s_obj[i2s_num]->rx != NULL && p_i2s_obj[i2s_num]->mode & I2S_MODE_RX) {
  881. i2s_destroy_dma_queue(i2s_num, p_i2s_obj[i2s_num]->rx);
  882. p_i2s_obj[i2s_num]->rx = NULL;
  883. }
  884. if (p_i2s_obj[i2s_num]->i2s_queue) {
  885. vQueueDelete(p_i2s_obj[i2s_num]->i2s_queue);
  886. p_i2s_obj[i2s_num]->i2s_queue = NULL;
  887. }
  888. if(p_i2s_obj[i2s_num]->use_apll) {
  889. // switch back to PLL clock source
  890. i2s_hal_set_clock_sel(&(p_i2s_obj[i2s_num]->hal), I2S_CLK_D2CLK);
  891. rtc_clk_apll_enable(0, 0, 0, 0, 0);
  892. }
  893. #ifdef CONFIG_PM_ENABLE
  894. if (p_i2s_obj[i2s_num]->pm_lock) {
  895. esp_pm_lock_delete(p_i2s_obj[i2s_num]->pm_lock);
  896. }
  897. #endif
  898. free(p_i2s_obj[i2s_num]);
  899. p_i2s_obj[i2s_num] = NULL;
  900. periph_module_disable(i2s_periph_signal[i2s_num].module);
  901. return ESP_OK;
  902. }
  903. 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)
  904. {
  905. char *data_ptr, *src_byte;
  906. int bytes_can_write;
  907. *bytes_written = 0;
  908. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  909. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  910. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  911. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  912. #ifdef CONFIG_PM_ENABLE
  913. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  914. #endif
  915. src_byte = (char *)src;
  916. while (size > 0) {
  917. 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) {
  918. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  919. break;
  920. }
  921. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  922. }
  923. 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);
  924. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  925. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  926. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  927. if (bytes_can_write > size) {
  928. bytes_can_write = size;
  929. }
  930. memcpy(data_ptr, src_byte, bytes_can_write);
  931. size -= bytes_can_write;
  932. src_byte += bytes_can_write;
  933. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  934. (*bytes_written) += bytes_can_write;
  935. }
  936. #ifdef CONFIG_PM_ENABLE
  937. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  938. #endif
  939. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  940. return ESP_OK;
  941. }
  942. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  943. {
  944. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  945. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  946. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  947. adc1_i2s_mode_acquire();
  948. _i2s_adc_mode_recover();
  949. i2s_hal_start_rx(&(p_i2s_obj[i2s_num]->hal));
  950. i2s_hal_reset(&(p_i2s_obj[i2s_num]->hal));
  951. 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);
  952. }
  953. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  954. {
  955. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  956. I2S_CHECK((p_i2s_obj[i2s_num] != NULL), "Not initialized yet", ESP_ERR_INVALID_STATE);
  957. I2S_CHECK((p_i2s_obj[i2s_num]->mode & I2S_MODE_ADC_BUILT_IN), "i2s built-in adc not enabled", ESP_ERR_INVALID_STATE);
  958. adc1_lock_release();
  959. return ESP_OK;
  960. }
  961. 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)
  962. {
  963. char *data_ptr;
  964. int bytes_can_write, tail;
  965. int src_bytes, aim_bytes, zero_bytes;
  966. *bytes_written = 0;
  967. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  968. I2S_CHECK((size > 0), "size must greater than zero", ESP_ERR_INVALID_ARG);
  969. I2S_CHECK((aim_bits * size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  970. I2S_CHECK((aim_bits >= src_bits), "aim_bits mustn't be less than src_bits", ESP_ERR_INVALID_ARG);
  971. I2S_CHECK((p_i2s_obj[i2s_num]->tx), "tx NULL", ESP_ERR_INVALID_ARG);
  972. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  973. ESP_LOGE(I2S_TAG,"bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
  974. return ESP_ERR_INVALID_ARG;
  975. }
  976. if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  977. ESP_LOGE(I2S_TAG,"bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  978. return ESP_ERR_INVALID_ARG;
  979. }
  980. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  981. ESP_LOGE(I2S_TAG,"size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  982. return ESP_ERR_INVALID_ARG;
  983. }
  984. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  985. ESP_LOGE(I2S_TAG,"size must be a multiple of 3 while src_bits is 24, size %d", size);
  986. return ESP_ERR_INVALID_ARG;
  987. }
  988. src_bytes = src_bits / 8;
  989. aim_bytes = aim_bits / 8;
  990. zero_bytes = aim_bytes - src_bytes;
  991. xSemaphoreTake(p_i2s_obj[i2s_num]->tx->mux, (portTickType)portMAX_DELAY);
  992. size = size * aim_bytes / src_bytes;
  993. ESP_LOGD(I2S_TAG,"aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  994. while (size > 0) {
  995. 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) {
  996. if (xQueueReceive(p_i2s_obj[i2s_num]->tx->queue, &p_i2s_obj[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  997. break;
  998. }
  999. p_i2s_obj[i2s_num]->tx->rw_pos = 0;
  1000. }
  1001. data_ptr = (char*)p_i2s_obj[i2s_num]->tx->curr_ptr;
  1002. data_ptr += p_i2s_obj[i2s_num]->tx->rw_pos;
  1003. bytes_can_write = p_i2s_obj[i2s_num]->tx->buf_size - p_i2s_obj[i2s_num]->tx->rw_pos;
  1004. if (bytes_can_write > size) {
  1005. bytes_can_write = size;
  1006. }
  1007. tail = bytes_can_write % aim_bytes;
  1008. bytes_can_write = bytes_can_write - tail;
  1009. memset(data_ptr, 0, bytes_can_write);
  1010. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1011. j += zero_bytes;
  1012. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1013. (*bytes_written) += (aim_bytes - zero_bytes);
  1014. }
  1015. size -= bytes_can_write;
  1016. p_i2s_obj[i2s_num]->tx->rw_pos += bytes_can_write;
  1017. }
  1018. xSemaphoreGive(p_i2s_obj[i2s_num]->tx->mux);
  1019. return ESP_OK;
  1020. }
  1021. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1022. {
  1023. char *data_ptr, *dest_byte;
  1024. int bytes_can_read;
  1025. *bytes_read = 0;
  1026. dest_byte = (char *)dest;
  1027. I2S_CHECK((i2s_num < I2S_NUM_MAX), "i2s_num error", ESP_ERR_INVALID_ARG);
  1028. I2S_CHECK((size < I2S_MAX_BUFFER_SIZE), "size is too large", ESP_ERR_INVALID_ARG);
  1029. I2S_CHECK((p_i2s_obj[i2s_num]->rx), "rx NULL", ESP_ERR_INVALID_ARG);
  1030. xSemaphoreTake(p_i2s_obj[i2s_num]->rx->mux, (portTickType)portMAX_DELAY);
  1031. #ifdef CONFIG_PM_ENABLE
  1032. esp_pm_lock_acquire(p_i2s_obj[i2s_num]->pm_lock);
  1033. #endif
  1034. while (size > 0) {
  1035. 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) {
  1036. if (xQueueReceive(p_i2s_obj[i2s_num]->rx->queue, &p_i2s_obj[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1037. break;
  1038. }
  1039. p_i2s_obj[i2s_num]->rx->rw_pos = 0;
  1040. }
  1041. data_ptr = (char*)p_i2s_obj[i2s_num]->rx->curr_ptr;
  1042. data_ptr += p_i2s_obj[i2s_num]->rx->rw_pos;
  1043. bytes_can_read = p_i2s_obj[i2s_num]->rx->buf_size - p_i2s_obj[i2s_num]->rx->rw_pos;
  1044. if (bytes_can_read > size) {
  1045. bytes_can_read = size;
  1046. }
  1047. memcpy(dest_byte, data_ptr, bytes_can_read);
  1048. size -= bytes_can_read;
  1049. dest_byte += bytes_can_read;
  1050. p_i2s_obj[i2s_num]->rx->rw_pos += bytes_can_read;
  1051. (*bytes_read) += bytes_can_read;
  1052. }
  1053. #ifdef CONFIG_PM_ENABLE
  1054. esp_pm_lock_release(p_i2s_obj[i2s_num]->pm_lock);
  1055. #endif
  1056. xSemaphoreGive(p_i2s_obj[i2s_num]->rx->mux);
  1057. return ESP_OK;
  1058. }