驱动开发day9

目录

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

头文件:

驱动代码:

应用层代码:

驱动安装:

应用层使用gpio子系统控制灯的亮灭效果:

1.控制LED2熄灭:

2.控制LED6熄灭:

3.控制LED2重新点亮:


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

头文件:

#ifndef __MYLED_H_
#define __MYLED_H_

typedef enum {
    LED1=1,
    LED2,
    LED3,
    LED4,
    LED5,
    LED6
}LED;
#define LED_ON _IOW('a',1,int)
#define LED_OFF _IOW('a',0,int)
#endif

驱动代码:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include "myled.h"
#define CNAME "myled"
struct device_node *node;
struct property *pr;
struct gpio_desc *gpiono[5];
#if 1
unsigned int major = 0;//动态申请设备号
#else
unsigned int major = 500; //静态指定设备号
#endif
char kbuf[128]={};
int minor=0;
const int count=6;
struct class *cls;
struct device *dev;
struct cdev *cdev;
int mycdev_open(struct inode *inode,struct file *file)
{
	return 0;
};
ssize_t mycdev_read(struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("copy to user is error\n");
		return -EIO;
    }
    return size;
}
ssize_t mycdev_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loffs)
{
    int ret;
	if(size>sizeof(kbuf)) size=sizeof(kbuf);
	ret=copy_from_user(kbuf,ubuf,size);
	if(ret)
	{
		printk("copy from user is error\n");
		return -EIO;
	}
	printk("copy from user kbuf=%s\n",kbuf);
    return size;
}
long mycdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    int whitch;
    int ret;
    switch (cmd)
    {
        case LED_ON:
            ret = copy_from_user(&whitch,(void*)arg,sizeof(int));
            if(ret)
            {
                printk("copy from user led on!!!!\n");
                return -EIO;
            }
            switch(whitch) 
            {
                case LED1:
                gpiod_set_value(gpiono[0],1);
                break;
                case LED2:
                gpiod_set_value(gpiono[1],1);
                break;
                case LED3:
                gpiod_set_value(gpiono[2],1);
                break;
                case LED4:
                gpiod_set_value(gpiono[3],1);
                break;
                case LED5:
                gpiod_set_value(gpiono[4],1);
                break;
                case LED6:
                gpiod_set_value(gpiono[5],1);
                break;
            }
            break;
        case LED_OFF:
            ret = copy_from_user(&whitch,(void*)arg,sizeof(int));
            if(ret)
            {
                printk("copy from user led on!!!!\n");
                return -EIO;
            }
            switch(whitch) 
            {
                case LED1:
                gpiod_set_value(gpiono[0],0);
                break;
                case LED2:
                gpiod_set_value(gpiono[1],0);
                break;
                case LED3:
                gpiod_set_value(gpiono[2],0);
                break;
                case LED4:
                gpiod_set_value(gpiono[3],0);
                break;
                case LED5:
                gpiod_set_value(gpiono[4],0);
                break;
                case LED6:
                gpiod_set_value(gpiono[5],0);
                break;
            }
            break;
    }
    return 0;
}
int mycdev_close(struct inode *inode,struct file *file)
{
	return 0;
}
const struct file_operations fops={
	.open=mycdev_open,
    .read=mycdev_read,
    .write=mycdev_write,
	.unlocked_ioctl=mycdev_ioctl,
	.release=mycdev_close,
};
static int __init mycdev_init(void)
{
    int ret;
    dev_t devno;
    int i;
    cdev=cdev_alloc();
    if(NULL==cdev)
    {
        printk("cdev alloc error\n");
        return -EIO;
        goto ERR1;
    }
    cdev_init(cdev,&fops);
    if(major>0)
    {
        ret=register_chrdev_region(MKDEV(major,minor),count,CNAME);
        if(ret)
        {
            printk("register chrdev region is error\n");
            ret= -ENOMEM;
            goto ERR2;
        }
    }
    else 
    {
        ret=alloc_chrdev_region(&devno,0,count,CNAME);
        if(ret)
        {
            printk("alloc chrdev region is error\n");
            ret=-ENOMEM;
            goto ERR2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    ret=cdev_add(cdev,MKDEV(major,minor),count);
    if(ret)
    {
        printk("cdev add is error\n");
        ret = -EIO;
        goto ERR3;
    }
    cls=class_create(THIS_MODULE,CNAME);
    if(IS_ERR(cls))
    {
    	printk("class create is error\n");
        ret= PTR_ERR(cls);
        goto ERR4;
    }
    for(i=0;i<count;i++)
    {
        dev=device_create(cls,NULL,MKDEV(major,i),NULL,"myled%d",i);
        if(IS_ERR(dev))
        {
            printk("device create is error\n");
            ret=PTR_ERR(dev);
            goto ERR5;
        }
    }
    node=of_find_node_by_name(NULL,"myleds");
    if(node==NULL)
    {
        printk("通过名字解析设备树节点失败\n");
        return -EFAULT;
    }
    printk("成功解析到设备树节点\n");
    gpiono[0]=gpiod_get_from_of_node(node,"myled1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono);
    }
    gpiono[1]=gpiod_get_from_of_node(node,"myled2",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono);
    }
    gpiono[2]=gpiod_get_from_of_node(node,"myled3",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono);
    }
    gpiono[3]=gpiod_get_from_of_node(node,"myled4",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono);
    }
    gpiono[4]=gpiod_get_from_of_node(node,"myled5",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono);
    }
    gpiono[5]=gpiod_get_from_of_node(node,"myled6",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("获取gpio编号失败\n");
        return PTR_ERR(gpiono);
    }
    printk("获取gpio编号成功\n");
    gpiod_direction_output(gpiono[0],0);
    gpiod_set_value(gpiono[0],1);
    gpiod_direction_output(gpiono[1],0);
    gpiod_set_value(gpiono[1],1);
    gpiod_direction_output(gpiono[2],0);
    gpiod_set_value(gpiono[2],1);
    gpiod_direction_output(gpiono[3],0);
    gpiod_set_value(gpiono[3],1);
    gpiod_direction_output(gpiono[4],0);
    gpiod_set_value(gpiono[4],1);
    gpiod_direction_output(gpiono[5],0);
    gpiod_set_value(gpiono[5],1);
    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(MKDEV(major,minor),count);
