提示:從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存儲功能。
目前ENV 功能有兩種主要模式,一種為V4.0 帶來的NG模式,還有一種為延續V3.0 的legacy模式
對應源碼文件為:
ef_env.c
自2019 年春節後,EasyFlash 經過4 年多的迭代,結合眾多開發者的需求及建議,終於發布了V4.0 版本,該版本中的ENV 功能被命名為NG (Next Generation) 模式,這是一個完全重構的新版本,具有以下新特性:
V4.0 設計及內部原理,V4.0 遷移指南等更多內容請繼續閱讀下面的文檔章節
注意:個別Flash 存在無法逆序寫入的問題,例如STM32L4 片內Flash,所以無法使用NG 模式,這種情況下建議使用V3.0 的legacy 模式
對應源碼文件為:
ef_env_legacy.c及ef_env_legacy_wl.c
legacy模式也具有磨損平衡及掉電保護功能,相比於V 4.0 NG 模式,使用legacy 模式,需要有額外的RAM 空間來臨時緩存每個ENV ,最終調用save 接口,統一擦除扇區再存儲到Flash 上。
| V4.0 NG 模式 | V3.0 legacy 模式 | |
|---|---|---|
| RAM 資源佔用 | 低 | 高 |
| 支持Flash 全面性 | 個別Flash 受限:例如STM32L4 片內 | 比較全面 |
| 是否需要GC 垃圾回收 | 需要GC ,這會導致觸發GC 時,寫入速度變慢 | 不需要 |
| value 類型限制 | 無限制 | 對字符串類型支持的比較好 |
| 掉電保護 | 支持 | 支持 |
| 磨損平衡 | 支持 | 支持 |
| 增量升級 | 支持 | 支持 |
最低要求: ROM: 6K bytes RAM: 0.1K bytes
目前已移植硬件平台有stm32f10x與stm32f4xx系列的片內Flash,片外的SPI Flash(基於SFUD),這些也是筆者產品使用的平台。其餘平台的移植難度不大,在項目的設計之初就有考慮針對所有平台的適配性問題(64位除外),所以對所有移植接口都有做預留。移植只需修改easyflashportef_port.c一個文件,實現裡面的擦、寫、讀及打印功能即可。
歡迎大家fork and pull request (Github|OSChina|Coding) 。如果覺得這個開源項目很贊,可以點擊項目主頁右上角的Star ,同時把它推薦給更多有需要的朋友。
下圖為通過控制台(終端)來調用環境變量的常用接口,演示了以下過程,這些接口都支持被應用層直接調用。
temp ,並且賦值為123 ;456並保存、重啟;
下圖演示了通過控制台來進行IAP升級軟件的過程,使用的是庫中自帶的IAP功能接口,演示採用的是串口+Ymodem協議的方式。你還也可以實現通過CAN、485、以太網等總線,來實現遠程網絡更新。

下圖過程為通過控制台輸出日誌,並將輸出的日誌存儲到Flash中。重啟再讀取上次保存的日誌,最後清空Flash日誌。

docszhapi.mddocszhport.mddocszhv4_migrate.mddocszhdesign.md務必保證在閱讀文檔後再移植使用。

如果EasyFlash 解決了你的問題,不妨掃描上面二維碼請我喝杯咖啡~
採用MIT 開源協議,細節請閱讀項目中的LICENSE 文件內容。
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.
Minimum : ROM: 6K bytes RAM: 0.2K bytes
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 easyflashportef_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.
The figure below shows an ENV's common interface be called by the console (terminal). These interfaces are supported by the application layer called.
temp and value is 123 ;456 and save, reboot;
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.

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.

All documents are in the docsen folder. Please read the documents before porting it and using it.
Using MIT open source license, please read the project LICENSE file.