Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LittleFS integration #150

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions inc/fdb_cfg.h → inc/fdb_cfg_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#ifndef _FDB_CFG_H_
#define _FDB_CFG_H_

#include "SEGGER_RTT.h"

/* using KVDB feature */
#define FDB_USING_KVDB

Expand All @@ -21,28 +23,26 @@
#endif

/* using TSDB (Time series database) feature */
#define FDB_USING_TSDB

/* Using FAL storage mode */
#define FDB_USING_FAL_MODE

#ifdef FDB_USING_FAL_MODE
/* the flash write granularity, unit: bit
* only support 1(nor flash)/ 8(stm32f2/f4)/ 32(stm32f1) */
#define FDB_WRITE_GRAN /* @note you must define it for a value */
#endif
//#define FDB_USING_TSDB

/* Using file storage mode by LIBC file API, like fopen/fread/fwrte/fclose */
/* #define FDB_USING_FILE_LIBC_MODE */

/* Using file storage mode by LIBC file API, like fopen/fread/fwrte/fclose */
#define FDB_USING_FILE_LITTLEFS_MODE

/* Using file storage mode by POSIX file API, like open/read/write/close */
/* #define FDB_USING_FILE_POSIX_MODE */

/* MCU Endian Configuration, default is Little Endian Order. */
/* #define FDB_BIG_ENDIAN */

/* log print macro. default EF_PRINT macro is printf() */
/* #define FDB_PRINT(...) my_printf(__VA_ARGS__) */
#if DEBUG_
#define FDB_PRINT(...) SEGGER_RTT_printf(0U, __VA_ARGS__)
#else
#define FDB_PRINT(...)
#endif

/* print debug information */
#define FDB_DEBUG_ENABLE
Expand Down
10 changes: 9 additions & 1 deletion inc/fdb_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#ifndef _FDB_DEF_H_
#define _FDB_DEF_H_

