在linux下写控制gpio管脚并写app测试

在linux下写控制gpio管脚并写app测试

dts:
gpio_driver: gpio_driver {
compatible = “gpios_driver”;
status = “disabled”;
gpios1 = <&gpio3 GPIO_B0 GPIO_ACTIVE_HIGH>;
gpios2 = <&gpio1 GPIO_D0 GPIO_ACTIVE_HIGH>;
gpios3 = <&gpio3 GPIO_D1 GPIO_ACTIVE_HIGH>;
};

&gpio_driver {
status = “okay”;
};

driver:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_gpio.h>

#include <linux/miscdevice.h>
#include <linux/fs.h>

#define DRIVER_NAME “gpios_driver”

int gpio_pin[3] = {-1};

int gpios_open(struct inode *inode,struct file *filp)
{
printk(“Device Opened Success!\n”);
return nonseekable_open(inode,filp);
}

int gpios_release(struct inode *inode,struct file *filp)
{
printk(“Device Closed Success!\n”);
return 0;
}

long gpios_ioctl(struct file *filp,unsigned int cmd,unsigned long arg)
{

printk("debug: gpios_ioctl cmd is %d,arg is %d\n" ,(int)cmd,(int)arg);

switch(cmd)
{
	case 0:
	case 1:
		gpio_set_value(gpio_pin[arg], cmd);
		break;

	default:
		return -EINVAL;
}
return 0;

}

static struct file_operations gpios_ops = {
.owner = THIS_MODULE,
.open = gpios_open,
.release= gpios_release,
.unlocked_ioctl = gpios_ioctl,
};

static struct miscdevice gpios_dev = {
.minor = MISC_DYNAMIC_MINOR,
.fops = &gpios_ops,
.name = DRIVER_NAME,
};

static int gpios_probe(struct platform_device * pdev)
{
struct device_node *node = pdev->dev.of_node;
int ret;
int i;

printk("led init\n");

gpio_pin[0] = of_get_named_gpio(node, "gpios1", 0);
gpio_pin[1] = of_get_named_gpio(node, "gpios2", 0);
gpio_pin[2] = of_get_named_gpio(node, "gpios3", 0);

for(i=0;i<3;i++){
		if (gpio_pin[i] < 0){
			  printk("gpio_pin[%d] is not available \n",i);	
		}else{
			    ret = gpio_request(gpio_pin[i], "L");
				if(ret!=0){
	                 printk("gpio_pin[%d] request %d failed.", i,gpio_pin[i]);
	                 return ret;
		         }				
         }
}
printk("gpio_pin[0] is %d\n",gpio_pin[0]);
printk("gpio_pin[1] is %d\n",gpio_pin[1]);
printk("gpio_pin[2] is %d\n",gpio_pin[2]);
gpio_direction_output(gpio_pin[0],0);
gpio_set_value(gpio_pin[0], 1);
gpio_direction_output(gpio_pin[1],0);
gpio_set_value(gpio_pin[1], 1);
gpio_direction_output(gpio_pin[2],0);
gpio_set_value(gpio_pin[2], 1);
ret = misc_register(&gpios_dev);
if(ret<0){
	printk("led:register device failed!\n");
	goto exit;
}
return 0;

exit:
misc_deregister(&gpios_dev);
return ret;
}

static int gpios_remove(struct platform_device * pdev)
{
printk(KERN_ALERT “Goodbye, curel world, this is remove\n”);
int i;
for (i = 0; i < 3 ; i++) {
gpio_free(gpio_pin[i]);
}
misc_deregister(&gpios_dev);
return 0;
}

static const struct of_device_id of_gpios_dt_match[] = {
{.compatible = DRIVER_NAME},
{},
};

MODULE_DEVICE_TABLE(of,of_gpios_dt_match);

static struct platform_driver gpios_driver = {
.probe = gpios_probe,
.remove = gpios_remove,
.driver = {
.name = DRIVER_NAME,
.owner = THIS_MODULE,
.of_match_table = of_gpios_dt_match,
},
};

static int gpios_init(void)
{
printk(KERN_ALERT “Hello, world\n”);
return platform_driver_register(&gpios_driver);
return 0;
}

static void gpios_exit(void)
{
printk(KERN_ALERT “Goodbye, curel world\n”);
platform_driver_unregister(&gpios_driver);
}
module_init(gpios_init);
module_exit(gpios_exit);

MODULE_LICENSE(“Dual BSD/GPL”);
MODULE_AUTHOR(“rty”);
MODULE_DESCRIPTION(“dev_drv”);

app:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc,char **argv)
{
int fd;

if(argc!=4){
	printf("arvc is 4:\nargv1 is device_node ,\nargv2 is cmd,\nargv2 is lednum\n");
}

if((fd = open(argv[1], O_RDWR|O_NOCTTY|O_NDELAY))<0){
	printf("open %s failed\n",argv[1]);   
	return -1;
}
else{
	printf("open %s ok\n",argv[1]); 
	ioctl(fd,atoi(argv[2]),atoi(argv[3]));		
}
close(fd);
return 0;

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值