RK3568驱动指南|第四篇 高级字符设备进阶-第36章 封装驱动API接口实验

瑞芯微RK3568芯片是一款定位中高端的通用型SOC,采用22nm制程工艺,搭载一颗四核Cortex-A55处理器和Mali G52 2EE 图形处理器。RK3568 支持4K 解码和 1080P 编码,支持SATA/PCIE/USB3.0 外围接口。RK3568内置独立NPU,可用于轻量级人工智能应用。RK3568 支持安卓 11 和 linux 系统,主要面向物联网网关、NVR 存储、工控平板、工业检测、工控盒、卡拉 OK、云终端、车载中控等行业。


【公众号】迅为电子

【粉丝群】824412014(加群获取文档+例程)

【视频观看】嵌入式学习之Linux驱动(第四期 高级字符设备进阶)_基于RK3568

【购买链接】迅为RK3568开发板瑞芯微Linux安卓鸿蒙ARM核心板人工智能AI主板

————————————————————————————————————————————

第36章 封装驱动API接口实验

相信经过前面两个章节的学习已经能够熟练的使用ioctl函数了,在本章节会进行两个实验,每个实验的要完成的任务如下所示:

实验一:通过ioctl对定时器进行控制,分别实现打开定时器、关闭定时器和设置定时时间的功能。

实验二:对实验一的应用程序进行封装,从而让应用编程人员更好的对设备进行编程。

36.1 ioctl控制定时器实验

首先进行ioctl控制定时器实验,通过该实验可以综合ioctl函数和定时器相关知识,从而进一步加深对ioctl的理解。

36.1.1 编写测试 APP

本实验对应的应用程序网盘路径为:iTOP-RK3568开发板【底板V1.7版本】\03_【iTOP-RK3568开发板】指南教程\02_Linux驱动配套资料\04_Linux驱动例程\29\app1。

首先来编写应用测试代码ioctl.c,编写好的代码如下所示:

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

#define TIME_OPEN _IO('L',0)
#define TIME_CLOSE _IO('L',1)
#define TIME_SET _IOW('L',2,int)

int main(int argc,char *argv[]){
	int fd;
	fd = open("/dev/test",O_RDWR,0777);//打开test节点
	if(fd < 0){
		printf("file open error \n");
	}
    ioctl(fd,TIME_SET,1000);
	ioctl(fd,TIME_OPEN);
	sleep(3);
    ioctl(fd,TIME_SET,3000);
	sleep(7);
	ioctl(fd,TIME_CLOSE);
	close(fd);
}

第8-10行通过合成宏定义了三个ioctl命令,分别代表定时器打开、定时器关闭、定时时间设置。

第18行和第21行将定时时间分别设置为1秒和3秒。

第19行打开定时器。

第23行关闭定时器。

36.1.2 驱动程序编写

本实验对应的网盘路径为:iTOP-RK3568开发板【底板V1.7版本】\03_【iTOP-RK3568开发板】指南教程\02_Linux驱动配套资料\04_Linux驱动例程\29\module。

编写好的驱动程序ioctl_timer.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/timer.h>
#define TIMER_OPEN _IO('L',0)
#define TIMER_CLOSE _IO('L',1)
#define TIMER_SET _IOW('L',2,int)

struct device_test{

    dev_t dev_num;  //设备号
    int major ;  //主设备号
    int minor ;  //次设备号
    struct cdev cdev_test; // cdev
    struct class *class;   //类
    struct device *device; //设备
	int counter; 
};
static struct device_test dev1;
static void fnction_test(struct timer_list *t);//定义function_test定时功能函数
DEFINE_TIMER(timer_test,fnction_test);//定义一个定时器
void fnction_test(struct timer_list *t)
{
    printk("this is fnction_test\n");
    mod_timer(&timer_test,jiffies_64 + msecs_to_jiffies(dev1.counter));//使用mod_timer函数重新设置定时时间
}
static int cdev_test_open(struct inode *inode, struct file *file)
{
    file->private_data=&dev1;//设置私有数据
    return 0;
}

static int cdev_test_release(struct inode *inode, struct file *file)
{
    file->private_data=&dev1;//设置私有数据

    return 0;
}

