0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
会员中心
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

【S32K146 RT-thread】基于内部PFLASH的littlefs适配

RT-Thread官方账号 2025-01-14 18:32 次阅读
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

LittleFS是一个应用于单片机内部flash和外挂NOR flash的文件系统。由于它相比传统的FAT文件系统更适合于小型嵌入式系统,具有以下特点:

掉电恢复能力: 设计用于处理随机电源故障。所有文件操作都有很强的写时拷贝保证,如果断电,文件系统将恢复到上一次已知的良好状态。

动态磨损均衡: 设计考虑到闪存,并提供动态块磨损均衡。此外,littlefs可以检测坏块并在它们周围工作。

有限RAM/ROM: 被设计为使用少量内存。RAM的使用是严格限制的,这意味着RAM的使用不会随着文件系统的增长而改变。文件系统不包含无界递归,动态内存仅限于可静态提供的可配置缓冲区。

官方的详细介绍参照此链接(https://github.com/littlefs-project/littlefs/
S32K146 内部的Pflash 资源大小为1M,这个大小对普通的嵌入式开发资源是有很大的空闲的,本次试验基于内部的pflash 将后512K资源划分为文件系统分区,使用littlefs 进行管理,我们修改链接脚本把后512K资源保留出来给文件系统使用,本次试验使用的IAR环境,link file 修改如下:

d3fc9456-d262-11ef-9434-92fbcf53809c.jpgd407e130-d262-11ef-9434-92fbcf53809c.jpg

littlefs 移植适配依赖物理层的配置结构体如下:

// Configuration provided during initialization of the littlefsstruct lfs_config { // Opaque user provided context that can be used to pass // information to the block device operations void *context;
// Read a region in a block. Negative error codes are propagated // to the user. int (*read)(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size);
// Program a region in a block. The block must have previously // been erased. Negative error codes are propagated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. int (*prog)(const struct lfs_config *c, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size);
// Erase a block. A block must be erased before being programmed. // The state of an erased block is undefined. Negative error codes // are propagated to the user. // May return LFS_ERR_CORRUPT if the block should be considered bad. int (*erase)(const struct lfs_config *c, lfs_block_t block);
// Sync the state of the underlying block device. Negative error codes // are propagated to the user. int (*sync)(const struct lfs_config *c);
#ifdef LFS_THREADSAFE // Lock the underlying block device. Negative error codes // are propagated to the user. int (*lock)(const struct lfs_config *c);
// Unlock the underlying block device. Negative error codes // are propagated to the user. int (*unlock)(const struct lfs_config *c);#endif
// Minimum size of a block read in bytes. All read operations will be a // multiple of this value. lfs_size_t read_size;
// Minimum size of a block program in bytes. All program operations will be // a multiple of this value. lfs_size_t prog_size;
// Size of an erasable block in bytes. This does not impact ram consumption // and may be larger than the physical erase size. However, non-inlined // files take up at minimum one block. Must be a multiple of the read and // program sizes. lfs_size_t block_size;
// Number of erasable blocks on the device. lfs_size_t block_count;
// Number of erase cycles before littlefs evicts metadata logs and moves // the metadata to another block. Suggested values are in the // range 100-1000, with large values having better performance at the cost // of less consistent wear distribution. // // Set to -1 to disable block-level wear-leveling. int32_t block_cycles;
// Size of block caches in bytes. Each cache buffers a portion of a block in // RAM. The littlefs needs a read cache, a program cache, and one additional // cache per file. Larger caches can improve performance by storing more // data and reducing the number of disk accesses. Must be a multiple of the // read and program sizes, and a factor of the block size. lfs_size_t cache_size;
// Size of the lookahead buffer in bytes. A larger lookahead buffer // increases the number of blocks found during an allocation pass. The // lookahead buffer is stored as a compact bitmap, so each byte of RAM // can track 8 blocks. Must be a multiple of 8. lfs_size_t lookahead_size;
// Optional statically allocated read buffer. Must be cache_size. // By default lfs_malloc is used to allocate this buffer. void *read_buffer;
// Optional statically allocated program buffer. Must be cache_size. // By default lfs_malloc is used to allocate this buffer. void *prog_buffer;
// Optional statically allocated lookahead buffer. Must be lookahead_size // and aligned to a 32-bit boundary. By default lfs_malloc is used to // allocate this buffer. void *lookahead_buffer;
// Optional upper limit on length of file names in bytes. No downside for // larger names except the size of the info struct which is controlled by // the LFS_NAME_MAX define. Defaults to LFS_NAME_MAX when zero. Stored in // superblock and must be respected by other littlefs drivers. lfs_size_t name_max;
// Optional upper limit on files in bytes. No downside for larger files // but must be <= LFS_FILE_MAX. Defaults to LFS_FILE_MAX when zero. Stored // in superblock and must be respected by other littlefs drivers. lfs_size_t file_max;
// Optional upper limit on custom attributes in bytes. No downside for // larger attributes size but must be <= LFS_ATTR_MAX. Defaults to // LFS_ATTR_MAX when zero. lfs_size_t attr_max;
// Optional upper limit on total space given to metadata pairs in bytes. On // devices with large blocks (e.g. 128kB) setting this to a low size (2-8kB) // can help bound the metadata compaction time. Must be <= block_size. // Defaults to block_size when zero. lfs_size_t metadata_max;};

主要包含物理层设备的读写/擦除,及FLASH最小编程块属性配置,查看S32K146 的features 可以知道,最小擦除的sector 为4096字节,最小写操作size 为8字节,我们按照littlefs 依赖的配置结构实现对应的函数。

read 接口实现:



int lfs_mflash_read(const struct lfs_config *lfsc, lfs_block_t block, lfs_off_t off, void *buffer, lfs_size_t size){ struct lfs_mflash_ctx *ctx; uint32_t flash_addr;
assert(lfsc); ctx = (struct lfs_mflash_ctx *)lfsc->context; assert(ctx);
flash_addr = ctx->start_addr + block * lfsc->block_size + off; for(lfs_size_t i=0; i < size; i++) { ((int8_t *)buffer)[i] = *((__IO int8_t*)flash_addr); flash_addr++; }
return LFS_ERR_OK;}

prog接口实现:


int32_t mflash_drv_program(uint32_t addr,uint32_t size,uint8_t * pdata){ status_t ret;
ret = FLASH_DRV_Program(&pSSDConfig,addr,size,pdata);
return ret == STATUS_SUCCESS ? 0 : -1;}
int lfs_mflash_prog( const struct lfs_config *lfsc, lfs_block_t block, lfs_off_t off, const void *buffer, lfs_size_t size){ struct lfs_mflash_ctx *ctx; uint32_t flash_addr; int32_t ret;
ctx = (struct lfs_mflash_ctx *)lfsc->context;
flash_addr = ctx->start_addr + block * lfsc->block_size + off; ret = mflash_drv_program(flash_addr,size,(uint8_t *)buffer); return (ret == 0) ? LFS_ERR_OK : LFS_ERR_IO;}

erase 接口实现;

int32_t mflash_drv_erase(uint32_t dest,uint32_t size)

对应的配置结构如下:

d41dda12-d262-11ef-9434-92fbcf53809c.jpg

适配接口已经对应完成,littlefs 会依赖动态malloc/free 内存接口,本次是基于RT-thread 系统lfs_util.h需做如下修改:

d42e7282-d262-11ef-9434-92fbcf53809c.jpg

至此Littfs 依赖的适配接口已经完成,我们追加shell 测试命令来验证littlefs的基本创建删除文件及文件夹及读写删除试验,对应的shell 测试命令代码如下:

#include "drv_mflash.h"#include #include #include #include #include
#define SHELL_Printf rt_kprintf#define PRINTF rt_kprintf
/******************************************************************************* * Variables ******************************************************************************/
lfs_t lfs;struct lfs_config cfg;int lfs_mounted;

static void format(int argc, char **argv){ int res;
if (lfs_mounted) { SHELL_Printf("LFS is mounted, please unmount it first.\r\n"); return; }
if (argc != 2 || strcmp(argv[1], "yes")) { SHELL_Printf("Are you sure? Please issue command "format yes" to proceed.\r\n"); return; }
res = lfs_format(&lfs, &cfg); if (res) { PRINTF("\rError formatting LFS: %d\r\n", res); }
return;}
MSH_CMD_EXPORT(format,"lfs format api");
static void mount(int argc, char **argv){ int res;
if (lfs_mounted) { SHELL_Printf("LFS already mounted\r\n"); return; }
res = lfs_mount(&lfs, &cfg); if (res) { PRINTF("\rError mounting LFS\r\n"); } else { lfs_mounted = 1; }
return;}MSH_CMD_EXPORT(mount,lfs mount api);
static void unmount(int argc, char **argv){ int res;
if (!lfs_mounted) { SHELL_Printf("LFS not mounted\r\n"); return; }
res = lfs_unmount(&lfs); if (res) { PRINTF("\rError unmounting LFS: %i\r\n", res); }
lfs_mounted = 0; return;}MSH_CMD_EXPORT(unmount,lfs unmount api);
static void cd(int argc, char **argv){ SHELL_Printf( "There is no concept of current directory in this example.\r\nPlease always specify the full path.\r\n"); return;}MSH_CMD_EXPORT(cd,lfs cd api);

static void lls(int argc, char **argv){ int res; char *path; lfs_dir_t dir; struct lfs_info info; int files; int dirs;
if (!lfs_mounted) { SHELL_Printf("LFS not mounted\r\n"); return; }
if (argc > 2) { SHELL_Printf("Invalid number of parameters\r\n"); return; }
if (argc < 2) { path = "/"; } else { path = argv[1]; }
/* open the directory */ res = lfs_dir_open(&lfs, &dir, path); if (res) { PRINTF("\rError opening directory: %i\r\n", res); return; }
PRINTF(" Directory of %s\r\n", path); files = 0; dirs = 0;
/* iterate until end of directory */ while ((res = lfs_dir_read(&lfs, &dir, &info)) != 0) { if (res < 0) { /* break the loop in case of an error */ PRINTF("\rError reading directory: %i\r\n", res); break; }
if (info.type == LFS_TYPE_REG) { SHELL_Printf("%8d %s\r\n", info.size, info.name); files++; } else if (info.type == LFS_TYPE_DIR) { SHELL_Printf("% DIR %s\r\n", info.name); dirs++; } else { SHELL_Printf("%???\r\n"); } }
res = lfs_dir_close(&lfs, &dir); if (res) { PRINTF("\rError closing directory: %i\r\n", res); return; }
PRINTF(" %d File(s), %d Dir(s)\r\n", files, dirs);
return;}MSH_CMD_EXPORT(lls,lfs ls api);

static void rm(int argc, char **argv){ int res;
if (!lfs_mounted) { SHELL_Printf("LFS not mounted\r\n"); return; }
res = lfs_remove(&lfs, argv[1]);
if (res) { PRINTF("\rError while removing: %i\r\n", res); }
return;}MSH_CMD_EXPORT(rm,lfs rm api);

static void lmkdir(int argc, char **argv){ int res;
if (!lfs_mounted) { SHELL_Printf("LFS not mounted\r\n"); return; }
res = lfs_mkdir(&lfs, argv[1]);
if (res) { PRINTF("\rError creating directory: %i\r\n", res); }
return;}MSH_CMD_EXPORT(lmkdir,lfs mkdir api);
static void write(int argc, char **argv){ int res; lfs_file_t file;
if (!lfs_mounted) { SHELL_Printf("LFS not mounted\r\n"); return; }
res = lfs_file_open(&lfs, &file, argv[1], LFS_O_WRONLY | LFS_O_APPEND | LFS_O_CREAT); if (res) { PRINTF("\rError opening file: %i\r\n", res); return; }
res = lfs_file_write(&lfs, &file, argv[2], strlen(argv[2])); if (res > 0) res = lfs_file_write(&lfs, &file, "\r\n", 2);
if (res < 0) { PRINTF("\rError writing file: %i\r\n", res); }
res = lfs_file_close(&lfs, &file); if (res) { PRINTF("\rError closing file: %i\r\n", res); }
return;}MSH_CMD_EXPORT(write,lfs write api);

static void cat(int argc, char **argv){ int res; lfs_file_t file; uint8_t buf[16];
if (!lfs_mounted) { SHELL_Printf("LFS not mounted\r\n"); return; }
res = lfs_file_open(&lfs, &file, argv[1], LFS_O_RDONLY); if (res) { PRINTF("\rError opening file: %i\r\n", res); return; }
do { res = lfs_file_read(&lfs, &file, buf, sizeof(buf)); if (res < 0) { PRINTF("\rError reading file: %i\r\n", res); break; } if(res > 0) { buf[res] = '\0'; PRINTF("%s",(char *)buf); } } while (res);
res = lfs_file_close(&lfs, &file); if (res) { PRINTF("\rError closing file: %i\r\n", res); }
return;}MSH_CMD_EXPORT(cat,lfs cat api);
static void lfsinit(int argc, char **argv){ mflash_drv_init(); lfs_get_default_config(&cfg); return;}MSH_CMD_EXPORT(lfsinit,lfs init api);
static void df(int argc, char **argv){ printf("used block %d total %d\r\n",lfs_fs_size(&lfs),cfg.block_count); return;}MSH_CMD_EXPORT(df,lfs init api);

至此准备工作已经完成,我们通过shell 来验证文件系统的功能,shell 命令验证文件系统已经可以通过操作文件文件夹。

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 单片机
    +关注

    关注

    6074

    文章

    45341

    浏览量

    663711
  • 嵌入式
    +关注

    关注

    5186

    文章

    20155

    浏览量

    328975
  • RT-Thread
    +关注

    关注

    32

    文章

    1541

    浏览量

    44294
收藏 人收藏
加入交流群
微信小助手二维码

扫码添加小助手

加入工程师交流群

    评论

    相关推荐
    热点推荐

    RT-Thread的C语言编码规范

    的编程风格。同时对于使用 RT-Thread 的用户,也可通过这份文档了解 RT-Thread代码内部一些约定从而比较容易的把握到 RT-Thread 的实现方式。
    的头像 发表于 02-21 16:50 3180次阅读

    S32K146的hard fault问题解决方案

    最近有个客户使用S32K146的产品在量产之后出现了三个售后件,ABBA测试之后的结果表明失效现象跟着S32K146走;同时客户反馈说试着将其中一个售后件重新烧录程序,S32K146又正常工作了。结合这两种情况,
    的头像 发表于 11-22 11:35 4927次阅读
    <b class='flag-5'>S32K146</b>的hard fault问题解决方案

    S32K146如何用中断唤醒VLPS模式?

    S32K146的模式设置为VLPS后,触发外部中断后CLOCK_SYS_GetFreq获取的频率没有变化,如何使用中断将电源模式从VLPS恢复为RUN?
    发表于 06-01 08:13

    RT-Thread编程指南

    RT-Thread编程指南——RT-Thread开发组(2015-03-31)。RT-Thread做为国内有较大影响力的开源实时操作系统,本文是RT-Thread实时操作系统的编程指南
    发表于 11-26 16:06 0次下载

    RT-Thread用户手册

    RT-Thread用户手册——本书是RT-Thread的编程手册,用于指导在RT-Thread实时操作系统环境下如何进行编 程。
    发表于 11-26 16:16 0次下载

    RT-Thread全球技术大会:Kconfig在RT-Thread中的工作机制

    RT-Thread全球技术大会:Kconfig在RT-Thread中的工作机制               审核编辑:彭静
    的头像 发表于 05-27 14:49 2146次阅读
    <b class='flag-5'>RT-Thread</b>全球技术大会:Kconfig在<b class='flag-5'>RT-Thread</b>中的工作机制

    RT-Thread全球技术大会:RT-Thread测试用例集合案例

    RT-Thread全球技术大会:RT-Thread测试用例集合案例           审核编辑:彭静
    的头像 发表于 05-27 16:34 2657次阅读
    <b class='flag-5'>RT-Thread</b>全球技术大会:<b class='flag-5'>RT-Thread</b>测试用例集合案例

    RT-Thread学习笔记 RT-Thread的架构概述

    RT-Thread 简介 作为一名 RTOS 的初学者,也许你对 RT-Thread 还比较陌生。然而,随着你的深入接触,你会逐渐发现 RT-Thread 的魅力和它相较于其他同类型 RTOS
    的头像 发表于 07-09 11:27 5528次阅读
    <b class='flag-5'>RT-Thread</b>学习笔记 <b class='flag-5'>RT-Thread</b>的架构概述

    RT-Thread文档_RT-Thread 简介

    RT-Thread文档_RT-Thread 简介
    发表于 02-22 18:22 5次下载
    <b class='flag-5'>RT-Thread</b>文档_<b class='flag-5'>RT-Thread</b> 简介

    RT-Thread文档_RT-Thread 潘多拉 STM32L475 上手指南

    RT-Thread文档_RT-Thread 潘多拉 STM32L475 上手指南
    发表于 02-22 18:23 10次下载
    <b class='flag-5'>RT-Thread</b>文档_<b class='flag-5'>RT-Thread</b> 潘多拉 STM32L475 上手指南

    RT-Thread文档_RT-Thread SMP 介绍与移植

    RT-Thread文档_RT-Thread SMP 介绍与移植
    发表于 02-22 18:31 9次下载
    <b class='flag-5'>RT-Thread</b>文档_<b class='flag-5'>RT-Thread</b> SMP 介绍与移植

    基于RT-Thread Studio学习

    前期准备:从官网下载 RT-Thread Studio,弄个账号登陆,开启rt-thread学习之旅。
    的头像 发表于 05-15 11:00 5969次阅读
    基于<b class='flag-5'>RT-Thread</b> Studio学习

    S32K146 RT-thread】之 SPI驱动适配

    概述RT-Thread对SPI总线的驱动,抽象出了spibus的设备驱动,我们基于S32K146的硬件学习spibus设备驱动。
    的头像 发表于 11-01 08:11 1775次阅读
    【<b class='flag-5'>S32K146</b> <b class='flag-5'>RT-thread</b>】之 SPI驱动<b class='flag-5'>适配</b>

    S32K146 RT-Thread】之 使用SFUD组件驱动spi flash

    S32K146 RT-Thread】之 使用SFUD组件驱动spi flash
    的头像 发表于 11-21 01:05 1644次阅读
    【<b class='flag-5'>S32K146</b> <b class='flag-5'>RT-Thread</b>】之 使用SFUD组件驱动spi flash

    首搭RT-Thread程翧车控平台| RT-Thread程翧 S32K344 快速原型开发平台正式上市!| 产品动态

    ——基于NXPS32K344的ECU快速原型开发平台,RT-Thread程翧S32K344快速原型开发平台,是首个深度内置RT-Thread程翧车控软件平台的快速原型开发
    的头像 发表于 10-31 11:53 501次阅读
    首搭<b class='flag-5'>RT-Thread</b>程翧车控平台| <b class='flag-5'>RT-Thread</b>程翧 <b class='flag-5'>S32K</b>344 快速原型开发平台正式上市!| 产品动态