10月19日 驱动作业

a.应用程序通过阻塞的io模型来读取number变量的值

b.number是内核驱动中的一个变量

c.number的值随着按键按下而改变(按键中断) 例如number=0 按下按键number=1 ,再次按下按键number=0

d.在按下按键的时候需要同时将led1的状态取反

e.驱动中需要编写字符设备驱动

f.驱动中需要自动创建设备节点

g.这个驱动需要的所有设备信息放在设备树的同一个节点中

3.使用工作队列进行定时器处理函数的耗时操作

zhongduan.c

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include <linux/cdev.h>
#include <linux/ioctl.h>
#include <linux/device.h>
#include <linux/uaccess.h>
#include<linux/slab.h>
#include<linux/of_irq.h>
#include<linux/interrupt.h>
#include<linux/gpio.h>
#include<linux/of_gpio.h>
#define CNAME  "myzhondduan"

//定义队列头
wait_queue_head_t wq_head;
unsigned int condition=0;//判断是否有数据准备好的标识变量
//定义一个指向设备节点的指针
struct device_node *node1;
struct property *pr1;//属性结构体指针
struct gpio_desc *desc;
/*myleds{
     led1=<&gpioe 10 0>; //&gpioe表示引用的是gpioe控制器 //10表示gpioe10
     //0表示默认状态
     led2=<&gpiof 10 0>; 
     led3=<&gpioe 8 0>; 
 };    */

//定义一个指向设备节点的指针
struct device_node *node;
struct property *pr;//属性结构体指针
int gpiono1;

/*  myirq{
        interrupt-parent=<&gpiof>;
        interrupts=<7 0>, <8 0>,<9 0>;
    };
*/
struct device_node *node;
unsigned int irqno;//接收软中断号
char count = '0';

int major;//定义变量接收主设备号
char kbuf[128]={};//定义数组用于存放和用户之间拷贝的数据
struct class *cls;//句柄
struct device *dev;

//中断处理函数
irqreturn_t irq_handler(int irq, void *dev)
{
   if(count == '0')
        count = '1';
    else
        count = '0';
    kbuf[0] = count;
    //电位反转
    gpiod_set_value(desc,!gpiod_get_value(desc));
    //唤醒 
    condition=1;
    wake_up_interruptible(&wq_head);  
    return IRQ_HANDLED;
}

