gpio控制驱动和应用测试程序

1.gpio 控制驱动

#include <dt-bindings/gpio/gpio.h>
#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/miscdevice.h>
#include <asm/types.h>
#include <linux/platform_device.h>
#include <asm/uaccess.h>
#include <linux/wait.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/slab.h>

#define GPIO_HIGH      1   
#define GPIO_LOW  0  
int gpio,gpio_value;
static struct class *gpio_class;

static struct of_device_id gpio_match_table[] = {
	{ .compatible = "gpio_driver",},
	{},
};

static int gpio_open(struct inode *inode, struct file *file)
{
	return 0;
}

static int gpio_release(struct inode *inode, struct file *file)
{
	return 0;
}

static long gpio_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	
	switch(cmd)
    {
           case GPIO_HIGH:
                        gpio_direction_output(gpio, cmd);
                        break;
           case GPIO_LOW:
                        gpio_direction_output(gpio, cmd);
                        break;
                default:
                        return -EINVAL;
    }
    return 0;
}

const struct file_operations gpio_fops = {
	.owner = THIS_MODULE,
	.open  = gpio_open,
	.release = gpio_release,
	.unlocked_ioctl = gpio_ioctl,
};

static int gpio_probe(struct platform_device *pdev)
{
	enum of_gpio_flags flag;
  struct device_node *gps_gpio_node = pdev->dev.of_node;
	int ret;
	
	ret = register_chrdev(103,"gpio_driver",&gpio_fops); //注册字符设备驱动
	if (ret<0) {
		printk("failed to register gpio_out device\n");
		return  -ENODEV;
	}	

	gpio_class = class_create(THIS_MODULE, "gpio_driver"); //注册一个类,可以在/dev/目录下面建立设备节点
	if (IS_ERR(gpio_class)) {
		return  -ENODEV;
	}

	/* not a big deal if we fail here :-) */
	device_create(gpio_class, NULL, MKDEV(103, 0), NULL, "gpio_driver"); //创建一个设备节点,节点名为"gpio_driver"
	
	

	gpio = of_get_named_gpio_flags(gps_gpio_node, "gpio_out", 0, &flag);  //获取gpio号
	if (!gpio_is_valid(gpio)) {
        printk("gpio_driver can't register major number\n");
        return -ENODEV;
    }

    //申请gpio
   if (gpio_request(gpio, "gpio_driver")) {
        printk("gpio_driver request failed!\n");
        gpio_free(gpio);//释放gpio
        return -ENODEV;
    }
  
	gpio_value = (flag == GPIO_ACTIVE_HIGH)? 1:0;       //输入GPIO_ACTIVE_HIGH为1,赋值给gpio_value
  gpio_direction_output(gpio, gpio_value);	
  printk("gpio_driver  putout\n");
	return 0;
}

static int gpio_remove(struct platform_device *pdev)
{
		gpio_free(gpio);
	  return 0;
}

static struct platform_driver gpio_driver = {
	.driver = {
		.name = "gpio_driver",
		.owner = THIS_MODULE,
		.of_match_table = gpio_match_table,
	},
	.probe = gpio_probe,
	.remove		= gpio_remove,
};

module_platform_driver(gpio_driver);

MODULE_DESCRIPTION("gpio_driver control driver");
MODULE_AUTHOR("liuy@armdesigner.com");
MODULE_LICENSE("GPL");

2.gpio应用测试程序

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>


int main()
{
    int fd,i;  
      
   while(1){
   	fd = open("/dev/gpio_driver", O_RDWR);  //打开设备
    scanf("%d",&i);
    	if(i==0){
    		ioctl(fd,0);    		  		   	
    }
   	 else if(i==1){
    		ioctl(fd,1);	
    }
    else if(i==2){
    		close(fd);    //关闭设备
    		printf("close gpio device !");
    		break;
    }
    else {
    	 perror("open device gpio failed !");  // 如果打开设备失败,退出
    }
  }
    return 0;
}

/**************************************************************************************************
1.
$ cp /opt/buildroot/rk3288_linux_v2.1/prebuilts/gcc/linux-x86/arm/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin

$ ./arm-linux-gnueabihf-gcc gpio_test.c -o gpio_test

2.

#	cp udisk/gpio_test  /bin/
#	gpio_test

*****************************************************************************************************/
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YY2065

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值