i2s_legacy.c 77 KB

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