1 Star 0 Fork 113

王栓 / drivers_liteos

forked from OpenHarmony / drivers_liteos 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
BSD-3-Clause

驱动

简介

内核驱动是软件与硬件交互的桥梁,通过文件系统接口访问OpenHarmony内核的硬件资源,是用户与内核之间、进程与进程之间通信的一种方式。每类驱动代表一种能力,用户可以根据需求选择对应驱动,完成数据的传输。内核初始化驱动并注册设备节点,用户通过文件系统接口操作该设备节点。内核驱动主要包括mem、random、video、quickstart、hievent和tzdriver,其中mem、random、video和quickstart位于kernel/liteos_a/drivers/char目录下,hievent和tzdrivert位于drivers/liteos目录下。驱动架构图如下所示:

图 1 内核驱动架构图

其中:

  • mem驱动用于用户态访问物理IO设备,与mmap接口结合使用;
  • random驱动用于获取随机数,包括真随机和伪随机两种设备驱动,设备节点分别为/dev/random和/dev/urandom,通过真随机设备节点获取的数据随机性相对较高;
  • video相关实现是framebuffer驱动框架,用户需要实现初始化接口完成驱动注册,通过文件系统接口操作framebuffer驱动。

目录

/drivers/liteos
├── hievent         # 事件日志管理驱动
├── include         # 对外头文件存放目录
├── tzdriver        # 用于ree/tee切换、通讯,提供应用层访问的设备节点

约束

tzdriver驱动实现部分尚未开源,三方厂商需要通过合作方式获取该组件的支持。

hievent驱动的配套功能暂不支持,待后续开源,请勿修改该驱动定义的接口和接口参数格式。

video源码实现在third_party/NuttX下面:video/fb.c和video/fb.h。

接口说明

由上文内核驱动框架图可以看出,内核驱动可以看成特殊的文件,用户调用文件系统标准接口,即可完成对驱动的读写操作,有open、close、read、write和ioctl等标准接口,不再阐述。下面列举各个驱动相关接口:

  • fb_register

    函数原型:

    int fb_register(int display, int plane);

    **函数功能:**加载framebuffer驱动,注册设备节点/dev/fb0;若成功则返回0, 否则返回相应的错误码。

    参数说明:

    参数

    描述

    display

    显示层编号,通常为0。若硬件支持多显示层,该变量可设置成其他值。

    plane

    标识颜色平面,通常为0。

  • fb_unregister

    函数原型:

    int fb_unregister(int display);

    **函数功能:**卸载framebuffer驱动;若成功则返回0, 否则返回相应的错误码。

    参数说明:

    参数

    描述

    display

    显示层编号。

  • up_fbinitialize

    函数原型:

    int up_fbinitialize(int display);

    **函数功能:**初始化framebuffer驱动,这个接口需要用户通过调用fb_register注册设备节点来实现,以提供framebuffer硬件驱动功能;若成功则返回0,否则返回相应的错误码。

    参数说明:

    参数

    描述

    display

    显示层编号。

  • up_fbuninitialize

    函数原型:

    void up_fbuninitialize(int display);

    **函数功能:**卸载framebuffer硬件驱动,这个接口需要用户通过调用fb_unregister注销设备节点来实现;无返回值。

    参数说明:

    参数

    描述

    display

    显示层编号。

  • up_fbgetvplane

    函数原型:

    struct fb_vtable_s *up_fbgetvplane(int display, int vplane);

    **函数功能:**获取framebuffer硬件驱动的句柄,这个接口需要用户实现;若成功则返回有效指针,否则返回NULL。

    参数说明:

    参数

    描述

    display

    显示层编号。

    vplane

    指定的颜色平面。

  • pse_ran_dev_register

    函数原型:

    int pse_ran_dev_register(void);

    **函数功能:**初始化软随机设备驱动,注册设备节点/dev/random;若成功则返回0, 否则返回相应的错误码。

    **参数说明:**无

  • ran_dev_register

    函数原型:

    int ran_dev_register(void);

    **函数功能:**初始化硬随机设备驱动,注册设备节点/dev/urandom;若成功则返回0, 否则返回相应的错误码。

    **参数说明:**无

  • mem_dev_register

    函数原型:

    int mem_dev_register(void);

    **函数功能:**初始化mem驱动,注册设备节点/dev/mem;若成功则返回0,否则返回相应的错误码。

    **参数说明:**无

使用说明

  • 以framebuffer驱动操作为例:
int up_fbinitialize(int display)
{
    //具体代码逻辑由用户实现,提供framebuffer硬件驱动能力;
}

void up_fbuninitialize(int display)
{
    //具体代码逻辑由用户实现,与up_fbinitialize匹配;
}

struct fb_vtable_s *up_fbgetvplane(int display, int vplane)
{
    //具体代码逻辑由用户实现,根据fb_vtable_s结构体类型信息注册硬件设备驱动,用户通过video框架层调用这些能力;
}

int FrameBufferFunc(void)
{
    int ret;
    int fd = -1;
    struct hifb_info info;
    char *pShowScreen = NULL;

    ret = fb_register(0, 0); //注册/dev/fb0设备节点,该接口会调用上述接口,完成硬件驱动能力使能;
    if (ret != 0) {
        return -1;
    }

    fd = open(file, O_RDWR, 0);  
    if (fd < 0) {
        return -1;
    }
 
    if (ioctl(fd, FBIOGET_SCREENINFO_HIFB, &info) < 0) {
        return -1;
    }
    info.vinfo.xres = 1920;
    info.vinfo.yres = 1080;
    info.oinfo.sarea.w = 1920;
    info.oinfo.sarea.h = 1080;
    info.oinfo.bpp = 16;
    info.activate = 0;
    info.vinfo.fmt = HIFB_FMT_ARGB1555;

    if (ioctl(fd, FBIOPUT_SCREENINFO_HIFB, &info) < 0) {
        return -1;
    }

    if (ioctl(fd, FBIOGET_SCREENINFO_HIFB, &info) < 0) {
        return -1;
    }

    pShowScreen = mmap(HI_NULL, info.oinfo.fblen, PROT_READ | PROT_WRITE, MAP_SHARED, pstInfo->fd, 0);
    if (pShowScreen == -1) {
        return -1;
    }

    //填充pShowScreen指针指向的内存,通过ioctl命令,显示图形;

    munmap(pShowScreen, info.oinfo.fblen); 
  
    close(fd);

    ret = fb_unregister(0);
    if (ret != 0) {
        return -1;
    }
}

相关仓

内核子系统

drivers_liteos

kernel_liteos_a

Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved. Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

简介

Liteos_a kernel-space drivers | liteos_a内核态驱动 展开 收起
BSD-3-Clause
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/wang-shuan/drivers_liteos.git
git@gitee.com:wang-shuan/drivers_liteos.git
wang-shuan
drivers_liteos
drivers_liteos
master

搜索帮助