Linux内核提供了定时,短延时函数,比如微秒、纳秒、毫秒延时函数等。
一.配置系统频率
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
-> Kernel Features
-> Timer frequency (<choice> [=y])
设置好以后打开 Linux 内核源码根目录下的.config 文件,
Linux 内核会使用 CONFIG_HZ 来设置自己的系统时钟。打开文件 include/asm-generic/param.h, 定义了一个宏 HZ,宏 HZ 就是 CONFIG_HZ,因此 HZ=100,我们后面编写 Linux 驱动的时候会常常用到 HZ,因为 HZ 表示一秒的节拍数,也就是频率。
二. jiffies
记录系统从启动以来的系统节拍数。
unkown 通常为 jiffies, known 通常是需要对比的值。
time_after(unkown,known)
time_before(unkown,known)
time_after_eq(unkown, known)
time_before_eq(unkown, known)
判断超时
使用 jiffies 判断超时
unsigned long timeout;
timeout = jiffies + (2 * HZ); /* 超时的时间点 */
/*************************************
具体的代码
************************************/
/* 判断有没有超时 */
if(time_before(jiffies, timeout)) {
/* 超时未发生 */
} else {
/* 超时发生 */
}
jiffies 和 ms、 us、 ns 之间的转换函数
三.内核定时器
Linux 内核定时器采用系统时钟来实现, 用软件的方式来实现, 并不是 SoC 提供硬件定时器。
1.结构体timer_list(include/linux/timer.h)
struct timer_list {
struct list_head entry;
unsigned long expires; /* 定时器超时时间,单位是节拍数 */
struct tvec_base *base;
void (*function)(unsigned long); /* 定时处理函数 */
unsigned long data; /* 要传递给 function 函数的参数 */
int slack;
};
2.API
//初始化
void init_timer(struct timer_list *timer)
timer:要初始化定时器。
//注册到内核,定时器就会开始运行
void add_timer(struct timer_list *timer)
//修改定时值
int mod_timer(struct timer_list *timer, unsigned long expires)
expires:修改后的超时时间。
//删除
int del_timer(struct timer_list * timer)
int del_timer_sync(struct timer_list *timer)
返回值: 0,定时器还没被激活; 1,定时器已经激活。
3.示例
struct timer_list timer; /* 定义定时器 */
/* 定时器回调函数 */
void function(unsigned long arg)
{
// 定时器处理代码
...
// 如果需要定时器周期性运行的话就使用 mod_timer 函数重新设置超时值并且启动定时器
mod_timer(&dev->timertest, jiffies + msecs_to_jiffies(2000));
}
/* 初始化函数 */
void init(void)
{
init_timer(&timer); /* 初始化定时器 */
timer.function = function; /* 设置定时处理函数 */
timer.expires=jffies + msecs_to_jiffies(2000);/* 超时时间 2 秒 */
timer.data = (unsigned long)&dev; /* 将设备结构体作为参数 */
//实际使用时,可以不用add_timer,直接使用mod_timer
add_timer(&timer); /* 启动定时器 */
}
/* 退出函数 */
void exit(void)
{
del_timer(&timer); /* 删除定时器 */
/* 或者使用 */
del_timer_sync(&timer);
}
四.短延时函数
void ndelay(unsigned long nsecs)
void udelay(unsigned long usecs)
void mdelay(unsigned long mseces)
五.定时器使用示例
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/io.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/pm_runtime.h>
#include <linux/of.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/gpio.h>
#include <linux/of_gpio.h>
#include <linux/uaccess.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#define CMD_LED_CLOSE (_IO(0XEF, 0x1)) /* 关闭 LED */
#define CMD_LED_OPEN (_IO(0XEF, 0x2)) /* 打开 LED */
#define CMD_SET_PERIOD (_IO(0XEF, 0x3)) /* 设置 LED 闪烁频率 */
struct gpioled_dev {
struct cdev cdev;
struct class *class;
struct device *device;
dev_t devid;
int major;
int minor;
struct device_node *nd;
int gpio;
struct spinlock spinlock;
struct timer_list timer;
unsigned long period;
};
static struct gpioled_dev gpioled;
void timer_func(unsigned long time)
{
static bool val = 1;
val = ~val & 0x1;
gpio_set_value(gpioled.gpio,val);
mod_timer(&gpioled.timer,jiffies + msecs_to_jiffies(gpioled.period));
return;
}
static ssize_t gpio_write(struct file *filp, const char __user *buf, size_t cnt, loff_t *offset)
{
char kernelbuf[1] = {0};
copy_from_user(kernelbuf,buf,1);
printk("kernelbuf[0];%d\r\n",kernelbuf[0]);
gpio_set_value(gpioled.gpio,kernelbuf[0]);
return 0;
}
long gpio_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
spin_trylock(&gpioled.spinlock);
switch(cmd)
{
case CMD_LED_OPEN:
del_timer_sync(&gpioled.timer);
printk("CMD_LED_OPEN...\r\n");
gpio_set_value(gpioled.gpio,1);
break;
case CMD_LED_CLOSE:
del_timer_sync(&gpioled.timer);
printk("CMD_LED_CLOSE...\r\n");
gpio_set_value(gpioled.gpio,0);
break;
case CMD_SET_PERIOD:
gpioled.period = arg;
mod_timer(&gpioled.timer,jiffies + msecs_to_jiffies(gpioled.period));
break;
}
spin_unlock(&gpioled.spinlock);
return 0;
}
static struct file_operations m_fops = {
.owner = THIS_MODULE,
.write = gpio_write,
.unlocked_ioctl = gpio_ioctl,
};
//驱动入口
static int __init xxx_init(void)
{
spin_lock_init(&gpioled.spinlock);
init_timer(&gpioled.timer);
gpioled.timer.function = timer_func;
const char *str;
//找到led节点
gpioled.nd = of_find_node_by_path("/led");
//读取属性
int ret = of_property_read_string(gpioled.nd,"status",&str);
if(ret == 0)
{
if(strcmp("okay",str) != 0)
{
return -EINVAL;
}
}
/*gpio操作:1.获取gpio; 2.申请使用; 3.设置输入输出 4.设置初始值*/
gpioled.gpio = of_get_named_gpio(gpioled.nd, "led-gpio", 0);
printk("of_get_named_gpio\r\n");
ret = gpio_request(gpioled.gpio, "led-gpio");
ret = gpio_direction_output(gpioled.gpio, 1);
gpio_set_value(gpioled.gpio, 1);
//1.创建设备号
if(gpioled.major){
gpioled.devid = MKDEV(gpioled.major,0);
register_chrdev_region(gpioled.devid,1,"test");
}else{
alloc_chrdev_region(&gpioled.devid,0,1,"test");
gpioled.major = MAJOR(gpioled.devid);
gpioled.minor = MINOR(gpioled.devid);
}
printk("newcheled major=%d,minor=%d\r\n",gpioled.major, gpioled.minor);
//2.初始化cdev
gpioled.cdev.owner = THIS_MODULE;
cdev_init(&gpioled.cdev,&m_fops);
//3.添加cdev
cdev_add(&gpioled.cdev,gpioled.devid,1);
//4.创建类
gpioled.class = class_create(THIS_MODULE,"xxx");
//5.创建设备
gpioled.device = device_create(gpioled.class,NULL,gpioled.devid,NULL,"xxx");
return 0;
}
//驱动退出
static void __exit xxx_exit(void)
{
del_timer_sync(&gpioled.timer);
gpio_free(gpioled.gpio);
//删除设备
device_destroy(gpioled.class,gpioled.devid);
//删除类
class_destroy(gpioled.class);
//删除cdev
cdev_del(&gpioled.cdev);
//注销
unregister_chrdev_region(gpioled.devid,1);
}
module_init(xxx_init);
module_exit(xxx_exit);
MODULE_AUTHOR("m0mo");
MODULE_DESCRIPTION("New Char Device Driver");
MODULE_LICENSE("GPL");