Skip to content

Commit 225ab53

Browse files
authored
Merge pull request #541 from espressif/feat/new_sd_api
feat(esp32_p4_function_ev_board): New SD card API (BSP-189)
2 parents 0d4fe45 + 6fccbf1 commit 225ab53

File tree

4 files changed

+236
-20
lines changed

4 files changed

+236
-20
lines changed

bsp/esp32_p4_function_ev_board/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ idf_component_register(
33
SRCS "esp32_p4_function_ev_board.c"
44
INCLUDE_DIRS "include"
55
PRIV_INCLUDE_DIRS "priv_include"
6-
REQUIRES driver
7-
PRIV_REQUIRES esp_lcd usb spiffs fatfs
6+
REQUIRES driver vfs fatfs
7+
PRIV_REQUIRES esp_lcd usb spiffs
88
)

bsp/esp32_p4_function_ev_board/esp32_p4_function_ev_board.c

Lines changed: 147 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ static const char *TAG = "ESP32_P4_EV";
4141
static 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
4545
static 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
4648
static TaskHandle_t usb_host_task; // USB Host Library task
4749
#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 3, 0))
4850
static 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

153270
esp_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

158290
esp_err_t bsp_spiffs_mount(void)

bsp/esp32_p4_function_ev_board/idf_component.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "4.2.3"
1+
version: "5.0.0"
22
description: Board Support Package (BSP) for ESP32-P4 Function EV Board (preview)
33
url: https://github.com/espressif/esp-bsp/tree/master/bsp/esp32_p4_function_ev_board
44

bsp/esp32_p4_function_ev_board/include/bsp/esp32_p4_function_ev_board.h

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
#include "driver/gpio.h"
1616
#include "driver/i2c_master.h"
1717
#include "driver/sdmmc_host.h"
18+
#include "driver/sdspi_host.h"
19+
#include "esp_vfs_fat.h"
1820
#include "driver/i2s_std.h"
1921
#include "bsp/config.h"
2022
#include "bsp/display.h"
@@ -67,14 +69,20 @@
6769
#define BSP_LCD_TOUCH_INT (GPIO_NUM_NC)
6870
#endif
6971

70-
/* uSD card */
72+
/* uSD card MMC */
7173
#define BSP_SD_D0 (GPIO_NUM_39)
7274
#define BSP_SD_D1 (GPIO_NUM_40)
7375
#define BSP_SD_D2 (GPIO_NUM_41)
7476
#define BSP_SD_D3 (GPIO_NUM_42)
7577
#define BSP_SD_CMD (GPIO_NUM_44)
7678
#define BSP_SD_CLK (GPIO_NUM_43)
7779

80+
/* uSD card SPI */
81+
#define BSP_SD_SPI_MISO (GPIO_NUM_39)
82+
#define BSP_SD_SPI_CS (GPIO_NUM_42)
83+
#define BSP_SD_SPI_MOSI (GPIO_NUM_44)
84+
#define BSP_SD_SPI_CLK (GPIO_NUM_43)
85+
7886
#ifdef __cplusplus
7987
extern "C" {
8088
#endif
@@ -180,6 +188,7 @@ esp_codec_dev_handle_t bsp_audio_codec_microphone_init(void);
180188
* \endcode
181189
**************************************************************************************************/
182190
#define BSP_SPIFFS_MOUNT_POINT CONFIG_BSP_SPIFFS_MOUNT_POINT
191+
#define BSP_SDSPI_HOST (SDSPI_DEFAULT_HOST)
183192

184193
/**
185194
* @brief Mount SPIFFS to virtual file system
@@ -218,7 +227,15 @@ esp_err_t bsp_spiffs_unmount(void);
218227
* \endcode
219228
**************************************************************************************************/
220229
#define BSP_SD_MOUNT_POINT CONFIG_BSP_SD_MOUNT_POINT
221-
extern sdmmc_card_t *bsp_sdcard;
230+
231+
typedef struct {
232+
const esp_vfs_fat_sdmmc_mount_config_t *mount;
233+
sdmmc_host_t *host;
234+
union {
235+
const sdmmc_slot_config_t *sdmmc;
236+
const sdspi_device_config_t *sdspi;
237+
} slot;
238+
} bsp_sdcard_cfg_t;
222239

223240
/**
224241
* @brief Mount microSD card to virtual file system
@@ -245,6 +262,73 @@ esp_err_t bsp_sdcard_mount(void);
245262
*/
246263
esp_err_t bsp_sdcard_unmount(void);
247264

265+
/**
266+
* @brief Get SD card handle
267+
*
268+
* @return SD card handle
269+
*/
270+
sdmmc_card_t *bsp_sdcard_get_handle(void);
271+
272+
/**
273+
* @brief Get SD card MMC host config
274+
*
275+
* @param slot SD card slot
276+
* @param config Structure which will be filled
277+
*/
278+
void bsp_sdcard_get_sdmmc_host(const int slot, sdmmc_host_t *config);
279+
280+
/**
281+
* @brief Get SD card SPI host config
282+
*
283+
* @param slot SD card slot
284+
* @param config Structure which will be filled
285+
*/
286+
void bsp_sdcard_get_sdspi_host(const int slot, sdmmc_host_t *config);
287+
288+
/**
289+
* @brief Get SD card MMC slot config
290+
*
291+
* @param slot SD card slot
292+
* @param config Structure which will be filled
293+
*/
294+
void bsp_sdcard_sdmmc_get_slot(const int slot, sdmmc_slot_config_t *config);
295+
296+
/**
297+
* @brief Get SD card SPI slot config
298+
*
299+
* @param spi_host SPI host ID
300+
* @param config Structure which will be filled
301+
*/
302+
void bsp_sdcard_sdspi_get_slot(const spi_host_device_t spi_host, sdspi_device_config_t *config);
303+
304+
/**
305+
* @brief Mount microSD card to virtual file system (MMC mode)
306+
*
307+
* @param cfg SD card configuration
308+
*
309+
* @return
310+
* - ESP_OK on success
311+
* - ESP_ERR_INVALID_STATE if esp_vfs_fat_sdmmc_mount was already called
312+
* - ESP_ERR_NO_MEM if memory cannot be allocated
313+
* - ESP_FAIL if partition cannot be mounted
314+
* - other error codes from SDMMC or SPI drivers, SDMMC protocol, or FATFS drivers
315+
*/
316+
esp_err_t bsp_sdcard_sdmmc_mount(bsp_sdcard_cfg_t *cfg);
317+
318+
/**
319+
* @brief Mount microSD card to virtual file system (SPI mode)
320+
*
321+
* @param cfg SD card configuration
322+
*
323+
* @return
324+
* - ESP_OK on success
325+
* - ESP_ERR_INVALID_STATE if esp_vfs_fat_sdmmc_mount was already called
326+
* - ESP_ERR_NO_MEM if memory cannot be allocated
327+
* - ESP_FAIL if partition cannot be mounted
328+
* - other error codes from SDMMC or SPI drivers, SDMMC protocol, or FATFS drivers
329+
*/
330+
esp_err_t bsp_sdcard_sdspi_mount(bsp_sdcard_cfg_t *cfg);
331+
248332
/**************************************************************************************************
249333
*
250334
* LCD interface

0 commit comments

Comments
 (0)