static long cdev_test_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
	struct device_test *test_dev = (struct device_test *)file->private_data;//设置私有数据
	switch(cmd){
        case TIMER_OPEN:
			add_timer(&timer_test);//添加一个定时器
            break;
        case TIMER_CLOSE:
			del_timer(&timer_test);//删除一个定时器
            break;
        case TIMER_SET:
			test_dev->counter = arg;
			timer_test.expires = jiffies_64 + msecs_to_jiffies(test_dev->counter);//设置定时时间
            break;

	default:
			break;
	}
	return 0;
}
/*设备操作函数*/
struct file_operations cdev_test_fops = {
    .owner = THIS_MODULE, //将owner字段指向本模块,可以避免在模块的操作正在被使用时卸载该模块
	.open = cdev_test_open,
	.release = cdev_test_release,
	.unlocked_ioctl = cdev_test_ioctl,
};
static int __init timer_dev_init(void) //驱动入口函数
{
    /*注册字符设备驱动*/
    int ret;
    /*1 创建设备号*/
    ret = alloc_chrdev_region(&dev1.dev_num, 0, 1, "alloc_name"); //动态分配设备号
    if (ret < 0)
    {
       goto err_chrdev;
    }
    printk("alloc_chrdev_region is ok\n");

    dev1.major = MAJOR(dev1.dev_num); //获取主设备号
    dev1.minor = MINOR(dev1.dev_num); //获取次设备号

    printk("major is %d \r\n", dev1.major); //打印主设备号
    printk("minor is %d \r\n", dev1.minor); //打印次设备号
     /*2 初始化cdev*/
    dev1.cdev_test.owner = THIS_MODULE;
    cdev_init(&dev1.cdev_test, &cdev_test_fops);

    /*3 添加一个cdev,完成字符设备注册到内核*/
   ret =  cdev_add(&dev1.cdev_test, dev1.dev_num, 1);
    if(ret<0)
    {
        goto  err_chr_add;
    }
    /*4 创建类*/
 dev1. class = class_create(THIS_MODULE, "test");
if(IS_ERR(dev1.class))
{
        ret=PTR_ERR(dev1.class);
        goto err_class_create;
    }
    /*5  创建设备*/
  	dev1.device = device_create(dev1.class, NULL, dev1.dev_num, NULL, "test");
    if(IS_ERR(dev1.device))
    {
        ret=PTR_ERR(dev1.device);
        goto err_device_create;
    }

return 0;

err_device_create:
        class_destroy(dev1.class);                 //删除类

err_class_create:
       cdev_del(&dev1.cdev_test);                 //删除cdev

err_chr_add:
        unregister_chrdev_region(dev1.dev_num, 1); //注销设备号

err_chrdev:
        return ret;
}

static void __exit timer_dev_exit(void) //驱动出口函数
{
    /*注销字符设备*/
    unregister_chrdev_region(dev1.dev_num, 1); //注销设备号
    cdev_del(&dev1.cdev_test);                 //删除cdev
    device_destroy(dev1.class, dev1.dev_num);       //删除设备
    class_destroy(dev1.class);                 //删除类
}
module_init(timer_dev_init);
module_exit(timer_dev_exit);
MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("topeet");

36.2 运行测试

36.2.1 编译驱动程序

在上一小节中的ioctl_timer.c代码同一目录下创建 Makefile 文件,Makefile 文件内容如下所示:

export ARCH=arm64#设置平台架构
export CROSS_COMPILE=aarch64-linux-gnu-#交叉编译器前缀
obj-m += ioctl_timer.o    #此处要和你的驱动源文件同名
KDIR :=/home/topeet/Linux/linux_sdk/kernel    #这里是你的内核目录                                                                                                                            
PWD ?= $(shell pwd)
all:
    make -C $(KDIR) M=$(PWD) modules    #make操作
clean:
    make -C $(KDIR) M=$(PWD) clean    #make clean操作

对于Makefile的内容注释已在上图添加,保存退出之后,来到存放ioctl_timer.c和Makefile文件目录下,如下图(图 36-1)所示:

img

然后使用命令“make”进行驱动的编译,编译完成如下图(图 36-2)所示:

img

编译完生成 ioctl_timer.ko目标文件,如下图(图 36-3)所示:

img

至此驱动模块就编译成功了,下面交叉编译应用程序。

36.2.2 编译应用程序

来到存放应用程序ioctl.c的文件夹下,使用以下命令对ioctl.c进行交叉编译,编译完成如下图(图 36-4)所示:

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

img

生成的ioctl文件就是之后放在开发板上运行的可执行文件,至此应用程序的编译就完成了。

36.2.3 运行测试

开发板启动之后,使用以下命令进行驱动模块的加载,如下图(图 36-5)所示:

insmod ioctl_timer.ko

img

输入以下命令运行可执行文件,运行成功如下图(图 36-6)所示:

img

可以看到前面三个打印信息间隔为1秒钟,后面三个打印信息间隔为3秒钟,至此,实验一就结束了,然后使用以下命令卸载驱动模块,如下图(图 36-7)所示:

rmmod ioctl_timer.ko

img

36.3 封装驱动API接口

至此,随着ioctl练习的结束,字符设备驱动框架相关的知识也就完结了,相信细心的小伙伴在上一小节应用程序的编写中会发现问题,应用程序是从驱动的角度进行编写的,具体内容如下:

#define TIME_OPEN _IO('L',0)
#define TIME_CLOSE _IO('L',1)
#define TIME_SET _IOW('L',2,int)

