i2s.c 78 KB

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