i2s_legacy.c 76 KB

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