int main(int argc,char *argv[]){
	int fd;
	fd = open("/dev/test",O_RDWR,0777);//打开test节点
	if(fd < 0){
		printf("file open error \n");
	}
    ioctl(fd,TIME_SET,1000);
	ioctl(fd,TIME_OPEN);
	sleep(3);
    ioctl(fd,TIME_SET,3000);
	sleep(7);
	ioctl(fd,TIME_CLOSE);
	close(fd);
}

作为驱动工程师的我们当然可以理解每一行代码所要完成的功能,而一般情况下,应用都是由专业的应用工程师来进行编写的,上述代码编写方式很不利于应用工程师的理解和程序的移植,所以对于应用程序API的封装是一件必然的事情。

封装好的应用程序网盘路径为:iTOP-RK3568开发板【底板V1.7版本】\03_【iTOP-RK3568开发板】指南教程\02_Linux驱动配套资料\04_Linux驱动例程\29\app2。

首先来编写整体库文件timerlib.h,编写好的代码如下所示:

#ifndef _TIMELIB_H_
#define _TIMELIB_H_
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
 
#define TIMER_OPEN _IO('L',0)
#define TIMER_CLOSE _IO('L',1)
#define TIMER_SET _IOW('L',2,int)
int dev_open();//定义设备打开函数
int timer_open(int fd);//定义定时器打开函数
int timer_close(int fd);//定义定时器关闭函数
int timer_set(int fd,int arg);//定义设置计时时间函数
 
#endif

在9-11行使用合成宏定义了三个ioctl命令,分别代表定时器打开、定时器关闭、定时时间设置。

在第12-15行定义了四个功能函数,所代表的功能分别为设备打开、定时器打开、定时器关闭、定时时间设置。

接下来将创建每个功能函数的c文件,最后编译为单独的库,首先编写dev_open.c文件,编写好的代码如下所示:

#include <stdio.h>
#include "timerlib.h"
int dev_open()
{
    int fd;
    fd = open("/dev/test",O_RDWR,0777);
    if(fd < 0){
        printf("file open error \n");
    }
    return fd;
}

然后编写定时器打开函数timeropen.c文件,编写好的代码如下所示:

#include <stdio.h>
#include "timerlib.h"
int timer_open(int fd)
{
	int ret;
	ret = ioctl(fd,TIMER_OPEN);
	if(ret < 0){
		printf("ioctl open error \n");
		return -1;
	}
	return ret;
}

编写定时器打开函数timerclose.c文件,编写好的代码如下所示:

#include <stdio.h>
#include "timerlib.h"
int timer_close(int fd)
{
	int ret;
	ret = ioctl(fd,TIMER_CLOSE);
	if(ret < 0){
		printf("ioctl  close error \n");
		return -1;
	}
	return ret;
}

编写定时器打开函数timerset.c文件,编写好的代码如下所示:

#include <stdio.h>
#include "timerlib.h"
int timer_set(int fd,int arg)
{
	int ret;
	ret = ioctl(fd,TIMER_SET,arg);
	if(ret < 0){
		printf("ioctl error \n");
		return -1;
	}
	return ret;
}

最后编写测试要用到的应用程序ioctl.c文件,编写好的代码如下所示:

#include <stdio.h>
#include "timerlib.h"
int main(int argc,char *argv[]){
	int fd;
	fd = dev_open();
    timer_set(fd,1000);
	timer_open(fd);
	sleep(3);
	timer_set(fd,3000);
	sleep(7);
	timer_close(fd);
	close(fd);
}

至此,要用到的文件就都编写完成了,会在下一小节进行库的制作,以及应用程序的编译。

36.4 运行测试

36.4.1 编译应用程序

首先使用以下命令将存放功能函数的c文件编译成.o文件,编译完成如下图(图 36-7)所示:

aarch64-linux-gnu-gcc -c dev_open.c

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

img

然后使用以下命令将相应的.o文件编译成.a静态库(这里要注意库的名称都以lib开头),编译完成如下图(图 36-8)所示:

aarch64-linux-gnu-ar rcs libtime.a timer*.o

aarch64-linux-gnu-ar rcs libopen.a dev_open.o

img

最后使用以下命令对ioctl.c进行交叉编译,编译完成如下图(图 36-9)所示:

aarch64-linux-gnu-gcc -o ioctl ioctl.c -L./ -ltime -lopen

img

生成的ioctl文件就是之后放在开发板上运行的可执行文件,至此应用程序的编译就完成了。

36.4.2 运行测试

开发板启动之后,使用以下命令进行驱动模块的加载,如下图(图 36-10)所示:

insmod ioctl_timer.ko

img

输入以下命令运行可执行文件,运行成功如下图(图 36-11)所示:

img

可以看到前面三个打印信息间隔为1秒钟,后面三个打印信息间隔为3秒钟,至此,实验一就结束了,然后使用以下命令卸载驱动模块,如下图(图 36-12)所示:

rmmod ioctl_timer.ko

img

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值