Android 從硬體到應用:一步一步向上爬 3 -- 硬體抽象層訪問硬體驅動

來源:互聯網
上載者:User

標籤:android   硬體   驅動   hal   

       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 closestatic 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 accessstatic 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 := optionalLOCAL_PRELINK_MODULE := falseLOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hwLOCAL_SHARED_LIBRARIES := liblogLOCAL_SRC_FILES := android_gpio.cLOCAL_MODULE := android_gpio.defaultinclude $(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 -- 硬體抽象層訪問硬體驅動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.