Skip to content

Commit

Permalink
sdmmc: allow toggling the SDMMC support
Browse files Browse the repository at this point in the history
  • Loading branch information
huming2207 committed Jan 9, 2024
1 parent bc1f1ed commit 4d6e517
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ file(GLOB SOURCES src/littlefs/*.c)
list(APPEND SOURCES src/esp_littlefs.c src/littlefs_esp_part.c src/littlefs_sdmmc.c src/lfs_config.c)

if(IDF_VERSION_MAJOR GREATER_EQUAL 5)
list(APPEND pub_requires esp_partition sdmmc)
if (CONFIG_LITTLEFS_SDMMC_SUPPORT)
list(APPEND pub_requires esp_partition sdmmc)
else()
list(APPEND pub_requires esp_partition sdmmc)
endif()
else()
list(APPEND pub_requires spi_flash)
endif()
list(APPEND priv_requires esptool_py spi_flash vfs sdmmc)
list(APPEND priv_requires esptool_py spi_flash vfs)

idf_component_register(
SRCS ${SOURCES}
Expand Down
7 changes: 7 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
menu "LittleFS"

config LITTLEFS_SDMMC_SUPPORT
bool "SDMMC support (requires ESP-IDF v5+)"
default n
help
Toggle SD card support
This requires IDF v5+ as older ESP-IDF do not support SD card erase.

config LITTLEFS_MAX_PARTITIONS
int "Maximum Number of Partitions"
default 3
Expand Down
4 changes: 4 additions & 0 deletions src/littlefs_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ int littlefs_esp_part_erase(const struct lfs_config *c, lfs_block_t block);
*/
int littlefs_esp_part_sync(const struct lfs_config *c);

#ifdef CONFIG_LITTLEFS_SDMMC_SUPPORT

/**
* @brief Read a region in a block on SD card
*
Expand Down Expand Up @@ -144,6 +146,8 @@ int littlefs_sdmmc_erase(const struct lfs_config *c, lfs_block_t block);
*/
int littlefs_sdmmc_sync(const struct lfs_config *c);

#endif // CONFIG_LITTLEFS_SDMMC_SUPPORT

#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 8 additions & 4 deletions src/littlefs_sdmmc.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@
#include <sys/param.h>
#include "littlefs_api.h"

#if CONFIG_LITTLEFS_SDMMC_SUPPORT && ESP_IDF_VERSION_MAJOR < 5
#error "SDMMC feature is only supported in ESP-IDF v5+, see: https://github.com/joltwallet/esp_littlefs/pull/170#issuecomment-1882484668"
#endif

int littlefs_sdmmc_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
esp_littlefs_t * efs = c->context;
size_t part_off = (block * c->block_size) + off;
uint32_t part_off = (block * c->block_size) + off;

esp_err_t ret = sdmmc_read_sectors(efs->sdcard, buffer, block, MIN(size / efs->cfg.read_size, 1));
if (ret != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "failed to read addr %08x, size %08x, err=0x%x", (unsigned int) part_off, (unsigned int) size, ret);
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to read addr 0x%08lx: off 0x%08lx, block 0x%08lx, size %lu, err=0x%x", part_off, off, block, size, ret);
return LFS_ERR_IO;
}

Expand All @@ -25,11 +29,11 @@ int littlefs_sdmmc_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t
int littlefs_sdmmc_write(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
{
esp_littlefs_t * efs = c->context;
size_t part_off = (block * c->block_size) + off;
uint32_t part_off = (block * c->block_size) + off;

esp_err_t ret = sdmmc_write_sectors(efs->sdcard, buffer, block, MIN(size / efs->cfg.prog_size, 1));
if (ret != ESP_OK) {
ESP_LOGE(ESP_LITTLEFS_TAG, "failed to write addr %08x, size %08x, err=0x%x", (unsigned int) part_off, (unsigned int) size, ret);
ESP_LOGE(ESP_LITTLEFS_TAG, "Failed to write addr 0x%08lx: off 0x%08lx, block 0x%08lx, size %lu, err=0x%x", part_off, off, block, size, ret);
return LFS_ERR_IO;
}

Expand Down

0 comments on commit 4d6e517

Please sign in to comment.