一、簡介
LittleFS是一個小型的Flash文件系統,它結合日志結構(log-structured)文件系統和COW(copy-on-write)文件系統的思想,以日志結構存儲元數據,以COW結構存儲數據。這種特殊的存儲方式,使LittleFS具有強大的掉電恢復能力(power-loss resilience)。分配COW數據塊時LittleFS采用了名為統計損耗均衡的動態損耗均衡算法,使Flash設備的壽命得到有效保障。同時LittleFS針對資源緊缺的小型設備進行設計,具有極其有限的ROM和RAM占用,并且所有RAM的使用都通過一個可配置的固定大小緩沖區進行分配,不會隨文件系統的擴大占據更多的系統資源。當在一個資源非常緊缺的小型設備上,尋找一個具有掉電恢復能力并支持損耗均衡的Flash文件系統時,LittleFS是一個比較好的選擇。
LittleFS在嵌入式開發過程中經常遇到,但是如何在OpenHarmony中使用呢?本文基于OpenHarmony 3.1Release + 小凌派-RK2206開發板做LittleFS文件系統移植,現將相關移植經驗發布,分享給大家。文中如有問題,請大家幫忙指正。
二、LittleFS移植過程
本文基于OpenHarmony3.1Release做LittleFS移植,小凌派-RK2206開發板內部Flash有8MB大小,其中4~8MB區間為空閑區域。我將4M~5M作為LittleFS文件系統的/data目錄掛載硬件設備。具體移植過程主要如下所示:
1、hcs配置
1.1、hdf.hcs
創建/device/soc/rockchip/rk2206/hcs_config/hdf.hcs文件,具體如下:
#include "device_info/device_info.hcs"#include "fs/fs_config.hcs"#include "gpio/gpio_config.hcs"#include "i2c/i2c_config.hcs"#include "spi/spi_config.hcs"root { module = "rockchip,rk2206_chip";}
如上所述,我將在device_info/device_info.hcs添加LittleFS設備,并在fs/fs_config.hcs添加LittleFS具體信息。
1.2、BUILD.gn
新建//device/soc/rockchip/rk2206/hdf_config/BUILD.gn,具體代碼如下所示:
import("http://drivers/adapter/khdf/liteos_m/hdf.gni")module_switch = defined(LOSCFG_DRIVERS_HDF)module_name = get_path_info(rebase_path("."), "name")hdf_driver(module_name) { hcs_sources = [ "hdf.hcs" ]}
上述代碼將在編譯OpenHarmony3.1Rlease時,將編譯hdf.hcs。
1.3、device_info.hcs
創建/device/soc/rockchip/rk2206/hcs_config/device_info/device_info.hcs文件,在文件中添加LittleFS設備,具體代碼如下所示:
device_fs :: device { device0 :: deviceNode { policy = 0; priority = 5; permission = 0777; moduleName = "HDF_PLATFORM_FS_LITTLEFS"; serviceName = "littlefs_config"; deviceMatchAttr = "rockchip_rk2206_fs_littlefs"; } }
上述代碼表示建設一個設備驅動,該驅動的模塊名稱(即moduleName)為“HDF_PLATFORM_FS_LITTLEFS”,OpenHamrony系統依據該名稱匹配驅動程序;設備匹配信息(即deviceMatchAttr)添加小凌派開發板Flash特殊信息(比如:分區信息,掛載目錄名、起始地址、結束地址等)。
1.4、fs_config.hcs
新建//device/soc/rockchip/rk2206/hdf_config/fs/fs_config.hcs文件,該文件主要寫清楚設備掛載信息,具體如下:
root { platform { fs_config { template fs_controller { match_attr = ""; mount_points = []; block_size = []; block_start = []; block_count = []; }
fs_littefs :: fs_controller { match_attr = "rockchip_rk2206_fs_littlefs"; mount_points = ["/data"]; block_size = [4096]; block_start = [1024]; block_count = [256]; } } }}
如上所述,我將LittleFS移植所需的Flash信息分為4個:
(1)mount_points:掛載目錄。
(2)block_size:Flash的擦除塊大小。
(3)block_start:該掛載Flash區域的起始塊地址,實際Flash地址為block_size * block_start。
(4)block_count:該掛載Flash區域的存儲塊總數。
注意:match_attr所表示的字符串要與device_info.hcs所表示的字符串要一致。
2、hdf驅動
新建//device/soc/rockchip/rk2206/hdf_driver/fs文件夾,文件夾下創建2個文件,具體如下所示:
2.1、fs_driver.c
2.1.1、添加必要的頭文件
#include#include#include "los_config.h"#include "hdf_log.h"#include "hdf_device_desc.h"#include "device_resource_if.h"#include "osal_mem.h"#include "lfs_api.h"
2.1.2、添加HDF驅動
添加LittleFS匹配的hdf驅動,具體代碼如下所示:
static struct HdfDriverEntry g_fsDriverEntry = { .moduleVersion = 1, .moduleName = "HDF_PLATFORM_FS_LITTLEFS", .Bind = fs_driver_bind, .Init = fs_driver_init, .Release = fs_driver_release,};
HDF_INIT(g_fsDriverEntry);
其中,moduleName必須要與device_info.hcs中的moduleName保持一致。
2.1.3、fs_driver_init函數
fs_driver_init為hdf驅動加載函數。OpenHarmony啟動時,將根據hcs的編寫匹配對應的驅動程序,并運行fs_driver_init函數。該函數負責:
(1)讀取hcs文件的配置參數。
(2)初始化Flash。
(3)適配LittleFS對應的read、write、erase和sync函數,并適配LittleFS相關參數。
(4)掛載LittleFS。
具體代碼如下所示:
static int32_t fs_driver_init(struct HdfDeviceObject *device){ int result; int32_t ret; struct FileOpInfo *file_op_info = NULL;
if (device == NULL) { PRINT_ERR("device is null\n"); return HDF_ERR_INVALID_OBJECT; } if (device->property == NULL) { PRINT_ERR("device is null\n"); return HDF_ERR_INVALID_OBJECT; }
/* Flash設備初始化 */ FlashInit();
/* 讀取hcs參數 */ ret = fs_driver_readdrs(device->property, &m_fs_cfg[0]); if (ret != HDF_SUCCESS) { PRINT_ERR("%s: fs_driver_readdrs failed(%d)\n", ret); return ret; } /* 適配LitteleFS對應的函數和參數 */ for (int i = 0; i < sizeof(m_fs_cfg) / sizeof(m_fs_cfg[0]); i++) { if (m_fs_cfg[i].mount_point == NULL) { PRINT_LOG("m_fs_cfg[%d].mount_point is null\n", i); continue; }
m_fs_cfg[i].lfs_cfg.read = flash_littlefs_read; m_fs_cfg[i].lfs_cfg.prog = flash_littlefs_write; m_fs_cfg[i].lfs_cfg.erase = flash_littlefs_erase; m_fs_cfg[i].lfs_cfg.sync = flash_littlefs_sync;
m_fs_cfg[i].lfs_cfg.read_size = 4; m_fs_cfg[i].lfs_cfg.prog_size = 4; m_fs_cfg[i].lfs_cfg.cache_size = 256; m_fs_cfg[i].lfs_cfg.lookahead_size = 64; m_fs_cfg[i].lfs_cfg.block_cycles = 1000;
m_fs_cfg[i].lfs_cfg.file_max = LFS_FILE_MAX; m_fs_cfg[i].lfs_cfg.name_max = LFS_NAME_MAX; /* 準備掛載 */ result = SetDefaultMountPath(i, m_fs_cfg[i].mount_point); if (result != VFS_OK) { PRINT_ERR("SetDefaultMountPath(%d, %d) failed(%d)\n", i, m_fs_cfg[i].mount_point, result); continue; } /* 掛載目錄 */ result = mount(NULL, m_fs_cfg[i].mount_point, "littlefs", 0, &m_fs_cfg[i].lfs_cfg); printf("%s: mount fs on '%s' %s\n", __func__, m_fs_cfg[i].mount_point, (result == 0) ? "succeed" : "failed"); if (CheckPathIsMounted(m_fs_cfg[i].mount_point, &file_op_info) == TRUE) { int lfs_ret = lfs_mkdir(&file_op_info->lfsInfo, m_fs_cfg[i].mount_point); if (lfs_ret == LFS_ERR_OK) { PRINT_LOG("create root dir(%s) success.\n", m_fs_cfg[i].mount_point); } else if (lfs_ret == LFS_ERR_EXIST) { PRINT_LOG("root dir(%s) exist.\n", m_fs_cfg[i].mount_point); } else { PRINT_LOG("create root dir(%s) failed.", m_fs_cfg[i].mount_point); } } } return HDF_SUCCESS;}
2.2、BUILD.gn
BUILD.gn負責將fs_driver.c編譯到內核中,具體源代碼如下所示:
import("http://drivers/adapter/khdf/liteos_m/hdf.gni")module_switch = defined(LOSCFG_SOC_SERIES_RK22XX) && defined(LOSCFG_DRIVERS_HDF_PLATFORM) && defined(LOSCFG_FS_LITTLEFS)module_name = get_path_info(rebase_path("."), "name")hdf_driver(module_name) { sources = [ "fs_driver.c", ] include_dirs = [ "." ]}
3、測試程序
我在main函數中添加一個任務,負責每隔5秒讀寫文件。具體代碼如下所示:
/* 文件系統測試 */
static void file_rw(){ static unsigned int cur = 0; char file_name[] = "/data/a.txt"; int fd_w, fd_r; unsigned char buffer[256];
/* 寫操作 */ fd_w = open(file_name, O_WRONLY | O_CREAT); if (fd_w == -1) { printf("write: %s open failed!\n", file_name); return; } memset(buffer, 0, sizeof(buffer)); snprintf(buffer, sizeof(buffer), "Hello World and %d\n", cur++); printf("write: %s", buffer); write(fd_w, buffer, strlen(buffer)); close(fd_w);
/* 讀操作 */ fd_r = open(file_name, O_RDONLY); if (fd_r == -1) { printf("read: %s open failed!\n", file_name); return; } lseek(fd_r, 0, SEEK_SET); memset(buffer, 0, sizeof(buffer)); read(fd_r, buffer, sizeof(buffer)); printf("read: %s", buffer); close(fd_r);}
static void IotProcess(void *arg){ static const unsigned int SLEEP_MAXSEC = 5; while (1) { printf("%s: sleep %d sec!\n", __func__, SLEEP_MAXSEC); /* 文件系統測試 */ file_rw();
LOS_Msleep(SLEEP_MAXSEC * 1000); }}
三、實驗結果
程序編譯燒寫到開發板后,按下開發板的RESET按鍵,通過串口軟件查看日志如下:
[MAIN:D]Main: OpenHarmony start schedule...
Entering scheduler
IotProcess: sleep 5 sec!
write: Hello World and 0
read: Hello World and 0
IotProcess: sleep 5 sec!
write: Hello World and 1
read: Hello World and 1
IotProcess: sleep 5 sec!
write: Hello World and 2
read: Hello World and 2
......
-
驅動
+關注
關注
12文章
1790瀏覽量
84910
發布評論請先 登錄
相關推薦
評論