Skip to content

Commit ec5e676

Browse files
authored
Merge pull request #563 from espressif/feat/new_sd_api
feat(bsp): New SD card API
2 parents 30f0111 + 57ed463 commit ec5e676

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2322
-231
lines changed

bsp/esp-box-3/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ idf_component_register(
22
SRCS "esp-box-3.c" "esp-box-3_idf5.c"
33
INCLUDE_DIRS "include"
44
PRIV_INCLUDE_DIRS "priv_include"
5-
REQUIRES driver spiffs
6-
PRIV_REQUIRES fatfs esp_lcd
5+
REQUIRES driver spiffs fatfs
6+
PRIV_REQUIRES esp_lcd
77
)

bsp/esp-box-3/esp-box-3.c

Lines changed: 158 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7+
#include <string.h>
78
#include "driver/gpio.h"
89
#include "driver/ledc.h"
910
#include "driver/spi_master.h"
@@ -60,7 +61,8 @@ static esp_lcd_panel_handle_t panel_handle = NULL;
6061
#endif // (BSP_CONFIG_NO_GRAPHIC_LIB == 0)
6162

6263
static esp_lcd_touch_handle_t tp; // LCD touch handle
63-
sdmmc_card_t *bsp_sdcard = NULL; // Global SD card handler
64+
static sdmmc_card_t *bsp_sdcard = NULL; // Global uSD card handler
65+
static bool spi_sd_initialized = false;
6466