#if defined(FDB_USING_FILE_LITTLEFS_MODE)
#include "lfs.h"
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -39,7 +43,7 @@ extern "C" {
#define FDB_KV_USING_CACHE
#endif

#if defined(FDB_USING_FILE_LIBC_MODE) || defined(FDB_USING_FILE_POSIX_MODE)
#if defined(FDB_USING_FILE_LIBC_MODE) || defined(FDB_USING_FILE_POSIX_MODE) || defined(FDB_USING_FILE_LITTLEFS_MODE)
#define FDB_USING_FILE_MODE
#endif

Expand Down Expand Up @@ -272,6 +276,10 @@ struct fdb_db {
int cur_file; /**< current file object */
#elif defined(FDB_USING_FILE_LIBC_MODE)
FILE *cur_file; /**< current file object */
#elif defined(FDB_USING_FILE_LITTLEFS_MODE)
lfs_file_t *cur_file; /**< current file object */
lfs_t *cur_lfs; /**< current partition object */
struct lfs_file_config cur_file_config; /**< current file object config usefull for file buffer static allocation */
#endif
uint32_t cur_sec; /**< current operate sector address */
#endif
Expand Down
14 changes: 11 additions & 3 deletions src/fdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ fdb_err_t _fdb_init_ex(fdb_db_t db, const char *name, const char *path, fdb_db_t
/* must set when using file mode */
FDB_ASSERT(db->sec_size != 0);
FDB_ASSERT(db->max_size != 0);
#ifdef FDB_USING_FILE_POSIX_MODE
#if defined(FDB_USING_FILE_POSIX_MODE)
db->cur_file = -1;
#else
#elif defined(FDB_USING_FILE_LIBC_MODE)
db->cur_file = 0;
#elif defined(FDB_USING_FILE_LITTLEFS_MODE)
db->cur_file = 0;
db->cur_lfs = 0;
memset(&db->cur_file_config, 0, sizeof(db->cur_file_config));
#endif
db->storage.dir = path;
FDB_ASSERT(strlen(path) != 0)
Expand Down Expand Up @@ -102,13 +106,17 @@ void _fdb_deinit(fdb_db_t db)

if (db->init_ok) {
#ifdef FDB_USING_FILE_MODE
#ifdef FDB_USING_FILE_POSIX_MODE
#if defined(FDB_USING_FILE_POSIX_MODE)
if (db->cur_file > 0) {
#if !defined(_MSC_VER)
#include <unistd.h>
#endif
close(db->cur_file);
}
#elif defined(FDB_USING_FILE_LITTLEFS_MODE )
if (db->cur_file != 0) {
lfs_file_close(db->cur_lfs, db->cur_file);
}
#else
if (db->cur_file != 0) {
fclose(db->cur_file);
Expand Down
116 changes: 115 additions & 1 deletion src/fdb_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string.h>
#include <flashdb.h>
#include <fdb_low_lvl.h>
#include "flash_manager.h"

#define FDB_LOG_TAG "[file]"

Expand Down Expand Up @@ -215,7 +216,120 @@ fdb_err_t _fdb_file_erase(fdb_db_t db, uint32_t addr, size_t size)
}
return result;
}
#endif /* defined(FDB_USING_FILE_LIBC_MODE) */
#elif defined(FDB_USING_FILE_LITTLEFS_MODE)
static lfs_file_t *open_db_file(fdb_db_t db, uint32_t addr, bool clean)
{
uint32_t sec_addr = FDB_ALIGN_DOWN(addr, db->sec_size);
lfs_file_t *fd = db->cur_file;
char path[DB_PATH_MAX];
static uint8_t ui8_file_buffer[FILE_CACHE_SIZE];
static lfs_file_t file;

/* check if partition is selected */
if(db->cur_lfs == 0) {
db->cur_lfs = &lfs; /* store partition from external variable */
}

/* check if file buffer is configured */
if(db->cur_file_config.buffer == 0) {
db->cur_file_config.buffer = &ui8_file_buffer[0];
}

if (sec_addr != db->cur_sec || fd <= 0 || clean) {
get_db_file_path(db, addr, path, DB_PATH_MAX);

if (fd > 0) {
lfs_file_close(db->cur_lfs, fd);
fd = 0;
}
if (clean) {
/* clean the old file */
if(fd == 0) {
fd = &file;
}

if(lfs_file_opencfg(db->cur_lfs, fd, path,
LFS_O_RDWR | LFS_O_CREAT | LFS_O_TRUNC, &db->cur_file_config) < 0) {
FDB_INFO("Error: open (%s) file failed.\n", path);
} else {
lfs_file_close(db->cur_lfs, fd);
fd = 0;
}
}

/* open the database file */
if(fd == 0) {
fd = &file;
}

if(lfs_file_opencfg(db->cur_lfs, fd, path,
LFS_O_RDWR, &db->cur_file_config) < 0) {
fd = 0;
}
db->cur_sec = sec_addr;
}
db->cur_file = fd;

return db->cur_file;
}

fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void *buf, size_t size)
{
fdb_err_t result = FDB_NO_ERR;
lfs_file_t *fp = open_db_file(db, addr, false);
if (fp) {
addr = addr % db->sec_size;
lfs_file_seek(db->cur_lfs, fp, addr, LFS_SEEK_SET);
lfs_file_read(db->cur_lfs, fp, buf, size);
} else {
result = FDB_READ_ERR;
}
return result;
}

fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync)
{
fdb_err_t result = FDB_NO_ERR;
lfs_file_t *fp = open_db_file(db, addr, false);
if (fp) {
addr = addr % db->sec_size;
lfs_file_seek(db->cur_lfs, fp, addr, LFS_SEEK_SET);
lfs_file_write(db->cur_lfs, fp, buf, size);
if(sync) {
lfs_file_sync(db->cur_lfs, fp);
}
} else {
result = FDB_READ_ERR;
}

return result;
}

fdb_err_t _fdb_file_erase(fdb_db_t db, uint32_t addr, size_t size)
{
fdb_err_t result = FDB_NO_ERR;

lfs_file_t *fp = open_db_file(db, addr, true);
if (fp != NULL) {
#define BUF_SIZE 32
uint8_t buf[BUF_SIZE];
size_t i;
lfs_file_seek(db->cur_lfs, fp, 0, LFS_SEEK_SET);
for (i = 0; i * BUF_SIZE < size; i++)
{
memset(buf, 0xFF, BUF_SIZE);
lfs_file_write(db->cur_lfs, fp, buf, BUF_SIZE);
}
memset(buf, 0xFF, BUF_SIZE);

lfs_file_write(db->cur_lfs, fp, buf, size - i * BUF_SIZE);
lfs_file_sync(db->cur_lfs, fp);
} else {
result = FDB_ERASE_ERR;
}
return result;
}
#endif /* defined(FDB_USING_FILE_LITTLEFS_MODE) */

#endif /* FDB_USING_FILE_MODE */

1 change: 1 addition & 0 deletions src/fdb_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ size_t fdb_blob_read(fdb_db_t db, fdb_blob_t blob)
extern fdb_err_t _fdb_file_read(fdb_db_t db, uint32_t addr, void *buf, size_t size);
extern fdb_err_t _fdb_file_write(fdb_db_t db, uint32_t addr, const void *buf, size_t size, bool sync);
extern fdb_err_t _fdb_file_erase(fdb_db_t db, uint32_t addr, size_t size);
extern fdb_err_t _fdb_file_close(fdb_db_t db);
#endif /* FDB_USING_FILE_LIBC */

fdb_err_t _fdb_flash_read(fdb_db_t db, uint32_t addr, void *buf, size_t size)
Expand Down