#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include "lfs.h"
#define FLASH_SIZE 524288
uint8_t s_flashmem[FLASH_SIZE];
int user_provided_block_device_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
memcpy(buffer, &s_flashmem[0] + c->block_size * block + off, size);
return 0;
}
int user_provided_block_device_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
{
memcpy(&s_flashmem[0] + block * c->block_size + off, buffer, size);
return 0;
}
int user_provided_block_device_erase(const struct lfs_config *c, lfs_block_t block)
{
memset(&s_flashmem[0] + block * c->block_size, 0, c->block_size);
return 0;
}
int user_provided_block_device_sync(const struct lfs_config *c) {
(void) c;
return 0;
}
// variables used by the filesystem
lfs_t lfs;
lfs_file_t file;
// configuration of the filesystem is provided by this struct
const struct lfs_config cfg = {
// block device operations
.read = user_provided_block_device_read,
.prog = user_provided_block_device_prog,
.erase = user_provided_block_device_erase,
.sync = user_provided_block_device_sync,
// block device configuration
.read_size = 16,
.prog_size = 16,
.block_size = 4096,
.block_count = 128,
.cache_size = 16,
.lookahead_size = 16,
};
// entry point
int main(void) {
memset(s_flashmem, 0, sizeof(s_flashmem));
// mount the filesystem
int err = lfs_mount(&lfs, &cfg);
// reformat if we can't mount the filesystem
// this should only happen on the first boot
if (err) {
printf("mounting ... \n");
lfs_format(&lfs, &cfg);
lfs_mount(&lfs, &cfg);
}
// read current count
uint32_t boot_count = 0;
int ret;
//创建目录
ret = lfs_mkdir(&lfs, "/sfr");
//打开并读文件
ret = lfs_file_open(&lfs, &file, "/sfr/boot_count", LFS_O_RDWR | LFS_O_CREAT);
//你会发现什么都没读到, ret 返回 0
ret = lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
//指针绕回到文件开始, 并把9写入文件
boot_count = 9;
ret = lfs_file_rewind(&lfs, &file);
ret = lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
ret = lfs_file_close(&lfs, &file);
//重新打开文件,并把文件数据读到变量
boot_count = -1;
ret = lfs_file_open(&lfs, &file, "/sfr/boot_count", LFS_O_RDWR | LFS_O_CREAT);
ret = lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
ret = lfs_file_close(&lfs, &file);
//卸载文件系统
ret = lfs_unmount(&lfs);
//读出发现, 果然是9
printf("boot_count: %d\n", boot_count);
}
littlefs 文件系统: https://github.com/ARMmbed/littlefs
在线
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include "lfs.h"#define FLASH_SIZE 524288
uint8_t s_flashmem[FLASH_SIZE];int user_provided_block_device_read(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size)
{
memcpy(buffer, &s_flashmem[0] + c->block_size * block + off, size);
return 0;
}int user_provided_block_device_prog(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size)
{
memcpy(&s_flashmem[0] + block * c->block_size + off, buffer, size);
return 0;
}int user_provided_block_device_erase(const struct lfs_config *c, lfs_block_t block)
{
memset(&s_flashmem[0] + block * c->block_size, 0, c->block_size);
return 0;
}int user_provided_block_device_sync(const struct lfs_config *c) {
(void) c;
return 0;
}// variables used by the filesystem
lfs_t lfs;
lfs_file_t file;// configuration of the filesystem is provided by this struct
const struct lfs_config cfg = {
// block device operations
.read = user_provided_block_device_read,
.prog = user_provided_block_device_prog,
.erase = user_provided_block_device_erase,
.sync = user_provided_block_device_sync,// block device configuration
.read_size = 16,
.prog_size = 16,
.block_size = 4096,
.block_count = 128,
.cache_size = 16,
.lookahead_size = 16,
};// entry point
int main(void) {
int ret;memset(s_flashmem, 0, sizeof(s_flashmem));
FILE* fpImage = fopen("d:\\1.img", "rb+");
ret = fread(s_flashmem, 1, sizeof(s_flashmem), fpImage);
// mount the filesystem
int err = lfs_mount(&lfs, &cfg);// reformat if we can't mount the filesystem
// this should only happen on the first boot
if (err) {
printf("mounting ... \n");
lfs_format(&lfs, &cfg);
lfs_mount(&lfs, &cfg);
}// read current count
uint32_t boot_count = 0;
//创建目录
ret = lfs_mkdir(&lfs, "/sfr");//打开并读文件
ret = lfs_file_open(&lfs, &file, "/sfr/boot_count", LFS_O_RDWR | LFS_O_CREAT);
//你会发现什么都没读到, ret 返回 0
ret = lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));//指针绕回到文件开始, 并把9写入文件
boot_count = 9;
ret = lfs_file_rewind(&lfs, &file);
ret = lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
ret = lfs_file_close(&lfs, &file);
//重新打开文件,并把文件数据读到变量
boot_count = -1;
ret = lfs_file_open(&lfs, &file, "/sfr/boot_count", LFS_O_RDWR | LFS_O_CREAT);
ret = lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
ret = lfs_file_close(&lfs, &file);//卸载文件系统
ret = lfs_unmount(&lfs);//读出发现, 果然是9
printf("boot_count: %d\n", boot_count);
ret = fwrite(s_flashmem, 1, sizeof(s_flashmem), fpImage);
fclose(fpImage);
}
添加了几行代码, 可以把 内存中 littlefs镜像文件保存到电脑磁盘.
在线
离线