2

基于OpenHarmony标准接口的文件读写实现案例

 1 year ago
source link: https://www.51cto.com/article/719065.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

基于OpenHarmony标准接口的文件读写实现案例

2022-09-19 13:49:55
本文基于小凌派-RK2206开发板 + OpenHarmony轻量级操作系统 + LitteFS文件系统,通过Hal_file标准接口实现对Flash读写功能。
3414aee25c6515fe60c8917c606c8ca41856aa.png

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

在嵌入式领域,FLASH是一种常用的存储设备,Flash闪存作为嵌入式系统的主要存储设备有其自身的特性。Fash的写入操作只能把对应位置的1修改成0,而不能把0修改为1,而擦除Fash就是把对应存储块的内容恢复为1。因此,一般情况下向Fash写入内容时,需要先擦除对应的存储区间,这种擦除是以块(Bock)为单位进行的。闪存主要有NOR和NAND两种技术。因为Flash存储器的擦写次数是有限的,NAND闪存还有特殊的硬件接口和读写时序,于是就出现了专门针对FLASH的文件系统。比较常用的有jffs2,yaffs2,logfs,ubifs。本文基于小凌派-RK2206开发板 + OpenHarmony轻量级操作系统 + LitteFS文件系统,通过hal_file标准接口实现对Flash读写功能。

二、hal_file标准接口

头文件://utils/native/lite/hals/file/hal_file.h。

1、HalFileOpen()

打开/创建文件,类似于Linux的open函数。

int HalFileOpen(const char *path, int oflag, int mode);

参数说明:

基于OpenHarmony标准接口的文件读写实现案例-开源基础软件社区

返回值为LOS_OK表示成功,其余为失败。

2、HalFileClose()

关闭文件,类似于Linux的close函数。

int HalFileClose(int fd);

参数说明:

基于OpenHarmony标准接口的文件读写实现案例-开源基础软件社区

返回值为LOS_OK表示成功,其余为失败。

3、HalFileRead()

从文件中读取一段内容,类似于Linux的read函数。

int HalFileRead(int fd, char* buf, unsigned int len);

参数说明:

基于OpenHarmony标准接口的文件读写实现案例-开源基础软件社区

返回值为从文件读取内容的大小,0或者小于0则为失败。

4、HalFileWrite()

往文件写入一段内容,类似于Linux的write函数。

int HalFileWrite(int fd, const char* buf, unsigned int len);

参数说明:

基于OpenHarmony标准接口的文件读写实现案例-开源基础软件社区

返回值为成功写入到文件的内容大小,0或者小于0则为失败。

5、HalFileDelete()

删除文件,类似于Linux的unlink函数。

int HalFileDelete(const char* path);

参数说明:

基于OpenHarmony标准接口的文件读写实现案例-开源基础软件社区

返回值为LOS_OK为成功,其余则为失败。

6、HalFileStat()

获取文件大小,类似于Linux的stat函数。

int HalFileStat(const char* path, unsigned int* fileSize);

参数说明:

基于OpenHarmony标准接口的文件读写实现案例-开源基础软件社区

返回值为LOS_OK为成功,其余则为失败。

7、HalFileSeek()

文件所在位置移动,类似于Linux的lseek函数。

int HalFileSeek(int fd, int offset, unsigned int whence);

参数说明:

基于OpenHarmony标准接口的文件读写实现案例-开源基础软件社区

返回值为LOS_OK为成功,其余则为失败。

三、程序设计

本例程演示如何在小凌派-RK2206开发板上使用鸿蒙LiteOS-M内核接口,进行文件读写开发。例程流程如下所示:

(1)创建一个文件。

(2)每5秒进行1次文件读写操作。

(3)文件标识移动到文件起始处,读文件内容,并打印。

(4)文件标识移动到文件起始处,写文件内容。

(5)循环上述的第2~4步骤。

1、任务创建代码分析

在file_example函数中通过LOS_TaskCreate函数创建一个线程:hal_file_thread。

void file_example()
{
    unsigned int thread_id;
    TSK_INIT_PARAM_S task = {0};
    unsigned int ret = LOS_OK;
    task.pfnTaskEntry = (TSK_ENTRY_FUNC)hal_file_thread;
    task.uwStackSize = 1024 * 10;
    task.pcName = "hal_file_thread";
    task.usTaskPrio = 25;
    ret = LOS_TaskCreate(&thread_id, &task);
    if (ret != LOS_OK)
    {
        printf("Falied to create hal_file_thread ret:0x%x\n", ret);
        return;
    }
}
APP_FEATURE_INIT(file_example);

2、文件读写代码分析

hal_file_thread函数负责打开文件,每5秒移动到文件头读取数据,再移动到文件头写入一段内容,重复以上流程。

void hal_file_thread()
{
    int fd;
    char buffer[1024];
    int read_length, write_length;
    int current = 0;

    /* 打开文件,如果没有该文件就创建,如有该文件则打开
     * O_TRUNC_FS => 清空文件内容
     */
    //fd = HalFileOpen(FILE_NAME, O_RDWR_FS | O_CREAT_FS, 0);
    fd = HalFileOpen(FILE_NAME, O_RDWR_FS | O_CREAT_FS | O_TRUNC_FS, 0);
    if (fd == -1)
    {
        printf("%s HalFileOpen failed!\n", FILE_NAME);
        return;
    }
    while (1)
    {
        /* 文件位置移动到文件开始位置 */
        HalFileSeek(fd, 0, SEEK_SET);
        memset(buffer, 0, sizeof(buffer));
        /* 读取文件内容 */
        read_length = HalFileRead(fd, buffer, sizeof(buffer));
        printf("read: \n");
        printf("    length = %d\n", read_length);
        printf("    content = %s\n", buffer);
        /* 文件位置移动到文件开始位置 */
        HalFileSeek(fd, 0, SEEK_SET);
        memset(buffer, 0, sizeof(buffer));
        snprintf(buffer, sizeof(buffer), "Hello World(%d) => ", current);
        /* 写入文件 */
        write_length = HalFileWrite(fd, buffer, strlen(buffer));
        current++;
        LOS_Msleep(5000);
    }
    HalFileClose(fd);
}

四、编译过程

1、搭建和下载源代码

我已将OpenHarmony源代码上传到Gitee社区中,大家可以根据以下网址下载。

https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts。

注意:编译环境可根据以下网址来操作:https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts/blob/master/vendor/lockzhiner/rk2206/README_zh.md。

2、修改编译脚本

修改 vendor/lockzhiner/rk2206/sample 路径下 BUILD.gn 文件,指定 a7_hal_file 参与编译。

“./a7_hal_file:hal_file_example”,

修改 device/lockzhiner/rk2206/sdk_liteos 路径下 Makefile 文件,添加 -lhal_file_example 参与编译。

apps_LIBS = -lhal_file_example

3、编译固件

hb set -root .
hb set
hb build -f

4、烧写固件

请参考Gitee网址的说明手册(“烧录打印”章节):https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk2206-openharmony3.0lts/blob/master/device/rockchip/README_zh.md。

五、实验结果

程序编译烧写到开发板后,按下开发板的RESET按键,通过串口软件查看日志如下:

HalFileInit: Flash Init Successful!
read:
    length = 0
    content =
read:
    length = 18
    content = Hello World(0) =>
read:
    length = 18
content = Hello World(1) =>

好了,今天的课程就到这里,我们下次再见!

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: ​​51CTO开源基础软件社区

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK