1.头文件
#include <linux/timer.h>
2.相关函数(一部分)
init_timer(&timer);
add_timer(&timer);
del_timer(&timer);
static void timer_func(unsigned long data)
这个回调函数的参数类型要一致。
3.样例
需要添加的代码,下面以添加备注。代码为部分代码,定时器的功能为通过标志位定时停止while循环的执行。
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/dcache.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/timer.h> //添加头文件
#include <linux/delay.h>
#include <asm/fcntl.h>
#include <asm/processor.h>
#include <asm/uaccess.h>
#include <soc/asr/board_manage.h>
#define DRIVER_AUTHOR "zihang zhang"
#define DRIVER_DESC "A sample driver"
typedef struct monitor_info {
struct mutex lock;
spinlock_t spinlock;
struct delayed_work work;
struct workqueue_struct *workqueue;
struct list_head head;
} monitor_info_t;
static monitor_info_t test_monitor_info;
static struct thermal_status_t thermal_status;
static int flag = 1;
static struct timer_list timer; //声明结构体
extern ssize_t i2c_slave_eeprom_buffer_write(int slave_num, char *buf, loff_t off, size_t count);
extern ssize_t i2c_slave_eeprom_buffer_read(int slave_num, char *buf, loff_t off, size_t count);
static void timer_func(unsigned long data) //回调函数
{
flag = 0;
}
static void test_monitor_func(struct work_struct *work)
{
flag = 1;
//初始化变量
init_timer(&timer);
timer.function = timer_func; //绑定回调函数
timer.expires = jiffies + msecs_to_jiffies(5000); //设置时间5000ms
//添加定时器
add_timer(&timer);
//thermal_status
while(flag)
{
boardmanage_get_thermal_status(&thermal_status);
pr_err("============================thermal status============================\n");
pr_err("thermal status num[0] - num[6]: %d %d %d %d %d %d %d\n", thermal_status.temperature[0], thermal_status.temperature[1], thermal_status.temperature[2],
thermal_status.temperature[3], thermal_status.temperature[4], thermal_status.temperature[5],
thermal_status.temperature[6]);
msleep(10);
}
del_timer(&timer);//删除定时器
//continue workqueue
queue_delayed_work(test_monitor_info.workqueue, &test_monitor_info.work, 500);
}
4.通过timer每隔一段时间固定执行
部分代码,具体实现为在timer的函数中,继续设置timer,并添加timer,添加部分在代码中已备注
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/dcache.h>
#include <linux/fs.h>
#include <linux/err.h>
#include <linux/string.h>
#include <linux/errno.h>
#include <linux/timer.h>
#include <linux/timex.h>
#include <linux/delay.h>
#include <asm/fcntl.h>
#include <asm/processor.h>
#include <asm/uaccess.h>
#include <soc/asr/board_manage.h>
#define DRIVER_AUTHOR "zihang zhang"
#define DRIVER_DESC "A sample driver"
typedef struct monitor_info {
struct mutex lock;
spinlock_t spinlock;
struct delayed_work work;
struct workqueue_struct *workqueue;
struct list_head head;
} monitor_info_t;
typedef unsigned long u64_t;
static monitor_info_t test_monitor_info;
static struct thermal_status_t thermal_status;
static int flag = 1;
static struct timer_list timer;
extern ssize_t i2c_slave_eeprom_buffer_write(int slave_num, char *buf, loff_t off, size_t count);
extern ssize_t i2c_slave_eeprom_buffer_read(int slave_num, char *buf, loff_t off, size_t count);
static void timer_func(unsigned long data)
{
boardmanage_get_thermal_status(&thermal_status);
pr_err("thermal status num[0] - num[6]: %d %d %d %d %d %d %d\n", thermal_status.temperature[0], thermal_status.temperature[1], thermal_status.temperature[2],
thermal_status.temperature[3], thermal_status.temperature[4], thermal_status.temperature[5],
thermal_status.temperature[6]);
/*添加部分*/
timer.expires = jiffies + msecs_to_jiffies(10);
timer.function = timer_func;
add_timer(&timer);
/*添加部分*/
}
static void test_monitor_func(struct work_struct *work)
{
flag = 1;
init_timer(&timer);
timer.expires = jiffies + msecs_to_jiffies(10);
timer.function = timer_func;
add_timer(&timer);
//del_timer(&timer);
}