【无标题】

1.使用驱动代码实现如下要求

a.应用程序通过阻塞的io模型来读取number变量的值
b.number是内核驱动中的一个变量
9
C
c.number的值随着按键按下而改变(按键中断)
例如number=0 按下按键number=1 再次按下按键number=0d.在按下按键的时候需要同时将1ed1的状态取反
e.驱动中需要编写字符设备驱动
r
a
f.驱动中需要自动创建设备节点
g.这个驱动需要的所有设备信息放在设备树的同一个节点中

irq_key_led.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/io.h>
#include<linux/of.h>
#include<linux/gpio.h>
#include<linux/of_gpio.h>
#include<linux/uaccess.h>
#include<linux/of_irq.h> 
#include<linux/interrupt.h> 

struct cdev *cdev;
unsigned int major = 0;
unsigned int minor = 0;
dev_t devno;
struct class *cls;
struct device *dev;

char number;


//key设备树节点
struct device_node *dnode_key;
//key软中断号
unsigned int irqno;
//led设备树节点
struct device_node *dnode_led;
struct gpio_desc *gpiono;

//定义等待队列头
wait_queue_head_t wq_head;
unsigned int condition = 0;

//中断处理函数
irqreturn_t myirq_handler(int irqno, void *dev_id)
{
    
    if(number == '0')
    {
        number = '1';
        //开灯
        gpiod_set_value(gpiono,1);
    }
    else
    {
        number = '0';
        gpiod_set_value(gpiono,0);//关灯
    }
     //表示硬件信息做好准备
    condition = 1;
    //唤醒睡眠的进程
    wake_up_interruptible(&wq_head);
    return IRQ_HANDLED;
}



int mycdev_open(struct inode *inode, struct file *file)
{
    return 0;
}
ssize_t mycdev_read (struct file *file, char *ubuf, size_t size, loff_t *lof)
{
    int ret;
    wait_event_interruptible(wq_head,condition);//将进程切换为休眠
    printk("size=%d\n",size);
    ret = copy_to_user(ubuf, &number, size);
    if(ret)
    {
        printk("copy_to_user filed\n");
        return -EIO;
    }
    printk("num%c\n",number);
    condition = 0;//表示下一次硬件没做好准备
    return 0;
}
ssize_t mycdev_write (struct file *file, const char *ubuf, size_t size, loff_t *lof)
{
    return 0;
}
int mycdev_close(struct inode *inode, struct file *file)
{
    printk("%s:%s:%d\n", __FILE__, __func__, __LINE__);
    return 0;
}
struct file_operations fops =
    {
        .open = mycdev_open,
        .read = mycdev_read,
        .write = mycdev_write,
        .release = mycdev_close,
};


static int __init mycdev_init(void)
{
    int ret;
    //初始化等待队列头
    init_waitqueue_head(&wq_head);
    //解析按键设备树节点
    dnode_key = of_find_node_by_name(NULL,"mykeys");
    if(dnode_key == NULL)
    {
        printk("解析key设备树节点失败");
        return -ENXIO;
    }
     printk("解析key设备节点成功\n");
    //获取中断号
    irqno = irq_of_parse_and_map(dnode_key,0);
    if(!irqno)
    {
        printk("软中断号获取失败\n");
        return -ENOMEM;
    }
    printk("软中断号获取成功irqno=%d\n",irqno);
    //注册中断号
    ret = request_irq(irqno,myirq_handler,IRQF_TRIGGER_FALLING,"key1",NULL);
    if(ret)
    {
        printk("注册驱动失败\n");
        return ret;
    }
    printk("key1中断注册成功\n");


    //申请led驱动节点
    dnode_led = of_find_node_by_path("/myleds");
    if(dnode_led == NULL)
    {
        printk("解析led设备树节点失败");
        return -ENXIO;
    }
     printk("解析led设备节点成功\n");
     //led1
    gpiono = gpiod_get_from_of_node(dnode_led,"led1",0,GPIOD_OUT_LOW,NULL);
    if(IS_ERR(gpiono))
    {
        printk("gpio编号解析失败");
        return -PTR_ERR(gpiono);
    }
     //灯灭
    gpiod_set_value(gpiono,0);





    // 1.分配字符设备驱动对象空间 cdev_alloc
    cdev = cdev_alloc();
    if (NULL == cdev)
    {
        printk("字符设备驱动对象空间失败\n");
        ret = -EFAULT;
        goto out1;
    }
    printk("字符设备驱动对象空间申请成功");

    // 2.字符设备驱动对象部分初始化 cdev_init
    cdev_init(cdev, &fops);
    // 3.申请设备号 register_chrdev_region/alloc_chrdev_region
    if (major > 0)
    {
        ret = register_chrdev_region(MKDEV(major, minor), 1, "myled");
        if (ret)
        {
            printk("静态指定设备号失败\n");
            goto out2;
        }
    }
    else // 动态申请设备号
    {
        ret = alloc_chrdev_region(&devno, minor, 1, "myled");
        if (ret)
        {
            printk("动态指定设备号失败\n");
            goto out2;
        }
        major = MAJOR(devno);
        minor = MINOR(devno);
    }
    printk("申请设备号成功\n");

    // 4.注册字符设备驱动对象 cdev_add()
    ret = cdev_add(cdev, MKDEV(major, minor), 1);
    if (ret)
    {
        printk("注册字符设备驱动对象失败\n");
        goto out3;
    }
    printk("注册字符设备驱动对象成功\n");
    // 5.向上提交目录
    // 向上提交目录
    cls = class_create(THIS_MODULE, "myled");
    if (IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        ret = -PTR_ERR(cls);
        goto out4;
    }
    printk("向上提交目录成功\n");

    // 6.向提交节点信息
    
    
        dev = device_create(cls, NULL, MKDEV(major, 0), NULL, "myled");
        if (IS_ERR(dev))
        {
            printk("向上提交设备节点信息失败\n");
            ret = -PTR_ERR(dev);
            goto out5;
        }
    
    printk("线上提交设备节点信息成功\n");

    return 0;
out5:
    
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);
out4:
    cdev_del(cdev);
out3:
    unregister_chrdev_region(MKDEV(major, minor), 1);
out2:
    kfree(cdev);
out1:
    return ret;
}

static void __exit mycdev_exit(void)
{
    // 1.销毁设备信息 device_destroy
        device_destroy(cls, MKDEV(major, 0));
    // 2.销毁目录 class_destroy
    class_destroy(cls);
    // 3.销毁对象 cdev_del
    cdev_del(cdev);
    // 4.释放设备号 unregister_chrdev_region
    unregister_chrdev_region(MKDEV(major, minor), 1);
    // 5.释放对象空间 kfree
    kfree(cdev);
    gpiod_put(gpiono);
    //注销中断
    free_irq(irqno,NULL);
}

module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

test.c

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

int main(int argc, char const *argv[])
{
    int fd;
    char number;

    fd = open("/dev/myled", O_RDWR);
    if (fd < 0)
    {
        printf("打开文件失败\n");
        exit(-1);
    }
    while (1)
    {
        read(fd, &number, sizeof(number));
        printf("number = %c\n", number);
    }

    close(fd);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值