Android kernel 操作gpio

简介: Android kernel 操作gpio

1.kernel/msm-5.4/drivers/gpio/gpio-ctl-tmp.c

1.先定义gpio 
int gpio_ctl_power;
2.在gpio_probe方法里获取gpio_ctl_power信息

ret = of_get_named_gpio_flags(np, "gpio-ctl-power", 0, &flags); //该函数返回一个gpio编号。
    if (ret < 0) {
        printk("%s() Can not read property gpio-ctl-power\n", __FUNCTION__);
    } else {
        gpio_ctl_power = ret;
        ret = devm_gpio_request(&pdev->dev, gpio_ctl_power, "gpio_ctl_power");//申请一个gpio口
        if(ret < 0){
            printk("%s() devm_gpio_request ggpio-ctl-power request ERROR\n", __FUNCTION__);
        }
        ret = gpio_direction_output(gpio_ctl_power,0); //在某个GPIO口写上某个值之后,还会把这个端口设置为输出模式
        if(ret < 0){
            printk("%s() gpio_direction_input gpio-ctl-power set ERROR\n", __FUNCTION__);
        }
    }
3.定义gpio的操作函数
static long gpio_ctl_tmp_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
    long ret = 0;

    return ret;
}

static ssize_t gpio_ctl_tmp_write(struct file *file, const char __user *buf,    size_t count, loff_t *off)
{

    //s32 ret = 0;
    //s8 cmd[3];
    //int write_value1 = 0;
    //int write_value2 =0;
    //int write_value =0;

    //memset(cmd,0,sizeof(cmd));
    //ret = copy_from_user(cmd, buf, 2);
    printk("gpio_ctl_tmp_write \n");

    //if (ret) {
    //    printk("copy_from_user failed.");
    //    return -EPERM;
    //}

    gpio_set_value(gpio_ctl_power,1);

    return count;
}

static ssize_t gpio_ctl_tmp_read(struct file *filp, char *buff,
        size_t count, loff_t *ppos){

    printk(". gpio_eintdemo_read ppos:%d \n", (int)*ppos);

    return 0;
}

static const struct file_operations gpio_ctl_tmp_fops = {
    .owner= THIS_MODULE,
    .write        = gpio_ctl_tmp_write,
    .read             = gpio_ctl_tmp_read,
    .unlocked_ioctl = gpio_ctl_tmp_ioctl,
};

static struct miscdevice gpio_ctl_tmp_device =
{
    .minor = MISC_DYNAMIC_MINOR,
    .name = "gpio-ctl-tmp",
    .fops = &gpio_ctl_tmp_fops,
};

4.在module_init对应的方法里注册gpio
 ret = misc_register(&gpio_ctl_tmp_device);//注册misc设备,会在/dev下生成相应节点,/dev/gpio-eintdemo
    if(ret)
    {
        printk("you init the driver failed!!");
    }

2.修改dts
vendor/qcom/proprietary/devicetree/qcom/yupik-idp-pm7250b.dtsi


tmp_gpios: tmp-gpios{
        compatible = "qcom,gpio-ctl-tmp";
        gpio-com1-enable = <&tlmm 57 0>;
        gpio-msr-enable = <&tlmm 94 0>;
        gpio-3V3-enable = <&tlmm 32 0>;
        gpio-ctl-power = <&tlmm 33 0>;
        status = "okay";
    };

3.在rc文件里配置权限

device/qcom/common/rootdir/etc/ueventd.qcom.rc
/dev/gpio-ctl-tmp        0666   system     system 

device/qcom/lahaina/init.target.rc
 chown system system /dev/gpio-ctl-tmp
    chmod 0666 /dev/gpio-ctl-tmp

4.配置selinux
device/qcom/sepolicy/generic/private/file_contexts

/dev/gpio-ctl-tmp      u:object_r:gpiocontrol_device:s0

5.java层操作gpio

private static final String TMP_GPIO_PATH = "/dev/gpio-ctl-tmp";

private void setTmpGpioCtl(){
        writeFileNode("00",TMP_GPIO_PATH);
    }

    public void shutdown(){
        setTmpGpioCtl();
    }

private void writeFileNode(String cmd, String filePath){     
        try {
            BufferedWriter bufWriter = null;
            bufWriter = new BufferedWriter(new FileWriter(filePath));
            bufWriter.write(cmd);  
            bufWriter.close();
            Slog.d("","wtrieValue = "+cmd);
            Slog.d("","filePath = "+filePath);
       } catch (IOException e) {
            e.printStackTrace();
            Slog.e("","can't write the " + filePath);
        }        
    }

    private void Write2File(File file, String mode) {
        //if (!checkFile() || (mode == null))
        //    return;
        Slog.d("", "Write2File,write mode = " + mode);

        try {
            FileOutputStream fout = new FileOutputStream(file);
            PrintWriter pWriter = new PrintWriter(fout);
            pWriter.println(mode);
            pWriter.flush();
            pWriter.close();
            fout.close();
        } catch (IOException re) {
        }
    }

    private String readFileNode(String filePath){     
        int size = 0;
        String line;

        try {
            BufferedReader bufReader = null;
            bufReader = new BufferedReader(new FileReader(filePath));

            while((line=bufReader.readLine())!=null) {
                Slog.e("","read line = " + line);
                break;
            }
            bufReader.close();            
            return line;
        }catch (IOException e) {
            e.printStackTrace();
            Slog.e("","can't read the " + filePath);
        }
        return "open";
    }
目录
相关文章
|
6月前
|
Java 数据库 Android开发
【专栏】Kotlin在Android开发中的多线程优化,包括线程池、协程的使用,任务分解、避免阻塞操作以及资源管理
【4月更文挑战第27天】本文探讨了Kotlin在Android开发中的多线程优化,包括线程池、协程的使用,任务分解、避免阻塞操作以及资源管理。通过案例分析展示了网络请求、图像处理和数据库操作的优化实践。同时,文章指出并发编程的挑战,如性能评估、调试及兼容性问题,并强调了多线程优化对提升应用性能的重要性。开发者应持续学习和探索新的优化策略,以适应移动应用市场的竞争需求。
160 5
|
6月前
|
Java Android开发
Android系统 获取用户最后操作时间回调实现和原理分析
Android系统 获取用户最后操作时间回调实现和原理分析
187 0
|
6月前
|
Linux 调度 Android开发
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
【系统启动】Kernel怎么跳转到Android:linux与安卓的交界
105 0
|
6月前
|
Linux Android开发
测试程序之提供ioctl函数应用操作GPIO适用于Linux/Android
测试程序之提供ioctl函数应用操作GPIO适用于Linux/Android
116 0
|
5月前
|
XML API 开发工具
Android Bitmap 加载与像素操作
Android Bitmap 加载与像素操作
48 2
|
5月前
|
存储 算法 Java
Android 进阶——代码插桩必知必会&ASM7字节码操作
Android 进阶——代码插桩必知必会&ASM7字节码操作
241 0
|
6月前
|
Shell 开发工具 Android开发
android 修改kernel编译版本信息
android 修改kernel编译版本信息
87 0
|
6月前
|
存储 Linux Android开发
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
RK3568 Android/Linux 系统动态更换 U-Boot/Kernel Logo
826 0
|
6月前
|
Linux Shell Android开发
自动化脚本之GPIO/LED相关适用于Android/Linux
自动化脚本之GPIO/LED相关适用于Android/Linux
53 0
|
6月前
|
XML Java API
Android 浅度解析:系统框架层修改,编译,推送相关操作
Android 浅度解析:系统框架层修改,编译,推送相关操作
223 0