Android应用层通过echo拉高gpio(非专业方法)

前言:

有的时候,我们想通过应用层拉高gpio、进行例如点灯或者其他操作,但如果不熟悉kernel修改的,不知道怎么实现。下面介绍一种非专业的办法

kernel修改

  1. 首先查看原理图,找到要控制的gpio引脚

  2. GPIO定义的位置

    kernel/arch/arm/mach-rk30/include/mach/gpio.h
  3. 再查看是否有被其他地方占用(例如)

    kernel/arch/arm/mach-rk3188/board-rk3188-box.c
  4. 修改在下面文件

    kernel/drivers/usb/dwc_otg/dwc_otg_driver.c

    此文件里有个debuglevel,可以仿照它

    static ssize_t test_show(struct device_driver *_drv, char *_buf)
    {
          return 0;
    }
    
    static ssize_t test_store(struct device_driver *_drv, const char *_buf,
                      size_t _count)
    {
        int value = (int)simple_strtoul(_buf, NULL, 16);
             printk("test value=%d.\n", value);
        if(value == 1)
             gpio_set_value(RK30_PIN1_PB4, GPIO_HIGH);
         else if(value == 0)
            gpio_set_value(RK30_PIN1_PB4, GPIO_LOW);
         return _count;
    }
    static DRIVER_ATTR(test, S_IRUGO|S_IWUSR, test_show, test_store);
    
    //原有
    if (driver_create_file(&dwc_otg_driver.driver, &driver_attr_debuglevel))
      pr_warning("DWC_OTG: Failed to create driver debug level file\n");
    //新增
    if (driver_create_file(&dwc_otg_driver.driver, &driver_attr_test))
         pr_warning("DWC_OTG: Failed to create driver debug level file\n");
  5. 编译烧录

    会在/sys/devices/platform/usb20_otg/driver/ 下生成test

    1

  6. 测试

    echo 0x1 > /sys/devices/platform/usb20_otg/driver/test

    1

应用层调用

在需要执行的地方调用

public static Process test(String[] command) {
    Process p = null;
    try {
        p = Runtime.getRuntime().exec("su");有su权限

        DataOutputStream os = new DataOutputStream(p.getOutputStream());
        for (String tmpCmd : cmds) {
            os.writeBytes(tmpCmd + "\n");
        }
        os.writeBytes("exit\n");
        os.flush();
        os.close();

    } catch (IOException e) {
        e.printStackTrace();
    }
    return p;
} 

public static void test2() {
    BufferedReader reader = null;
    Process process = null;
    try {
        process = test(new String[]{"echo 0x1 > /sys/devices/platform/usb20_otg/driver/test"});
        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line;
        while ((line = reader.readLine()) != null) {
            if (DBG)
                Log.d(TAG, "shell msg : " + line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

转载请注明出处:http://www.wolfnx.com/2018/08/19/StupidWayToPullUpGpio

作者 : wolfnx
邮箱 : wolfnx@outlook.com
邮箱2 : lostnx@gmail.com

Click Me