HAL框架

本文详细解析HAL层的三个关键结构体:hw_module_methods_t、hw_module_t和hw_device_t,阐述了HAL框架提供的核心函数hw_get_module,并通过实例展示了如何创建设备并操作设备。介绍了从模块ID到设备操作的客户端调用流程,以及设备关闭和状态管理的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HAL层结构体

HAL只有三个struct结构:
hw_module_methods_t (1)
hw_module_t (2)
hw_device_t (3)

typedef struct hw_module_methods_t {//硬件模块方法列表的定义,这里只定义了一个open函数  
    /**并不是真正的操作设备,只是初始化一个hw_device_t结构体,这个结构体中含有设备处理函数,所以
    是间接‘打开’设备*/  
    int (*open)(const struct hw_module_t* module, const char* id, 
            struct hw_device_t** device);  
} hw_module_methods_t; 
typedef struct hw_module_t {  
    /** tag must be initialized to HARDWARE_MODULE_TAG */  
    uint32_t tag;  
    /** major version number for the module */  
    uint16_t version_major;  
    /** minor version number of the module */  
    uint16_t version_minor;  
    /** Identifier of module */  
    const char *id;  
    /** Name of this module */  
    const char *name;  
    /** Author/owner/implementor of the module */  
    const char *author;  
    /** Modules methods */  
    //模块方法列表,指向hw_module_methods_t*  
    struct hw_module_methods_t* methods;  
    /** module's dso */  
    void* dso;  
    /** padding to 128 bytes, reserved for future use */  
    uint32_t reserved[32-7];  
} hw_module_t;
typedef struct hw_device_t {  
    /** tag must be initialized to HARDWARE_DEVICE_TAG */  
    uint32_t tag;  
    /** version number for hw_device_t */  
    uint32_t version;  
    /** reference to the module this device belongs to */  
    struct hw_module_t* module;  
    /** padding reserved for future use */  
    uint32_t reserved[12];  
    /** Close this device */  
    int (*close)(struct hw_device_t* device);  
} hw_device_t; 

HAL API

HAL框架提供了一个公用函数:

hw_get_module(const char *id,const struct hw_module_t** module);

功能:根据模块id(module_id)去朝找注册在当前系统中与id对应的硬件对象,然后载入(load)其相应的HAL层模块的.so
文件。载入以后从.so文件中查找”HMI”这个符号,如果在.so代码中有定义的函数名或者变量名为HMI,返回其地址(存在第二个参数当中)。

实例

结构体声明:

    struct led_module_t{
        struct hw_module_t common;
        int status;
    };
    struct led_device_t{
        struct hw_device_t common;
        int (*set_on)(struct led_device_t* dev);
        int (*set_off)(struct led_device_t* dev);
    };

实现:

static int led_device_close(struct hw_device_t *device){
    struct led_deivce_t ldev=(struct led_device_t *)device;
    if(ldev)
        free(ldev);
    return 0;
}

static int led_set_on(struct led_deivce_t *dev){
    //调用HAL_Drive
    return 0;
}

static int led_set_off(struct led_deivce_t *dev){
    ////调用HAL_Drive
    return 0;
}

static int led_open(const struct hw_module_t* module,const char *name,struct hw_device_t **device){
    struct led_deivce_t *dev;
    LOGD("led_dev_open");
    dev=(struct led_deivce_t*)malloc(sizeof(struct led_deivce_t));
    memset(dev,0,sizeof(struct led_deivce_t));

    dev->common.tag=HARDWARE_DEVICE_TAG;
    dev->common.version=0;
    dev->common.module=(struct hw_module_t*)module;
    dev->common.close=led_device_close;
    dev->set_on=led_set_on;
    dev->set_off=led_set_off;
    *device=(struct hw_device_t*)dev;
    return 0;
}
const struct led_module_t HAL_MODULE_INFO_SYM={{HARDWARE_DEVICE_TAG,1,0,LED_HARDWARE_MODULE_ID,"TestLEDSTub","Test Project Team",&my_methods},-1};
static struct hw_module_methods_t my_methods={led_open}

客户端调用流程

(1)hw_get_module(const char id,const struct hw_module_t* module) 获取到hw_module_t对象的指针。
(2)hw_module_t中保存了函数指针表,查找到hw_module_methods_t中的led_open函数。
(3)led_open会创建hw_device_t对象,最后调用hw_device_t对象的函数(led_set_on/led_set_off)操作设备。
led_set_on—->HAL_Driver(不开源)—->Linux_Driver(开源)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值