6567
/**
6668
* @brief I2C handle for BSP usage
@@ -185,8 +187,78 @@ esp_err_t bsp_spiffs_unmount(void)
185187
return esp_vfs_spiffs_unregister(CONFIG_BSP_SPIFFS_PARTITION_LABEL);
186188
}
187189

188-
esp_err_t bsp_sdcard_mount(void)
190+
sdmmc_card_t *bsp_sdcard_get_handle(void)
191+
{
192+
return bsp_sdcard;
193+
}
194+
195+
void bsp_sdcard_get_sdmmc_host(const int slot, sdmmc_host_t *config)
196+
{
197+
assert(config);
198+
199+
sdmmc_host_t host_config = SDMMC_HOST_DEFAULT();
200+
201+
memcpy(config, &host_config, sizeof(sdmmc_host_t));
202+
}
203+
204+
void bsp_sdcard_get_sdspi_host(const int slot, sdmmc_host_t *config)
205+
{
206+
assert(config);
207+
208+
sdmmc_host_t host_config = SDSPI_HOST_DEFAULT();
209+
host_config.slot = slot;
210+
211+
memcpy(config, &host_config, sizeof(sdmmc_host_t));
212+
}
213+
214+
void bsp_sdcard_sdmmc_get_slot(const int slot, sdmmc_slot_config_t *config)
215+
{
216+
assert(config);
217+
memset(config, 0, sizeof(sdmmc_slot_config_t));
218+
219+
/* SD card is connected to Slot 0 pins. Slot 0 uses IO MUX, so not specifying the pins here */
220+
config->cd = SDMMC_SLOT_NO_CD;
221+
config->wp = SDMMC_SLOT_NO_WP;
222+
config->cmd = BSP_SD_CMD;
223+
config->clk = BSP_SD_CLK;
224+
config->d0 = BSP_SD_D0;
225+
config->d1 = BSP_SD_D1;
226+
config->d2 = BSP_SD_D2;
227+
config->d3 = BSP_SD_D3;
228+
config->width = 4;
229+
config->flags = 0;
230+
}
231+
232+
void bsp_sdcard_sdspi_get_slot(const spi_host_device_t spi_host, sdspi_device_config_t *config)
233+
{
234+
assert(config);
235+
memset(config, 0, sizeof(sdspi_device_config_t));
236+
237+
config->gpio_cs = BSP_SD_SPI_CS;
238+
config->gpio_cd = SDSPI_SLOT_NO_CD;
239+
config->gpio_wp = SDSPI_SLOT_NO_WP;
240+
config->gpio_int = GPIO_NUM_NC;
241+
config->host_id = spi_host;
242+
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 2, 0)
243+
config->gpio_wp_polarity = SDSPI_IO_ACTIVE_LOW;
244+
#endif
245+
}
246+
247+
esp_err_t bsp_sdcard_sdmmc_mount(bsp_sdcard_cfg_t *cfg)
189248
{
249+
sdmmc_host_t sdhost = {0};
250+
sdmmc_slot_config_t sdslot = {0};
251+
const esp_vfs_fat_sdmmc_mount_config_t mount_config = {
252+
#ifdef CONFIG_BSP_SD_FORMAT_ON_MOUNT_FAIL
253+
.format_if_mount_failed = true,
254+
#else
255+
.format_if_mount_failed = false,
256+
#endif
257+
.max_files = 5,
258+
.allocation_unit_size = 16 * 1024
259+
};
260+
assert(cfg);
261+
190262
gpio_config_t power_gpio_config = {
191263
.mode = GPIO_MODE_OUTPUT,
192264
.pin_bit_mask = 1ULL << BSP_SD_POWER
@@ -196,6 +268,31 @@ esp_err_t bsp_sdcard_mount(void)
196268
/* SD card power on first */
197269
ESP_ERROR_CHECK(gpio_set_level(BSP_SD_POWER, 0));
198270

271+
if (!cfg->mount) {
272+
cfg->mount = &mount_config;
273+
}
274+
275+
if (!cfg->host) {
276+
bsp_sdcard_get_sdmmc_host(SDMMC_HOST_SLOT_0, &sdhost);
277+
cfg->host = &sdhost;
278+
}
279+
280+
if (!cfg->slot.sdmmc) {
281+
bsp_sdcard_sdmmc_get_slot(SDMMC_HOST_SLOT_0, &sdslot);
282+
cfg->slot.sdmmc = &sdslot;
283+
}
284+
285+
#if !CONFIG_FATFS_LONG_FILENAMES
286+
ESP_LOGW(TAG, "Warning: Long filenames on SD card are disabled in menuconfig!");
287+
#endif
288+
289+
return esp_vfs_fat_sdmmc_mount(BSP_SD_MOUNT_POINT, cfg->host, cfg->slot.sdmmc, cfg->mount, &bsp_sdcard);
290+
}
291+
292+
esp_err_t bsp_sdcard_sdspi_mount(bsp_sdcard_cfg_t *cfg)
293+
{
294+
sdmmc_host_t sdhost = {0};
295+
sdspi_device_config_t sdslot = {0};
199296
const esp_vfs_fat_sdmmc_mount_config_t mount_config = {
200297
#ifdef CONFIG_BSP_SD_FORMAT_ON_MOUNT_FAIL
201298
.format_if_mount_failed = true,
@@ -205,23 +302,71 @@ esp_err_t bsp_sdcard_mount(void)
205302
.max_files = 5,
206303
.allocation_unit_size = 16 * 1024
207304
};
305+
assert(cfg);
208306

209-
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
210-
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
211-
slot_config.width = 4;
212-
slot_config.cmd = BSP_SD_CMD;
213-
slot_config.clk = BSP_SD_CLK;
214-
slot_config.d0 = BSP_SD_D0;
215-
slot_config.d1 = BSP_SD_D1;
216-
slot_config.d2 = BSP_SD_D2;
217-
slot_config.d3 = BSP_SD_D3;
307+
gpio_config_t power_gpio_config = {
308+
.mode = GPIO_MODE_OUTPUT,
309+
.pin_bit_mask = 1ULL << BSP_SD_POWER
310+
};
311+
ESP_ERROR_CHECK(gpio_config(&power_gpio_config));
218312

219-
return esp_vfs_fat_sdmmc_mount(BSP_SD_MOUNT_POINT, &host, &slot_config, &mount_config, &bsp_sdcard);
313+
/* SD card power on first */
314+
ESP_ERROR_CHECK(gpio_set_level(BSP_SD_POWER, 0));
315+
316+
ESP_LOGD(TAG, "Initialize SPI bus");
317+
const spi_bus_config_t buscfg = {
318+
.sclk_io_num = BSP_SD_SPI_CLK,
319+
.mosi_io_num = BSP_SD_SPI_MOSI,
320+
.miso_io_num = BSP_SD_SPI_MISO,
321+
.quadwp_io_num = GPIO_NUM_NC,
322+
.quadhd_io_num = GPIO_NUM_NC,
323+
.max_transfer_sz = 4000,
324+
};
325+
ESP_RETURN_ON_ERROR(spi_bus_initialize(BSP_SDSPI_HOST, &buscfg, SPI_DMA_CH_AUTO), TAG, "SPI init failed");
326+
spi_sd_initialized = true;
327+
328+
if (!cfg->mount) {
329+
cfg->mount = &mount_config;
330+
}
331+
332+
if (!cfg->host) {
333+
bsp_sdcard_get_sdspi_host(SDMMC_HOST_SLOT_0, &sdhost);
334+
cfg->host = &sdhost;
335+
}
336+
337+
if (!cfg->slot.sdspi) {
338+
bsp_sdcard_sdspi_get_slot(BSP_SDSPI_HOST, &sdslot);
339+
cfg->slot.sdspi = &sdslot;
340+
}
341+
342+
#if !CONFIG_FATFS_LONG_FILENAMES
343+
ESP_LOGW(TAG, "Warning: Long filenames on SD card are disabled in menuconfig!");
344+
#endif
345+
346+
return esp_vfs_fat_sdspi_mount(BSP_SD_MOUNT_POINT, cfg->host, cfg->slot.sdspi, cfg->mount, &bsp_sdcard);
347+
}
348+
349+
esp_err_t bsp_sdcard_mount(void)
350+
{
351+
bsp_sdcard_cfg_t cfg = {0};
352+
return bsp_sdcard_sdmmc_mount(&cfg);
220353
}
221354

222355
esp_err_t bsp_sdcard_unmount(void)
223356
{
224-
return esp_vfs_fat_sdcard_unmount(BSP_SD_MOUNT_POINT, bsp_sdcard);
357+
esp_err_t ret = ESP_OK;
358+
359+
ret |= esp_vfs_fat_sdcard_unmount(BSP_SD_MOUNT_POINT, bsp_sdcard);
360+
bsp_sdcard = NULL;
361+
362+
if (spi_sd_initialized) {
363+
ret |= spi_bus_free(BSP_SDSPI_HOST);
364+
spi_sd_initialized = false;
365+
}
366+
367+
gpio_reset_pin(BSP_SD_POWER);
368+
369+
return ret;
225370
}
226371

227372
esp_codec_dev_handle_t bsp_audio_codec_speaker_init(void)

bsp/esp-box-3/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
version: "2.0.1"
2+
version: "3.0.0"
33
description: Board Support Package (BSP) for ESP32-S3-BOX-3
44
url: https://github.com/espressif/esp-bsp/tree/master/bsp/esp-box-3
55

bsp/esp-box-3/include/bsp/esp-box-3.h

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include "driver/i2s_std.h"
1717
#include "driver/i2c_master.h"
1818
#include "driver/sdmmc_host.h"
19+
#include "driver/sdspi_host.h"
20+
#include "esp_vfs_fat.h"
1921
#include "esp_codec_dev.h"
2022
#include "iot_button.h"
2123
#include "bsp/config.h"
@@ -73,7 +75,7 @@
7375
#define BSP_BUTTON_CONFIG_IO (GPIO_NUM_0)
7476
#define BSP_BUTTON_MUTE_IO (GPIO_NUM_1)
7577

76-
/* SD card */
78+
/* uSD card MMC */
7779
#define BSP_SD_D0 (GPIO_NUM_9)
7880
#define BSP_SD_D1 (GPIO_NUM_13)
7981
#define BSP_SD_D2 (GPIO_NUM_42)
@@ -83,6 +85,12 @@
8385
#define BSP_SD_DET (GPIO_NUM_NC)
8486
#define BSP_SD_POWER (GPIO_NUM_43)
8587

88+
/* uSD card SPI */
89+
#define BSP_SD_SPI_MISO (GPIO_NUM_9)
90+
#define BSP_SD_SPI_CS (GPIO_NUM_12)
91+
#define BSP_SD_SPI_MOSI (GPIO_NUM_14)
92+
#define BSP_SD_SPI_CLK (GPIO_NUM_11)
93+
8694
/* PMOD */
8795
/*
8896
* PMOD interface (peripheral module interface) is an open standard defined by Digilent Inc.
@@ -284,7 +292,16 @@ esp_err_t bsp_spiffs_unmount(void);
284292
* @attention IO2 is also routed to RGB LED and push button
285293
**************************************************************************************************/
286294
#define BSP_SD_MOUNT_POINT CONFIG_BSP_SD_MOUNT_POINT
287-
extern sdmmc_card_t *bsp_sdcard;
295+
#define BSP_SDSPI_HOST (SPI2_HOST)
296+
297+
typedef struct {
298+
const esp_vfs_fat_sdmmc_mount_config_t *mount;
299+
sdmmc_host_t *host;
300+
union {
301+
const sdmmc_slot_config_t *sdmmc;
302+
const sdspi_device_config_t *sdspi;
303+
} slot;
304+
} bsp_sdcard_cfg_t;
288305

289306
/**
290307
* @brief Mount microSD card to virtual file system
@@ -311,6 +328,73 @@ esp_err_t bsp_sdcard_mount(void);
311328
*/
312329
esp_err_t bsp_sdcard_unmount(void);
313330

331+
/**
332+
* @brief Get SD card handle
333+
*
334+
* @return SD card handle
335+
*/
336+
sdmmc_card_t *bsp_sdcard_get_handle(void);
337+
338+
/**
339+
* @brief Get SD card MMC host config
340+
*
341+
* @param slot SD card slot
342+
* @param config Structure which will be filled
343+
*/
344+
void bsp_sdcard_get_sdmmc_host(const int slot, sdmmc_host_t *config);
345+
346+
/**
347+
* @brief Get SD card SPI host config
348+
*
349+
* @param slot SD card slot
350+
* @param config Structure which will be filled
351+
*/
352+
void bsp_sdcard_get_sdspi_host(const int slot, sdmmc_host_t *config);
353+
354+
/**
355+
* @brief Get SD card MMC slot config
356+
*
357+
* @param slot SD card slot
358+
* @param config Structure which will be filled
359+
*/
360+
void bsp_sdcard_sdmmc_get_slot(const int slot, sdmmc_slot_config_t *config);
361+
362+
/**
363+
* @brief Get SD card SPI slot config
364+
*
365+
* @param spi_host SPI host ID
366+
* @param config Structure which will be filled
367+
*/
368+
void bsp_sdcard_sdspi_get_slot(const spi_host_device_t spi_host, sdspi_device_config_t *config);
369+
370+
/**
371+
* @brief Mount microSD card to virtual file system (MMC mode)
372+
*
373+
* @param cfg SD card configuration
374+
*
375+
* @return
376+
* - ESP_OK on success
377+
* - ESP_ERR_INVALID_STATE if esp_vfs_fat_sdmmc_mount was already called
378+
* - ESP_ERR_NO_MEM if memory cannot be allocated
379+
* - ESP_FAIL if partition cannot be mounted
380+
* - other error codes from SDMMC or SPI drivers, SDMMC protocol, or FATFS drivers
381+
*/
382+
esp_err_t bsp_sdcard_sdmmc_mount(bsp_sdcard_cfg_t *cfg);
383+
384+
/**
385+
* @brief Mount microSD card to virtual file system (SPI mode)
386+
*
387+
* @param cfg SD card configuration
388+
*
389+
* @return
390+
* - ESP_OK on success
391+
* - ESP_ERR_INVALID_STATE if esp_vfs_fat_sdmmc_mount was already called
392+
* - ESP_ERR_NO_MEM if memory cannot be allocated
393+
* - ESP_FAIL if partition cannot be mounted
394+
* - other error codes from SDMMC or SPI drivers, SDMMC protocol, or FATFS drivers
395+
*/
396+
esp_err_t bsp_sdcard_sdspi_mount(bsp_sdcard_cfg_t *cfg);
397+
314398
/**************************************************************************************************
315399
*
316400
* LCD interface

bsp/esp32_azure_iot_kit/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ idf_component_register(
22
SRCS "esp32_azure_iot_kit.c"
33
INCLUDE_DIRS "include"
44
PRIV_INCLUDE_DIRS "priv_include"
5-
REQUIRES driver esp_lcd
6-
PRIV_REQUIRES fatfs spiffs
5+
REQUIRES driver esp_lcd fatfs
6+
PRIV_REQUIRES spiffs
77
)

0 commit comments

Comments
 (0)