Android (Java) controls GPIO methods and time consumption analysis, androidgpio

Source: Internet
Author: User

Android (Java) controls GPIO methods and time consumption analysis, androidgpio

The previous two Articles respectively introduced how to control gpio by using scripts and C code to read/sys/class/GPIO. In actual project debugging, you often need to control GPIO in the Java code. Its implementation is similar to that in C code. The only difference is the permission. This article focuses on permission configuration and briefly analyzes the time consumption for controlling GPIO at the Java layer.

Taking the Qualcomm platform as an example, the permission configuration mainly modifies the file. te, file_contexts, and system_app.te files in the HLOS/device/qcom/sepolicy/common directory.

Modify file. te as follows,

# GPIO accessed by system apptype sysfs_gpio, fs_type, sysfs_type;

Modify file_contexts as follows,

/sys/devices/soc/1010000.pinctrl/gpio/gpio62/value                        u:object_r:sysfs_gpio:s0/sys/devices/soc/1010000.pinctrl/gpio/gpio63/value                        u:object_r:sysfs_gpio:s0

Modify system_app.te as follows,

allow system_app sysfs_gpio:file rw_file_perms;

After the modification is complete, run the make bootimage command to generate boot. img and use fastboot for burning.

The Android app modifies the AndroidManifest. xml file and adds android: sharedUserId = "android. uid. system" to the manifest node. Modify the Android. mk file and add LOCAL_CERTIFICATE: = platform to set the signature. The sample code for controlling GPIO in Java is as follows,

    void IOCtrl(int pin, int level) {        String path;        path = "/sys/class/gpio/gpio" + pin + "/value";        try {            FileOutputStream out = new FileOutputStream(path);            out.write(level);            out.flush();            out.close();        } catch (IOException e) {            e.printStackTrace();            return;        }    }    if (trigger == 0x01) {        trigger = 0x00;        IOCtrl(62, '1');        IOCtrl(63, '1');    } else {        trigger = 0x01;        IOCtrl(62, '0');        IOCtrl(63, '0');    }

As shown in the preceding code execution result, we can see that the difference between the two IO increase is about 2.5 ms.

Note: If you only want to perform a test, you can use the setenforce 0 command to set SELinux to the permissive mode. Then, you can use Android Studio to compile and test the application to control GPIO, which is more convenient.

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.