您尚未登录。

楼主 #1 2019-05-09 22:40:42

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,245
积分: 9197

发一个 VC2017 littlefs 文件系统电脑模拟工程

QQ20190509224236.png

#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

本站下载: lfs_test_vc2017_20190509.7z





离线

楼主 #2 2019-05-09 23:00:59

晕哥
管理员
所在地: 微信 whycan_cn
注册时间: 2017-09-06
已发帖子: 9,245
积分: 9197

Re: 发一个 VC2017 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镜像文件保存到电脑磁盘.





离线

#3 2019-05-10 13:45:05

oayzw
会员
注册时间: 2019-04-08
已发帖子: 23
积分: 8

Re: 发一个 VC2017 littlefs 文件系统电脑模拟工程

smile

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn