首页 > 代码库 > Android 从硬件到应用:一步一步向上爬 3 -- 硬件抽象层访问硬件驱动

Android 从硬件到应用:一步一步向上爬 3 -- 硬件抽象层访问硬件驱动

       Android 标准的硬件驱动分为两个部分,一个是运行在linux内核里的硬件驱动,而另外一部分是运行在用户空间的硬件抽象层。采用这种方法,就可以使系统具有硬件无关性,也保护了部分厂商的利益。在 Android 从硬件到应用:一步一步向上爬 1 -- 从零编写底层硬件驱动程序 中已经有了编写硬件驱动到linux内核里的步骤,下面就要接着这个工程去看看怎么在硬件抽象层增加硬件模块和我们的内核驱动程序进行交互,完成硬件控制。

进入hardware/libhardware/include/hardware目录,新建android_gpio.h:

#ifndef ANDROID_ANDROID_GPIO_INTERFACE_H  
#define ANDROID_ANDROID_GPIO_INTERFACE_H  
#include <hardware/hardware.h>  
      
__BEGIN_DECLS  
      
/*module ID*/  
#define ANDROID_GPIO_HARDWARE_MODULE_ID "android_gpio"  
/*module struct*/  
struct android_gpio_module_t {
	struct hw_module_t common;  
};       
/*interface struct*/  
struct android_gpio_device_t {
	struct hw_device_t common;  
	int fd;  
	int (*set_val)(struct android_gpio_device_t* dev, int val);  
	int (*get_val)(struct android_gpio_device_t* dev, int* val);  
}; 
__END_DECLS
#endif  
其中set_val和get_val是HAL层向上层应用提供的API接口。

cd到hardware/libhardware/modules目录,新建android_gpio目录,在里面新建android.c文件:

#include <hardware/hardware.h>  
#include <hardware/android_gpio.h>  
#include <fcntl.h>  
#include <errno.h>  
#include <cutils/log.h>  
#include <cutils/atomic.h>  
#define DEVICE_NAME "/dev/AdrIO"  
#define MODULE_NAME "Android_gpio"  

//open and close
static int android_gpio_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);  
static int android_gpio_device_close(struct hw_device_t* device);  
//device access
static int android_gpio_set_val(struct android_gpio_device_t* dev, int val);  
static int android_gpio_get_val(struct android_gpio_device_t* dev, int* val);  

static struct hw_module_methods_t android_gpio_module_methods = {  
    open: android_gpio_device_open  
};  

struct android_gpio_module_t HAL_MODULE_INFO_SYM = {  
    common: {  
        tag: HARDWARE_MODULE_TAG,  
        version_major: 1,  
        version_minor: 0,  
        id: ANDROID_GPIO_HARDWARE_MODULE_ID,  
        name: MODULE_NAME,  
        author: "HAL",  
        methods: &android_gpio_module_methods, }  
}; 

static int android_gpio_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) 
{  
	struct android_gpio_device_t* dev;
	dev = (struct android_gpio_device_t*)malloc(sizeof(struct android_gpio_device_t));  
	memset(dev, 0, sizeof(struct android_gpio_device_t));  
	dev->common.tag = HARDWARE_DEVICE_TAG;  
	dev->common.version = 0;  
	dev->common.module = (hw_module_t*)module;  
	dev->common.close = android_gpio_device_close;  
	dev->set_val = android_gpio_set_val;
	dev->get_val = android_gpio_get_val;  
	if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {  
		LOGE("android_gpio: failed to open /dev/AdrIO -- %s.", strerror(errno));
		free(dev);  
		return -EFAULT;  
	}  
	*device = &(dev->common);  
	return 0;  
}  

static int android_gpio_device_close(struct hw_device_t* device) 
{  
	struct android_gpio_device_t* android_gpio_device = (struct android_gpio_device_t*)device;  

	if(android_gpio_device) {  
	    close(android_gpio_device->fd);  
	    free(android_gpio_device);  
	}    
	return 0;  
}  

static int android_gpio_set_val(struct android_gpio_device_t* dev, int val) 
{  
	LOGI("android_gpio: set value %d to device.", val);  
	write(dev->fd, &val, sizeof(val));  
	return 0;  
}  

static int android_gpio_get_val(struct android_gpio_device_t* dev, int* val) 
{  
	return 0;
}
为了防止调用时出现 Permission denied的情况:

打开system/core/rootdir目录,打开ueventd.rc添加:

/dev/android_gpio 0666 root root
在android_gpio目录中继续添加Android.mk文件:

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := android_gpio.c
LOCAL_MODULE := android_gpio.default
include $(BUILD_SHARED_LIBRARY)
编译HAL层:

mmm hardware/libhardware/modules/android_gpio
如果出现错误,参考:

No command ‘mmm‘ found 

没有规则可以创建 /lib/liblog.so 

如果成功,就可以生成 android_gpio.default.so

Install: out/target/product/generic/system/lib/hw/android_gpio.default.so
这个就是我们需要的硬件抽象层模块,这一步完成之后,还要接着向上走,最终完成硬件调用。

Android 从硬件到应用:一步一步向上爬 3 -- 硬件抽象层访问硬件驱动