Android Exploration and drive development and thinking (9)

Source: Internet
Author: User

Why Add HAL to Android

1. The calling interface of the unified hardware. Because HAL has a standard calling interface, HAL can be used to mask Linux to drive complex, non-uniform interfaces.

2. Fixed the GPL copyright issue. Because the Linux kernel is based on the GPL protocol, Android is based on the Apache Licence 2.0 protocol. So Google played a "traversal", moving the Linux-powered sensitive code that was originally in the Linux kernel up one level. That way, these sensitive code is freed from the restrictions of the GPL. There's no need to open source for Linux-powered authors who don't want to open up.

3. For a number of special requirements. For some hardware, you may need access to some user-space resources, or work that is inconvenient in kernel space, and special needs. In this case, the HAL code located in the user space can be used to assist the Linux driver to do some work.

Android HAL Architecture :

Old schema: New schema:

Then you'll write a Linux driver that supports HAL, with the following steps:

First step: Writing Linux drivers

Step two: Write the HAL Library

Step three: Write the service Library

Finally, the HAL-based LED driver removes all code related to the read-write register rule, leaving only the code led driver that created the device file that has interacted with the register to read and write the specified register using the read and write functions of the device file. The rationale is to read or write only 5 bytes from a specified register. The 1th byte is used to specify both read and write actions and registers. The second 4 bytes are read and write

Write the HAL module of the chanting point LED driver;

In fact: TheHal module is also a common Linux shared library (. so file), except Hal Modules can be automatically loaded by the Android System instead of the developer to load . so files themselves. .

Linux driven init main linux Span style= "FONT-FAMILY:CALIBRI;" >init function

Since the HAL module can also be automatically called by the Android system, that nature also has a similar main or init The interface of the function. Except that this interface is not a function , but a fixed-name struct-body variable hal_module_info_sym.

The steps and principles for writing the HAL module are as follows :

TheLsteps: Defining structs and Macros: WritingHALmodules need to be used to3a very important structural body (hw_module_t,hw_device_tand theHW _ module_ method_ t), in sectionLto define two new structures, the first of these two structuresLthe data type of a variable must behw_module_tand thebw_device_t. In addition, it is necessary toHALmodule defines aID.actually, in thisLstep is to writeleds_hal.hthe code for the header file.

Step 2 : write the HAL module's open function:

The open function is The entry point for the HAL module. The function mainly does the following 3 tasks : Initialize the device_t of HW . Open the device file. Initializes the register.

Step 3 : Define hw_module_methods_ t struct variables

Step 4 : Define hal_module_info_sym variables

Step 5 : Write the close function of the HAL module

Step 6 : write a function to control LEDs

Summary: In the LED driver, we join HAL to use the program does not need to care about the Linux driver and device files interactive way. To write a linux driver that supports HAL:

First step: Writing Linux drivers

Step two: Write the HAL Library

Step three: Write the Service library

The steps and principles for writing the HAL module are as follows:

First step: defining structs and macros

Describes the hw-module-t structure of the HAL module

Describes the hw-device-t structure of a HAL device

hw-module-methods-t structure describing the module entry function

Step two: Write the HAL module's open function

Step three: Define hw-module-methods-t structure variables

Fourth step: Define the hal-module-info-sym variable

Fifth step: Write the close function of the HAL module

Sixth step: Write the function that controls the LED

In this chapter I learned:

1. On Android. used in the system Linux There are two ways of driving. One is to interact directly with Linux drivers in a traditional way .

2. write a macro to understand the following two points of knowledge: variable-parameter macros,do{... while (0)

Android Exploration and drive development and thinking (9)

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.