1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

pkg/littlefs: make block size configurable

This commit is contained in:
Fabian Hüßler 2022-05-28 11:28:54 +02:00
parent 95716d4486
commit 41c0b1e7f0
3 changed files with 38 additions and 14 deletions

View File

@ -62,7 +62,7 @@ static int littlefs_err_to_errno(ssize_t err)
}
static int _dev_read(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, void *buffer, lfs_size_t size)
lfs_off_t off, void *buffer, lfs_size_t size)
{
littlefs_desc_t *fs = c->context;
mtd_dev_t *mtd = fs->dev;
@ -70,12 +70,12 @@ static int _dev_read(const struct lfs_config *c, lfs_block_t block,
DEBUG("lfs_read: c=%p, block=%" PRIu32 ", off=%" PRIu32 ", buf=%p, size=%" PRIu32 "\n",
(void *)c, block, off, buffer, size);
return mtd_read_page(mtd, buffer, (fs->base_addr + block) * mtd->pages_per_sector,
off, size);
uint32_t page = (fs->base_addr + block) * fs->sectors_per_block * mtd->pages_per_sector;
return mtd_read_page(mtd, buffer, page, off, size);
}
static int _dev_write(const struct lfs_config *c, lfs_block_t block,
lfs_off_t off, const void *buffer, lfs_size_t size)
lfs_off_t off, const void *buffer, lfs_size_t size)
{
littlefs_desc_t *fs = c->context;
mtd_dev_t *mtd = fs->dev;
@ -83,8 +83,8 @@ static int _dev_write(const struct lfs_config *c, lfs_block_t block,
DEBUG("lfs_write: c=%p, block=%" PRIu32 ", off=%" PRIu32 ", buf=%p, size=%" PRIu32 "\n",
(void *)c, block, off, buffer, size);
return mtd_write_page_raw(mtd, buffer, (fs->base_addr + block) * mtd->pages_per_sector,
off, size);
uint32_t page = (fs->base_addr + block) * fs->sectors_per_block * mtd->pages_per_sector;
return mtd_write_page_raw(mtd, buffer, page, off, size);
}
static int _dev_erase(const struct lfs_config *c, lfs_block_t block)
@ -94,7 +94,8 @@ static int _dev_erase(const struct lfs_config *c, lfs_block_t block)
DEBUG("lfs_erase: c=%p, block=%" PRIu32 "\n", (void *)c, block);
return mtd_erase_sector(mtd, fs->base_addr + block, 1);
uint32_t sector = (fs->base_addr + block) * fs->sectors_per_block;
return mtd_erase_sector(mtd, sector, fs->sectors_per_block);
}
static int _dev_sync(const struct lfs_config *c)
@ -117,11 +118,19 @@ static int prepare(littlefs_desc_t *fs)
memset(&fs->fs, 0, sizeof(fs->fs));
if (!fs->config.block_count) {
fs->config.block_count = fs->dev->sector_count - fs->base_addr;
}
size_t block_size = fs->dev->pages_per_sector * fs->dev->page_size;
#if LITTLEFS_MIN_BLOCK_SIZE_EXP >= 0
block_size = ((block_size - 1) + (1u << LITTLEFS_MIN_BLOCK_SIZE_EXP))
/ block_size * block_size;
#endif
fs->sectors_per_block = block_size / (fs->dev->pages_per_sector * fs->dev->page_size);
size_t block_count = fs->dev->sector_count / fs->sectors_per_block;
if (!fs->config.block_size) {
fs->config.block_size = fs->dev->page_size * fs->dev->pages_per_sector;
fs->config.block_size = block_size;
}
if (!fs->config.block_count) {
fs->config.block_count = block_count - fs->base_addr;
}
if (!fs->config.prog_size) {
fs->config.prog_size = fs->dev->page_size;
@ -443,7 +452,8 @@ static int _statvfs(vfs_mount_t *mountp, const char *restrict path, struct statv
mutex_unlock(&fs->lock);
buf->f_bsize = fs->fs.cfg->block_size; /* block size */
buf->f_frsize = fs->fs.cfg->block_size; /* fundamental block size */
buf->f_frsize = fs->dev->page_size *
fs->dev->pages_per_sector; /* fundamental block size */
buf->f_blocks = fs->fs.cfg->block_count; /* Blocks total */
buf->f_bfree = buf->f_blocks - nb_blocks; /* Blocks free */
buf->f_bavail = buf->f_blocks - nb_blocks; /* Blocks available to non-privileged processes */

View File

@ -58,6 +58,13 @@ extern "C" {
* If set, it must be program size */
#define LITTLEFS_PROG_BUFFER_SIZE (0)
#endif
#ifndef LITTLEFS_MIN_BLOCK_SIZE_EXP
/**
* The exponent of the minimum acceptable block size in bytes (2^n).
* The desired block size is not guaranteed to be applicable but will be respected. */
#define LITTLEFS_MIN_BLOCK_SIZE_EXP (-1)
#endif
/** @} */
/**
@ -72,6 +79,7 @@ typedef struct {
* total number of block is defined in @p config.
* if set to 0, the total number of sectors from the mtd is used */
uint32_t base_addr;
uint16_t sectors_per_block; /**< number of sectors per block */
#if LITTLEFS_FILE_BUFFER_SIZE || DOXYGEN
/** file buffer to use internally if LITTLEFS_FILE_BUFFER_SIZE is set */
uint8_t file_buf[LITTLEFS_FILE_BUFFER_SIZE];

View File

@ -383,7 +383,10 @@ static void tests_littlefs_statvfs(void)
int res = vfs_statvfs("/test-littlefs/", &stat1);
TEST_ASSERT_EQUAL_INT(0, res);
TEST_ASSERT_EQUAL_INT(_dev->page_size * _dev->pages_per_sector, stat1.f_bsize);
TEST_ASSERT_EQUAL_INT(_dev->page_size *
_dev->pages_per_sector *
littlefs_desc.sectors_per_block,
stat1.f_bsize);
TEST_ASSERT_EQUAL_INT(_dev->page_size * _dev->pages_per_sector, stat1.f_frsize);
TEST_ASSERT((_dev->pages_per_sector * _dev->page_size * _dev->sector_count) >=
stat1.f_blocks);
@ -400,7 +403,10 @@ static void tests_littlefs_statvfs(void)
res = vfs_statvfs("/test-littlefs/", &stat2);
TEST_ASSERT_EQUAL_INT(0, res);
TEST_ASSERT_EQUAL_INT(_dev->page_size * _dev->pages_per_sector, stat2.f_bsize);
TEST_ASSERT_EQUAL_INT(_dev->page_size *
_dev->pages_per_sector *
littlefs_desc.sectors_per_block,
stat2.f_bsize);
TEST_ASSERT_EQUAL_INT(_dev->page_size * _dev->pages_per_sector, stat2.f_frsize);
TEST_ASSERT(stat1.f_bfree > stat2.f_bfree);
TEST_ASSERT(stat1.f_bavail > stat2.f_bavail);