Added static buffers to lfs_config and changed all lfs_file_open to lfs_file_opencfg

This commit is contained in:
Steven Dan
2026-05-19 16:10:41 +08:00
parent 69715f51d9
commit 70f32e4e63
3 changed files with 92 additions and 37 deletions

View File

@@ -25,6 +25,15 @@ static rtos_qspi_flash_t qspi_flash_ctx_s;
#define SECTOR_SIZE 4096 #define SECTOR_SIZE 4096
rtos_qspi_flash_t *qspi_flash_ctx = &qspi_flash_ctx_s; rtos_qspi_flash_t *qspi_flash_ctx = &qspi_flash_ctx_s;
// Static buffers for LFS caches — avoids all malloc/free inside LittleFS.
// xCORE bare-metal newlib malloc is not thread-safe; dynamic allocation in
// multi-core LFS operations can corrupt the heap free-list.
static uint8_t lfs_read_buf[16];
static uint8_t lfs_prog_buf[16];
static uint8_t lfs_lookahead_buf[16];
static uint8_t lfs_file_buf[16];
static struct lfs_file_config file_cfg = { .buffer = lfs_file_buf };
__attribute__((fptrgroup("local_block_device_read_fptr_grp"))) __attribute__((fptrgroup("local_block_device_read_fptr_grp")))
int local_block_device_read(const struct lfs_config *c, lfs_block_t block, int local_block_device_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)
@@ -73,6 +82,11 @@ const struct lfs_config cfg = {
.cache_size = 16, .cache_size = 16,
.lookahead_size = 16, .lookahead_size = 16,
.block_cycles = 500, .block_cycles = 500,
// Static buffers — eliminates all malloc/free in LittleFS hot path
.read_buffer = lfs_read_buf,
.prog_buffer = lfs_prog_buf,
.lookahead_buffer = lfs_lookahead_buf,
}; };
// Session-based exclusive flash access: // Session-based exclusive flash access:
@@ -173,7 +187,7 @@ void lfs_read_config(unsigned char * config, unsigned char * buffer, unsigned si
#endif #endif
debug_printf("lfs_read_config: %s, size: %d\n", config, size); debug_printf("lfs_read_config: %s, size: %d\n", config, size);
int result = lfs_file_open(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT); int result = lfs_file_opencfg(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
debug_printf("lfs_read_config: %s, result: %d\n", config, result); debug_printf("lfs_read_config: %s, result: %d\n", config, result);
if (result != 0) { if (result != 0) {
debug_printf("lfs_read_config: %s, open file failed\n", config); debug_printf("lfs_read_config: %s, open file failed\n", config);
@@ -203,7 +217,7 @@ void lfs_write_config(unsigned char * config, unsigned char * buffer, unsigned s
#endif #endif
debug_printf("lfs_write_config: %s, size: %d\n", config, size); debug_printf("lfs_write_config: %s, size: %d\n", config, size);
int result = lfs_file_open(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT); int result = lfs_file_opencfg(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
if (result != 0) { if (result != 0) {
debug_printf("lfs_write_config: open file failed\n"); debug_printf("lfs_write_config: open file failed\n");
return; return;
@@ -223,10 +237,12 @@ void lfs_write_config(unsigned char * config, unsigned char * buffer, unsigned s
#pragma stackfunction 1200 #pragma stackfunction 1200
int lfs_file_exists(const char * file_path) int lfs_file_exists(const char * file_path)
{ {
lfs_file_t file; lfs_file_t local_file;
int result = lfs_file_open(&lfs, &file, file_path, LFS_O_RDONLY); static uint8_t local_file_buf[16];
static struct lfs_file_config local_file_cfg = { .buffer = local_file_buf };
int result = lfs_file_opencfg(&lfs, &local_file, file_path, LFS_O_RDONLY, &local_file_cfg);
if (result == 0) { if (result == 0) {
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &local_file);
return 1; // 文件存在 return 1; // 文件存在
} }
return 0; // 文件不存在 return 0; // 文件不存在
@@ -236,7 +252,7 @@ int lfs_file_exists(const char * file_path)
#pragma stackfunction 1200 #pragma stackfunction 1200
void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned size) void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned size)
{ {
lfs_file_open(&lfs, &file, file_path, LFS_O_RDONLY); lfs_file_opencfg(&lfs, &file, file_path, LFS_O_RDONLY, &file_cfg);
lfs_file_read(&lfs, &file, buffer, size); lfs_file_read(&lfs, &file, buffer, size);
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &file);
} }
@@ -244,7 +260,7 @@ void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned
#pragma stackfunction 1500 #pragma stackfunction 1500
void lfs_write_eq_config(const char * file_path, unsigned char * buffer, unsigned size) void lfs_write_eq_config(const char * file_path, unsigned char * buffer, unsigned size)
{ {
lfs_file_open(&lfs, &file, file_path, LFS_O_RDWR | LFS_O_CREAT); lfs_file_opencfg(&lfs, &file, file_path, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
lfs_file_rewind(&lfs, &file); lfs_file_rewind(&lfs, &file);
lfs_file_write(&lfs, &file, buffer, size); lfs_file_write(&lfs, &file, buffer, size);
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &file);
@@ -265,10 +281,12 @@ int lfs_create_directory(const char * dir_path)
char temp_file[256]; char temp_file[256];
snprintf(temp_file, sizeof(temp_file), "%s/.dir", dir_path); snprintf(temp_file, sizeof(temp_file), "%s/.dir", dir_path);
lfs_file_t file; lfs_file_t local_file;
int result = lfs_file_open(&lfs, &file, temp_file, LFS_O_RDWR | LFS_O_CREAT); static uint8_t local_file_buf[16];
static struct lfs_file_config local_file_cfg = { .buffer = local_file_buf };
int result = lfs_file_opencfg(&lfs, &local_file, temp_file, LFS_O_RDWR | LFS_O_CREAT, &local_file_cfg);
if (result == 0) { if (result == 0) {
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &local_file);
lfs_remove(&lfs, temp_file); lfs_remove(&lfs, temp_file);
return 0; // 成功 return 0; // 成功
} }
@@ -279,7 +297,7 @@ int lfs_create_directory(const char * dir_path)
#if 0 #if 0
// read current count // read current count
uint32_t boot_count = 0; uint32_t boot_count = 0;
lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT); lfs_file_opencfg(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count)); lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
// update boot count // update boot count

View File

@@ -25,6 +25,15 @@ static rtos_qspi_flash_t qspi_flash_ctx_s;
#define SECTOR_SIZE 4096 #define SECTOR_SIZE 4096
rtos_qspi_flash_t *qspi_flash_ctx = &qspi_flash_ctx_s; rtos_qspi_flash_t *qspi_flash_ctx = &qspi_flash_ctx_s;
// Static buffers for LFS caches — avoids all malloc/free inside LittleFS.
// xCORE bare-metal newlib malloc is not thread-safe; dynamic allocation in
// multi-core LFS operations can corrupt the heap free-list.
static uint8_t lfs_read_buf[16];
static uint8_t lfs_prog_buf[16];
static uint8_t lfs_lookahead_buf[16];
static uint8_t lfs_file_buf[16];
static struct lfs_file_config file_cfg = { .buffer = lfs_file_buf };
__attribute__((fptrgroup("local_block_device_read_fptr_grp"))) __attribute__((fptrgroup("local_block_device_read_fptr_grp")))
int local_block_device_read(const struct lfs_config *c, lfs_block_t block, int local_block_device_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)
@@ -73,6 +82,11 @@ const struct lfs_config cfg = {
.cache_size = 16, .cache_size = 16,
.lookahead_size = 16, .lookahead_size = 16,
.block_cycles = 500, .block_cycles = 500,
// Static buffers — eliminates all malloc/free in LittleFS hot path
.read_buffer = lfs_read_buf,
.prog_buffer = lfs_prog_buf,
.lookahead_buffer = lfs_lookahead_buf,
}; };
// Session-based exclusive flash access: // Session-based exclusive flash access:
@@ -173,7 +187,7 @@ void lfs_read_config(unsigned char * config, unsigned char * buffer, unsigned si
#endif #endif
debug_printf("lfs_read_config: %s, size: %d\n", config, size); debug_printf("lfs_read_config: %s, size: %d\n", config, size);
int result = lfs_file_open(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT); int result = lfs_file_opencfg(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
debug_printf("lfs_read_config: %s, result: %d\n", config, result); debug_printf("lfs_read_config: %s, result: %d\n", config, result);
if (result != 0) { if (result != 0) {
debug_printf("lfs_read_config: %s, open file failed\n", config); debug_printf("lfs_read_config: %s, open file failed\n", config);
@@ -203,7 +217,7 @@ void lfs_write_config(unsigned char * config, unsigned char * buffer, unsigned s
#endif #endif
debug_printf("lfs_write_config: %s, size: %d\n", config, size); debug_printf("lfs_write_config: %s, size: %d\n", config, size);
int result = lfs_file_open(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT); int result = lfs_file_opencfg(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
if (result != 0) { if (result != 0) {
debug_printf("lfs_write_config: open file failed\n"); debug_printf("lfs_write_config: open file failed\n");
return; return;
@@ -223,10 +237,12 @@ void lfs_write_config(unsigned char * config, unsigned char * buffer, unsigned s
#pragma stackfunction 1200 #pragma stackfunction 1200
int lfs_file_exists(const char * file_path) int lfs_file_exists(const char * file_path)
{ {
lfs_file_t file; lfs_file_t local_file;
int result = lfs_file_open(&lfs, &file, file_path, LFS_O_RDONLY); static uint8_t local_file_buf[16];
static struct lfs_file_config local_file_cfg = { .buffer = local_file_buf };
int result = lfs_file_opencfg(&lfs, &local_file, file_path, LFS_O_RDONLY, &local_file_cfg);
if (result == 0) { if (result == 0) {
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &local_file);
return 1; // 文件存在 return 1; // 文件存在
} }
return 0; // 文件不存在 return 0; // 文件不存在
@@ -236,7 +252,7 @@ int lfs_file_exists(const char * file_path)
#pragma stackfunction 1200 #pragma stackfunction 1200
void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned size) void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned size)
{ {
lfs_file_open(&lfs, &file, file_path, LFS_O_RDONLY); lfs_file_opencfg(&lfs, &file, file_path, LFS_O_RDONLY, &file_cfg);
lfs_file_read(&lfs, &file, buffer, size); lfs_file_read(&lfs, &file, buffer, size);
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &file);
} }
@@ -244,7 +260,7 @@ void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned
#pragma stackfunction 1500 #pragma stackfunction 1500
void lfs_write_eq_config(const char * file_path, unsigned char * buffer, unsigned size) void lfs_write_eq_config(const char * file_path, unsigned char * buffer, unsigned size)
{ {
lfs_file_open(&lfs, &file, file_path, LFS_O_RDWR | LFS_O_CREAT); lfs_file_opencfg(&lfs, &file, file_path, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
lfs_file_rewind(&lfs, &file); lfs_file_rewind(&lfs, &file);
lfs_file_write(&lfs, &file, buffer, size); lfs_file_write(&lfs, &file, buffer, size);
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &file);
@@ -265,10 +281,12 @@ int lfs_create_directory(const char * dir_path)
char temp_file[256]; char temp_file[256];
snprintf(temp_file, sizeof(temp_file), "%s/.dir", dir_path); snprintf(temp_file, sizeof(temp_file), "%s/.dir", dir_path);
lfs_file_t file; lfs_file_t local_file;
int result = lfs_file_open(&lfs, &file, temp_file, LFS_O_RDWR | LFS_O_CREAT); static uint8_t local_file_buf[16];
static struct lfs_file_config local_file_cfg = { .buffer = local_file_buf };
int result = lfs_file_opencfg(&lfs, &local_file, temp_file, LFS_O_RDWR | LFS_O_CREAT, &local_file_cfg);
if (result == 0) { if (result == 0) {
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &local_file);
lfs_remove(&lfs, temp_file); lfs_remove(&lfs, temp_file);
return 0; // 成功 return 0; // 成功
} }
@@ -279,7 +297,7 @@ int lfs_create_directory(const char * dir_path)
#if 0 #if 0
// read current count // read current count
uint32_t boot_count = 0; uint32_t boot_count = 0;
lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT); lfs_file_opencfg(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count)); lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
// update boot count // update boot count

View File

@@ -31,6 +31,15 @@ static rtos_qspi_flash_t qspi_flash_ctx_s;
#define SECTOR_SIZE 4096 #define SECTOR_SIZE 4096
rtos_qspi_flash_t *qspi_flash_ctx = &qspi_flash_ctx_s; rtos_qspi_flash_t *qspi_flash_ctx = &qspi_flash_ctx_s;
// Static buffers for LFS caches — avoids all malloc/free inside LittleFS.
// xCORE bare-metal newlib malloc is not thread-safe; dynamic allocation in
// multi-core LFS operations can corrupt the heap free-list.
static uint8_t lfs_read_buf[16];
static uint8_t lfs_prog_buf[16];
static uint8_t lfs_lookahead_buf[16];
static uint8_t lfs_file_buf[16];
static struct lfs_file_config file_cfg = { .buffer = lfs_file_buf };
__attribute__((fptrgroup("local_block_device_read_fptr_grp"))) __attribute__((fptrgroup("local_block_device_read_fptr_grp")))
int local_block_device_read(const struct lfs_config *c, lfs_block_t block, int local_block_device_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)
@@ -79,6 +88,11 @@ const struct lfs_config cfg = {
.cache_size = 16, .cache_size = 16,
.lookahead_size = 16, .lookahead_size = 16,
.block_cycles = 500, .block_cycles = 500,
// Static buffers — eliminates all malloc/free in LittleFS hot path
.read_buffer = lfs_read_buf,
.prog_buffer = lfs_prog_buf,
.lookahead_buffer = lfs_lookahead_buf,
}; };
// Session-based exclusive flash access: // Session-based exclusive flash access:
@@ -180,7 +194,7 @@ void lfs_read_config(unsigned char * config, unsigned char * buffer, unsigned si
#endif #endif
debug_printf("lfs_read_config: %s, size: %d\n", config, size); debug_printf("lfs_read_config: %s, size: %d\n", config, size);
int result = lfs_file_open(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT); int result = lfs_file_opencfg(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
debug_printf("lfs_read_config: %s, result: %d\n", config, result); debug_printf("lfs_read_config: %s, result: %d\n", config, result);
if (result != 0) { if (result != 0) {
debug_printf("lfs_read_config: %s, open file failed\n", config); debug_printf("lfs_read_config: %s, open file failed\n", config);
@@ -210,7 +224,7 @@ void lfs_write_config(unsigned char * config, unsigned char * buffer, unsigned s
#endif #endif
debug_printf("lfs_write_config: %s, size: %d\n", config, size); debug_printf("lfs_write_config: %s, size: %d\n", config, size);
int result = lfs_file_open(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT); int result = lfs_file_opencfg(&lfs, &file, config, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
if (result != 0) { if (result != 0) {
debug_printf("lfs_write_config: open file failed\n"); debug_printf("lfs_write_config: open file failed\n");
return; return;
@@ -230,10 +244,12 @@ void lfs_write_config(unsigned char * config, unsigned char * buffer, unsigned s
#pragma stackfunction 1200 #pragma stackfunction 1200
int lfs_file_exists(const char * file_path) int lfs_file_exists(const char * file_path)
{ {
lfs_file_t file; lfs_file_t local_file;
int result = lfs_file_open(&lfs, &file, file_path, LFS_O_RDONLY); static uint8_t local_file_buf[16];
static struct lfs_file_config local_file_cfg = { .buffer = local_file_buf };
int result = lfs_file_opencfg(&lfs, &local_file, file_path, LFS_O_RDONLY, &local_file_cfg);
if (result == 0) { if (result == 0) {
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &local_file);
return 1; // 文件存在 return 1; // 文件存在
} }
return 0; // 文件不存在 return 0; // 文件不存在
@@ -243,7 +259,7 @@ int lfs_file_exists(const char * file_path)
#pragma stackfunction 1200 #pragma stackfunction 1200
void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned size) void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned size)
{ {
lfs_file_open(&lfs, &file, file_path, LFS_O_RDONLY); lfs_file_opencfg(&lfs, &file, file_path, LFS_O_RDONLY, &file_cfg);
lfs_file_read(&lfs, &file, buffer, size); lfs_file_read(&lfs, &file, buffer, size);
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &file);
} }
@@ -251,7 +267,7 @@ void lfs_read_eq_config(const char * file_path, unsigned char * buffer, unsigned
#pragma stackfunction 1500 #pragma stackfunction 1500
void lfs_write_eq_config(const char * file_path, unsigned char * buffer, unsigned size) void lfs_write_eq_config(const char * file_path, unsigned char * buffer, unsigned size)
{ {
lfs_file_open(&lfs, &file, file_path, LFS_O_RDWR | LFS_O_CREAT); lfs_file_opencfg(&lfs, &file, file_path, LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
lfs_file_rewind(&lfs, &file); lfs_file_rewind(&lfs, &file);
lfs_file_write(&lfs, &file, buffer, size); lfs_file_write(&lfs, &file, buffer, size);
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &file);
@@ -272,20 +288,23 @@ int lfs_create_directory(const char * dir_path)
char temp_file[256]; char temp_file[256];
snprintf(temp_file, sizeof(temp_file), "%s/.dir", dir_path); snprintf(temp_file, sizeof(temp_file), "%s/.dir", dir_path);
lfs_file_t file; lfs_file_t local_file;
int result = lfs_file_open(&lfs, &file, temp_file, LFS_O_RDWR | LFS_O_CREAT); static uint8_t local_file_buf[16];
static struct lfs_file_config local_file_cfg = { .buffer = local_file_buf };
int result = lfs_file_opencfg(&lfs, &local_file, temp_file, LFS_O_RDWR | LFS_O_CREAT, &local_file_cfg);
if (result == 0) { if (result == 0) {
lfs_file_close(&lfs, &file); lfs_file_close(&lfs, &local_file);
lfs_remove(&lfs, temp_file); lfs_remove(&lfs, temp_file);
return 0; // 成功 return 0; // 成功
} }
return -1; // 失败 return -1; // 失败
} }
#if 0 #if 0
// read current count // read current count
uint32_t boot_count = 0; uint32_t boot_count = 0;
lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT); lfs_file_opencfg(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT, &file_cfg);
lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count)); lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
// update boot count // update boot count