应用层写程序控制6盏灯的亮灭,要求使用GPIO子系统()

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/uaccess.h>
#include "myled.h"
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#define CNAME "myled"
/*myleds{

    myled1 = <&gpioe 10 0>;// &gpioe标识引用gpioe控制器  10表示gpioe10, 0表示设置gpio管脚状态为默认状态
    myled2 = <&gpiof 10 0>;
    myled3 = <&gpioe 8 0>;
	myled4 = <&gpioz 5 0>;
	myled5 = <&gpioz 6 0>;
	myled6 = <&gpioz 7 0>;
    };*/

//定义指针指向获取的设备树节点信息空间
 struct device_node *node;
 unsigned int gpiono1,gpiono2,gpiono3,gpiono4,gpiono5,gpiono6;
unsigned int minor=0;
#if 0
unsigned int major=0;
#else
unsigned int major=500;
#endif
char kbuf[128]={};
struct cdev *cdev;
struct class *cls;
struct device *dev;
	int whitch;
const int count=6;
 int ret,ret1,ret2,ret3,ret4,ret5,ret6;


int mycdev_open(struct inode* inode,struct file *file)
{
	printk("打开成功\n");
	return 0;
}

long mycdev_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
{
	switch(cmd)
	{
		case LED1_ON:
					gpio_set_value(gpiono1,1);
					break;
		case LED1_OFF:
					gpio_set_value(gpiono1,0);
					break;
		case LED2_ON:
					gpio_set_value(gpiono2,1);
					break;
		case LED2_OFF:
					gpio_set_value(gpiono2,0);
					break;
		case LED3_ON:
					gpio_set_value(gpiono3,1);
					break;
		case LED3_OFF:
					gpio_set_value(gpiono3,0);
					break;
		case LED4_ON:
					gpio_set_value(gpiono4,1);
					break;
		case LED4_OFF:
					gpio_set_value(gpiono4,0);
					break;
		case LED5_ON:
					gpio_set_value(gpiono5,1);
					break;
		case LED5_OFF:
					gpio_set_value(gpiono5,0);
					break;
		case LED6_ON:
					gpio_set_value(gpiono6,1);
					break;
		case LED6_OFF:
					gpio_set_value(gpiono6,0);
					break;
					
	}
	return 0;

}

int mycdev_close(struct inode *inode,struct file *file)
{
	printk("关闭成功\n");
	return 0;
}

const struct file_operations fops=
{
	.open = mycdev_open,
	.unlocked_ioctl=mycdev_ioctl,
	.release=mycdev_close,
};

