驱动-定时-秒-字符设备


目的

通过定时器timer_list、字符设备、规避竞争关系-原子操作,综合运用 实现一个程序,加深之前知识的理解。

  • 实现字符设备驱动框架, 自动生成设备节点。
  • 根据上一小节学到的知识, 实现秒计时。
  • 通过原子变量来记录递增的秒数, 避免竞争的发生。
  • 通过用户空间和内核空间的数据交换, 将记录的秒数传递到应用空间, 并通过应用程
    序打印出来

相关资料参考

驱动-原子操作
驱动-Linux定时-timer_list

实验

驱动程序-timer_dev.c


#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/kdev_t.h>
#include <linux/uaccess.h>
#include <linux/atomic.h>

 
struct device_test
{
   dev_t  dev_num;  //定义dev_t类型变量来表示设备号
   int major,minor; //定义int 类型的主设备号和次设备号
   struct cdev cdev_test;   //定义字符设备
   struct class *class;   //定义结构体变量class 类
   struct device *device;  // 设备
    int sec;               //秒

};
atomic64_t v = ATOMIC_INIT(0);//定义原子类型变量v,并定义为0

struct device_test dev1;  
 
static void function_test(struct timer_list *t);//定义function_test定时功能函数
DEFINE_TIMER(timer_test,function_test);//定义一个定时器
static void function_test(struct timer_list *t)
{
	atomic64_inc(&v);//原子变量v自增
	dev1.sec = atomic_read(&v);//将读取到的原子变量v,赋值给sec
	printk("the sec is %d\n",dev1.sec);
	mod_timer(&timer_test,jiffies_64 + msecs_to_jiffies(1000));//使用mod_timer函数将定时时间设置为一秒后
}


/*打开设备函数*/
static int open_test(struct  inode  *inode,struct file *file)
 {
    
    file->private_data=&dev1;//设置私有数据
    printk("\n this is open_test \n");
	add_timer(&timer_test);	//添加一个定时器
    return 0;
 };
 

static ssize_t read_test(struct file *file, char __user *buf, size_t size, loff_t *off)
{
	if(copy_to_user(buf,&dev1.sec,sizeof(dev1.sec))){//使用copy_to_user函数将sec传递到应用层
		printk("copy_to_user error \n");
        return -1;
	}
    return 0;
}
 
 

static int release_test(struct inode *inode,struct file *file)
{
   del_timer(&timer_test);//删除一个定时器
   printk("\nthis is release_test \n");
   return 0;
}






static struct file_operations fops_test = {
	.owner=THIS_MODULE,//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块
    .open = open_test,//将open字段指向chrdev_open(...)函数
    .read = read_test,//将open字段指向chrdev_read(...)函数
    .release = release_test,//将open字段指向chrdev_release(...)函数
};//定义file_operations结构体类型的变量cdev_test_ops




static int __init timer_dev_init(void)//驱动入口函数
{

    if(alloc_chrdev_region(&dev1.dev_num,0,1,"chrdev_name") < 0){
            printk("alloc_chrdev_region is error\n");
    }   
        printk("alloc_chrdev_region is ok\n");
        dev1.major=MAJOR(dev1.dev_num);//通过MAJOR()函数进行主设备号获取
        dev1.minor=MINOR(dev1.dev_num);//通过MINOR()函数进行次设备号获取
        printk("major is %d\n",dev1.major);
        printk("minor is %d\n",dev1.minor);
 
        使用cdev_init()函数初始化cdev_test结构体,并链接到cdev_test_ops结构体
        cdev_init(&dev1.cdev_test,&fops_test);
        dev1.cdev_test.owner = THIS_MODULE;//将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块 

        cdev_add(&dev1.cdev_test,dev1.dev_num,1);
        printk("cdev_add is ok\n");
        dev1.class  = class_create(THIS_MODULE,"test");//使用class_create进行类的创建,类名称为class_test
        device_create(dev1.class,NULL,dev1.dev_num,NULL,"test");//使用device_create进行设备的创建,设备名称为device_test

    return 0;
}
static void __exit timer_dev_exit(void)//驱动出口函数
{
    cdev_del(&dev1.cdev_test);//使用cdev_del()函数进行字符设备的删除
    unregister_chrdev_region(dev1.dev_num,1);//释放字符驱动设备号 
    device_destroy(dev1.class,dev1.dev_num);//删除创建的设备
    class_destroy(dev1.class);//删除创建的类
    printk("module exit \n");

}
module_init(timer_dev_init);//注册入口函数
module_exit(timer_dev_exit);//注册出口函数
MODULE_LICENSE("GPL v2");//同意GPL开源协议
MODULE_AUTHOR("wang fang chen "); //作者信息

编译文件-Makefile

#!/bin/bash
export ARCH=arm64
export CROSS_COMPILE=aarch64-linux-gnu-
obj-m += timer_dev.o
KDIR :=/home/wfc123/Linux/rk356x_linux/kernel
PWD ?= $(shell pwd)
all:
	make -C $(KDIR) M=$(PWD) modules

clean:
	make -C $(KDIR) M=$(PWD) clean

测试程序-timer.c


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

int main(int argc,char *argv[]){
	int fd;//定义int类型的文件描述符fd
	int count;//定义int类型记录秒数的变量count
	fd = open("/dev/test",O_RDWR);//使用open()函数以可读可写的方式打开设备文件
	while(1)
	{	
		read(fd,&count,sizeof(count));//使用read函数读取内核传递来的秒数
		sleep(1);
		printf("num is %d\n",count);
	}
	return 0;
}

编译测试程序变成可执行文件:

aarch64-linux-gnu-gcc -o timer timer.c

分析

通过函数 read(fd,&count,sizeof(count)) 调用, 为什么count 值会变化。 有的人会问,count 默认值是0 ,read 读取。

ssize_t read(int fd, void *buf, size_t count);

函数read 参数解释:

  • fd 参数代表文件描述符,是一个整数,用于标识要读取的文件或设备。
  • buf 参数是一个指向缓冲区的指针,read 函数会将读取的数据存储在这个缓冲区中。
  • count 参数指定了最多要读取的字节数。

这里好好思考一下如下,会不会有问题,需要理解清楚,思考清楚。

测试程序每隔1秒读取count值 放到$count 缓冲区, 然后读取;内核端每隔一秒增加sec 值,通过原子操作+1,然后内核层copy_to_user。 用户层在缓冲区读取的值就是内核层每隔一秒通过 buffer 传递过来的。

在这里插入图片描述

加载驱动-运行测试程序

insmod timer_dev.ko

./timer

实际返回值,如下:
在这里插入图片描述

总结

这个程序实验,用到了原子操作+定时器timer_list+字符设备操作。 简要了解即可,加深前面知识印象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

野火少年

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值