//对应的是open()
int mycdev_open(struct inode *inode, struct file *file)
{
      printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
      return 0;
}
//read()
ssize_t mycdev_read(struct file *   file, char __user *ubuf, size_t size, loff_t *loff)
{
    //size参数是用户期待读到的字节长度
    int ret;
    if(file->f_flags&O_NONBLOCK)
    {
        //非阻塞
        return -EINVAL;
    }
    else{
        //阻塞
        ret=wait_event_interruptible(wq_head,condition);
        if(ret)
        {
            printk("接收阻塞休眠\n");
            return ret;
        }
    }
    //把内核里的数据拷贝给用户
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_to_user(ubuf,kbuf,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
    //condition=0;
    condition=0;
     
       return size;
}
//write()
ssize_t mycdev_write(struct file *file, const char __user *ubuf, size_t size, loff_t *loff)
{
    int ret;
    if(size>sizeof(kbuf))
    size=sizeof(kbuf);
    ret=copy_from_user(kbuf,ubuf,size);
    if(ret)
    {
        printk("数据从内核向用户拷贝失败\n");
        return -EIO;
    }
       return size;
}
//close()
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 demo_init(void)
{
     int ret;
    //动态注册字符设备驱动
    major=register_chrdev(0,CNAME,&fops);
    if(major<0)
    {
        printk("字符设备驱动注册失败\n");
        return major;
    }
    printk("字符设备驱动注册成功major=%d\n",major);
    //向上提交节点目录
    cls=class_create(THIS_MODULE,"ZD");
    if(IS_ERR(cls))
    {
        printk("向上提交目录失败\n");
        return PTR_ERR(cls);
    }
     printk("向上提交目录成功\n");
     //创建设备节点
     dev=device_create(cls,NULL,MKDEV(major,0),NULL,"myzhongduan");
     if(IS_ERR(dev))
     {
        printk("创建节点失败\n");
        return PTR_ERR(dev);
     }
     printk("创建节点成功\n");

   
    //解析设备树节点
    node=of_find_node_by_name(NULL,"myirq");
    if(node==NULL)
    {
        printk("解析设备树节点失败\n");
        return EAGAIN;
    }
    printk("解析设备树节点成功\n");
    //根据设备树节点获取软中断号
    irqno=irq_of_parse_and_map(node,0);
    if(irqno==0)
    {
        printk("获取软中断号失败\n");
        return EINVAL;
    }
     printk("获取软中断号成功\n");
    //注册中断
    ret=request_irq(irqno,irq_handler,IRQF_TRIGGER_FALLING,"key2_inte",NULL);
    if(ret)
    {
        printk("注册中断失败\n");
        return ret;
    }
    printk("注册中断成功\n");

      //根据设备节点路径获取设备节点信息
    node1=of_find_node_by_path("/myleds");
    if(node==NULL)
    {
        printk("获取节点信息失败\n");
        return ENODATA;
    }
    printk("获取节点信息成功\n");
    
    //根据设备节点路径获取设备节点信息
    node1=of_find_node_by_path("/myleds");
    if(node1==NULL)
    {
        printk("获取节点信息失败\n");
        return ENODATA;
    }
    printk("获取节点信息成功\n");
   //获取并申请gpio编号
  desc=gpiod_get_from_of_node(node1,"led1",0,GPIOD_OUT_LOW,NULL);
  if(IS_ERR(desc))
  {
    printk("申请gpio编号失败\n");
    return PTR_ERR(desc);
  }
  printk("申请gpio编号成功\n");

  //初始化对头
  init_waitqueue_head(&wq_head);
    return 0;
}

//出口函数,卸载驱动的时候执行
static void __exit demo_exit(void)
{
    //销毁节点
    device_destroy(cls,MKDEV(major,0));
    //销毁目录
    class_destroy(cls);
    //注销字符设备驱动
    unregister_chrdev(major,CNAME);
    //注销中断
    free_irq(irqno,NULL);
    //释放GPIO编号
    gpiod_set_value(desc,0);
    gpiod_put(desc);
   
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");

 text.c

#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>

int main(int argc, char const *argv[])
{
    char buf[128]={};
    int fd=open("/dev/myzhongduan",O_RDWR);
    int which;
    if(fd<0)
    {

        printf("设备文件打开失败\n");
        exit(-1);
    }
    printf("设备文件打开成功\n");
    while(1)
    {
        read(fd,buf,sizeof(buf));
        printf("%s\n",buf);
    }
    close(fd);
    return 0;
}

 dingshiqi.c

#include <linux/init.h>
#include <linux/module.h>
#include<linux/of.h>
#include<linux/of_irq.h>
#include<linux/interrupt.h>
#include<linux/workqueue.h>
#include<linux/gpio.h>
#include<linux/timer.h>
#include<linux/of_gpio.h>
/*  myirq{
        interrupt-parent=<&gpiof>;
        interrupts=<7 0>, <8 0>,<9 0>;
    };
*/
struct device_node *node;
struct work_struct work;
//给定时器分配对象
struct timer_list mytimer;
//底半部中断处理函数
void work_func(struct work_struct *w)
{
   int i;
   for(i=0;i<50;i++)
   {
    printk("i=%d\n",i);
   }
}

//定时器处理函数:
void timer_handler(struct timer_list *timer)
{
     //启用底半部中断处理
     schedule_work(&work);
     //再次启用定时器
     mod_timer(&mytimer,jiffies+HZ);
}

static int __init mycdev_init(void)
{
    //工作队列初始化
    INIT_WORK(&work, work_func);
    //初始化定时器
   mytimer.expires=jiffies+HZ;//定时1s
   timer_setup(&mytimer,timer_handler,0);
   //添加定时器
   add_timer(&mytimer);
   
    return 0;
}
static void __exit mycdev_exit(void)
{
     //删除定时器对象
    del_timer(&mytimer);   
}
module_init(mycdev_init);
module_exit(mycdev_exit);
MODULE_LICENSE("GPL");

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值