File systems for embedded systems and IoT devices have some additional requirements:
Power-cut resilience - it requires strong guarantees that the file system remains consistent, and data is flushed to the underlying storage.
Wear-leveling - typically storage supports a limited number of erases per block, so making use of the entire storage device is important for reliability.
Tiny footprint - IoT devices are constrained by ROM and RAM. A small footprint saves money.
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
用数组来模拟falsh的。
- #include "lfs.h"
- #include "lfs_emubd.h"
- #include "lfs_ram.h"
- // variables used by the filesystem
- lfs_t lfs;
- lfs_file_t file;
- // configuration of the filesystem is provided by this struct
- struct lfs_config cfg = {
- // block device operations
- .read = lfs_ram_read,
- .prog = lfs_ram_prog,
- .erase = lfs_ram_erase,
- .sync = lfs_ram_sync,
- // block device configuration
- .read_size = 16,
- .prog_size = 16,
- .block_size = 1024,
- .block_count =10240,
- .lookahead = 128,
- .context=&cfg,
- };
- // entry point
- int main(void)
- {
- // 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) {
- lfs_format(&lfs, &cfg);
- lfs_mount(&lfs, &cfg);
- }
- // read current count
- uint32_t boot_count = 0;
- lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
- lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
- // update boot count
- boot_count += 1;
- lfs_file_rewind(&lfs, &file);
- lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
- // remember the storage is not updated until the file is closed successfully
- lfs_file_close(&lfs, &file);
- // release any resources we were using
- lfs_unmount(&lfs);
- // print the boot count
- printf("boot_count: %d
", boot_count);
- //test
- boot_count=0;
- lfs_mount(&lfs, &cfg);
- lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
- lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
- printf("boot_count: %d
", boot_count);
- boot_count++;
- lfs_file_rewind(&lfs, &file);
- lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
- lfs_file_close(&lfs, &file);
- lfs_unmount(&lfs);
- //test
- boot_count=0;
- lfs_mount(&lfs, &cfg);
- lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
- lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
- printf("boot_count: %d
", boot_count);
- boot_count++;
- lfs_file_rewind(&lfs, &file);
- lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
- lfs_file_close(&lfs, &file);
- lfs_unmount(&lfs);
- //test
- boot_count=0;
- lfs_mount(&lfs, &cfg);
- lfs_file_open(&lfs, &file, "boot_count", LFS_O_RDWR | LFS_O_CREAT);
- lfs_file_read(&lfs, &file, &boot_count, sizeof(boot_count));
- printf("boot_count: %d
", boot_count);
- boot_count++;
- lfs_file_rewind(&lfs, &file);
- lfs_file_write(&lfs, &file, &boot_count, sizeof(boot_count));
- lfs_file_close(&lfs, &file);
- lfs_unmount(&lfs);
- }
复制代码一周热门 更多>