@@ -41,8 +41,10 @@ static const char *TAG = "ESP32_P4_EV";
4141static lv_indev_t * disp_indev = NULL ;
4242#endif // (BSP_CONFIG_NO_GRAPHIC_LIB == 0)
4343
44- sdmmc_card_t * bsp_sdcard = NULL ; // Global uSD card handler
44+ static sdmmc_card_t * bsp_sdcard = NULL ; // uSD card handle
4545static bool i2c_initialized = false;
46+ static bool spi_sd_initialized = false;
47+ static sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL ; //SD LDO handle
4648static TaskHandle_t usb_host_task ; // USB Host Library task
4749#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL (5 , 3 , 0 ))
4850static i2c_master_bus_handle_t i2c_handle = NULL ; // I2C Handle
@@ -112,8 +114,61 @@ i2c_master_bus_handle_t bsp_i2c_get_handle(void)
112114 return i2c_handle ;
113115}
114116
115- esp_err_t bsp_sdcard_mount (void )
117+ sdmmc_card_t * bsp_sdcard_get_handle (void )
118+ {
119+ return bsp_sdcard ;
120+ }
121+
122+ void bsp_sdcard_get_sdmmc_host (const int slot , sdmmc_host_t * config )
123+ {
124+ assert (config );
125+
126+ sdmmc_host_t host_config = SDMMC_HOST_DEFAULT ();
127+ host_config .slot = slot ;
128+ host_config .max_freq_khz = SDMMC_FREQ_HIGHSPEED ;
129+
130+ memcpy (config , & host_config , sizeof (sdmmc_host_t ));
131+ }
132+
133+ void bsp_sdcard_get_sdspi_host (const int slot , sdmmc_host_t * config )
134+ {
135+ assert (config );
136+
137+ sdmmc_host_t host_config = SDSPI_HOST_DEFAULT ();
138+ host_config .slot = slot ;
139+
140+ memcpy (config , & host_config , sizeof (sdmmc_host_t ));
141+ }
142+
143+ void bsp_sdcard_sdmmc_get_slot (const int slot , sdmmc_slot_config_t * config )
144+ {
145+ assert (config );
146+ memset (config , 0 , sizeof (sdmmc_slot_config_t ));
147+
148+ /* SD card is connected to Slot 0 pins. Slot 0 uses IO MUX, so not specifying the pins here */
149+ config -> cd = SDMMC_SLOT_NO_CD ;
150+ config -> wp = SDMMC_SLOT_NO_WP ;
151+ config -> width = 4 ;
152+ config -> flags = 0 ;
153+ }
154+
155+ void bsp_sdcard_sdspi_get_slot (const spi_host_device_t spi_host , sdspi_device_config_t * config )
156+ {
157+ assert (config );
158+ memset (config , 0 , sizeof (sdspi_device_config_t ));
159+
160+ config -> gpio_cs = BSP_SD_SPI_CS ;
161+ config -> gpio_cd = SDSPI_SLOT_NO_CD ;
162+ config -> gpio_wp = SDSPI_SLOT_NO_WP ;
163+ config -> gpio_int = GPIO_NUM_NC ;
164+ config -> gpio_wp_polarity = SDSPI_IO_ACTIVE_LOW ;
165+ config -> host_id = spi_host ;
166+ }
167+
168+ esp_err_t bsp_sdcard_sdmmc_mount (bsp_sdcard_cfg_t * cfg )
116169{
170+ sdmmc_host_t sdhost = {0 };
171+ sdmmc_slot_config_t sdslot = {0 };
117172 const esp_vfs_fat_sdmmc_mount_config_t mount_config = {
118173#ifdef CONFIG_BSP_SD_FORMAT_ON_MOUNT_FAIL
119174 .format_if_mount_failed = true,
@@ -123,36 +178,113 @@ esp_err_t bsp_sdcard_mount(void)
123178 .max_files = 5 ,
124179 .allocation_unit_size = 64 * 1024
125180 };
181+ assert (cfg );
182+
183+ if (!cfg -> mount ) {
184+ cfg -> mount = & mount_config ;
185+ }
186+
187+ if (!cfg -> host ) {
188+ bsp_sdcard_get_sdmmc_host (SDMMC_HOST_SLOT_0 , & sdhost );
189+ cfg -> host = & sdhost ;
190+ }
126191
127- sdmmc_host_t host = SDMMC_HOST_DEFAULT ();
128- host .slot = SDMMC_HOST_SLOT_0 ;
129- host .max_freq_khz = SDMMC_FREQ_HIGHSPEED ;
192+ if (!cfg -> slot .sdmmc ) {
193+ bsp_sdcard_sdmmc_get_slot (SDMMC_HOST_SLOT_0 , & sdslot );
194+ cfg -> slot .sdmmc = & sdslot ;
195+ }
130196
131197 sd_pwr_ctrl_ldo_config_t ldo_config = {
132198 .ldo_chan_id = 4 ,
133199 };
134- sd_pwr_ctrl_handle_t pwr_ctrl_handle = NULL ;
135200 esp_err_t ret = sd_pwr_ctrl_new_on_chip_ldo (& ldo_config , & pwr_ctrl_handle );
136201 if (ret != ESP_OK ) {
137202 ESP_LOGE (TAG , "Failed to create a new on-chip LDO power control driver" );
138203 return ret ;
139204 }
140- host .pwr_ctrl_handle = pwr_ctrl_handle ;
205+ cfg -> host -> pwr_ctrl_handle = pwr_ctrl_handle ;
206+
207+ return esp_vfs_fat_sdmmc_mount (BSP_SD_MOUNT_POINT , cfg -> host , cfg -> slot .sdmmc , cfg -> mount , & bsp_sdcard );
208+ }
209+
210+ esp_err_t bsp_sdcard_sdspi_mount (bsp_sdcard_cfg_t * cfg )
211+ {
212+ sdmmc_host_t sdhost = {0 };
213+ sdspi_device_config_t sdslot = {0 };
214+ const esp_vfs_fat_sdmmc_mount_config_t mount_config = {
215+ #ifdef CONFIG_BSP_SD_FORMAT_ON_MOUNT_FAIL
216+ .format_if_mount_failed = true,
217+ #else
218+ .format_if_mount_failed = false,
219+ #endif
220+ .max_files = 5 ,
221+ .allocation_unit_size = 64 * 1024
222+ };
223+ assert (cfg );
224+
225+ ESP_LOGD (TAG , "Initialize SPI bus" );
226+ const spi_bus_config_t buscfg = {
227+ .sclk_io_num = BSP_SD_SPI_CLK ,
228+ .mosi_io_num = BSP_SD_SPI_MOSI ,
229+ .miso_io_num = BSP_SD_SPI_MISO ,
230+ .quadwp_io_num = GPIO_NUM_NC ,
231+ .quadhd_io_num = GPIO_NUM_NC ,
232+ .max_transfer_sz = 4000 ,
233+ };
234+ ESP_RETURN_ON_ERROR (spi_bus_initialize (BSP_SDSPI_HOST , & buscfg , SDSPI_DEFAULT_DMA ), TAG , "SPI init failed" );
235+ spi_sd_initialized = true;
236+
237+ if (!cfg -> mount ) {
238+ cfg -> mount = & mount_config ;
239+ }
240+
241+ if (!cfg -> host ) {
242+ bsp_sdcard_get_sdspi_host (SDMMC_HOST_SLOT_0 , & sdhost );
243+ cfg -> host = & sdhost ;
244+ }
245+
246+ if (!cfg -> slot .sdspi ) {
247+ bsp_sdcard_sdspi_get_slot (BSP_SDSPI_HOST , & sdslot );
248+ cfg -> slot .sdspi = & sdslot ;
249+ }
141250
142- const sdmmc_slot_config_t slot_config = {
143- /* SD card is connected to Slot 0 pins. Slot 0 uses IO MUX, so not specifying the pins here */
144- .cd = SDMMC_SLOT_NO_CD ,
145- .wp = SDMMC_SLOT_NO_WP ,
146- .width = 4 ,
147- .flags = 0 ,
251+ sd_pwr_ctrl_ldo_config_t ldo_config = {
252+ .ldo_chan_id = 4 ,
148253 };
254+ esp_err_t ret = sd_pwr_ctrl_new_on_chip_ldo (& ldo_config , & pwr_ctrl_handle );
255+ if (ret != ESP_OK ) {
256+ ESP_LOGE (TAG , "Failed to create a new on-chip LDO power control driver" );
257+ return ret ;
258+ }
259+ cfg -> host -> pwr_ctrl_handle = pwr_ctrl_handle ;
149260
150- return esp_vfs_fat_sdmmc_mount (BSP_SD_MOUNT_POINT , & host , & slot_config , & mount_config , & bsp_sdcard );
261+ return esp_vfs_fat_sdspi_mount (BSP_SD_MOUNT_POINT , cfg -> host , cfg -> slot .sdspi , cfg -> mount , & bsp_sdcard );
262+ }
263+
264+ esp_err_t bsp_sdcard_mount (void )
265+ {
266+ bsp_sdcard_cfg_t cfg = {0 };
267+ return bsp_sdcard_sdmmc_mount (& cfg );
151268}
152269
153270esp_err_t bsp_sdcard_unmount (void )
154271{
155- return esp_vfs_fat_sdcard_unmount (BSP_SD_MOUNT_POINT , bsp_sdcard );
272+ esp_err_t ret = ESP_OK ;
273+
274+ if (pwr_ctrl_handle ) {
275+ ret |= sd_pwr_ctrl_del_on_chip_ldo (pwr_ctrl_handle );
276+ pwr_ctrl_handle = NULL ;
277+ }
278+
279+ ret |= esp_vfs_fat_sdcard_unmount (BSP_SD_MOUNT_POINT , bsp_sdcard );
280+ bsp_sdcard = NULL ;
281+
282+ if (spi_sd_initialized ) {
283+ ret |= spi_bus_free (BSP_SDSPI_HOST );
284+ spi_sd_initialized = false;
285+ }
286+
287+ return ret ;
156288}
157289
158290esp_err_t bsp_spiffs_mount (void )
0 commit comments