armink / EasyFlash

Lightweight IoT device information storage solution: KV/IAP/LOG. | 轻量级物联网设备信息存储方案:参数存储、在线升级及日志存储 ,全新一代版本请移步至 https://github.com/armink/FlashDB
MIT License
1.99k stars 760 forks source link
embedded flash iap iot key-value kv log

EasyFlash

GitHub release GitHub commits MIT licensed

1、介绍(English

提示 :从 EasyFlash V4.1 后,基于 EasyFlash 全新设计开发的 FlashDB 开源项目正式上线,新集成了时序数据库、多分区管理,多数据库实例等功能,也从一定程度上提升了整体性能,欢迎关注:https://github.com/armink/FlashDB 。同时,现有的 EasyFlash 也会继续维护。

EasyFlash是一款开源的轻量级嵌入式Flash存储器库,方便开发者更加轻松的实现基于Flash存储器的常见应用开发。非常适合智能家居、可穿戴、工控、医疗、物联网等需要断电存储功能的产品,资源占用极低,支持各种 MCU 片上存储器。该库主要包括 三大实用功能

EasyFlash不仅能够实现对产品的 设定参数运行日志 等信息的掉电保存功能,还封装了简洁的 增加、删除、修改及查询 方法, 降低了开发者对产品参数的处理难度,也保证了产品在后期升级时拥有更好的扩展性。让Flash变为NoSQL(非关系型数据库)模型的小型键值(Key-Value)存储数据库。

该库封装了IAP(In-Application Programming)功能常用的接口,支持CRC32校验,同时支持Bootloader及Application的升级。

非常适合应用在小型的不带文件系统的产品中,方便开发人员快速定位、查找系统发生崩溃或死机的原因。同时配合EasyLogger(我开源的超轻量级、高性能C日志库,它提供与EasyFlash的无缝接口)一起使用,轻松实现C日志的Flash存储功能。

1.1 两种 ENV 模式

目前 ENV 功能有两种主要模式,一种为 V4.0 带来的 NG 模式,还有一种为延续 V3.0 的 legacy 模式

1.1.1、V4.0 引入的 NG 模式

对应源码文件为: ef_env.c

自 2019 年春节后,EasyFlash 经过 4 年多的迭代,结合众多开发者的需求及建议,终于发布了 V4.0 版本,该版本中的 ENV 功能被命名为 NG (Next Generation) 模式,这是一个完全重构的新版本,具有以下新特性:

V4.0 设计及内部原理,V4.0 迁移指南等更多内容请继续阅读下面的 文档章节

注意 :个别 Flash 存在无法逆序写入的问题,例如 STM32L4 片内 Flash,所以无法使用 NG 模式,这种情况下建议使用 V3.0 的 legacy 模式

1.1.2、延续 V3.0 的 legacy 模式

对应源码文件为: ef_env_legacy.cef_env_legacy_wl.c

legacy 模式也具有磨损平衡及掉电保护功能,相比于 V 4.0 NG 模式,使用 legacy 模式,需要有额外的 RAM 空间来临时缓存每个 ENV ,最终调用 save 接口,统一擦除扇区再存储到 Flash 上。

1.1.3 ENV 模式对比

V4.0 NG 模式 V3.0 legacy 模式
RAM 资源占用
支持 Flash 全面性 个别 Flash 受限:例如 STM32L4 片内 比较全面
是否需要 GC 垃圾回收 需要 GC ,这会导致触发 GC 时,写入速度变慢 不需要
value 类型限制 无限制 对字符串类型支持的比较好
掉电保护 支持 支持
磨损平衡 支持 支持
增量升级 支持 支持

1.2、资源占用

最低要求: ROM: 6K bytes     RAM: 0.1K bytes

1.3、支持平台

目前已移植硬件平台有 stm32f10xstm32f4xx 系列的片内Flash,片外的 SPI Flash(基于 SFUD),这些也是笔者产品使用的平台。其余平台的移植难度不大,在项目的设计之初就有考虑针对所有平台的适配性问题(64位除外),所以对所有移植接口都有做预留。移植只需修改 \easyflash\port\ef_port.c 一个文件,实现里面的擦、写、读及打印功能即可。

欢迎大家 fork and pull request(Github|OSChina|Coding) 。如果觉得这个开源项目很赞,可以点击项目主页 右上角的 Star,同时把它推荐给更多有需要的朋友。

2、流程

2.1、ENV:环境变量(KV数据库)

下图为通过控制台(终端)来调用环境变量的常用接口,演示了以下过程,这些接口都支持被应用层直接调用。

easy_flash_env

2.2、IAP:在线升级

下图演示了通过控制台来进行IAP升级软件的过程,使用的是库中自带的IAP功能接口,演示采用的是串口+Ymodem协议的方式。你还也可以实现通过CAN、485、以太网等总线,来实现远程网络更新。

easy_flash_iap

2.3、Log:日志存储

下图过程为通过控制台输出日志,并将输出的日志存储到Flash中。重启再读取上次保存的日志,最后清空Flash日志。

easy_flash_log

3、文档

务必保证在 阅读文档 后再移植使用。

4、支持

support

如果 EasyFlash 解决了你的问题,不妨扫描上面二维码请我 喝杯咖啡~

5、许可

采用 MIT 开源协议,细节请阅读项目中的 LICENSE 文件内容。


1 Introduction

EasyFlash is an open source lightweight embedded flash memory library. It provides convenient application interface for MCU (Micro Control Unit). The developers can achieve more efficient and common application development based on Flash memory. The library currently provides three useful features

EasyFlash can store setting parameters or running logs and other information which you want to keep after power down. It contains add, delete, modify and query methods. It helps developer to process the product parameters, and makes sure the product has better scalability after upgrade. Turns the Flash into a small NoSQL (non-relational databases) model and Key-Value stores database.

The library encapsulates the IAP (In-Application Programming) feature common interface. Support CRC32 checksum. While supporting the bootloader and application upgrade.

It's very suitable for small without a file system products. The developer can easy to locate and query problem when system crashes or freezes. You can use EasyLogger( A super-lightweight, high-performance C log library which open source by me. It provides a seamless interface with EasyFlash) at the same time. So, it's so easy to store the logs to flash.

1.1 Resource consumption

Minimum : ROM: 6K bytes     RAM: 0.2K bytes

1.2 Supported platforms

Hardware platform has been ported SPI Flash, stm32f10x and stm32f4xx series of on-chip Flash. These are my product platforms. Remaining platform porting difficulty is little. To port it just modify \easyflash\port\ef_port.c file. Implement erase, write, read, print feature.

Welcome everyone to fork and pull request(Github|OSChina|Coding). If you think this open source project is awesome. You can press the Star on the top right corner of project home page, and recommend it to more friends.

2 Flow

2.1 Env(KV database)

The figure below shows an ENV's common interface be called by the console (terminal). These interfaces are supported by the application layer called.

easy_flash_env

2.2 IAP

The figure below shows the process of upgrading software through the console by IAP. It use this library comes with IAP function interface. It uses a serial port + Ymodem protocol mode. You can also be achieved through CAN, 485, Ethernet bus to online upgrade.

easy_flash_iap

2.3 Log

The following figure is the output of the log process through the console. The logs are saved to flash at real time. Then the board is rebooted and the logs back are read back from flash. At last logs will be erased.

easy_flash_log

3 Documents

All documents are in the \docs\en\ folder. Please read the documents before porting it and using it.

4 License

Using MIT open source license, please read the project LICENSE file.