5
5
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (5 , 0 , 0 )
6
6
#include "driver/i2s_std.h"
7
7
#include "driver/i2s_tdm.h"
8
+ #include "driver/i2s_pdm.h"
8
9
#include "soc/soc_caps.h"
9
10
#else
10
11
#include "driver/i2s.h"
@@ -102,7 +103,12 @@ static int _i2c_init(uint8_t port)
102
103
103
104
void * get_i2c_bus_handle (uint8_t port )
104
105
{
105
- ESP_LOGI (TAG , "Get mater handle %d %p" , port , i2c_bus_handle [port ]);
106
+ #if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (5 , 4 , 0 )
107
+ // Try to get port from I2C driver directly
108
+ i2c_master_bus_handle_t bus_handle = NULL ;
109
+ i2c_master_get_bus_handle (port , & bus_handle );
110
+ return bus_handle ;
111
+ #endif
106
112
return i2c_bus_handle [port ];
107
113
}
108
114
@@ -182,6 +188,7 @@ static int _i2s_init(uint8_t port, esp_codec_dev_type_t dev_type, codec_init_cfg
182
188
};
183
189
tdm_cfg .slot_cfg .total_slot = 4 ;
184
190
#endif
191
+ chan_cfg .id = I2S_NUM_AUTO ; // Use auto ID
185
192
int ret = i2s_new_channel (& chan_cfg , output == false ? NULL : & i2s_keep [port ]-> tx_handle ,
186
193
input == false ? NULL : & i2s_keep [port ]-> rx_handle );
187
194
ESP_LOGI (TAG , "tx:%p rx:%p" , i2s_keep [port ]-> tx_handle , i2s_keep [port ]-> rx_handle );
@@ -195,6 +202,23 @@ static int _i2s_init(uint8_t port, esp_codec_dev_type_t dev_type, codec_init_cfg
195
202
ret = i2s_channel_init_tdm_mode (i2s_keep [port ]-> tx_handle , & tdm_cfg );
196
203
ESP_LOGI (TAG , "output init tdm ret %d" , ret );
197
204
}
205
+ #endif
206
+ #ifdef SOC_I2S_SUPPORTS_PDM_TX
207
+ else if (init_cfg -> out_mode == CODEC_I2S_MODE_PDM ) {
208
+ i2s_pdm_tx_config_t pdm_cfg = {
209
+ .clk_cfg = I2S_PDM_TX_CLK_DEFAULT_CONFIG (16000 ),
210
+ .slot_cfg = I2S_PDM_TX_SLOT_DEFAULT_CONFIG (16 , I2S_SLOT_MODE_STEREO ),
211
+ .gpio_cfg = {
212
+ .dout = i2s_cfg .dout ,
213
+ .clk = i2s_cfg .bclk ,
214
+ .invert_flags = {
215
+ .clk_inv = false,
216
+ },
217
+ },
218
+ };
219
+ ret = i2s_channel_init_pdm_tx_mode (i2s_keep [port ]-> tx_handle , & pdm_cfg );
220
+ ESP_LOGI (TAG , "output init pdm ret %d" , ret );
221
+ }
198
222
#endif
199
223
}
200
224
if (i2s_keep [port ]-> rx_handle ) {
@@ -207,6 +231,23 @@ static int _i2s_init(uint8_t port, esp_codec_dev_type_t dev_type, codec_init_cfg
207
231
ret = i2s_channel_init_tdm_mode (i2s_keep [port ]-> rx_handle , & tdm_cfg );
208
232
ESP_LOGI (TAG , "Input init tdm ret %d" , ret );
209
233
}
234
+ #endif
235
+ #ifdef SOC_I2S_SUPPORTS_PDM_RX
236
+ else if (init_cfg -> in_mode == CODEC_I2S_MODE_PDM ) {
237
+ i2s_pdm_rx_config_t pdm_cfg = {
238
+ .clk_cfg = I2S_PDM_RX_CLK_DEFAULT_CONFIG (16000 ),
239
+ .slot_cfg = I2S_PDM_RX_SLOT_DEFAULT_CONFIG (16 , I2S_SLOT_MODE_STEREO ),
240
+ .gpio_cfg = {
241
+ .din = i2s_cfg .din ,
242
+ .clk = i2s_cfg .bclk ,
243
+ .invert_flags = {
244
+ .clk_inv = false,
245
+ },
246
+ },
247
+ };
248
+ ret = i2s_channel_init_pdm_rx_mode (i2s_keep [port ]-> rx_handle , & pdm_cfg );
249
+ ESP_LOGI (TAG , "Input init pdm ret %d" , ret );
250
+ }
210
251
#endif
211
252
}
212
253
// Enable I2S here for maybe some codec need I2S clock to set register correctly
@@ -275,6 +316,28 @@ static int _i2s_deinit(uint8_t port)
275
316
return 0 ;
276
317
}
277
318
319
+ int init_i2c (uint8_t port )
320
+ {
321
+ return _i2c_init (port );
322
+ }
323
+
324
+ int deinit_i2c (uint8_t port )
325
+ {
326
+ return _i2c_deinit (port );
327
+ }
328
+
329
+ static int check_i2c_inited (int8_t port )
330
+ {
331
+ if (port < 0 ) {
332
+ return 0 ;
333
+ }
334
+ // Already installed
335
+ if (get_i2c_bus_handle (port )) {
336
+ return 0 ;
337
+ }
338
+ return _i2c_init (port );
339
+ }
340
+
278
341
int init_codec (codec_init_cfg_t * cfg )
279
342
{
280
343
if (cfg == NULL ) {
@@ -299,13 +362,13 @@ int init_codec(codec_init_cfg_t *cfg)
299
362
ESP_LOGE (TAG , "No codec device found" );
300
363
return -1 ;
301
364
}
302
- // Force to init I2C firstly
303
- _i2c_init (0 );
365
+ // Try to get I2C handle
366
+ check_i2c_inited (0 );
304
367
// Init i2c and i2s
305
368
bool same_i2s = (has_in && has_out && out_cfg .i2s_port == in_cfg .i2s_port );
306
369
ESP_LOGI (TAG , "in:%d out:%d port: %d" , has_in , has_out , out_cfg .i2s_port == in_cfg .i2s_port );
307
370
if (has_out ) {
308
- if (out_cfg .i2c_port >= 0 && _i2c_init ( out_cfg . i2c_port ) ) {
371
+ if (check_i2c_inited ( out_cfg .i2c_port ) < 0 ) {
309
372
ESP_LOGE (TAG , "Fail to int i2c: %d" , out_cfg .i2c_port );
310
373
return -1 ;
311
374
}
@@ -317,7 +380,7 @@ int init_codec(codec_init_cfg_t *cfg)
317
380
ESP_LOGI (TAG , "Success to init i2s: %d" , in_cfg .i2s_port );
318
381
}
319
382
if (has_in ) {
320
- if (in_cfg .i2c_port >= 0 && _i2c_init ( in_cfg . i2c_port ) ) {
383
+ if (check_i2c_inited ( in_cfg .i2c_port ) < 0 ) {
321
384
ESP_LOGE (TAG , "Fail to int i2c: %d" , in_cfg .i2c_port );
322
385
return -1 ;
323
386
}
@@ -346,7 +409,7 @@ int init_codec(codec_init_cfg_t *cfg)
346
409
audio_codec_i2c_cfg_t i2c_cfg = {
347
410
.port = out_cfg .i2c_port ,
348
411
#ifdef USE_I2C_MASTER
349
- .bus_handle = i2c_bus_handle [ out_cfg .i2c_port ] ,
412
+ .bus_handle = get_i2c_bus_handle ( out_cfg .i2c_port ) ,
350
413
#endif
351
414
};
352
415
// TODO add other codec support
@@ -420,7 +483,7 @@ int init_codec(codec_init_cfg_t *cfg)
420
483
audio_codec_i2c_cfg_t i2c_cfg = {
421
484
.port = in_cfg .i2c_port ,
422
485
#ifdef USE_I2C_MASTER
423
- .bus_handle = i2c_bus_handle [ in_cfg .i2c_port ] ,
486
+ .bus_handle = get_i2c_bus_handle ( in_cfg .i2c_port ) ,
424
487
#endif
425
488
};
426
489
// TODO add other codec support
@@ -464,12 +527,18 @@ int init_codec(codec_init_cfg_t *cfg)
464
527
};
465
528
codec_res .record_dev = esp_codec_dev_new (& dev_cfg );
466
529
}
467
- int ret = esp_codec_dev_set_out_vol (codec_res .play_dev , 60.0 );
468
- ret = esp_codec_dev_set_in_gain (codec_res .record_dev , 30.0 );
469
- if (ret == 0 ) {
530
+ // Set default volume and gain for play and record
531
+ if (codec_res .play_dev ) {
532
+ esp_codec_dev_set_out_vol (codec_res .play_dev , 60.0 );
533
+ }
534
+ if (codec_res .record_dev ) {
535
+ esp_codec_dev_set_in_gain (codec_res .record_dev , 30.0 );
536
+ }
537
+ if ((codec_res .play_dev != NULL ) || (codec_res .record_dev != NULL )) {
470
538
codec_res .inited = true;
539
+ return 0 ;
471
540
}
472
- return ret ;
541
+ return -1 ;
473
542
}
474
543
475
544
esp_codec_dev_handle_t get_playback_handle (void )
@@ -554,6 +623,17 @@ static void enable_mmc_phy_power(void)
554
623
#endif
555
624
}
556
625
626
+ #if CONFIG_IDF_TARGET_ESP32P4
627
+ static void sdmmc_get_slot (const int slot , sdmmc_slot_config_t * config )
628
+ {
629
+ memset (config , 0 , sizeof (sdmmc_slot_config_t ));
630
+ config -> cd = SDMMC_SLOT_NO_CD ;
631
+ config -> wp = SDMMC_SLOT_NO_WP ;
632
+ config -> width = 4 ;
633
+ config -> flags = 0 ;
634
+ }
635
+ #endif
636
+
557
637
int mount_sdcard (void )
558
638
{
559
639
sdcard_cfg_t cfg = { 0 };
@@ -562,17 +642,33 @@ int mount_sdcard(void)
562
642
if (ret != 0 ) {
563
643
return ret ;
564
644
}
645
+
646
+ #if defined CONFIG_IDF_TARGET_ESP32
647
+ gpio_config_t sdcard_pwr_pin_cfg = {
648
+ .pin_bit_mask = 1UL << GPIO_NUM_13 ,
649
+ .mode = GPIO_MODE_OUTPUT ,
650
+ .pull_up_en = GPIO_PULLUP_DISABLE ,
651
+ .pull_down_en = GPIO_PULLDOWN_DISABLE ,
652
+ .intr_type = GPIO_INTR_DISABLE ,
653
+ };
654
+ gpio_config (& sdcard_pwr_pin_cfg );
655
+ gpio_set_level (GPIO_NUM_13 , 0 );
656
+ #endif
657
+
565
658
#if SOC_SDMMC_HOST_SUPPORTED
566
659
esp_vfs_fat_sdmmc_mount_config_t mount_config = {
567
660
.format_if_mount_failed = false,
568
661
.max_files = 5 ,
569
662
};
570
663
sdmmc_host_t host = SDMMC_HOST_DEFAULT ();
571
664
#if CONFIG_IDF_TARGET_ESP32P4
665
+ host .slot = 0 ;
572
666
host .max_freq_khz = SDMMC_FREQ_HIGHSPEED ;
573
667
#endif
574
668
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT ();
575
669
slot_config .width = cfg .d3 ? 4 : 1 ;
670
+ #if SOC_SDMMC_USE_GPIO_MATRIX
671
+ slot_config .width = cfg .d3 ? 4 : 1 ;
576
672
slot_config .flags |= SDMMC_SLOT_FLAG_INTERNAL_PULLUP ;
577
673
slot_config .d4 = -1 ;
578
674
slot_config .d5 = -1 ;
@@ -586,14 +682,24 @@ int mount_sdcard(void)
586
682
slot_config .d1 = cfg .d1 ? cfg .d1 : -1 ;
587
683
slot_config .d2 = cfg .d2 ? cfg .d2 : -1 ;
588
684
slot_config .d3 = cfg .d3 ? cfg .d3 : -1 ;
685
+ #endif /* SOC_SDMMC_USE_GPIO_MATRIX */
686
+ #if CONFIG_IDF_TARGET_ESP32P4
687
+ sdmmc_get_slot (0 , & slot_config );
688
+ #endif
689
+
589
690
printf ("use %d %d %d %d\n" , cfg .d0 , cfg .d1 , cfg .d2 , cfg .d3 );
590
691
return esp_vfs_fat_sdmmc_mount ("/sdcard" , & host , & slot_config , & mount_config , & card );
591
692
#else
592
693
return -1 ;
593
694
#endif
594
695
}
595
696
596
- void unmount_sdcard ()
697
+ void * get_sdcard_handle (void )
698
+ {
699
+ return card ;
700
+ }
701
+
702
+ void unmount_sdcard (void )
597
703
{
598
704
if (card ) {
599
705
esp_vfs_fat_sdcard_unmount ("/sdcard" , card );
0 commit comments