i2s.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdbool.h>
  8. #include <math.h>
  9. #include <esp_types.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/queue.h"
  12. #include "freertos/semphr.h"
  13. #include "soc/lldesc.h"
  14. #include "driver/gpio.h"
  15. #include "hal/gpio_hal.h"
  16. #include "hal/i2s_hal.h"
  17. #include "hal/i2s_std.h"
  18. #include "hal/i2s_pdm.h"
  19. #include "hal/i2s_tdm.h"
  20. #include "driver/i2s.h"
  21. #if SOC_I2S_SUPPORTS_DAC
  22. #include "driver/dac.h"
  23. #include "driver/adc.h"
  24. #include "adc1_private.h"
  25. #endif // SOC_I2S_SUPPORTS_ADC
  26. #if SOC_GDMA_SUPPORTED
  27. #include "esp_private/gdma.h"
  28. #endif
  29. #include "clk_ctrl_os.h"
  30. #include "esp_intr_alloc.h"
  31. #include "esp_err.h"
  32. #include "esp_check.h"
  33. #include "esp_attr.h"
  34. #include "esp_log.h"
  35. #include "esp_pm.h"
  36. #include "esp_efuse.h"
  37. #include "esp_rom_gpio.h"
  38. #include "esp_private/i2s_platform.h"
  39. #include "esp_private/periph_ctrl.h"
  40. #include "sdkconfig.h"
  41. static const char *TAG = "I2S";
  42. #define I2S_ENTER_CRITICAL_ISR(i2s_num) portENTER_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  43. #define I2S_EXIT_CRITICAL_ISR(i2s_num) portEXIT_CRITICAL_ISR(&i2s_spinlock[i2s_num])
  44. #define I2S_ENTER_CRITICAL(i2s_num) portENTER_CRITICAL(&i2s_spinlock[i2s_num])
  45. #define I2S_EXIT_CRITICAL(i2s_num) portEXIT_CRITICAL(&i2s_spinlock[i2s_num])
  46. #define I2S_DMA_BUFFER_MAX_SIZE 4092
  47. #if SOC_I2S_SUPPORTS_ADC_DAC
  48. #define I2S_COMM_MODE_ADC_DAC -1
  49. #endif
  50. /**
  51. * @brief DMA buffer object
  52. *
  53. */
  54. typedef struct {
  55. char **buf;
  56. int buf_size;
  57. volatile int rw_pos;
  58. volatile void *curr_ptr;
  59. SemaphoreHandle_t mux;
  60. QueueHandle_t 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. uint32_t last_buf_size; /*!< DMA last buffer size */
  72. i2s_dma_t *tx; /*!< DMA Tx buffer*/
  73. i2s_dma_t *rx; /*!< DMA Rx buffer*/
  74. #if SOC_GDMA_SUPPORTED
  75. gdma_channel_handle_t rx_dma_chan; /*!< I2S rx gDMA channel handle*/
  76. gdma_channel_handle_t tx_dma_chan; /*!< I2S tx gDMA channel handle*/
  77. #else
  78. intr_handle_t i2s_isr_handle; /*!< I2S Interrupt handle*/
  79. #endif
  80. uint32_t dma_desc_num;
  81. uint32_t dma_frame_num;
  82. bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor on underflow */
  83. bool use_apll; /*!< I2S use APLL clock */
  84. int fixed_mclk; /*!< I2S fixed MLCK clock */
  85. i2s_mclk_multiple_t mclk_multiple; /*!< The multiple of I2S master clock(MCLK) to sample rate */
  86. #ifdef CONFIG_PM_ENABLE
  87. esp_pm_lock_handle_t pm_lock;
  88. #endif
  89. i2s_hal_context_t hal; /*!< I2S hal context*/
  90. /* New config */
  91. i2s_dir_t dir;
  92. i2s_role_t role;
  93. i2s_comm_mode_t mode;
  94. void *slot_cfg;
  95. void *clk_cfg;
  96. uint32_t active_slot; /*!< Active slot number */
  97. uint32_t total_slot; /*!< Total slot number */
  98. } i2s_obj_t;
  99. static i2s_obj_t *p_i2s[SOC_I2S_NUM] = {
  100. [0 ... SOC_I2S_NUM - 1] = NULL,
  101. };
  102. static portMUX_TYPE i2s_platform_spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
  103. static portMUX_TYPE i2s_spinlock[SOC_I2S_NUM] = {
  104. [0 ... SOC_I2S_NUM - 1] = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED,
  105. };
  106. /*-------------------------------------------------------------
  107. I2S DMA operation
  108. -------------------------------------------------------------*/
  109. #if SOC_GDMA_SUPPORTED
  110. static bool IRAM_ATTR i2s_dma_rx_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data)
  111. {
  112. i2s_obj_t *p_i2s = (i2s_obj_t *) user_data;
  113. portBASE_TYPE need_awoke = 0;
  114. portBASE_TYPE tmp = 0;
  115. int dummy;
  116. i2s_event_t i2s_event;
  117. uint32_t finish_desc;
  118. if (p_i2s->rx) {
  119. finish_desc = event_data->rx_eof_desc_addr;
  120. i2s_event.size = ((lldesc_t *)finish_desc)->size;
  121. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  122. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &tmp);
  123. need_awoke |= tmp;
  124. if (p_i2s->i2s_queue) {
  125. i2s_event.type = I2S_EVENT_RX_Q_OVF;
  126. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  127. need_awoke |= tmp;
  128. }
  129. }
  130. xQueueSendFromISR(p_i2s->rx->queue, &(((lldesc_t *)finish_desc)->buf), &tmp);
  131. need_awoke |= tmp;
  132. if (p_i2s->i2s_queue) {
  133. i2s_event.type = I2S_EVENT_RX_DONE;
  134. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  135. need_awoke |= tmp;
  136. }
  137. }
  138. return need_awoke;
  139. }
  140. static bool IRAM_ATTR i2s_dma_tx_callback(gdma_channel_handle_t dma_chan, gdma_event_data_t *event_data, void *user_data)
  141. {
  142. i2s_obj_t *p_i2s = (i2s_obj_t *) user_data;
  143. portBASE_TYPE need_awoke = 0;
  144. portBASE_TYPE tmp = 0;
  145. int dummy;
  146. i2s_event_t i2s_event;
  147. uint32_t finish_desc;
  148. if (p_i2s->tx) {
  149. finish_desc = event_data->tx_eof_desc_addr;
  150. i2s_event.size = ((lldesc_t *)finish_desc)->size;
  151. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  152. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &tmp);
  153. need_awoke |= tmp;
  154. if (p_i2s->tx_desc_auto_clear) {
  155. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  156. }
  157. if (p_i2s->i2s_queue) {
  158. i2s_event.type = I2S_EVENT_TX_Q_OVF;
  159. i2s_event.size = p_i2s->tx->buf_size;
  160. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  161. need_awoke |= tmp;
  162. }
  163. }
  164. xQueueSendFromISR(p_i2s->tx->queue, &(((lldesc_t *)finish_desc)->buf), &tmp);
  165. need_awoke |= tmp;
  166. if (p_i2s->i2s_queue) {
  167. i2s_event.type = I2S_EVENT_TX_DONE;
  168. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  169. need_awoke |= tmp;
  170. }
  171. }
  172. return need_awoke;
  173. }
  174. #else
  175. static void IRAM_ATTR i2s_intr_handler_default(void *arg)
  176. {
  177. i2s_obj_t *p_i2s = (i2s_obj_t *) arg;
  178. uint32_t status = i2s_hal_get_intr_status(&(p_i2s->hal));
  179. if (status == 0) {
  180. //Avoid spurious interrupt
  181. return;
  182. }
  183. i2s_event_t i2s_event;
  184. int dummy;
  185. portBASE_TYPE need_awoke = 0;
  186. portBASE_TYPE tmp = 0;
  187. uint32_t finish_desc = 0;
  188. if ((status & I2S_LL_EVENT_TX_DSCR_ERR) || (status & I2S_LL_EVENT_RX_DSCR_ERR)) {
  189. ESP_EARLY_LOGE(TAG, "dma error, interrupt status: 0x%08x", status);
  190. if (p_i2s->i2s_queue) {
  191. i2s_event.type = I2S_EVENT_DMA_ERROR;
  192. if (xQueueIsQueueFullFromISR(p_i2s->i2s_queue)) {
  193. xQueueReceiveFromISR(p_i2s->i2s_queue, &dummy, &tmp);
  194. need_awoke |= tmp;
  195. }
  196. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  197. need_awoke |= tmp;
  198. }
  199. }
  200. if ((status & I2S_LL_EVENT_TX_EOF) && p_i2s->tx) {
  201. i2s_hal_get_out_eof_des_addr(&(p_i2s->hal), &finish_desc);
  202. i2s_event.size = ((lldesc_t *)finish_desc)->size;
  203. // All buffers are empty. This means we have an underflow on our hands.
  204. if (xQueueIsQueueFullFromISR(p_i2s->tx->queue)) {
  205. xQueueReceiveFromISR(p_i2s->tx->queue, &dummy, &tmp);
  206. need_awoke |= tmp;
  207. // See if tx descriptor needs to be auto cleared:
  208. // This will avoid any kind of noise that may get introduced due to transmission
  209. // of previous data from tx descriptor on I2S line.
  210. if (p_i2s->tx_desc_auto_clear == true) {
  211. memset((void *) dummy, 0, p_i2s->tx->buf_size);
  212. }
  213. if (p_i2s->i2s_queue) {
  214. i2s_event.type = I2S_EVENT_TX_Q_OVF;
  215. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  216. need_awoke |= tmp;
  217. }
  218. }
  219. xQueueSendFromISR(p_i2s->tx->queue, &(((lldesc_t *)finish_desc)->buf), &tmp);
  220. need_awoke |= tmp;
  221. if (p_i2s->i2s_queue) {
  222. i2s_event.type = I2S_EVENT_TX_DONE;
  223. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  224. need_awoke |= tmp;
  225. }
  226. }
  227. if ((status & I2S_LL_EVENT_RX_EOF) && p_i2s->rx) {
  228. // All buffers are full. This means we have an overflow.
  229. i2s_hal_get_in_eof_des_addr(&(p_i2s->hal), &finish_desc);
  230. i2s_event.size = ((lldesc_t *)finish_desc)->size;
  231. if (xQueueIsQueueFullFromISR(p_i2s->rx->queue)) {
  232. xQueueReceiveFromISR(p_i2s->rx->queue, &dummy, &tmp);
  233. need_awoke |= tmp;
  234. if (p_i2s->i2s_queue) {
  235. i2s_event.type = I2S_EVENT_RX_Q_OVF;
  236. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  237. need_awoke |= tmp;
  238. }
  239. }
  240. xQueueSendFromISR(p_i2s->rx->queue, &(((lldesc_t *)finish_desc)->buf), &tmp);
  241. need_awoke |= tmp;
  242. if (p_i2s->i2s_queue) {
  243. i2s_event.type = I2S_EVENT_RX_DONE;
  244. xQueueSendFromISR(p_i2s->i2s_queue, (void * )&i2s_event, &tmp);
  245. need_awoke |= tmp;
  246. }
  247. }
  248. i2s_hal_clear_intr_status(&(p_i2s->hal), status);
  249. if (need_awoke == pdTRUE) {
  250. portYIELD_FROM_ISR();
  251. }
  252. }
  253. #endif
  254. static esp_err_t i2s_dma_intr_init(i2s_port_t i2s_num, int intr_flag)
  255. {
  256. #if SOC_GDMA_SUPPORTED
  257. /* Set GDMA trigger module */
  258. gdma_trigger_t trig = {.periph = GDMA_TRIG_PERIPH_I2S};
  259. switch (i2s_num) {
  260. #if SOC_I2S_NUM > 1
  261. case I2S_NUM_1:
  262. trig.instance_id = SOC_GDMA_TRIG_PERIPH_I2S1;
  263. break;
  264. #endif
  265. default:
  266. trig.instance_id = SOC_GDMA_TRIG_PERIPH_I2S0;
  267. break;
  268. }
  269. /* Set GDMA config */
  270. gdma_channel_alloc_config_t dma_cfg = {};
  271. if ( p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  272. dma_cfg.direction = GDMA_CHANNEL_DIRECTION_TX;
  273. /* Register a new GDMA tx channel */
  274. ESP_RETURN_ON_ERROR(gdma_new_channel(&dma_cfg, &p_i2s[i2s_num]->tx_dma_chan), TAG, "Register tx dma channel error");
  275. ESP_RETURN_ON_ERROR(gdma_connect(p_i2s[i2s_num]->tx_dma_chan, trig), TAG, "Connect tx dma channel error");
  276. gdma_tx_event_callbacks_t cb = {.on_trans_eof = i2s_dma_tx_callback};
  277. /* Set callback function for GDMA, the interrupt is triggered by GDMA, then the GDMA ISR will call the callback function */
  278. gdma_register_tx_event_callbacks(p_i2s[i2s_num]->tx_dma_chan, &cb, p_i2s[i2s_num]);
  279. }
  280. if ( p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  281. dma_cfg.direction = GDMA_CHANNEL_DIRECTION_RX;
  282. /* Register a new GDMA rx channel */
  283. ESP_RETURN_ON_ERROR(gdma_new_channel(&dma_cfg, &p_i2s[i2s_num]->rx_dma_chan), TAG, "Register rx dma channel error");
  284. ESP_RETURN_ON_ERROR(gdma_connect(p_i2s[i2s_num]->rx_dma_chan, trig), TAG, "Connect rx dma channel error");
  285. gdma_rx_event_callbacks_t cb = {.on_recv_eof = i2s_dma_rx_callback};
  286. /* Set callback function for GDMA, the interrupt is triggered by GDMA, then the GDMA ISR will call the callback function */
  287. gdma_register_rx_event_callbacks(p_i2s[i2s_num]->rx_dma_chan, &cb, p_i2s[i2s_num]);
  288. }
  289. #else
  290. /* Initial I2S module interrupt */
  291. ESP_RETURN_ON_ERROR(esp_intr_alloc(i2s_periph_signal[i2s_num].irq, intr_flag, i2s_intr_handler_default, p_i2s[i2s_num], &p_i2s[i2s_num]->i2s_isr_handle), TAG, "Register I2S Interrupt error");
  292. #endif // SOC_GDMA_SUPPORTED
  293. return ESP_OK;
  294. }
  295. static void i2s_tx_reset(i2s_port_t i2s_num)
  296. {
  297. p_i2s[i2s_num]->tx->curr_ptr = NULL;
  298. p_i2s[i2s_num]->tx->rw_pos = 0;
  299. i2s_hal_tx_reset(&(p_i2s[i2s_num]->hal));
  300. #if SOC_GDMA_SUPPORTED
  301. gdma_reset(p_i2s[i2s_num]->tx_dma_chan);
  302. #else
  303. i2s_hal_tx_reset_dma(&(p_i2s[i2s_num]->hal));
  304. #endif
  305. i2s_hal_tx_reset_fifo(&(p_i2s[i2s_num]->hal));
  306. }
  307. /**
  308. * @brief I2S rx reset
  309. *
  310. * @param i2s_num I2S device number
  311. */
  312. static void i2s_rx_reset(i2s_port_t i2s_num)
  313. {
  314. p_i2s[i2s_num]->rx->curr_ptr = NULL;
  315. p_i2s[i2s_num]->rx->rw_pos = 0;
  316. i2s_hal_rx_reset(&(p_i2s[i2s_num]->hal));
  317. #if SOC_GDMA_SUPPORTED
  318. gdma_reset(p_i2s[i2s_num]->rx_dma_chan);
  319. #else
  320. i2s_hal_rx_reset_dma(&(p_i2s[i2s_num]->hal));
  321. #endif
  322. i2s_hal_rx_reset_fifo(&(p_i2s[i2s_num]->hal));
  323. }
  324. static void i2s_tx_start(i2s_port_t i2s_num)
  325. {
  326. #if SOC_GDMA_SUPPORTED
  327. gdma_start(p_i2s[i2s_num]->tx_dma_chan, (uint32_t) p_i2s[i2s_num]->tx->desc[0]);
  328. #else
  329. i2s_hal_tx_enable_dma(&(p_i2s[i2s_num]->hal));
  330. i2s_hal_tx_enable_intr(&(p_i2s[i2s_num]->hal));
  331. i2s_hal_tx_start_link(&(p_i2s[i2s_num]->hal), (uint32_t) p_i2s[i2s_num]->tx->desc[0]);
  332. #endif
  333. i2s_hal_tx_start(&(p_i2s[i2s_num]->hal));
  334. }
  335. static void i2s_rx_start(i2s_port_t i2s_num)
  336. {
  337. #if SOC_GDMA_SUPPORTED
  338. gdma_start(p_i2s[i2s_num]->rx_dma_chan, (uint32_t) p_i2s[i2s_num]->rx->desc[0]);
  339. #else
  340. i2s_hal_rx_enable_dma(&(p_i2s[i2s_num]->hal));
  341. i2s_hal_rx_enable_intr(&(p_i2s[i2s_num]->hal));
  342. i2s_hal_rx_start_link(&(p_i2s[i2s_num]->hal), (uint32_t) p_i2s[i2s_num]->rx->desc[0]);
  343. #endif
  344. i2s_hal_rx_start(&(p_i2s[i2s_num]->hal));
  345. }
  346. static void i2s_tx_stop(i2s_port_t i2s_num)
  347. {
  348. i2s_hal_tx_stop(&(p_i2s[i2s_num]->hal));
  349. #if SOC_GDMA_SUPPORTED
  350. gdma_stop(p_i2s[i2s_num]->tx_dma_chan);
  351. #else
  352. i2s_hal_tx_stop_link(&(p_i2s[i2s_num]->hal));
  353. i2s_hal_tx_disable_intr(&(p_i2s[i2s_num]->hal));
  354. i2s_hal_tx_disable_dma(&(p_i2s[i2s_num]->hal));
  355. #endif
  356. }
  357. static void i2s_rx_stop(i2s_port_t i2s_num)
  358. {
  359. i2s_hal_rx_stop(&(p_i2s[i2s_num]->hal));
  360. #if SOC_GDMA_SUPPORTED
  361. gdma_stop(p_i2s[i2s_num]->rx_dma_chan);
  362. #else
  363. i2s_hal_rx_stop_link(&(p_i2s[i2s_num]->hal));
  364. i2s_hal_rx_disable_intr(&(p_i2s[i2s_num]->hal));
  365. i2s_hal_rx_disable_dma(&(p_i2s[i2s_num]->hal));
  366. #endif
  367. }
  368. esp_err_t i2s_start(i2s_port_t i2s_num)
  369. {
  370. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  371. //start DMA link
  372. I2S_ENTER_CRITICAL(i2s_num);
  373. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  374. i2s_tx_reset(i2s_num);
  375. i2s_tx_start(i2s_num);
  376. }
  377. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  378. i2s_rx_reset(i2s_num);
  379. i2s_rx_start(i2s_num);
  380. }
  381. #if !SOC_GDMA_SUPPORTED
  382. esp_intr_enable(p_i2s[i2s_num]->i2s_isr_handle);
  383. #endif
  384. I2S_EXIT_CRITICAL(i2s_num);
  385. return ESP_OK;
  386. }
  387. esp_err_t i2s_stop(i2s_port_t i2s_num)
  388. {
  389. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  390. I2S_ENTER_CRITICAL(i2s_num);
  391. #if !SOC_GDMA_SUPPORTED
  392. esp_intr_disable(p_i2s[i2s_num]->i2s_isr_handle);
  393. #endif
  394. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  395. i2s_tx_stop(i2s_num);
  396. }
  397. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  398. i2s_rx_stop(i2s_num);
  399. }
  400. #if !SOC_GDMA_SUPPORTED
  401. i2s_hal_clear_intr_status(&(p_i2s[i2s_num]->hal), I2S_INTR_MAX);
  402. #endif
  403. I2S_EXIT_CRITICAL(i2s_num);
  404. return ESP_OK;
  405. }
  406. /*-------------------------------------------------------------
  407. I2S buffer operation
  408. -------------------------------------------------------------*/
  409. static inline uint32_t i2s_get_buf_size(i2s_port_t i2s_num)
  410. {
  411. i2s_slot_config_t *slot_cfg = (i2s_slot_config_t *)p_i2s[i2s_num]->slot_cfg;
  412. /* Calculate bytes per sample, align to 16 bit */
  413. uint32_t bytes_per_sample = ((slot_cfg->data_bit_width + 15) / 16) * 2;
  414. /* The DMA buffer limitation is 4092 bytes */
  415. uint32_t bytes_per_frame = bytes_per_sample * p_i2s[i2s_num]->active_slot;
  416. p_i2s[i2s_num]->dma_frame_num = (p_i2s[i2s_num]->dma_frame_num * bytes_per_frame > I2S_DMA_BUFFER_MAX_SIZE) ?
  417. I2S_DMA_BUFFER_MAX_SIZE / bytes_per_frame : p_i2s[i2s_num]->dma_frame_num;
  418. return p_i2s[i2s_num]->dma_frame_num * bytes_per_frame;
  419. }
  420. static esp_err_t i2s_delete_dma_buffer(i2s_port_t i2s_num, i2s_dma_t *dma_obj)
  421. {
  422. ESP_RETURN_ON_FALSE(dma_obj, ESP_ERR_INVALID_ARG, TAG, "I2S DMA object can't be NULL");
  423. uint32_t buf_cnt = p_i2s[i2s_num]->dma_desc_num;
  424. /* Loop to destroy every descriptor and buffer */
  425. for (int cnt = 0; cnt < buf_cnt; cnt++) {
  426. if (dma_obj->desc && dma_obj->desc[cnt]) {
  427. free(dma_obj->desc[cnt]);
  428. dma_obj->desc[cnt] = NULL;
  429. }
  430. if (dma_obj->buf && dma_obj->buf[cnt]) {
  431. free(dma_obj->buf[cnt]);
  432. dma_obj->buf[cnt] = NULL;
  433. }
  434. }
  435. return ESP_OK;
  436. }
  437. static esp_err_t i2s_alloc_dma_buffer(i2s_port_t i2s_num, i2s_dma_t *dma_obj)
  438. {
  439. esp_err_t ret = ESP_OK;
  440. ESP_GOTO_ON_FALSE(dma_obj, ESP_ERR_INVALID_ARG, err, TAG, "I2S DMA object can't be NULL");
  441. uint32_t buf_cnt = p_i2s[i2s_num]->dma_desc_num;
  442. for (int cnt = 0; cnt < buf_cnt; cnt++) {
  443. /* Allocate DMA buffer */
  444. dma_obj->buf[cnt] = (char *) heap_caps_calloc(dma_obj->buf_size, sizeof(char), MALLOC_CAP_DMA);
  445. ESP_GOTO_ON_FALSE(dma_obj->buf[cnt], ESP_ERR_NO_MEM, err, TAG, "Error malloc dma buffer");
  446. /* Initialize DMA buffer to 0 */
  447. memset(dma_obj->buf[cnt], 0, dma_obj->buf_size);
  448. ESP_LOGD(TAG, "Addr[%d] = %d", cnt, (int)dma_obj->buf[cnt]);
  449. /* Allocate DMA descpriptor */
  450. dma_obj->desc[cnt] = (lldesc_t *) heap_caps_calloc(1, sizeof(lldesc_t), MALLOC_CAP_DMA);
  451. ESP_GOTO_ON_FALSE(dma_obj->desc[cnt], ESP_ERR_NO_MEM, err, TAG, "Error malloc dma description entry");
  452. }
  453. /* DMA descriptor must be initialize after all descriptor has been created, otherwise they can't be linked together as a chain */
  454. for (int cnt = 0; cnt < buf_cnt; cnt++) {
  455. /* Initialize DMA descriptor */
  456. dma_obj->desc[cnt]->owner = 1;
  457. dma_obj->desc[cnt]->eof = 1;
  458. dma_obj->desc[cnt]->sosf = 0;
  459. dma_obj->desc[cnt]->length = dma_obj->buf_size;
  460. dma_obj->desc[cnt]->size = dma_obj->buf_size;
  461. dma_obj->desc[cnt]->buf = (uint8_t *) dma_obj->buf[cnt];
  462. dma_obj->desc[cnt]->offset = 0;
  463. /* Link to the next descriptor */
  464. dma_obj->desc[cnt]->empty = (uint32_t)((cnt < (buf_cnt - 1)) ? (dma_obj->desc[cnt + 1]) : dma_obj->desc[0]);
  465. }
  466. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  467. i2s_ll_rx_set_eof_num(p_i2s[i2s_num]->hal.dev, dma_obj->buf_size);
  468. }
  469. ESP_LOGI(TAG, "DMA Malloc info, datalen=blocksize=%d, dma_desc_num=%d", dma_obj->buf_size, buf_cnt);
  470. return ESP_OK;
  471. err:
  472. /* Delete DMA buffer if failed to allocate memory */
  473. i2s_delete_dma_buffer(i2s_num, dma_obj);
  474. return ret;
  475. }
  476. static esp_err_t i2s_realloc_dma_buffer(i2s_port_t i2s_num, i2s_dma_t *dma_obj)
  477. {
  478. ESP_RETURN_ON_FALSE(dma_obj, ESP_ERR_INVALID_ARG, TAG, "I2S DMA object can't be NULL");
  479. /* Destroy old dma descriptor and buffer */
  480. i2s_delete_dma_buffer(i2s_num, dma_obj);
  481. /* Alloc new dma descriptor and buffer */
  482. ESP_RETURN_ON_ERROR(i2s_alloc_dma_buffer(i2s_num, dma_obj), TAG, "Failed to allocate dma buffer");
  483. return ESP_OK;
  484. }
  485. static esp_err_t i2s_destroy_dma_object(i2s_port_t i2s_num, i2s_dma_t **dma)
  486. {
  487. /* Check if DMA truely need destroy */
  488. ESP_RETURN_ON_FALSE(p_i2s[i2s_num], ESP_ERR_INVALID_ARG, TAG, "I2S not initialized yet");
  489. if (!(*dma)) {
  490. return ESP_OK;
  491. }
  492. /* Destroy every descriptor and buffer */
  493. i2s_delete_dma_buffer(i2s_num, (*dma));
  494. /* Destroy descriptor pointer */
  495. if ((*dma)->desc) {
  496. free((*dma)->desc);
  497. (*dma)->desc = NULL;
  498. }
  499. /* Destroy buffer pointer */
  500. if ((*dma)->buf) {
  501. free((*dma)->buf);
  502. (*dma)->buf = NULL;
  503. }
  504. /* Delete DMA mux */
  505. vSemaphoreDelete((*dma)->mux);
  506. /* Delete DMA queue */
  507. vQueueDelete((*dma)->queue);
  508. /* Free DMA structure */
  509. free(*dma);
  510. *dma = NULL;
  511. ESP_LOGI(TAG, "DMA queue destroyed");
  512. return ESP_OK;
  513. }
  514. static esp_err_t i2s_create_dma_object(i2s_port_t i2s_num, i2s_dma_t **dma)
  515. {
  516. ESP_RETURN_ON_FALSE(dma, ESP_ERR_INVALID_ARG, TAG, "DMA object secondary pointer is NULL");
  517. ESP_RETURN_ON_FALSE((*dma == NULL), ESP_ERR_INVALID_ARG, TAG, "DMA object has been created");
  518. uint32_t buf_cnt = p_i2s[i2s_num]->dma_desc_num;
  519. /* Allocate new DMA structure */
  520. *dma = (i2s_dma_t *) calloc(1, sizeof(i2s_dma_t));
  521. ESP_RETURN_ON_FALSE(*dma, ESP_ERR_NO_MEM, TAG, "DMA object allocate failed");
  522. /* Allocate DMA buffer poiter */
  523. (*dma)->buf = (char **)heap_caps_calloc(buf_cnt, sizeof(char *), MALLOC_CAP_DMA);
  524. if (!(*dma)->buf) {
  525. goto err;
  526. }
  527. /* Allocate secondary pointer of DMA descriptor chain */
  528. (*dma)->desc = (lldesc_t **)heap_caps_calloc(buf_cnt, sizeof(lldesc_t *), MALLOC_CAP_DMA);
  529. if (!(*dma)->desc) {
  530. goto err;
  531. }
  532. /* Create queue and mutex */
  533. (*dma)->queue = xQueueCreate(buf_cnt - 1, sizeof(char *));
  534. if (!(*dma)->queue) {
  535. goto err;
  536. }
  537. (*dma)->mux = xSemaphoreCreateMutex();
  538. if (!(*dma)->mux) {
  539. goto err;
  540. }
  541. return ESP_OK;
  542. err:
  543. ESP_LOGE(TAG, "I2S DMA object create failed, preparing to uninstall");
  544. /* Destroy DMA queue if failed to allocate memory */
  545. i2s_destroy_dma_object(i2s_num, dma);
  546. return ESP_ERR_NO_MEM;
  547. }
  548. /*-------------------------------------------------------------
  549. I2S clock operation
  550. -------------------------------------------------------------*/
  551. static uint32_t i2s_config_source_clock(i2s_port_t i2s_num, bool use_apll, uint32_t mclk)
  552. {
  553. #if SOC_I2S_SUPPORTS_APLL
  554. if (use_apll) {
  555. /* Calculate the expected APLL */
  556. int div = (int)((SOC_APLL_MIN_HZ / mclk) + 1);
  557. /* apll_freq = mclk * div
  558. * when div = 1, hardware will still divide 2
  559. * when div = 0, the final mclk will be unpredictable
  560. * So the div here should be at least 2 */
  561. div = div < 2 ? 2 : div;
  562. uint32_t expt_freq = mclk * div;
  563. /* Set APLL coefficients to the given frequency */
  564. uint32_t real_freq = 0;
  565. esp_err_t ret = periph_rtc_apll_freq_set(expt_freq, &real_freq);
  566. if (ret == ESP_ERR_INVALID_ARG) {
  567. ESP_LOGE(TAG, "set APLL coefficients failed");
  568. return 0;
  569. }
  570. if (ret == ESP_ERR_INVALID_STATE) {
  571. ESP_LOGW(TAG, "APLL is occupied already, it is working at %d Hz", real_freq);
  572. }
  573. ESP_LOGI(TAG, "APLL expected frequency is %d Hz, real frequency is %d Hz", expt_freq, real_freq);
  574. /* In APLL mode, there is no sclk but only mclk, so return 0 here to indicate APLL mode */
  575. return real_freq;
  576. }
  577. return I2S_LL_BASE_CLK;
  578. #else
  579. if (use_apll) {
  580. ESP_LOGW(TAG, "APLL not supported on current chip, use I2S_CLK_D2CLK as default clock source");
  581. }
  582. return I2S_LL_BASE_CLK;
  583. #endif
  584. }
  585. #if SOC_I2S_SUPPORTS_ADC || SOC_I2S_SUPPORTS_DAC
  586. static esp_err_t i2s_calculate_adc_dac_clock(int i2s_num, i2s_clock_info_t *clk_info)
  587. {
  588. /* For ADC/DAC mode, the built-in ADC/DAC is driven by 'mclk' instead of 'bclk'
  589. * 'bclk' should be fixed to the double of sample rate
  590. * 'bclk_div' is the real coefficient that affects the slot bit */
  591. i2s_clk_config_t *clk_cfg = (i2s_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  592. i2s_slot_config_t *slot_cfg = (i2s_slot_config_t *)p_i2s[i2s_num]->slot_cfg;
  593. uint32_t slot_bits = slot_cfg->slot_bit_width;
  594. /* Set I2S bit clock */
  595. clk_info->bclk = clk_cfg->sample_rate_hz * I2S_LL_AD_BCK_FACTOR;
  596. /* Set I2S bit clock default division */
  597. clk_info->bclk_div = slot_bits;
  598. /* If fixed_mclk and use_apll are set, use fixed_mclk as mclk frequency, otherwise calculate by mclk = bclk * bclk_div */
  599. clk_info->mclk = (p_i2s[i2s_num]->use_apll && p_i2s[i2s_num]->fixed_mclk) ?
  600. p_i2s[i2s_num]->fixed_mclk : clk_info->bclk * clk_info->bclk_div;
  601. /* Calculate bclk_div = mclk / bclk */
  602. clk_info->bclk_div = clk_info->mclk / clk_info->bclk;
  603. /* Get I2S system clock by config source clock */
  604. clk_info->sclk = i2s_config_source_clock(i2s_num, p_i2s[i2s_num]->use_apll, clk_info->mclk);
  605. /* Get I2S master clock rough division, later will calculate the fine division parameters in HAL */
  606. clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
  607. /* Check if the configuration is correct */
  608. ESP_RETURN_ON_FALSE(clk_info->sclk / (float)clk_info->mclk > 1.99, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large, the mclk division is below minimum value 2");
  609. ESP_RETURN_ON_FALSE(clk_info->mclk_div < 256, ESP_ERR_INVALID_ARG, TAG, "sample rate is too small, the mclk division exceed the maximum value 255");
  610. return ESP_OK;
  611. }
  612. #endif // SOC_I2S_SUPPORTS_ADC || SOC_I2S_SUPPORTS_DAC
  613. #if SOC_I2S_SUPPORTS_PDM_TX
  614. static esp_err_t i2s_calculate_pdm_tx_clock(int i2s_num, i2s_clock_info_t *clk_info)
  615. {
  616. i2s_pdm_tx_clk_config_t *clk_cfg = (i2s_pdm_tx_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  617. int fp = clk_cfg->up_sample_fp;
  618. int fs = clk_cfg->up_sample_fs;
  619. /* Set I2S bit clock */
  620. clk_info->bclk = clk_cfg->sample_rate_hz * I2S_LL_PDM_BCK_FACTOR * fp / fs;
  621. /* Set I2S bit clock default division */
  622. clk_info->bclk_div = 8;
  623. /* If fixed_mclk and use_apll are set, use fixed_mclk as mclk frequency, otherwise calculate by mclk = sample_rate_hz * multiple */
  624. clk_info->mclk = (p_i2s[i2s_num]->use_apll && p_i2s[i2s_num]->fixed_mclk) ?
  625. p_i2s[i2s_num]->fixed_mclk : clk_info->bclk * clk_info->bclk_div;
  626. /* Calculate bclk_div = mclk / bclk */
  627. clk_info->bclk_div = clk_info->mclk / clk_info->bclk;
  628. /* Get I2S system clock by config source clock */
  629. clk_info->sclk = i2s_config_source_clock(i2s_num, p_i2s[i2s_num]->use_apll, clk_info->mclk);
  630. /* Get I2S master clock rough division, later will calculate the fine division parameters in HAL */
  631. clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
  632. /* Check if the configuration is correct */
  633. ESP_RETURN_ON_FALSE(clk_info->sclk / (float)clk_info->mclk > 1.99, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large, the mclk division is below minimum value 2");
  634. ESP_RETURN_ON_FALSE(clk_info->mclk_div < 256, ESP_ERR_INVALID_ARG, TAG, "sample rate is too small, the mclk division exceed the maximum value 255");
  635. return ESP_OK;
  636. }
  637. #endif // SOC_I2S_SUPPORTS_PDM_TX
  638. #if SOC_I2S_SUPPORTS_PDM_RX
  639. static esp_err_t i2s_calculate_pdm_rx_clock(int i2s_num, i2s_clock_info_t *clk_info)
  640. {
  641. i2s_pdm_rx_clk_config_t *clk_cfg = (i2s_pdm_rx_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  642. i2s_pdm_dsr_t dsr = clk_cfg->dn_sample_mode;
  643. /* Set I2S bit clock */
  644. clk_info->bclk = clk_cfg->sample_rate_hz * I2S_LL_PDM_BCK_FACTOR * (dsr == I2S_PDM_DSR_16S ? 2 : 1);
  645. /* Set I2S bit clock default division */
  646. clk_info->bclk_div = 8;
  647. /* If fixed_mclk and use_apll are set, use fixed_mclk as mclk frequency, otherwise calculate by mclk = sample_rate_hz * multiple */
  648. clk_info->mclk = (p_i2s[i2s_num]->use_apll && p_i2s[i2s_num]->fixed_mclk) ?
  649. p_i2s[i2s_num]->fixed_mclk : clk_info->bclk * clk_info->bclk_div;
  650. /* Calculate bclk_div = mclk / bclk */
  651. clk_info->bclk_div = clk_info->mclk / clk_info->bclk;
  652. /* Get I2S system clock by config source clock */
  653. clk_info->sclk = i2s_config_source_clock(i2s_num, p_i2s[i2s_num]->use_apll, clk_info->mclk);
  654. /* Get I2S master clock rough division, later will calculate the fine division parameters in HAL */
  655. clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
  656. /* Check if the configuration is correct */
  657. ESP_RETURN_ON_FALSE(clk_info->sclk / (float)clk_info->mclk > 1.99, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large, the mclk division is below minimum value 2");
  658. ESP_RETURN_ON_FALSE(clk_info->mclk_div < 256, ESP_ERR_INVALID_ARG, TAG, "sample rate is too small, the mclk division exceed the maximum value 255");
  659. return ESP_OK;
  660. }
  661. #endif // SOC_I2S_SUPPORTS_PDM_RX
  662. static esp_err_t i2s_calculate_common_clock(int i2s_num, i2s_clock_info_t *clk_info)
  663. {
  664. i2s_clk_config_t *clk_cfg = (i2s_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  665. i2s_slot_config_t *slot_cfg = (i2s_slot_config_t *)p_i2s[i2s_num]->slot_cfg;
  666. uint32_t rate = clk_cfg->sample_rate_hz;
  667. uint32_t slot_num = p_i2s[i2s_num]->total_slot < 2 ? 2 : p_i2s[i2s_num]->total_slot;
  668. uint32_t slot_bits = slot_cfg->slot_bit_width;
  669. /* Calculate multiple */
  670. if (p_i2s[i2s_num]->role == I2S_ROLE_MASTER) {
  671. clk_info->bclk = rate * slot_num * slot_bits;
  672. clk_info->mclk = rate * clk_cfg->mclk_multiple;
  673. clk_info->bclk_div = clk_info->mclk / clk_info->bclk;
  674. } else {
  675. /* For slave mode, mclk >= bclk * 8, so fix bclk_div to 8 first */
  676. clk_info->bclk_div = 8;
  677. clk_info->bclk = rate * slot_num * slot_bits;
  678. clk_info->mclk = clk_info->bclk * clk_info->bclk_div;
  679. }
  680. /* Get I2S system clock by config source clock */
  681. clk_info->sclk = i2s_config_source_clock(i2s_num, p_i2s[i2s_num]->use_apll, clk_info->mclk);
  682. /* Get I2S master clock rough division, later will calculate the fine division parameters in HAL */
  683. clk_info->mclk_div = clk_info->sclk / clk_info->mclk;
  684. /* Check if the configuration is correct */
  685. ESP_RETURN_ON_FALSE(clk_info->mclk <= clk_info->sclk, ESP_ERR_INVALID_ARG, TAG, "sample rate is too large");
  686. return ESP_OK;
  687. }
  688. static esp_err_t i2s_calculate_clock(i2s_port_t i2s_num, i2s_clock_info_t *clk_info)
  689. {
  690. /* Calculate clock for ADC/DAC mode */
  691. #if SOC_I2S_SUPPORTS_ADC_DAC
  692. if ((int)p_i2s[i2s_num]->mode == I2S_COMM_MODE_ADC_DAC) {
  693. ESP_RETURN_ON_ERROR(i2s_calculate_adc_dac_clock(i2s_num, clk_info), TAG, "ADC/DAC clock calculate failed");
  694. return ESP_OK;
  695. }
  696. #endif // SOC_I2S_SUPPORTS_ADC
  697. /* Calculate clock for PDM mode */
  698. #if SOC_I2S_SUPPORTS_PDM
  699. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_PDM) {
  700. #if SOC_I2S_SUPPORTS_PDM_TX
  701. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  702. ESP_RETURN_ON_ERROR(i2s_calculate_pdm_tx_clock(i2s_num, clk_info), TAG, "PDM TX clock calculate failed");
  703. }
  704. #endif // SOC_I2S_SUPPORTS_PDM_TX
  705. #if SOC_I2S_SUPPORTS_PDM_RX
  706. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  707. ESP_RETURN_ON_ERROR(i2s_calculate_pdm_rx_clock(i2s_num, clk_info), TAG, "PDM RX clock calculate failed");
  708. }
  709. #endif // SOC_I2S_SUPPORTS_PDM_RX
  710. return ESP_OK;
  711. }
  712. #endif // SOC_I2S_SUPPORTS_PDM_TX || SOC_I2S_SUPPORTS_PDM_RX
  713. /* Calculate clock for common mode */
  714. ESP_RETURN_ON_ERROR(i2s_calculate_common_clock(i2s_num, clk_info), TAG, "Common clock calculate failed");
  715. return ESP_OK;
  716. }
  717. /*-------------------------------------------------------------
  718. I2S configuration
  719. -------------------------------------------------------------*/
  720. #if SOC_I2S_SUPPORTS_TDM
  721. static uint32_t i2s_get_max_channel_num(uint32_t chan_mask)
  722. {
  723. int max_chan;
  724. for (max_chan = 0; chan_mask; max_chan++, chan_mask >>= 1);
  725. /* Can't be smaller than 2 */
  726. return max_chan < 2 ? 2 : max_chan;
  727. }
  728. static uint32_t i2s_get_active_channel_num(uint32_t chan_mask)
  729. {
  730. uint32_t num = 0;
  731. for (int i = 0; chan_mask; i++, chan_mask >>= 1) {
  732. if (chan_mask & 0x01) {
  733. num++;
  734. }
  735. }
  736. return num;
  737. }
  738. #endif
  739. #if SOC_I2S_SUPPORTS_ADC_DAC
  740. static void i2s_dac_set_slot_legacy(void)
  741. {
  742. i2s_dev_t *dev = p_i2s[0]->hal.dev;
  743. i2s_slot_config_t *slot_cfg = p_i2s[0]->slot_cfg;
  744. i2s_ll_tx_reset(dev);
  745. i2s_ll_tx_set_slave_mod(dev, false);
  746. i2s_ll_tx_set_sample_bit(dev, slot_cfg->slot_bit_width, slot_cfg->data_bit_width);
  747. i2s_ll_tx_enable_mono_mode(dev, false); // DAC not work in mono mode
  748. i2s_ll_tx_enable_msb_shift(dev, false);
  749. i2s_ll_tx_set_ws_width(dev, slot_cfg->slot_bit_width);
  750. i2s_ll_tx_enable_msb_right(dev, false);
  751. i2s_ll_tx_enable_right_first(dev, true);
  752. /* Should always enable fifo */
  753. i2s_ll_tx_force_enable_fifo_mod(dev, true);
  754. }
  755. esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode)
  756. {
  757. ESP_RETURN_ON_FALSE((dac_mode < I2S_DAC_CHANNEL_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s dac mode error");
  758. if (dac_mode == I2S_DAC_CHANNEL_DISABLE) {
  759. dac_output_disable(DAC_CHANNEL_1);
  760. dac_output_disable(DAC_CHANNEL_2);
  761. dac_i2s_disable();
  762. } else {
  763. dac_i2s_enable();
  764. }
  765. if (dac_mode & I2S_DAC_CHANNEL_RIGHT_EN) {
  766. //DAC1, right channel
  767. dac_output_enable(DAC_CHANNEL_1);
  768. }
  769. if (dac_mode & I2S_DAC_CHANNEL_LEFT_EN) {
  770. //DAC2, left channel
  771. dac_output_enable(DAC_CHANNEL_2);
  772. }
  773. return ESP_OK;
  774. }
  775. static void i2s_adc_set_slot_legacy(void)
  776. {
  777. i2s_dev_t *dev = p_i2s[0]->hal.dev;
  778. i2s_slot_config_t *slot_cfg = p_i2s[0]->slot_cfg;
  779. // When ADC/DAC are installed as duplex mode, ADC will share the WS and BCLK clock by working in slave mode
  780. i2s_ll_rx_set_slave_mod(dev, false);
  781. i2s_ll_rx_set_sample_bit(dev, slot_cfg->slot_bit_width, slot_cfg->data_bit_width);
  782. i2s_ll_rx_enable_mono_mode(dev, true); // ADC should use mono mode to meet the sample rate
  783. i2s_ll_rx_enable_msb_shift(dev, false);
  784. i2s_ll_rx_set_ws_width(dev, slot_cfg->slot_bit_width);
  785. i2s_ll_rx_enable_msb_right(dev, false);
  786. i2s_ll_rx_enable_right_first(dev, false);
  787. /* Should always enable fifo */
  788. i2s_ll_rx_force_enable_fifo_mod(dev, true);
  789. }
  790. static int _i2s_adc_unit = -1;
  791. static int _i2s_adc_channel = -1;
  792. static esp_err_t _i2s_adc_mode_recover(void)
  793. {
  794. ESP_RETURN_ON_FALSE(((_i2s_adc_unit != -1) && (_i2s_adc_channel != -1)), ESP_ERR_INVALID_ARG, TAG, "i2s ADC recover error, not initialized...");
  795. return adc_i2s_mode_init(_i2s_adc_unit, _i2s_adc_channel);
  796. }
  797. esp_err_t i2s_set_adc_mode(adc_unit_t adc_unit, adc1_channel_t adc_channel)
  798. {
  799. ESP_RETURN_ON_FALSE((adc_unit < ADC_UNIT_2), ESP_ERR_INVALID_ARG, TAG, "i2s ADC unit error, only support ADC1 for now");
  800. // For now, we only support SAR ADC1.
  801. _i2s_adc_unit = adc_unit;
  802. _i2s_adc_channel = adc_channel;
  803. return adc_i2s_mode_init(adc_unit, adc_channel);
  804. }
  805. esp_err_t i2s_adc_enable(i2s_port_t i2s_num)
  806. {
  807. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  808. ESP_RETURN_ON_FALSE((p_i2s[i2s_num] != NULL), ESP_ERR_INVALID_STATE, TAG, "Not initialized yet");
  809. ESP_RETURN_ON_FALSE(((int)p_i2s[i2s_num]->mode == I2S_COMM_MODE_ADC_DAC) && (p_i2s[i2s_num]->dir & I2S_DIR_RX),
  810. ESP_ERR_INVALID_STATE, TAG, "i2s built-in adc not enabled");
  811. adc1_dma_mode_acquire();
  812. _i2s_adc_mode_recover();
  813. i2s_rx_reset(i2s_num);
  814. return i2s_start(i2s_num);
  815. }
  816. esp_err_t i2s_adc_disable(i2s_port_t i2s_num)
  817. {
  818. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  819. ESP_RETURN_ON_FALSE((p_i2s[i2s_num] != NULL), ESP_ERR_INVALID_STATE, TAG, "Not initialized yet");
  820. ESP_RETURN_ON_FALSE(((int)p_i2s[i2s_num]->mode == I2S_COMM_MODE_ADC_DAC) && (p_i2s[i2s_num]->dir & I2S_DIR_RX),
  821. ESP_ERR_INVALID_STATE, TAG, "i2s built-in adc not enabled");
  822. i2s_hal_rx_stop(&(p_i2s[i2s_num]->hal));
  823. adc1_lock_release();
  824. return ESP_OK;
  825. }
  826. #endif
  827. static esp_err_t i2s_check_cfg_validity(i2s_port_t i2s_num, const i2s_config_t *cfg)
  828. {
  829. /* Step 1: Check the validity of input parameters */
  830. /* Check the validity of i2s device number */
  831. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  832. ESP_RETURN_ON_FALSE(p_i2s[i2s_num] == NULL, ESP_ERR_INVALID_STATE, TAG, "this i2s port is in use");
  833. ESP_RETURN_ON_FALSE(cfg, ESP_ERR_INVALID_ARG, TAG, "I2S configuration must not be NULL");
  834. /* Check the size of DMA buffer */
  835. ESP_RETURN_ON_FALSE((cfg->dma_desc_num >= 2 && cfg->dma_desc_num <= 128), ESP_ERR_INVALID_ARG, TAG, "I2S buffer count less than 128 and more than 2");
  836. ESP_RETURN_ON_FALSE((cfg->dma_frame_num >= 8 && cfg->dma_frame_num <= 1024), ESP_ERR_INVALID_ARG, TAG, "I2S buffer length at most 1024 and more than 8");
  837. #if SOC_I2S_SUPPORTS_PDM_TX || SOC_I2S_SUPPORTS_PDM_RX
  838. /* Check PDM mode */
  839. if (cfg->mode & I2S_MODE_PDM) {
  840. ESP_RETURN_ON_FALSE(i2s_num == I2S_NUM_0, ESP_ERR_INVALID_ARG, TAG, "I2S PDM mode only support on I2S0");
  841. #if !SOC_I2S_SUPPORTS_PDM_TX
  842. ESP_RETURN_ON_FALSE(!(cfg->mode & I2S_MODE_TX), ESP_ERR_INVALID_ARG, TAG, "PDM does not support TX on this chip");
  843. #endif // SOC_I2S_SUPPORTS_PDM_TX
  844. #if !SOC_I2S_SUPPORTS_PDM_RX
  845. ESP_RETURN_ON_FALSE(!(cfg->mode & I2S_MODE_RX), ESP_ERR_INVALID_ARG, TAG, "PDM does not support RX on this chip");
  846. #endif // SOC_I2S_SUPPORTS_PDM_RX
  847. }
  848. #else
  849. ESP_RETURN_ON_FALSE(!(cfg->mode & I2S_MODE_PDM), ESP_ERR_INVALID_ARG, TAG, "I2S PDM mode not supported on current chip");
  850. #endif // SOC_I2S_SUPPORTS_PDM_TX || SOC_I2S_SUPPORTS_PDM_RX
  851. #if SOC_I2S_SUPPORTS_ADC || SOC_I2S_SUPPORTS_DAC
  852. /* Check built-in ADC/DAC mode */
  853. if (cfg->mode & (I2S_MODE_ADC_BUILT_IN | I2S_MODE_DAC_BUILT_IN)) {
  854. ESP_RETURN_ON_FALSE(i2s_num == I2S_NUM_0, ESP_ERR_INVALID_ARG, TAG, "I2S built-in ADC/DAC only support on I2S0");
  855. }
  856. #else
  857. /* Check the transmit/receive mode */
  858. ESP_RETURN_ON_FALSE((cfg->mode & I2S_MODE_TX) || (cfg->mode & I2S_MODE_RX), ESP_ERR_INVALID_ARG, TAG, "I2S no TX/RX mode selected");
  859. /* Check communication format */
  860. ESP_RETURN_ON_FALSE(cfg->communication_format && (cfg->communication_format < I2S_COMM_FORMAT_STAND_MAX), ESP_ERR_INVALID_ARG, TAG, "invalid communication formats");
  861. #endif // SOC_I2S_SUPPORTS_ADC || SOC_I2S_SUPPORTS_DAC
  862. return ESP_OK;
  863. }
  864. static void i2s_set_slot_legacy(i2s_port_t i2s_num)
  865. {
  866. bool is_tx_slave = p_i2s[i2s_num]->role == I2S_ROLE_SLAVE;
  867. bool is_rx_slave = is_tx_slave;
  868. if (p_i2s[i2s_num]->dir == (I2S_DIR_TX | I2S_DIR_RX)) {
  869. i2s_ll_share_bck_ws(p_i2s[i2s_num]->hal.dev, true);
  870. /* Since bck and ws are shared, only tx or rx can be master
  871. Force to set rx as slave to avoid conflict of clock signal */
  872. is_rx_slave = true;
  873. } else {
  874. i2s_ll_share_bck_ws(p_i2s[i2s_num]->hal.dev, false);
  875. }
  876. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_STD) {
  877. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  878. i2s_hal_std_set_tx_slot(&(p_i2s[i2s_num]->hal), is_tx_slave, p_i2s[i2s_num]->slot_cfg );
  879. }
  880. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  881. i2s_hal_std_set_rx_slot(&(p_i2s[i2s_num]->hal), is_rx_slave, p_i2s[i2s_num]->slot_cfg );
  882. }
  883. }
  884. #if SOC_I2S_SUPPORTS_PDM
  885. else if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_PDM) {
  886. #if SOC_I2S_SUPPORTS_PDM_TX
  887. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  888. i2s_hal_pdm_set_tx_slot(&(p_i2s[i2s_num]->hal), is_tx_slave, p_i2s[i2s_num]->slot_cfg );
  889. }
  890. #endif
  891. #if SOC_I2S_SUPPORTS_PDM_RX
  892. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  893. i2s_hal_pdm_set_rx_slot(&(p_i2s[i2s_num]->hal), is_rx_slave, p_i2s[i2s_num]->slot_cfg );
  894. }
  895. #endif
  896. }
  897. #endif
  898. #if SOC_I2S_SUPPORTS_TDM
  899. else if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_TDM) {
  900. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  901. i2s_hal_tdm_set_tx_slot(&(p_i2s[i2s_num]->hal), is_tx_slave, p_i2s[i2s_num]->slot_cfg );
  902. }
  903. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  904. i2s_hal_tdm_set_rx_slot(&(p_i2s[i2s_num]->hal), is_rx_slave, p_i2s[i2s_num]->slot_cfg );
  905. }
  906. }
  907. #endif
  908. #if SOC_I2S_SUPPORTS_ADC_DAC
  909. else if ((int)p_i2s[i2s_num]->mode == I2S_COMM_MODE_ADC_DAC) {
  910. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  911. i2s_dac_set_slot_legacy();
  912. }
  913. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  914. i2s_adc_set_slot_legacy();
  915. }
  916. }
  917. #endif
  918. }
  919. static void i2s_set_clock_legacy(i2s_port_t i2s_num)
  920. {
  921. i2s_clk_config_t *clk_cfg = (i2s_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  922. i2s_clock_info_t clk_info;
  923. i2s_calculate_clock(i2s_num, &clk_info);
  924. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  925. i2s_hal_set_tx_clock(&(p_i2s[i2s_num]->hal), &clk_info, clk_cfg->clk_src);
  926. }
  927. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  928. i2s_hal_set_rx_clock(&(p_i2s[i2s_num]->hal), &clk_info, clk_cfg->clk_src);
  929. }
  930. }
  931. float i2s_get_clk(i2s_port_t i2s_num)
  932. {
  933. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  934. i2s_clk_config_t *clk_cfg = (i2s_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  935. return (float)clk_cfg->sample_rate_hz;
  936. }
  937. esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, uint32_t bits_cfg, i2s_channel_t ch)
  938. {
  939. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  940. ESP_RETURN_ON_FALSE(p_i2s[i2s_num], ESP_ERR_INVALID_ARG, TAG, "I2S%d has not installed yet", i2s_num);
  941. /* Stop I2S */
  942. i2s_stop(i2s_num);
  943. i2s_clk_config_t *clk_cfg = (i2s_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  944. i2s_slot_config_t *slot_cfg = (i2s_slot_config_t *)p_i2s[i2s_num]->slot_cfg;
  945. clk_cfg->sample_rate_hz = rate;
  946. slot_cfg->data_bit_width = bits_cfg & 0xFFFF;
  947. ESP_RETURN_ON_FALSE((slot_cfg->data_bit_width % 8 == 0), ESP_ERR_INVALID_ARG, TAG, "Invalid bits per sample");
  948. slot_cfg->slot_bit_width = (bits_cfg >> 16) > slot_cfg->data_bit_width ?
  949. (bits_cfg >> 16) : slot_cfg->data_bit_width;
  950. ESP_RETURN_ON_FALSE((slot_cfg->slot_bit_width % 8 == 0), ESP_ERR_INVALID_ARG, TAG, "Invalid bits per channel");
  951. ESP_RETURN_ON_FALSE(((int)slot_cfg->slot_bit_width <= (int)I2S_BITS_PER_SAMPLE_32BIT), ESP_ERR_INVALID_ARG, TAG, "Invalid bits per sample");
  952. slot_cfg->slot_mode = ((ch & 0xFFFF) == I2S_CHANNEL_MONO) ? I2S_SLOT_MODE_MONO : I2S_SLOT_MODE_STEREO;
  953. #if SOC_I2S_SUPPORTS_TDM
  954. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_TDM) {
  955. uint32_t slot_mask = ch >> 16;
  956. if (slot_mask == 0) {
  957. slot_mask = (slot_cfg->slot_mode == I2S_SLOT_MODE_MONO) ? 1 : 2;
  958. }
  959. ESP_RETURN_ON_FALSE(p_i2s[i2s_num]->total_slot >= i2s_get_max_channel_num(slot_mask), ESP_ERR_INVALID_ARG, TAG,
  960. "The max channel number can't be greater than CH%d\n", p_i2s[i2s_num]->total_slot);
  961. p_i2s[i2s_num]->active_slot = i2s_get_active_channel_num(slot_mask);
  962. } else
  963. #endif
  964. {
  965. p_i2s[i2s_num]->active_slot = (slot_cfg->slot_mode == I2S_SLOT_MODE_MONO) ? 1 : 2;
  966. }
  967. i2s_set_slot_legacy(i2s_num);
  968. i2s_set_clock_legacy(i2s_num);
  969. uint32_t buf_size = i2s_get_buf_size(i2s_num);
  970. bool need_realloc = buf_size != p_i2s[i2s_num]->last_buf_size;
  971. if (need_realloc) {
  972. esp_err_t ret = ESP_OK;
  973. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  974. xSemaphoreTake(p_i2s[i2s_num]->tx->mux, portMAX_DELAY);
  975. p_i2s[i2s_num]->tx->buf_size = buf_size;
  976. ret = i2s_realloc_dma_buffer(i2s_num, p_i2s[i2s_num]->tx);
  977. xQueueReset(p_i2s[i2s_num]->tx->queue);
  978. xSemaphoreGive(p_i2s[i2s_num]->tx->mux);
  979. ESP_RETURN_ON_ERROR(ret, TAG, "I2S%d tx DMA buffer malloc failed", i2s_num);
  980. }
  981. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  982. xSemaphoreTake(p_i2s[i2s_num]->rx->mux, portMAX_DELAY);
  983. p_i2s[i2s_num]->rx->buf_size = buf_size;
  984. ret = i2s_realloc_dma_buffer(i2s_num, p_i2s[i2s_num]->rx);
  985. xQueueReset(p_i2s[i2s_num]->rx->queue);
  986. xSemaphoreGive(p_i2s[i2s_num]->rx->mux);
  987. ESP_RETURN_ON_ERROR(ret, TAG, "I2S%d rx DMA buffer malloc failed", i2s_num);
  988. }
  989. }
  990. /* Update last buffer size */
  991. p_i2s[i2s_num]->last_buf_size = buf_size;
  992. /* I2S start */
  993. i2s_start(i2s_num);
  994. return ESP_OK;
  995. }
  996. esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate)
  997. {
  998. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  999. i2s_slot_config_t *slot_cfg = (i2s_slot_config_t *)p_i2s[i2s_num]->slot_cfg;
  1000. uint32_t mask = 0;
  1001. #if SOC_I2S_SUPPORTS_TDM
  1002. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_TDM) {
  1003. mask = ((i2s_tdm_slot_config_t *)slot_cfg)->slot_mask;;
  1004. }
  1005. #endif
  1006. return i2s_set_clk(i2s_num, rate, slot_cfg->data_bit_width, slot_cfg->slot_mode | (mask << 16));
  1007. }
  1008. #if SOC_I2S_SUPPORTS_PCM
  1009. esp_err_t i2s_pcm_config(i2s_port_t i2s_num, const i2s_pcm_cfg_t *pcm_cfg)
  1010. {
  1011. ESP_RETURN_ON_FALSE(p_i2s[i2s_num], ESP_FAIL, TAG, "i2s has not installed yet");
  1012. i2s_stop(i2s_num);
  1013. I2S_ENTER_CRITICAL(i2s_num);
  1014. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1015. i2s_ll_tx_set_pcm_type(p_i2s[i2s_num]->hal.dev, pcm_cfg->pcm_type);
  1016. }
  1017. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1018. i2s_ll_rx_set_pcm_type(p_i2s[i2s_num]->hal.dev, pcm_cfg->pcm_type);
  1019. }
  1020. I2S_EXIT_CRITICAL(i2s_num);
  1021. i2s_start(i2s_num);
  1022. return ESP_OK;
  1023. }
  1024. #endif
  1025. #if SOC_I2S_SUPPORTS_PDM_RX
  1026. esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t downsample)
  1027. {
  1028. ESP_RETURN_ON_FALSE(p_i2s[i2s_num], ESP_FAIL, TAG, "i2s has not installed yet");
  1029. ESP_RETURN_ON_FALSE((p_i2s[i2s_num]->mode == I2S_COMM_MODE_PDM), ESP_ERR_INVALID_ARG, TAG, "i2s mode is not PDM mode");
  1030. i2s_stop(i2s_num);
  1031. i2s_pdm_rx_slot_config_t *slot_cfg = (i2s_pdm_rx_slot_config_t*)p_i2s[i2s_num];
  1032. i2s_pdm_rx_clk_config_t *clk_cfg = (i2s_pdm_rx_clk_config_t*)p_i2s[i2s_num];
  1033. clk_cfg->dn_sample_mode = downsample;
  1034. i2s_ll_rx_set_pdm_dsr(p_i2s[i2s_num]->hal.dev, downsample);
  1035. // i2s will start in 'i2s_set_clk'
  1036. return i2s_set_clk(i2s_num, clk_cfg->sample_rate_hz, slot_cfg->data_bit_width, slot_cfg->slot_mode);
  1037. }
  1038. #endif
  1039. #if SOC_I2S_SUPPORTS_PDM_TX
  1040. esp_err_t i2s_set_pdm_tx_up_sample(i2s_port_t i2s_num, const i2s_pdm_tx_upsample_cfg_t *upsample_cfg)
  1041. {
  1042. ESP_RETURN_ON_FALSE(p_i2s[i2s_num], ESP_FAIL, TAG, "i2s has not installed yet");
  1043. ESP_RETURN_ON_FALSE((p_i2s[i2s_num]->mode == I2S_COMM_MODE_PDM) && (p_i2s[i2s_num]->dir & I2S_DIR_TX),
  1044. ESP_ERR_INVALID_ARG, TAG, "i2s mode is not PDM mode");
  1045. i2s_stop(i2s_num);
  1046. i2s_pdm_tx_clk_config_t *clk_cfg = (i2s_pdm_tx_clk_config_t *)p_i2s[i2s_num]->clk_cfg;
  1047. i2s_pdm_tx_slot_config_t *slot_cfg = (i2s_pdm_tx_slot_config_t *)p_i2s[i2s_num]->slot_cfg;
  1048. clk_cfg->up_sample_fp = upsample_cfg->fp;
  1049. clk_cfg->up_sample_fs = upsample_cfg->fs;
  1050. i2s_ll_tx_set_pdm_fpfs(p_i2s[i2s_num]->hal.dev, upsample_cfg->fp, upsample_cfg->fs);
  1051. // i2s will start in 'i2s_set_clk'
  1052. return i2s_set_clk(i2s_num, clk_cfg->sample_rate_hz, slot_cfg->data_bit_width, slot_cfg->slot_mode);
  1053. }
  1054. #endif
  1055. static esp_err_t i2s_dma_object_init(i2s_port_t i2s_num)
  1056. {
  1057. uint32_t buf_size = i2s_get_buf_size(i2s_num);
  1058. p_i2s[i2s_num]->last_buf_size = buf_size;
  1059. /* Create DMA object */
  1060. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1061. ESP_RETURN_ON_ERROR(i2s_create_dma_object(i2s_num, &p_i2s[i2s_num]->tx), TAG, "I2S TX DMA object create failed");
  1062. p_i2s[i2s_num]->tx->buf_size = buf_size;
  1063. }
  1064. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1065. ESP_RETURN_ON_ERROR(i2s_create_dma_object(i2s_num, &p_i2s[i2s_num]->rx), TAG, "I2S RX DMA object create failed");
  1066. p_i2s[i2s_num]->rx->buf_size = buf_size;
  1067. }
  1068. return ESP_OK;
  1069. }
  1070. static void i2s_mode_identify(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  1071. {
  1072. p_i2s[i2s_num]->mode = I2S_COMM_MODE_STD;
  1073. if (i2s_config->mode & I2S_MODE_MASTER) {
  1074. p_i2s[i2s_num]->role = I2S_ROLE_MASTER;
  1075. } else if (i2s_config->mode & I2S_MODE_SLAVE) {
  1076. p_i2s[i2s_num]->role = I2S_ROLE_SLAVE;
  1077. }
  1078. if (i2s_config->mode & I2S_MODE_TX) {
  1079. p_i2s[i2s_num]->dir |= I2S_DIR_TX;
  1080. }
  1081. if (i2s_config->mode & I2S_MODE_RX) {
  1082. p_i2s[i2s_num]->dir |= I2S_DIR_RX;
  1083. }
  1084. #if SOC_I2S_SUPPORTS_PDM
  1085. if (i2s_config->mode & I2S_MODE_PDM) {
  1086. p_i2s[i2s_num]->mode = I2S_COMM_MODE_PDM;
  1087. }
  1088. #endif // SOC_I2S_SUPPORTS_PDM
  1089. #if SOC_I2S_SUPPORTS_TDM
  1090. if (i2s_config->channel_format == I2S_CHANNEL_FMT_MULTIPLE) {
  1091. p_i2s[i2s_num]->mode = I2S_COMM_MODE_TDM;
  1092. }
  1093. #endif // SOC_I2S_SUPPORTS_TDM
  1094. #if SOC_I2S_SUPPORTS_ADC_DAC
  1095. if ((i2s_config->mode & I2S_MODE_DAC_BUILT_IN) ||
  1096. (i2s_config->mode & I2S_MODE_ADC_BUILT_IN)) {
  1097. p_i2s[i2s_num]->mode = (i2s_comm_mode_t)I2S_COMM_MODE_ADC_DAC;
  1098. }
  1099. #endif // SOC_I2S_SUPPORTS_ADC_DAC
  1100. }
  1101. static esp_err_t i2s_config_transfer(i2s_port_t i2s_num, const i2s_config_t *i2s_config)
  1102. {
  1103. /* Convert legacy configuration into general part of slot and clock configuration */
  1104. i2s_slot_config_t slot_cfg = {};
  1105. slot_cfg.mode = p_i2s[i2s_num]->mode;
  1106. slot_cfg.data_bit_width = i2s_config->bits_per_sample;
  1107. slot_cfg.slot_bit_width = (int)i2s_config->bits_per_chan < (int)i2s_config->bits_per_sample ?
  1108. i2s_config->bits_per_sample : i2s_config->bits_per_chan;
  1109. i2s_clk_config_t clk_cfg = {};
  1110. clk_cfg.sample_rate_hz = i2s_config->sample_rate;
  1111. clk_cfg.mclk_multiple = i2s_config->mclk_multiple == 0 ? I2S_MCLK_MULTIPLE_256 : i2s_config->mclk_multiple;
  1112. clk_cfg.clk_src = I2S_CLK_D2CLK;
  1113. p_i2s[i2s_num]->fixed_mclk = i2s_config->fixed_mclk;
  1114. p_i2s[i2s_num]->use_apll = false;
  1115. #if SOC_I2S_SUPPORTS_APLL
  1116. clk_cfg.clk_src = i2s_config->use_apll ? I2S_CLK_APLL : I2S_CLK_D2CLK;
  1117. p_i2s[i2s_num]->use_apll = i2s_config->use_apll;
  1118. #endif // SOC_I2S_SUPPORTS_APLL
  1119. /* Convert legacy configuration into particular part of slot and clock configuration */
  1120. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_STD) {
  1121. /* Generate STD slot configuration */
  1122. i2s_std_slot_config_t *std_slot = (i2s_std_slot_config_t *)calloc(1, sizeof(i2s_std_slot_config_t));
  1123. ESP_RETURN_ON_FALSE(std_slot, ESP_ERR_NO_MEM, TAG, "no memory for slot configuration struct");
  1124. memcpy(std_slot, &slot_cfg, sizeof(i2s_slot_config_t));
  1125. std_slot->slot_mode = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ?
  1126. I2S_SLOT_MODE_STEREO : I2S_SLOT_MODE_MONO;
  1127. std_slot->ws_width = i2s_config->bits_per_sample;
  1128. std_slot->ws_pol = false;
  1129. if (i2s_config->communication_format == I2S_COMM_FORMAT_STAND_I2S) {
  1130. std_slot->bit_shift = true;
  1131. }
  1132. if (i2s_config->communication_format & I2S_COMM_FORMAT_STAND_PCM_SHORT) {
  1133. std_slot->bit_shift = true;
  1134. std_slot->ws_width = 1;
  1135. std_slot->ws_pol = true;
  1136. }
  1137. #if SOC_I2S_HW_VERSION_1
  1138. std_slot->msb_right = false;
  1139. #elif SOC_I2S_HW_VERSION_2
  1140. std_slot->left_align = i2s_config->left_align;
  1141. std_slot->big_endian = i2s_config->big_edin;
  1142. std_slot->bit_order_lsb = i2s_config->bit_order_msb; // The old name is incorrect
  1143. #endif // SOC_I2S_HW_VERSION_1
  1144. p_i2s[i2s_num]->slot_cfg = std_slot;
  1145. /* Generate STD clock configuration */
  1146. i2s_std_clk_config_t *std_clk = (i2s_std_clk_config_t *)calloc(1, sizeof(i2s_std_clk_config_t));
  1147. ESP_RETURN_ON_FALSE(std_clk, ESP_ERR_NO_MEM, TAG, "no memory for clock configuration struct");
  1148. memcpy(std_clk, &clk_cfg, sizeof(i2s_clk_config_t));
  1149. p_i2s[i2s_num]->clk_cfg = std_clk;
  1150. p_i2s[i2s_num]->active_slot = (int)std_slot->slot_mode;
  1151. p_i2s[i2s_num]->total_slot = 2;
  1152. goto finish;
  1153. }
  1154. #if SOC_I2S_SUPPORTS_PDM_TX
  1155. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_PDM) {
  1156. /* Generate PDM TX slot configuration */
  1157. i2s_pdm_tx_slot_config_t *pdm_tx_slot = (i2s_pdm_tx_slot_config_t *)calloc(1, sizeof(i2s_pdm_tx_slot_config_t));
  1158. ESP_RETURN_ON_FALSE(pdm_tx_slot, ESP_ERR_NO_MEM, TAG, "no memory for slot configuration struct");
  1159. memcpy(pdm_tx_slot, &slot_cfg, sizeof(i2s_slot_config_t));
  1160. pdm_tx_slot->slot_mode = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ?
  1161. I2S_SLOT_MODE_STEREO : I2S_SLOT_MODE_MONO;
  1162. pdm_tx_slot->sd_prescale = 0;
  1163. pdm_tx_slot->sd_scale = I2S_PDM_SIG_SCALING_MUL_1;
  1164. pdm_tx_slot->hp_scale = I2S_PDM_SIG_SCALING_MUL_1;
  1165. pdm_tx_slot->lp_scale = I2S_PDM_SIG_SCALING_MUL_1;
  1166. pdm_tx_slot->sinc_scale = I2S_PDM_SIG_SCALING_MUL_1;
  1167. #if SOC_I2S_HW_VERSION_2
  1168. pdm_tx_slot->sd_en = true;
  1169. pdm_tx_slot->hp_en = true;
  1170. pdm_tx_slot->hp_cut_off_freq_hz = 49;
  1171. pdm_tx_slot->sd_dither = 0;
  1172. pdm_tx_slot->sd_dither2 = 0;
  1173. #endif // SOC_I2S_HW_VERSION_2
  1174. p_i2s[i2s_num]->slot_cfg = pdm_tx_slot;
  1175. /* Generate PDM TX clock configuration */
  1176. i2s_pdm_tx_clk_config_t *pdm_tx_clk = (i2s_pdm_tx_clk_config_t *)calloc(1, sizeof(i2s_pdm_tx_clk_config_t));
  1177. ESP_RETURN_ON_FALSE(pdm_tx_clk, ESP_ERR_NO_MEM, TAG, "no memory for clock configuration struct");
  1178. memcpy(pdm_tx_clk, &clk_cfg, sizeof(i2s_clk_config_t));
  1179. pdm_tx_clk->up_sample_fp = 960;
  1180. pdm_tx_clk->up_sample_fs = i2s_config->sample_rate / 100;
  1181. p_i2s[i2s_num]->clk_cfg = pdm_tx_clk;
  1182. p_i2s[i2s_num]->active_slot = (int)pdm_tx_slot->slot_mode;
  1183. p_i2s[i2s_num]->total_slot = 2;
  1184. goto finish;
  1185. }
  1186. #endif // SOC_I2S_SUPPORTS_PDM_TX
  1187. #if SOC_I2S_SUPPORTS_PDM_RX
  1188. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_PDM) {
  1189. /* Generate PDM RX slot configuration */
  1190. i2s_pdm_rx_slot_config_t *pdm_rx_slot = (i2s_pdm_rx_slot_config_t *)calloc(1, sizeof(i2s_pdm_rx_slot_config_t));
  1191. ESP_RETURN_ON_FALSE(pdm_rx_slot, ESP_ERR_NO_MEM, TAG, "no memory for slot configuration struct");
  1192. memcpy(pdm_rx_slot, &slot_cfg, sizeof(i2s_slot_config_t));
  1193. pdm_rx_slot->slot_mode = i2s_config->channel_format < I2S_CHANNEL_FMT_ONLY_RIGHT ?
  1194. I2S_SLOT_MODE_STEREO : I2S_SLOT_MODE_MONO;
  1195. p_i2s[i2s_num]->slot_cfg = pdm_rx_slot;
  1196. /* Generate PDM RX clock configuration */
  1197. i2s_pdm_rx_clk_config_t *pdm_rx_clk = (i2s_pdm_rx_clk_config_t *)calloc(1, sizeof(i2s_pdm_rx_clk_config_t));
  1198. ESP_RETURN_ON_FALSE(pdm_rx_clk, ESP_ERR_NO_MEM, TAG, "no memory for clock configuration struct");
  1199. memcpy(pdm_rx_clk, &clk_cfg, sizeof(i2s_clk_config_t));
  1200. pdm_rx_clk->dn_sample_mode = I2S_PDM_DSR_8S;
  1201. p_i2s[i2s_num]->clk_cfg = pdm_rx_clk;
  1202. p_i2s[i2s_num]->active_slot = (int)pdm_rx_slot->slot_mode;
  1203. p_i2s[i2s_num]->total_slot = 2;
  1204. goto finish;
  1205. }
  1206. #endif // SOC_I2S_SUPPOTYS_PDM_RX
  1207. #if SOC_I2S_SUPPORTS_TDM
  1208. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_TDM) {
  1209. /* Generate TDM slot configuration */
  1210. i2s_tdm_slot_config_t *tdm_slot = (i2s_tdm_slot_config_t *)calloc(1, sizeof(i2s_tdm_slot_config_t));
  1211. ESP_RETURN_ON_FALSE(tdm_slot, ESP_ERR_NO_MEM, TAG, "no memory for slot configuration struct");
  1212. memcpy(tdm_slot, &slot_cfg, sizeof(i2s_slot_config_t));
  1213. tdm_slot->slot_mask = i2s_config->chan_mask >> 16;
  1214. uint32_t mx_slot = i2s_get_max_channel_num(tdm_slot->slot_mask);
  1215. tdm_slot->total_slot = mx_slot < i2s_config->total_chan ? mx_slot : i2s_config->total_chan;
  1216. tdm_slot->ws_width = I2S_TDM_AUTO_WS_WIDTH;
  1217. tdm_slot->slot_mode = I2S_SLOT_MODE_STEREO;
  1218. tdm_slot->ws_pol = false;
  1219. if (i2s_config->communication_format == I2S_COMM_FORMAT_STAND_I2S) {
  1220. tdm_slot->bit_shift = true;
  1221. }
  1222. else if (i2s_config->communication_format == I2S_COMM_FORMAT_STAND_PCM_SHORT) {
  1223. tdm_slot->bit_shift = true;
  1224. tdm_slot->ws_width = 1;
  1225. tdm_slot->ws_pol = true;
  1226. }
  1227. else if (i2s_config->communication_format == I2S_COMM_FORMAT_STAND_PCM_LONG) {
  1228. tdm_slot->bit_shift = true;
  1229. tdm_slot->ws_width = tdm_slot->slot_bit_width;
  1230. tdm_slot->ws_pol = true;
  1231. }
  1232. tdm_slot->left_align = i2s_config->left_align;
  1233. tdm_slot->big_endian = i2s_config->big_edin;
  1234. tdm_slot->bit_order_lsb = i2s_config->bit_order_msb; // The old name is incorrect
  1235. tdm_slot->skip_mask = i2s_config->skip_msk;
  1236. p_i2s[i2s_num]->slot_cfg = tdm_slot;
  1237. /* Generate TDM clock configuration */
  1238. i2s_tdm_clk_config_t *tdm_clk = (i2s_tdm_clk_config_t *)calloc(1, sizeof(i2s_tdm_clk_config_t));
  1239. ESP_RETURN_ON_FALSE(tdm_clk, ESP_ERR_NO_MEM, TAG, "no memory for clock configuration struct");
  1240. memcpy(tdm_clk, &clk_cfg, sizeof(i2s_clk_config_t));
  1241. p_i2s[i2s_num]->clk_cfg = tdm_clk;
  1242. p_i2s[i2s_num]->active_slot = i2s_get_active_channel_num(tdm_slot->slot_mode);
  1243. p_i2s[i2s_num]->total_slot = tdm_slot->total_slot;
  1244. goto finish;
  1245. }
  1246. #endif // SOC_I2S_SUPPORTS_TDM
  1247. #if SOC_I2S_SUPPORTS_ADC_DAC
  1248. if ((int)p_i2s[i2s_num]->mode == I2S_COMM_MODE_ADC_DAC) {
  1249. i2s_slot_config_t *adc_dac_slot = (i2s_slot_config_t *)calloc(1, sizeof(i2s_slot_config_t));
  1250. ESP_RETURN_ON_FALSE(adc_dac_slot, ESP_ERR_NO_MEM, TAG, "no memory for slot configuration struct");
  1251. memcpy(adc_dac_slot, &slot_cfg, sizeof(i2s_slot_config_t));
  1252. p_i2s[i2s_num]->slot_cfg = adc_dac_slot;
  1253. i2s_clk_config_t *adc_dac_clk = (i2s_clk_config_t *)calloc(1, sizeof(i2s_clk_config_t));
  1254. ESP_RETURN_ON_FALSE(adc_dac_clk, ESP_ERR_NO_MEM, TAG, "no memory for clock configuration struct");
  1255. memcpy(adc_dac_clk, &clk_cfg, sizeof(i2s_clk_config_t));
  1256. p_i2s[i2s_num]->clk_cfg = adc_dac_clk;
  1257. p_i2s[i2s_num]->active_slot = (p_i2s[i2s_num]->dir & I2S_DIR_TX) ? 2 : 1;
  1258. p_i2s[i2s_num]->total_slot = 2;
  1259. }
  1260. #endif // SOC_I2S_SUPPORTS_ADC_DAC
  1261. finish:
  1262. return ESP_OK;
  1263. }
  1264. static esp_err_t i2s_init_legacy(i2s_port_t i2s_num, int intr_alloc_flag)
  1265. {
  1266. /* Create power management lock */
  1267. #ifdef CONFIG_PM_ENABLE
  1268. esp_pm_lock_type_t pm_lock = ESP_PM_APB_FREQ_MAX;
  1269. #if SOC_I2S_SUPPORTS_APLL
  1270. if (p_i2s[i2s_num]->use_apll) {
  1271. pm_lock = ESP_PM_NO_LIGHT_SLEEP;
  1272. }
  1273. #endif // SOC_I2S_SUPPORTS_APLL
  1274. ESP_RETURN_ON_ERROR(esp_pm_lock_create(pm_lock, 0, "i2s_driver", &p_i2s[i2s_num]->pm_lock), TAG, "I2S pm lock error");
  1275. #endif //CONFIG_PM_ENABLE
  1276. #if SOC_I2S_SUPPORTS_APLL
  1277. if (p_i2s[i2s_num]->use_apll) {
  1278. periph_rtc_apll_acquire();
  1279. }
  1280. #endif
  1281. /* Enable communicaiton mode */
  1282. if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_STD) {
  1283. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1284. i2s_hal_std_enable_tx_channel(&(p_i2s[i2s_num]->hal));
  1285. }
  1286. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1287. i2s_hal_std_enable_rx_channel(&(p_i2s[i2s_num]->hal));
  1288. }
  1289. }
  1290. #if SOC_I2S_SUPPORTS_PDM
  1291. else if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_PDM) {
  1292. #if SOC_I2S_SUPPORTS_PDM_TX
  1293. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1294. i2s_hal_pdm_enable_tx_channel(&(p_i2s[i2s_num]->hal));
  1295. }
  1296. #endif
  1297. #if SOC_I2S_SUPPORTS_PDM_RX
  1298. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1299. i2s_hal_pdm_enable_rx_channel(&(p_i2s[i2s_num]->hal));
  1300. }
  1301. #endif
  1302. }
  1303. #endif
  1304. #if SOC_I2S_SUPPORTS_TDM
  1305. else if (p_i2s[i2s_num]->mode == I2S_COMM_MODE_TDM) {
  1306. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1307. i2s_hal_tdm_enable_tx_channel(&(p_i2s[i2s_num]->hal));
  1308. }
  1309. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1310. i2s_hal_tdm_enable_rx_channel(&(p_i2s[i2s_num]->hal));
  1311. }
  1312. }
  1313. #endif
  1314. #if SOC_I2S_SUPPORTS_ADC_DAC
  1315. if ((int)p_i2s[i2s_num]->mode == I2S_COMM_MODE_ADC_DAC) {
  1316. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1317. adc_power_acquire();
  1318. adc_set_i2s_data_source(ADC_I2S_DATA_SRC_ADC);
  1319. i2s_ll_enable_builtin_adc(p_i2s[i2s_num]->hal.dev, true);
  1320. }
  1321. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1322. i2s_ll_enable_builtin_dac(p_i2s[i2s_num]->hal.dev, true);
  1323. }
  1324. } else {
  1325. adc_set_i2s_data_source(ADC_I2S_DATA_SRC_IO_SIG);
  1326. i2s_ll_enable_builtin_adc(p_i2s[i2s_num]->hal.dev, false);
  1327. i2s_ll_enable_builtin_dac(p_i2s[i2s_num]->hal.dev, false);
  1328. }
  1329. #endif
  1330. i2s_set_slot_legacy(i2s_num);
  1331. i2s_set_clock_legacy(i2s_num);
  1332. ESP_RETURN_ON_ERROR(i2s_dma_intr_init(i2s_num, intr_alloc_flag), TAG, "I2S interrupt initailze failed");
  1333. ESP_RETURN_ON_ERROR(i2s_dma_object_init(i2s_num), TAG, "I2S dma object create failed");
  1334. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1335. ESP_RETURN_ON_ERROR(i2s_realloc_dma_buffer(i2s_num, p_i2s[i2s_num]->tx), TAG, "Allocate I2S dma tx buffer failed");
  1336. }
  1337. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1338. ESP_RETURN_ON_ERROR(i2s_realloc_dma_buffer(i2s_num, p_i2s[i2s_num]->rx), TAG, "Allocate I2S dma rx buffer failed");
  1339. }
  1340. /* Initialize I2S DMA object */
  1341. #if SOC_I2S_HW_VERSION_2
  1342. /* Enable tx/rx submodule clock */
  1343. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1344. i2s_ll_tx_enable_clock(p_i2s[i2s_num]->hal.dev);
  1345. }
  1346. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1347. i2s_ll_rx_enable_clock(p_i2s[i2s_num]->hal.dev);
  1348. }
  1349. #endif
  1350. return ESP_OK;
  1351. }
  1352. esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num)
  1353. {
  1354. ESP_RETURN_ON_FALSE(i2s_num < I2S_NUM_MAX, ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  1355. ESP_RETURN_ON_FALSE(p_i2s[i2s_num], ESP_ERR_INVALID_STATE, TAG, "I2S port %d has not installed", i2s_num);
  1356. i2s_obj_t *obj = p_i2s[i2s_num];
  1357. i2s_stop(i2s_num);
  1358. #if SOC_I2S_SUPPORTS_ADC_DAC
  1359. if ((int)(obj->mode) == I2S_COMM_MODE_ADC_DAC) {
  1360. if (obj->dir & I2S_DIR_TX) {
  1361. // Deinit DAC
  1362. i2s_set_dac_mode(I2S_DAC_CHANNEL_DISABLE);
  1363. }
  1364. if (obj->dir & I2S_DIR_RX) {
  1365. // Deinit ADC
  1366. adc_set_i2s_data_source(ADC_I2S_DATA_SRC_IO_SIG);
  1367. adc_power_release();
  1368. }
  1369. }
  1370. #endif
  1371. #if SOC_GDMA_SUPPORTED
  1372. if (obj->tx_dma_chan) {
  1373. gdma_disconnect(obj->tx_dma_chan);
  1374. gdma_del_channel(obj->tx_dma_chan);
  1375. }
  1376. if (obj->rx_dma_chan) {
  1377. gdma_disconnect(obj->rx_dma_chan);
  1378. gdma_del_channel(obj->rx_dma_chan);
  1379. }
  1380. #else
  1381. if (obj->i2s_isr_handle) {
  1382. esp_intr_free(obj->i2s_isr_handle);
  1383. }
  1384. #endif
  1385. /* Destroy dma object if exist */
  1386. i2s_destroy_dma_object(i2s_num, &obj->tx);
  1387. i2s_destroy_dma_object(i2s_num, &obj->rx);
  1388. if (obj->i2s_queue) {
  1389. vQueueDelete(obj->i2s_queue);
  1390. obj->i2s_queue = NULL;
  1391. }
  1392. #if SOC_I2S_SUPPORTS_APLL
  1393. if (obj->use_apll) {
  1394. // switch back to PLL clock source
  1395. if (obj->dir & I2S_DIR_TX) {
  1396. i2s_ll_tx_clk_set_src(obj->hal.dev, I2S_CLK_D2CLK);
  1397. }
  1398. if (obj->dir & I2S_DIR_RX) {
  1399. i2s_ll_rx_clk_set_src(obj->hal.dev, I2S_CLK_D2CLK);
  1400. }
  1401. periph_rtc_apll_release();
  1402. }
  1403. #endif
  1404. #ifdef CONFIG_PM_ENABLE
  1405. if (obj->pm_lock) {
  1406. esp_pm_lock_delete(obj->pm_lock);
  1407. obj->pm_lock = NULL;
  1408. }
  1409. #endif
  1410. #if SOC_I2S_HW_VERSION_2
  1411. if (obj->dir & I2S_DIR_TX) {
  1412. i2s_ll_tx_disable_clock(obj->hal.dev);
  1413. }
  1414. if (obj->dir & I2S_DIR_RX) {
  1415. i2s_ll_rx_disable_clock(obj->hal.dev);
  1416. }
  1417. #endif
  1418. /* Disable module clock */
  1419. i2s_priv_deregister_object(i2s_num);
  1420. if (obj->clk_cfg) {
  1421. free(obj->clk_cfg);
  1422. obj->clk_cfg = NULL;
  1423. }
  1424. if (obj->slot_cfg) {
  1425. free(obj->slot_cfg);
  1426. obj->slot_cfg = NULL;
  1427. }
  1428. free(obj);
  1429. p_i2s[i2s_num] = NULL;
  1430. return ESP_OK;
  1431. }
  1432. esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void *i2s_queue)
  1433. {
  1434. esp_err_t ret = ESP_OK;
  1435. /* Step 1: Check the validity of input parameters */
  1436. ESP_RETURN_ON_ERROR(i2s_check_cfg_validity(i2s_num, i2s_config), TAG, "I2S configuration is invalid");
  1437. /* Step 2: Allocate driver object and register to platform */
  1438. i2s_obj_t *i2s_obj = calloc(1, sizeof(i2s_obj_t));
  1439. ESP_RETURN_ON_FALSE(i2s_obj, ESP_ERR_NO_MEM, TAG, "no mem for I2S driver");
  1440. if (i2s_priv_register_object(i2s_obj, i2s_num) != ESP_OK) {
  1441. free(i2s_obj);
  1442. ESP_LOGE(TAG, "register I2S object to platform failed");
  1443. return ESP_ERR_INVALID_STATE;
  1444. }
  1445. i2s_hal_init(&i2s_obj->hal, i2s_num);
  1446. /* Step 3: Store and assign configarations */
  1447. i2s_mode_identify(i2s_num, i2s_config);
  1448. ESP_GOTO_ON_ERROR(i2s_config_transfer(i2s_num, i2s_config), err, TAG, "I2S install failed");
  1449. i2s_obj->dma_desc_num = i2s_config->dma_desc_num;
  1450. i2s_obj->dma_frame_num = i2s_config->dma_frame_num;
  1451. i2s_obj->tx_desc_auto_clear = i2s_config->tx_desc_auto_clear;
  1452. /* Step 4: Apply configurations and init hardware */
  1453. ESP_GOTO_ON_ERROR(i2s_init_legacy(i2s_num, i2s_config->intr_alloc_flags), err, TAG, "I2S init failed");
  1454. /* Step 5: Initialise i2s event queue if user needs */
  1455. if (i2s_queue) {
  1456. i2s_obj->i2s_queue = xQueueCreate(queue_size, sizeof(i2s_event_t));
  1457. ESP_GOTO_ON_FALSE(i2s_obj->i2s_queue, ESP_ERR_NO_MEM, err, TAG, "I2S queue create failed");
  1458. *((QueueHandle_t *) i2s_queue) = i2s_obj->i2s_queue;
  1459. ESP_LOGI(TAG, "queue free spaces: %d", uxQueueSpacesAvailable(i2s_obj->i2s_queue));
  1460. } else {
  1461. i2s_obj->i2s_queue = NULL;
  1462. }
  1463. /* Step 6: Start I2S for backward compatibility */
  1464. ESP_GOTO_ON_ERROR(i2s_start(i2s_num), err, TAG, "I2S start failed");
  1465. return ESP_OK;
  1466. err:
  1467. /* I2S install failed, prepare to uninstall */
  1468. i2s_driver_uninstall(i2s_num);
  1469. return ret;
  1470. }
  1471. 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)
  1472. {
  1473. char *data_ptr;
  1474. char *src_byte;
  1475. size_t bytes_can_write;
  1476. *bytes_written = 0;
  1477. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  1478. ESP_RETURN_ON_FALSE((p_i2s[i2s_num]->tx), ESP_ERR_INVALID_ARG, TAG, "TX mode is not enabled");
  1479. xSemaphoreTake(p_i2s[i2s_num]->tx->mux, portMAX_DELAY);
  1480. #ifdef CONFIG_PM_ENABLE
  1481. esp_pm_lock_acquire(p_i2s[i2s_num]->pm_lock);
  1482. #endif
  1483. src_byte = (char *)src;
  1484. while (size > 0) {
  1485. if (p_i2s[i2s_num]->tx->rw_pos == p_i2s[i2s_num]->tx->buf_size || p_i2s[i2s_num]->tx->curr_ptr == NULL) {
  1486. if (xQueueReceive(p_i2s[i2s_num]->tx->queue, &p_i2s[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1487. break;
  1488. }
  1489. p_i2s[i2s_num]->tx->rw_pos = 0;
  1490. }
  1491. ESP_LOGD(TAG, "size: %d, rw_pos: %d, buf_size: %d, curr_ptr: %d", size, p_i2s[i2s_num]->tx->rw_pos, p_i2s[i2s_num]->tx->buf_size, (int)p_i2s[i2s_num]->tx->curr_ptr);
  1492. data_ptr = (char *)p_i2s[i2s_num]->tx->curr_ptr;
  1493. data_ptr += p_i2s[i2s_num]->tx->rw_pos;
  1494. bytes_can_write = p_i2s[i2s_num]->tx->buf_size - p_i2s[i2s_num]->tx->rw_pos;
  1495. if (bytes_can_write > size) {
  1496. bytes_can_write = size;
  1497. }
  1498. memcpy(data_ptr, src_byte, bytes_can_write);
  1499. size -= bytes_can_write;
  1500. src_byte += bytes_can_write;
  1501. p_i2s[i2s_num]->tx->rw_pos += bytes_can_write;
  1502. (*bytes_written) += bytes_can_write;
  1503. }
  1504. #ifdef CONFIG_PM_ENABLE
  1505. esp_pm_lock_release(p_i2s[i2s_num]->pm_lock);
  1506. #endif
  1507. xSemaphoreGive(p_i2s[i2s_num]->tx->mux);
  1508. return ESP_OK;
  1509. }
  1510. 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)
  1511. {
  1512. char *data_ptr;
  1513. int bytes_can_write;
  1514. int tail;
  1515. int src_bytes;
  1516. int aim_bytes;
  1517. int zero_bytes;
  1518. *bytes_written = 0;
  1519. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  1520. ESP_RETURN_ON_FALSE((size > 0), ESP_ERR_INVALID_ARG, TAG, "size must greater than zero");
  1521. ESP_RETURN_ON_FALSE((aim_bits >= src_bits), ESP_ERR_INVALID_ARG, TAG, "aim_bits mustn't be less than src_bits");
  1522. ESP_RETURN_ON_FALSE((p_i2s[i2s_num]->tx), ESP_ERR_INVALID_ARG, TAG, "TX mode is not enabled");
  1523. if (src_bits < I2S_BITS_PER_SAMPLE_8BIT || aim_bits < I2S_BITS_PER_SAMPLE_8BIT) {
  1524. ESP_LOGE(TAG, "bits mustn't be less than 8, src_bits %d aim_bits %d", src_bits, aim_bits);
  1525. return ESP_ERR_INVALID_ARG;
  1526. }
  1527. if (src_bits > I2S_BITS_PER_SAMPLE_32BIT || aim_bits > I2S_BITS_PER_SAMPLE_32BIT) {
  1528. ESP_LOGE(TAG, "bits mustn't be greater than 32, src_bits %d aim_bits %d", src_bits, aim_bits);
  1529. return ESP_ERR_INVALID_ARG;
  1530. }
  1531. if ((src_bits == I2S_BITS_PER_SAMPLE_16BIT || src_bits == I2S_BITS_PER_SAMPLE_32BIT) && (size % 2 != 0)) {
  1532. ESP_LOGE(TAG, "size must be a even number while src_bits is even, src_bits %d size %d", src_bits, size);
  1533. return ESP_ERR_INVALID_ARG;
  1534. }
  1535. if (src_bits == I2S_BITS_PER_SAMPLE_24BIT && (size % 3 != 0)) {
  1536. ESP_LOGE(TAG, "size must be a multiple of 3 while src_bits is 24, size %d", size);
  1537. return ESP_ERR_INVALID_ARG;
  1538. }
  1539. src_bytes = src_bits / 8;
  1540. aim_bytes = aim_bits / 8;
  1541. zero_bytes = aim_bytes - src_bytes;
  1542. xSemaphoreTake(p_i2s[i2s_num]->tx->mux, portMAX_DELAY);
  1543. size = size * aim_bytes / src_bytes;
  1544. ESP_LOGD(TAG, "aim_bytes %d src_bytes %d size %d", aim_bytes, src_bytes, size);
  1545. while (size > 0) {
  1546. if (p_i2s[i2s_num]->tx->rw_pos == p_i2s[i2s_num]->tx->buf_size || p_i2s[i2s_num]->tx->curr_ptr == NULL) {
  1547. if (xQueueReceive(p_i2s[i2s_num]->tx->queue, &p_i2s[i2s_num]->tx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1548. break;
  1549. }
  1550. p_i2s[i2s_num]->tx->rw_pos = 0;
  1551. }
  1552. data_ptr = (char *)p_i2s[i2s_num]->tx->curr_ptr;
  1553. data_ptr += p_i2s[i2s_num]->tx->rw_pos;
  1554. bytes_can_write = p_i2s[i2s_num]->tx->buf_size - p_i2s[i2s_num]->tx->rw_pos;
  1555. if (bytes_can_write > (int)size) {
  1556. bytes_can_write = size;
  1557. }
  1558. tail = bytes_can_write % aim_bytes;
  1559. bytes_can_write = bytes_can_write - tail;
  1560. memset(data_ptr, 0, bytes_can_write);
  1561. for (int j = 0; j < bytes_can_write; j += (aim_bytes - zero_bytes)) {
  1562. j += zero_bytes;
  1563. memcpy(&data_ptr[j], (const char *)(src + *bytes_written), aim_bytes - zero_bytes);
  1564. (*bytes_written) += (aim_bytes - zero_bytes);
  1565. }
  1566. size -= bytes_can_write;
  1567. p_i2s[i2s_num]->tx->rw_pos += bytes_can_write;
  1568. }
  1569. xSemaphoreGive(p_i2s[i2s_num]->tx->mux);
  1570. return ESP_OK;
  1571. }
  1572. esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_read, TickType_t ticks_to_wait)
  1573. {
  1574. char *data_ptr;;
  1575. char*dest_byte;
  1576. int bytes_can_read;
  1577. *bytes_read = 0;
  1578. dest_byte = (char *)dest;
  1579. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  1580. ESP_RETURN_ON_FALSE((p_i2s[i2s_num]->rx), ESP_ERR_INVALID_ARG, TAG, "RX mode is not enabled");
  1581. xSemaphoreTake(p_i2s[i2s_num]->rx->mux, portMAX_DELAY);
  1582. #ifdef CONFIG_PM_ENABLE
  1583. esp_pm_lock_acquire(p_i2s[i2s_num]->pm_lock);
  1584. #endif
  1585. while (size > 0) {
  1586. if (p_i2s[i2s_num]->rx->rw_pos == p_i2s[i2s_num]->rx->buf_size || p_i2s[i2s_num]->rx->curr_ptr == NULL) {
  1587. if (xQueueReceive(p_i2s[i2s_num]->rx->queue, &p_i2s[i2s_num]->rx->curr_ptr, ticks_to_wait) == pdFALSE) {
  1588. break;
  1589. }
  1590. p_i2s[i2s_num]->rx->rw_pos = 0;
  1591. }
  1592. data_ptr = (char *)p_i2s[i2s_num]->rx->curr_ptr;
  1593. data_ptr += p_i2s[i2s_num]->rx->rw_pos;
  1594. bytes_can_read = p_i2s[i2s_num]->rx->buf_size - p_i2s[i2s_num]->rx->rw_pos;
  1595. if (bytes_can_read > (int)size) {
  1596. bytes_can_read = size;
  1597. }
  1598. memcpy(dest_byte, data_ptr, bytes_can_read);
  1599. size -= bytes_can_read;
  1600. dest_byte += bytes_can_read;
  1601. p_i2s[i2s_num]->rx->rw_pos += bytes_can_read;
  1602. (*bytes_read) += bytes_can_read;
  1603. }
  1604. #ifdef CONFIG_PM_ENABLE
  1605. esp_pm_lock_release(p_i2s[i2s_num]->pm_lock);
  1606. #endif
  1607. xSemaphoreGive(p_i2s[i2s_num]->rx->mux);
  1608. return ESP_OK;
  1609. }
  1610. /*-------------------------------------------------------------
  1611. I2S GPIO operation
  1612. -------------------------------------------------------------*/
  1613. static void gpio_matrix_out_check_and_set(gpio_num_t gpio, uint32_t signal_idx, bool out_inv, bool oen_inv)
  1614. {
  1615. //if pin = -1, do not need to configure
  1616. if (gpio != -1) {
  1617. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  1618. gpio_set_direction(gpio, GPIO_MODE_OUTPUT);
  1619. esp_rom_gpio_connect_out_signal(gpio, signal_idx, out_inv, oen_inv);
  1620. }
  1621. }
  1622. static void gpio_matrix_in_check_and_set(gpio_num_t gpio, uint32_t signal_idx, bool inv)
  1623. {
  1624. if (gpio != -1) {
  1625. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[gpio], PIN_FUNC_GPIO);
  1626. /* Set direction, for some GPIOs, the input function are not enabled as default */
  1627. gpio_set_direction(gpio, GPIO_MODE_INPUT);
  1628. esp_rom_gpio_connect_in_signal(gpio, signal_idx, inv);
  1629. }
  1630. }
  1631. static esp_err_t i2s_check_set_mclk(i2s_port_t i2s_num, gpio_num_t gpio_num)
  1632. {
  1633. if (gpio_num == -1) {
  1634. return ESP_OK;
  1635. }
  1636. #if CONFIG_IDF_TARGET_ESP32
  1637. ESP_RETURN_ON_FALSE((gpio_num == GPIO_NUM_0 || gpio_num == GPIO_NUM_1 || gpio_num == GPIO_NUM_3),
  1638. ESP_ERR_INVALID_ARG, TAG,
  1639. "ESP32 only support to set GPIO0/GPIO1/GPIO3 as mclk signal, error GPIO number:%d", gpio_num);
  1640. bool is_i2s0 = i2s_num == I2S_NUM_0;
  1641. if (gpio_num == GPIO_NUM_0) {
  1642. PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
  1643. WRITE_PERI_REG(PIN_CTRL, is_i2s0 ? 0xFFF0 : 0xFFFF);
  1644. } else if (gpio_num == GPIO_NUM_1) {
  1645. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0TXD_U, FUNC_U0TXD_CLK_OUT3);
  1646. WRITE_PERI_REG(PIN_CTRL, is_i2s0 ? 0xF0F0 : 0xF0FF);
  1647. } else {
  1648. PIN_FUNC_SELECT(PERIPHS_IO_MUX_U0RXD_U, FUNC_U0RXD_CLK_OUT2);
  1649. WRITE_PERI_REG(PIN_CTRL, is_i2s0 ? 0xFF00 : 0xFF0F);
  1650. }
  1651. #else
  1652. ESP_RETURN_ON_FALSE(GPIO_IS_VALID_GPIO(gpio_num), ESP_ERR_INVALID_ARG, TAG, "mck_io_num invalid");
  1653. gpio_matrix_out_check_and_set(gpio_num, i2s_periph_signal[i2s_num].mck_out_sig, 0, 0);
  1654. #endif
  1655. ESP_LOGI(TAG, "I2S%d, MCLK output by GPIO%d", i2s_num, gpio_num);
  1656. return ESP_OK;
  1657. }
  1658. esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num)
  1659. {
  1660. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  1661. uint32_t buf_cnt = p_i2s[i2s_num]->dma_desc_num;
  1662. /* Clear I2S RX DMA buffer */
  1663. if (p_i2s[i2s_num]->rx && p_i2s[i2s_num]->rx->buf != NULL && p_i2s[i2s_num]->rx->buf_size != 0) {
  1664. for (int i = 0; i < buf_cnt; i++) {
  1665. memset(p_i2s[i2s_num]->rx->buf[i], 0, p_i2s[i2s_num]->rx->buf_size);
  1666. }
  1667. }
  1668. /* Clear I2S TX DMA buffer */
  1669. if (p_i2s[i2s_num]->tx && p_i2s[i2s_num]->tx->buf != NULL && p_i2s[i2s_num]->tx->buf_size != 0) {
  1670. /* Finish to write all tx data */
  1671. int bytes_left = (p_i2s[i2s_num]->tx->buf_size - p_i2s[i2s_num]->tx->rw_pos) % 4;
  1672. if (bytes_left) {
  1673. size_t zero_bytes = 0;
  1674. size_t bytes_written;
  1675. i2s_write(i2s_num, (void *)&zero_bytes, bytes_left, &bytes_written, portMAX_DELAY);
  1676. }
  1677. for (int i = 0; i < buf_cnt; i++) {
  1678. memset(p_i2s[i2s_num]->tx->buf[i], 0, p_i2s[i2s_num]->tx->buf_size);
  1679. }
  1680. }
  1681. return ESP_OK;
  1682. }
  1683. esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin)
  1684. {
  1685. ESP_RETURN_ON_FALSE((i2s_num < I2S_NUM_MAX), ESP_ERR_INVALID_ARG, TAG, "i2s_num error");
  1686. if (pin == NULL) {
  1687. #if SOC_I2S_SUPPORTS_DAC
  1688. return i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  1689. #else
  1690. return ESP_ERR_INVALID_ARG;
  1691. #endif
  1692. }
  1693. /* Check validity of selected pins */
  1694. ESP_RETURN_ON_FALSE((pin->bck_io_num == -1 || GPIO_IS_VALID_GPIO(pin->bck_io_num)),
  1695. ESP_ERR_INVALID_ARG, TAG, "bck_io_num invalid");
  1696. ESP_RETURN_ON_FALSE((pin->ws_io_num == -1 || GPIO_IS_VALID_GPIO(pin->ws_io_num)),
  1697. ESP_ERR_INVALID_ARG, TAG, "ws_io_num invalid");
  1698. ESP_RETURN_ON_FALSE((pin->data_out_num == -1 || GPIO_IS_VALID_GPIO(pin->data_out_num)),
  1699. ESP_ERR_INVALID_ARG, TAG, "data_out_num invalid");
  1700. ESP_RETURN_ON_FALSE((pin->data_in_num == -1 || GPIO_IS_VALID_GPIO(pin->data_in_num)),
  1701. ESP_ERR_INVALID_ARG, TAG, "data_in_num invalid");
  1702. if (p_i2s[i2s_num]->role == I2S_ROLE_SLAVE) {
  1703. /* For "tx + rx + slave" or "rx + slave" mode, we should select RX signal index for ws and bck */
  1704. if (p_i2s[i2s_num]->dir & I2S_DIR_RX) {
  1705. gpio_matrix_in_check_and_set(pin->ws_io_num, i2s_periph_signal[i2s_num].s_rx_ws_sig, 0);
  1706. gpio_matrix_in_check_and_set(pin->bck_io_num, i2s_periph_signal[i2s_num].s_rx_bck_sig, 0);
  1707. /* For "tx + slave" mode, we should select TX signal index for ws and bck */
  1708. } else {
  1709. gpio_matrix_in_check_and_set(pin->ws_io_num, i2s_periph_signal[i2s_num].s_tx_ws_sig, 0);
  1710. gpio_matrix_in_check_and_set(pin->bck_io_num, i2s_periph_signal[i2s_num].s_tx_bck_sig, 0);
  1711. }
  1712. } else {
  1713. /* mclk only available in master mode */
  1714. ESP_RETURN_ON_ERROR(i2s_check_set_mclk(i2s_num, pin->mck_io_num), TAG, "mclk config failed");
  1715. /* For "tx + rx + master" or "tx + master" mode, we should select TX signal index for ws and bck */
  1716. if (p_i2s[i2s_num]->dir & I2S_DIR_TX) {
  1717. gpio_matrix_out_check_and_set(pin->ws_io_num, i2s_periph_signal[i2s_num].m_tx_ws_sig, 0, 0);
  1718. gpio_matrix_out_check_and_set(pin->bck_io_num, i2s_periph_signal[i2s_num].m_tx_bck_sig, 0, 0);
  1719. /* For "rx + master" mode, we should select RX signal index for ws and bck */
  1720. } else {
  1721. gpio_matrix_out_check_and_set(pin->ws_io_num, i2s_periph_signal[i2s_num].m_rx_ws_sig, 0, 0);
  1722. gpio_matrix_out_check_and_set(pin->bck_io_num, i2s_periph_signal[i2s_num].m_rx_bck_sig, 0, 0);
  1723. }
  1724. }
  1725. /* Set data input/ouput GPIO */
  1726. gpio_matrix_out_check_and_set(pin->data_out_num, i2s_periph_signal[i2s_num].data_out_sig, 0, 0);
  1727. gpio_matrix_in_check_and_set(pin->data_in_num, i2s_periph_signal[i2s_num].data_in_sig, 0);
  1728. return ESP_OK;
  1729. }
  1730. esp_err_t i2s_priv_register_object(void *driver_obj, int port_id)
  1731. {
  1732. esp_err_t ret = ESP_ERR_NOT_FOUND;
  1733. ESP_RETURN_ON_FALSE(driver_obj && (port_id < SOC_I2S_NUM), ESP_ERR_INVALID_ARG, TAG, "invalid arguments");
  1734. portENTER_CRITICAL(&i2s_platform_spinlock);
  1735. if (!p_i2s[port_id]) {
  1736. ret = ESP_OK;
  1737. p_i2s[port_id] = driver_obj;
  1738. periph_module_enable(i2s_periph_signal[port_id].module);
  1739. }
  1740. portEXIT_CRITICAL(&i2s_platform_spinlock);
  1741. return ret;
  1742. }
  1743. esp_err_t i2s_priv_deregister_object(int port_id)
  1744. {
  1745. esp_err_t ret = ESP_ERR_INVALID_STATE;
  1746. ESP_RETURN_ON_FALSE(port_id < SOC_I2S_NUM, ESP_ERR_INVALID_ARG, TAG, "invalid arguments");
  1747. portENTER_CRITICAL(&i2s_platform_spinlock);
  1748. if (p_i2s[port_id]) {
  1749. ret = ESP_OK;
  1750. p_i2s[port_id] = NULL;
  1751. periph_module_disable(i2s_periph_signal[port_id].module);
  1752. }
  1753. portEXIT_CRITICAL(&i2s_platform_spinlock);
  1754. return ret;
  1755. }