@@ -40,6 +40,7 @@ typedef struct {
4040 uint32_t d5 ;
4141 uint32_t d6 ;
4242 uint32_t d7 ;
43+ uint8_t d3_gpio ;
4344 uint8_t card_detect ;
4445 uint8_t write_protect ;
4546 uint8_t width ;
@@ -57,6 +58,7 @@ static const sdmmc_slot_info_t s_slot_info[2] = {
5758 .d1 = PERIPHS_IO_MUX_SD_DATA1_U ,
5859 .d2 = PERIPHS_IO_MUX_SD_DATA2_U ,
5960 .d3 = PERIPHS_IO_MUX_SD_DATA3_U ,
61+ .d3_gpio = 10 ,
6062 .d4 = PERIPHS_IO_MUX_GPIO16_U ,
6163 .d5 = PERIPHS_IO_MUX_GPIO17_U ,
6264 .d6 = PERIPHS_IO_MUX_GPIO5_U ,
@@ -72,6 +74,7 @@ static const sdmmc_slot_info_t s_slot_info[2] = {
7274 .d1 = PERIPHS_IO_MUX_GPIO4_U ,
7375 .d2 = PERIPHS_IO_MUX_MTDI_U ,
7476 .d3 = PERIPHS_IO_MUX_MTCK_U ,
77+ .d3_gpio = 13 ,
7578 .card_detect = HOST_CARD_DETECT_N_2_IDX ,
7679 .write_protect = HOST_CARD_WRITE_PRT_2_IDX ,
7780 .width = 4
@@ -82,6 +85,7 @@ static const char* TAG = "sdmmc_periph";
8285static intr_handle_t s_intr_handle ;
8386static QueueHandle_t s_event_queue ;
8487
88+ size_t s_slot_width [2 ] = {1 ,1 };
8589
8690void sdmmc_host_reset ()
8791{
@@ -324,14 +328,25 @@ esp_err_t sdmmc_host_init_slot(int slot, const sdmmc_slot_config_t* slot_config)
324328 else if (slot_width > pslot -> width ) {
325329 return ESP_ERR_INVALID_ARG ;
326330 }
331+ s_slot_width [slot ] = slot_width ;
327332
328333 configure_pin (pslot -> clk );
329334 configure_pin (pslot -> cmd );
330335 configure_pin (pslot -> d0 );
336+
331337 if (slot_width >= 4 ) {
332338 configure_pin (pslot -> d1 );
333339 configure_pin (pslot -> d2 );
334- configure_pin (pslot -> d3 );
340+ //force pull-up D3 to make slave detect SD mode. connect to peripheral after width configuration.
341+ gpio_config_t gpio_conf = {
342+ .pin_bit_mask = BIT (pslot -> d3_gpio ),
343+ .mode = GPIO_MODE_OUTPUT ,
344+ .pull_up_en = 0 ,
345+ .pull_down_en = 0 ,
346+ .intr_type = GPIO_INTR_DISABLE ,
347+ };
348+ gpio_config ( & gpio_conf );
349+ gpio_set_level ( pslot -> d3_gpio , 1 );
335350 if (slot_width == 8 ) {
336351 configure_pin (pslot -> d4 );
337352 configure_pin (pslot -> d5 );
@@ -404,15 +419,23 @@ esp_err_t sdmmc_host_set_bus_width(int slot, size_t width)
404419 } else if (width == 4 ) {
405420 SDMMC .ctype .card_width_8 &= ~mask ;
406421 SDMMC .ctype .card_width |= mask ;
422+ configure_pin (s_slot_info [slot ].d3 ); // D3 was set to GPIO high to force slave into SD 1-bit mode, until 4-bit mode is set
407423 } else if (width == 8 ){
408424 SDMMC .ctype .card_width_8 |= mask ;
425+ configure_pin (s_slot_info [slot ].d3 ); // D3 was set to GPIO high to force slave into SD 1-bit mode, until 4-bit mode is set
409426 } else {
410427 return ESP_ERR_INVALID_ARG ;
411428 }
412429 ESP_LOGD (TAG , "slot=%d width=%d" , slot , width );
413430 return ESP_OK ;
414431}
415432
433+ size_t sdmmc_host_get_slot_width (int slot )
434+ {
435+ assert ( slot == 0 || slot == 1 );
436+ return s_slot_width [slot ];
437+ }
438+
416439static void sdmmc_host_dma_init ()
417440{
418441 SDMMC .ctrl .dma_enable = 1 ;
0 commit comments