Android from hardware to application: Step by Step 3-Hardware Abstraction Layer access hardware driver

Source: Internet
Author: User

Android from hardware to application: Step by Step 3-Hardware Abstraction Layer access hardware driver

The Android standard hardware driver is divided into two parts: one is the hardware driver running in the Linux kernel, and the other is the hardware abstraction layer running in the user space. By using this method, the system can be unrelated to hardware and protect the interests of some vendors. From hardware to application in Android: Step by Step, step by step. 1. Write the underlying hardware driver from scratch to the Linux kernel, next, let's take a look at how to add a hardware module to the hardware abstraction layer to interact with our kernel driver to complete hardware control.

Go to the hardware/libhardware/include/hardware directory and create android_gpio.h:

#ifndef ANDROID_ANDROID_GPIO_INTERFACE_H  #define ANDROID_ANDROID_GPIO_INTERFACE_H  #include 
 
          __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 and get_val are the APIS provided by the upper-layer applications of the HAL layer.

Cd to the hardware/libhardware/modules directory, create the android_gpio directory, and create the android. c file in it:

#include 
 
    #include 
  
     #include 
   
      #include 
    
       #include 
     
        #include 
      
        #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;}
      
     
    
   
  
 
To prevent calling with Permission denied:

Open the system/core/rootdir directory and open ueventd. rc to add:

/dev/android_gpio 0666 root root
Add the Android. mk file in the android_gpio directory:

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)
Compile the HAL layer:

mmm hardware/libhardware/modules/android_gpio
If an error occurs, refer:

No command 'mmm' found

You can create/lib/liblog. so without rules.

If successful, you can generate android_gpio.default.so

Install: out/target/product/generic/system/lib/hw/android_gpio.default.so
This is the hardware abstraction layer module we need. After this step is completed, we need to go up and finally complete the hardware call.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.