ERR2:
    kfree(cdev);
ERR1:
    return -EIO;
}
static void __exit mycdev_exit(void)
{
    int i;
	for(i=0;i<count;i++)
    {
        device_destroy(cls,MKDEV(major,i));
    }
    kfree(cdev);
    unregister_chrdev_region(MKDEV(major,minor),count);
    cdev_del(cdev);
	class_destroy(cls);
    gpiod_set_value(gpiono[0],0);
    gpiod_set_value(gpiono[1],0);
    gpiod_set_value(gpiono[2],0);
    gpiod_set_value(gpiono[3],0);
    gpiod_set_value(gpiono[4],0);
    gpiod_set_value(gpiono[5],0);
    gpiod_put(gpiono[0]);
    gpiod_put(gpiono[1]);
    gpiod_put(gpiono[2]);
    gpiod_put(gpiono[3]);
    gpiod_put(gpiono[4]);
    gpiod_put(gpiono[5]);
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

应用层代码:

#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"
int main(int argc, char const *argv[])
{
    char buf[128]={};
    int whitch;
    int fd1,fd2,fd3,fd4,fd5,fd6;
    fd1=open("/dev/myled0",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd2=open("/dev/myled1",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd3=open("/dev/myled2",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd4=open("/dev/myled3",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd5=open("/dev/myled4",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    fd6=open("/dev/myled5",O_RDWR);
    if(fd1<0)
    {
        perror("open is error");
        exit(1);
    }
    while (1)
    {
        printf("请输入:0->LED OFF|1->LED ON\n>>");
        fgets(buf,sizeof(buf),stdin);
        buf[strlen(buf)-1]='\0';
        if(buf[0]=='1')
        {
            printf("请选择要控制的LED灯:1->LED1 2->LED2 3->LED3 4->LED4 5->LED5 6->LED6\n>>");
            scanf("%d",&whitch);
            getchar();
            if(whitch==1)
            {
                ioctl(fd1,LED_ON,&whitch);
            }
            if(whitch==2)
            {
                ioctl(fd2,LED_ON,&whitch);
            }
            if(whitch==3)
            {
                ioctl(fd3,LED_ON,&whitch);
            }
            if(whitch==4)
            {
                ioctl(fd4,LED_ON,&whitch);
            }
            if(whitch==5)
            {
                ioctl(fd5,LED_ON,&whitch);
            }
            if(whitch==6)
            {
                ioctl(fd6,LED_ON,&whitch);
            }
        }
        else if(buf[0]=='0')
        {
            printf("请选择要控制的led灯:1->LED1 2->LED2 3->LED3 4->LED4 5->LED5 6->LED6\n>>");
            scanf("%d",&whitch);
            getchar();
            if(whitch==1)
            {
                ioctl(fd1,LED_OFF,&whitch);
            }
            if(whitch==2)
            {
                ioctl(fd2,LED_OFF,&whitch);
            }
            if(whitch==3)
            {
                ioctl(fd3,LED_OFF,&whitch);
            }
            if(whitch==4)
            {
                ioctl(fd4,LED_OFF,&whitch);
            }
            if(whitch==5)
            {
                ioctl(fd5,LED_OFF,&whitch);
            }
            if(whitch==6)
            {
                ioctl(fd6,LED_OFF,&whitch);
            }
        }
        else
        {
            printf("请重新输入!!!\n");
        }
    }
    close(fd1);
    close(fd2);
    close(fd3);
    close(fd4);
    close(fd5);
    close(fd6);
    return 0;
}

驱动安装:

6盏灯全部点亮

应用层使用gpio子系统控制灯的亮灭效果:

1.控制LED2熄灭:

2.控制LED6熄灭:

3.控制LED2重新点亮:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值