一. 简介
前面文章学习了 Linux内核提供的定时器来实现 Led灯的定时亮灭。文章地址如下:
Linux内核定时器实现Led灯的定时亮灭说明二-CSDN博客
实验效果:通过 应用程序调用驱动,从而实现对 Led灯的控制。具体实现 ioctrl函数的驱动代码,从而控制 Led灯。
二. Linux内核定时器ioctrl实现对Led灯的控制
1. 准备工程代码
这里在 前面定时器实验代码的基础上,来进行添加。
驱动所存放的目录为:/home/wangtian/zhengdian_Linux/Linux_Drivers/
将 12_timer工程代码拷贝一份(包括 .vscode目录及其下的文件),重命名新的工程 12_ioctrl_timer:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers$ cp ./12_timer/* ./12_ioctrl_timer/ -rf
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers$ cp ./12_timer/.vscode/ ./12_ioctrl_timer/ -rf
2. 实现思路
该实验的思路:通过 ioctrl函数实现对 Led灯的控制。具体是应用程序通过调用 ioctrl函数,从而实现控制 Led灯。
驱动实现思路:
(1) 定义定时器变量 :一个 timer_list类型的定时器
(2)编写驱动层的 ioctrl函数。
应用层 ioctrl()函数对应于 驱动实现,有两个接口 unlocked_ioctl函数和compat_ioctl函数。如下所示:
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
区别:
三. Linux内核定时器ioctrl实现对Led灯的控制
(1) struct file_operations函数集结构体
基于以上的区别,这里是 32位应用程序运行在 32位系统上,所以,这里需要实现的是 unlocked_ioctl()函数。所以,这会涉及 struct file_operations 的函数集中 ioctrl函数实现赋值给哪一个?(unlocked_ioctrl还是 compat_ioctrl )
static const struct file_operations timer_fops = {
.open = timer_open,
.owner = THIS_MODULE,
.write = timer_write,
.release = timer_close,
.unlocked_ioctl = timer_ioctrl,
};
(2) cmd命令的定义规则
ioctl的命令是自己定义的,但是要符合linux规则。构建命令:
#define _IO(type,nr) /没有参数的命令
#define _IOR(type,nr,size) //该命令是从驱动读取数据
#define _IOW(type,nr,size) //该命令是向驱动写入数据
#define _IOWR(type,nr,size) //双向数据传输
其中,type是幻数,nr 是序号,size 是大小。
(3) 驱动代码编写
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#define TIMER_NAME "timer"
#define TIMER_CNT 1
#define CLOSE_TIMER_CMD _IO(0XEF, 1) //打开定时器命令
#define OPEN_TIMER_CMD _IO(0XEF, 2) //关闭定时器命令
#define SET_PERIOD_CMD _IOW(0XEF, 3, int)//设置定时器的周期值
static int timer_open(struct inode *inode, struct file *filp);
ssize_t timer_write(struct file * filp, const char __user * buf, size_t count, loff_t * ppos);
int timer_close(struct inode * inode, struct file * filp);
static long timer_ioctrl(struct file *filp, unsigned int cmd, unsigned long arg);
static const struct file_operations timer_fops = {
.open = timer_open,
.owner = THIS_MODULE,
.write = timer_write,
.release = timer_close,
.unlocked_ioctl = timer_ioctrl,
};
/*定时器结构体 */
struct timer_dev{
dev_t devid; //主设备号+次设备号
int major; //主设备号
int minor; //次设备号
struct cdev cdev;
struct class* class;
struct device* device;
struct device_node * dev_node;//设备节点
int gpio_number;//gpio编号
struct timer_list timing_dev; //定时器
int timer_period; //定时器周期ms
spinlock_t lock; //自旋锁
};
struct timer_dev timer;
/*定时器处理函数 */
static void timer_func(unsigned long data)
{
static int led_status = 0;
struct timer_dev* dev = (struct timer_dev*)data;
led_status = !led_status;
//设置Led灯电平
gpio_set_value(dev->gpio_number, led_status);
/*重新开启定时器 */
mod_timer(&dev->timing_dev, jiffies + msecs_to_jiffies(dev->timer_period));
}
/* 打开设备函数*/
static int timer_open(struct inode *inode, struct file *filp)
{
filp->private_data = &timer;
return 0;
}
/*ioctrl函数对应驱动层的实现函数 */
static long timer_ioctrl(struct file *filp, unsigned int cmd, unsigned long arg)
{
int ret = 0;
int period_value = 0;
unsigned long flags = 0;
struct timer_dev* dev = filp->private_data;
switch (cmd) {
case CLOSE_TIMER_CMD: //关闭定时器
del_timer_sync(&dev->timing_dev);
break;
case OPEN_TIMER_CMD: //打开定时器
mod_timer(&dev->timing_dev, dev->timer_period);
break;
case SET_PERIOD_CMD: //设置定时器的周期值
ret = copy_from_user(&period_value, (int*)arg, sizeof(period_value));
if(ret != 0)
{
printk("copy_from_user failed!\n");
}
spin_lock_irqsave(&timer.lock, flags); //加自旋锁
dev->timer_period = period_value;
spin_unlock_irqrestore(&timer.lock, flags); //释放自旋锁
mod_timer(&dev->timing_dev, jiffies+ msecs_to_jiffies(dev->timer_period));
break;
default:
ret = -ENOTTY;
}
return ret;
}
/* 从设备中读取数据函数*/
ssize_t timer_write(struct file * filp, const char __user * buf, size_t count, loff_t * ppos)
{
// struct timer_dev* dev = filp->private_data;
return 0;
}
/* 关闭设备函数 */
int timer_close(struct inode * inode, struct file * filp)
{
return 0;
}
/*Led设备初始化 */
int led_io_init(struct timer_dev* dev)
{
int ret = 0;
/*1. 查找Led设备节点(从设备树文件中)*/
dev->dev_node = of_find_node_by_path("/gpioled");
if(NULL == dev->dev_node)
{
printk("find dev_node failed!\n");
goto find_devnode;
}
/*2. 获取Led设备编号 */
dev->gpio_number = of_get_named_gpio(dev->dev_node, "led-gpio", 0);
if(dev->gpio_number < 0)
{
printk("get led_io_number failed!\n");
goto get_led_gpio_number;
}
/*3. 请求设备GPIO */
ret = gpio_request(dev->gpio_number, "led");
if(ret != 0)
{
printk("gpio_request failed!\n");
goto gpio_request_failed;
}
/*4. 设置IO为输出功能,设置高电平(关闭Led灯) */
ret = gpio_direction_output(dev->gpio_number, 1);
if(ret < 0)
{
printk("set io_output failed!\n");
goto set_output_io;
}
return 0;
set_output_io:
gpio_free(dev->gpio_number);
gpio_request_failed:
get_led_gpio_number:
find_devnode:
return ret;
}
/*驱动入口函数 */
static int __init timer_init(void)
{
int ret = 0;
/* 自旋锁初始化*/
spin_lock_init(&timer.lock);
timer.major = 0;
/*1. 注册/申请设备号 */
if(timer.major) //如果给出主设备号,则注册设备号
{
timer.devid = MKDEV(timer.major, 0);
ret = register_chrdev_region(timer.devid, TIMER_CNT, TIMER_NAME);
}
else //否则申请设备号
{
ret = alloc_chrdev_region(&timer.devid, 0, TIMER_CNT, TIMER_NAME);
timer.major = MAJOR(timer.devid);
timer.minor = MINOR(timer.devid);
}
if(ret < 0)
{
printk("devid apply failed!\n");
goto devid_failed;
}
printk("major: %d minor: %d\n", timer.major,timer.minor);
/*2. 设备初始化,添加设备*/
timer.cdev.owner = THIS_MODULE;
cdev_init(&timer.cdev, &timer_fops);
ret = cdev_add(&timer.cdev, timer.devid, TIMER_CNT);
if(ret < 0)
{
printk("cdev_add failed!\n");
goto cdev_init_failed;
}
/*3. 自动创建设备节点 */
timer.class = class_create(THIS_MODULE, TIMER_NAME);
if(IS_ERR(timer.class)){
printk(KERN_ERR "class_create failed!\n");
ret = PTR_ERR(timer.class);
goto class_create_failed;
}
timer.device = device_create(timer.class, NULL, timer.devid, NULL, TIMER_NAME);
if (IS_ERR(timer.device)) {
printk(KERN_ERR "device_create failed!\n");
ret = PTR_ERR(timer.device);
goto device_create_failed;
}
/*4. Led设备初始化 */
led_io_init(&timer);
/*5. 定时器初始化*/
init_timer(&timer.timing_dev);
timer.timer_period = 500; //500ms
timer.timing_dev.function = timer_func; //回调函数
timer.timing_dev.data = (unsigned long)&timer; //将设备结构体作为参数
timer.timing_dev.expires = jiffies + msecs_to_jiffies(timer.timer_period); //500ms
//向内核注册定时器
add_timer(&timer.timing_dev);
return 0;
device_create_failed:
class_destroy(timer.class);
class_create_failed:
cdev_del(&timer.cdev);
cdev_init_failed:
unregister_chrdev_region(timer.devid, TIMER_CNT);
devid_failed:
return ret;
}
/*驱动出口函数 */
static void __exit timer_exit(void)
{
/* 删除定时器 */
del_timer_sync(&timer.timing_dev);
/*关闭Led灯 */
gpio_set_value(timer.gpio_number, 1);
/*1. 释放设备编号 */
gpio_free(timer.gpio_number);
/*2. 删除设备 */
cdev_del(&timer.cdev);
/*3. 注销设备号*/
unregister_chrdev_region(timer.devid, TIMER_CNT);
/*4. 销毁设备 & 类*/
device_destroy(timer.class, timer.devid);
class_destroy(timer.class);
}
/*驱动入口与出口函数 */
module_init(timer_init);
module_exit(timer_exit);
MODULE_LICENSE("GPL"); //驱动 License
MODULE_AUTHOR("WeiYing"); //作者
三. 编译驱动
进入 12_ioctl_timer工程目录下,编译驱动代码:
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/12_ioctrl_timer$ make
make -C /home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga M=/home/wangtian/zhengdian_Linux/Linux_Drivers/12_ioctrl_timer modules
make[1]: 进入目录“/home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga”
CC [M] /home/wangtian/zhengdian_Linux/Linux_Drivers/12_ioctrl_timer/timer.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/wangtian/zhengdian_Linux/Linux_Drivers/12_ioctrl_timer/timer.mod.o
LD [M] /home/wangtian/zhengdian_Linux/Linux_Drivers/12_ioctrl_timer/timer.ko
make[1]: 离开目录“/home/wangtian/zhengdian_Linux/linux/kernel/linux-imx-rel_imx_4.1.15_2.1.0_ga”
wangtian@wangtian-virtual-machine:~/zhengdian_Linux/Linux_Drivers/12_ioctrl_timer$
可以看出,驱动模块可以正常编译。接下来就是编写测试程序,对驱动模块进行测试。