static int __init mycdev_init(void)
{
	int ret,i;
	dev_t devno;
	cdev=cdev_alloc();
	if(cdev==NULL)
	{
		printk("cdev_alloc is error\n");

		ret=-ENOMEM;
		goto ERR1;
	}
	printk("alloc success\n");
	//对象初始化
	cdev_init(cdev,&fops);
	//设备号的申请
	if(major==0)
	{
		ret=alloc_chrdev_region(&devno,0,count,CNAME);
		if(ret)
		{
			printk("dev_no error\n");
			goto ERR2;
		}
		printk("dev_no success\n");
	printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
	}
	major=MAJOR(devno);
	minor=MINOR(devno);
	ret=cdev_add(cdev,MKDEV(major,minor),count);
	if(ret)
	{
		printk("字符设备驱动注册失败\n");
		goto ERR3;
	}
	printk("注册成功\n");
	//上传目录
	cls=class_create(THIS_MODULE,CNAME);
	if(IS_ERR(cls))
	{
		printk("上传目录失败\n");
		ret=PTR_ERR(cls);
		goto ERR4;
	}
	printk("上传目录成功\n");
	//向上提交结点信息
	for(i=0;i<count;i++)
	{
		dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
		if(IS_ERR(dev))
		{
			printk("失败\n");
			goto ERR5;
		}
	}
    //通过名字获取设备树节点信息
    node=of_find_node_by_path("/myleds");
    if(node==NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析到设备树节点\n");
    //获取gpio编号
    gpiono1=of_get_named_gpio(node,"myled1",0);
    if(gpiono1<0)
    {
        printk("获取gpio1编号失败\n");
        return gpiono1;
    }
    printk("获取gpio1编号成功\n");
    gpiono2=of_get_named_gpio(node,"myled2",0);
    if(gpiono2<0)
    {
        printk("获取gpio2编号失败\n");
        return gpiono2;
    }
    printk("获取gpio2编号成功\n");
    gpiono3=of_get_named_gpio(node,"myled3",0);
    if(gpiono3<0)
    {
        printk("获取gpio3编号失败\n");
        return gpiono3;
    }
    printk("获取gpio3编号成功\n");
    gpiono4=of_get_named_gpio(node,"myled4",0);
    if(gpiono4<0)
    {
        printk("获取gpio4编号失败\n");
        return gpiono4;
    }
    printk("获取gpio4编号成功\n");
    gpiono5=of_get_named_gpio(node,"myled5",0);
    if(gpiono5<0)
    {
        printk("获取gpio5编号失败\n");
        return gpiono5;
    }
    printk("获取gpio5编号成功\n");
    gpiono6=of_get_named_gpio(node,"myled6",0);
    if(gpiono6<0)
    {
        printk("获取gpio6编号失败\n");
        return gpiono6;
    }
    printk("获取gpio6编号成功\n");
    //申请GPIO编号
   ret1=gpio_request(gpiono1,NULL);
   if(ret1)
   {
         printk("申请gpio1编号失败\n");
        return ret1;
   }
   printk("申请gpio1编号成功\n");
   ret2=gpio_request(gpiono2,NULL);
   if(ret2)
   {
         printk("申请gpio2编号失败\n");
        return ret2;
   }
   printk("申请gpio2编号成功\n");
   ret3=gpio_request(gpiono3,NULL);
   if(ret3)
   {
         printk("申请gpio3编号失败\n");
        return ret3;
   }
   printk("申请gpio3编号成功\n");
   ret4=gpio_request(gpiono4,NULL);
   if(ret4)
   {
         printk("申请gpio4编号失败\n");
        return ret4;
   }
   printk("申请gpio4编号成功\n");
   ret5=gpio_request(gpiono5,NULL);
   if(ret5)
   {
         printk("申请gpio5编号失败\n");
        return ret5;
   }
   printk("申请gpio5编号成功\n");
   ret6=gpio_request(gpiono6,NULL);
   if(ret6)
   {
         printk("申请gpio6编号失败\n");
        return ret6;
   }
   printk("申请gpio6编号成功\n");
   //设置管脚为输出
   gpio_direction_output(gpiono1,0);
   gpio_direction_output(gpiono2,0);
   gpio_direction_output(gpiono3,0);
   gpio_direction_output(gpiono4,0);
   gpio_direction_output(gpiono5,0);
   gpio_direction_output(gpiono6,0);
   return 0;
   ERR5:
    for(--i;i>=0;i--)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    class_destroy(cls);
ERR4:
    cdev_del(cdev);
ERR3:
    unregister_chrdev_region(devno,count);
ERR2:
    kfree(cdev);
ERR1:
    return ret;
}

static void __exit mycdev_exit(void)
{
	//销毁设备节点
	int i;
	for(i=0;i<count;i++)
	{
		device_destroy(cls,MKDEV(major,i));
	}
	class_destroy(cls);
	cdev_del(cdev);
	unregister_chrdev_region(MKDEV(major,minor),count);
	kfree(cdev);
    //卸载驱动前熄灭灯
       gpio_set_value(gpiono1,0);
    //释放申请得到的gpio编号
    gpio_free(gpiono1);
    //卸载驱动前熄灭灯
       gpio_set_value(gpiono2,0);
    //释放申请得到的gpio编号
    gpio_free(gpiono2);
    //卸载驱动前熄灭灯
       gpio_set_value(gpiono3,0);
    //释放申请得到的gpio编号
    gpio_free(gpiono3);
    //卸载驱动前熄灭灯
       gpio_set_value(gpiono4,0);
    //释放申请得到的gpio编号
    gpio_free(gpiono4);
    //卸载驱动前熄灭灯
       gpio_set_value(gpiono5,0);
    //释放申请得到的gpio编号
    gpio_free(gpiono5);
    //卸载驱动前熄灭灯
       gpio_set_value(gpiono6,0);
    //释放申请得到的gpio编号
    gpio_free(gpiono6);

}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");
#ifndef __LED_H__
#define __LED_H__

#define LED1_ON _IOW('a',1,int)
#define LED1_OFF _IOW('a',0,int)
#define LED2_ON _IOW('b',1,int)
#define LED2_OFF _IOW('b',0,int)
#define LED3_ON _IOW('c',1,int)
#define LED3_OFF _IOW('c',0,int)
#define LED4_ON _IOW('d',1,int)
#define LED4_OFF _IOW('d',0,int)
#define LED5_ON _IOW('e',1,int)
#define LED5_OFF _IOW('e',0,int)
#define LED6_ON _IOW('f',1,int)
#define LED6_OFF _IOW('f',0,int)

#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include "myled.h"

char buf[128]={0};
char ubuf[128]={0};
int main(int argc, char const *argv[])
{
	int fd1,fd2,fd3,fd4,fd5,fd6;
	int whitch;
	fd1=open("/dev/myled0",O_RDWR);
	if(-1==fd1)
	{
		perror("open is error");
		exit(1);
	}
	fd2=open("/dev/myled1",O_RDWR);
	if(-1==fd2)
	{
		perror("open is error");
		exit(1);
	}
	fd3=open("/dev/myled2",O_RDWR);
	if(-1==fd3)
	{
		perror("open is error");
		exit(1);
	}
	fd4=open("/dev/myled3",O_RDWR);
	if(-1==fd4)
	{
		perror("open is error");
		exit(1);
	}
	fd5=open("/dev/myled4",O_RDWR);
	if(-1==fd5)
	{
		perror("open is error");
		exit(1);
	}
	fd6=open("/dev/myled5",O_RDWR);
	if(-1==fd6)
	{
		perror("open is error");
		exit(1);
	}


	while(1)
	{
		ioctl(fd1,LED1_ON);
		sleep(1);
		ioctl(fd2,LED1_OFF);
		sleep(1);

		ioctl(fd2,LED2_ON);
		sleep(1);
		ioctl(fd2,LED2_OFF);
		sleep(1);

		ioctl(fd3,LED3_ON);
		sleep(1);
		ioctl(fd3,LED3_OFF);
		sleep(1);

		ioctl(fd4,LED4_ON);
		sleep(1);
		ioctl(fd4,LED4_OFF);
		sleep(1);

		ioctl(fd5,LED5_ON);
		sleep(1);
		ioctl(fd5,LED5_OFF);
		sleep(1);

		ioctl(fd6,LED6_ON);
		sleep(1);
		ioctl(fd6,LED6_OFF);
		sleep(1);
	}

	close(fd1);
	close(fd2);
	close(fd3);
	close(fd4);
	close(fd5);
	close(fd6);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值