1. timer
每隔8个jiffies,打印一个hello
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/types.h>
-
struct timer_list timer;
-
static void do_timer_process(unsigned long arg)
-
{
-
printk("%s\n", (char*)arg);
-
timer.expires = jiffies +8;
-
add_timer(&timer);
-
}
-
-
static int __init scull_init(void)
-
{
-
printk(KERN_ALERT "enter scull dirver\n");
-
setup_timer(&timer, do_timer_process, (unsigned long)"Hello");
-
timer.expires = jiffies +8;
-
add_timer(&timer);
-
return 0;
-
}
-
-
static void __exit scull_exit(void)
-
{
-
printk(KERN_ALERT "goodbye scull dirver\n");
-
return ;
-
}
-
-
MODULE_LICENSE("GPL");
-
-
module_init(scull_init);
-
module_exit(scull_exit);
2.高精度定时器:
利用hrtimer延时8ms
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/types.h>
-
#include <linux/dma-mapping.h>
-
#include <linux/hrtimer.h>
-
#include <linux/time.h>
-
#include <linux/timer.h>
-
#include <linux/timex.h>
-
#include <linux/rtc.h>
-
#define dbmsg(fmt, args ...) printk(KERN_NOTICE "%s[%d]: "fmt"\n", __FUNCTION__, __LINE__,##args)
-
static struct hrtimer vibe_timer;
-
static int value = 8; //delay 8ms
-
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
-
{
-
struct timeval tv;
-
do_gettimeofday(&tv);
-
dbmsg("time %u:%u",tv.tv_sec,tv.tv_usec);
-
vibe_timer.function = vibrator_timer_func;
-
hrtimer_start(&vibe_timer, ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
-
return HRTIMER_NORESTART;
-
}
-
-
static int scull_init(void)
-
{
-
dbmsg("scull_init");
-
hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
-
vibe_timer.function = vibrator_timer_func;
-
hrtimer_start(&vibe_timer, ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
-
return 0;
-
}
-
-
static void scull_exit(void)
-
{
-
while(hrtimer_cancel(&vibe_timer))
-
{
-
dbmsg("scull exit");
-
}
-
}
-
-
MODULE_LICENSE("GPL");
-
-
module_init(scull_init);
-
module_exit(scull_exit);
打印结果:
lcm3d: vibrator_timer_func[20]: time 87433:710
lcm3d: vibrator_timer_func[20]: time 87433:10708
lcm3d: vibrator_timer_func[20]: time 87433:20702
lcm3d: vibrator_timer_func[20]: time 87433:30706
lcm3d: vibrator_timer_func[20]: time 87433:40702
lcm3d: vibrator_timer_func[20]: time 87433:50702
注:从上述打印来看,大约每次执行了10ms,但是去掉打印从示波器上看的确是8ms
3. 工作队列延时
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/types.h>
-
#include <linux/dma-mapping.h>
-
#include <linux/hrtimer.h>
-
#include <linux/time.h>
-
#include <linux/timer.h>
-
#include <linux/timex.h>
-
#include <linux/rtc.h>
-
#define dbmsg(fmt, args ...) printk(KERN_NOTICE "lcm3d: %s[%d]: "fmt"\n", __FUNCTION__, __LINE__,##args)
-
-
struct delayed_work dwork;
-
static void dwork_func(void)
-
{
-
unsigned long delay = msecs_to_jiffies(8); //每8ms执行一次
-
schedule_delayed_work(&dwork, delay);
-
dbmsg("in dwork_func");
-
}
-
static int scull_init(void)
-
{
-
dbmsg("scull_init");
-
INIT_DELAYED_WORK(&dwork, dwork_func);
-
schedule_work(&dwork); //by feng
-
return 0;
-
}
-
-
static void scull_exit(void)
-
{
-
dbmsg("scull exit");
-
}
-
-
MODULE_LICENSE("GPL");
-
-
module_init(scull_init);
-
module_exit(scull_exit);
4. 工作队列的一个例子
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/types.h>
-
#include <linux/workqueue.h>
-
struct timer_list timer;
-
struct work_struct my_work;
-
struct workqueue_struct *my_work_queue;
-
static void my_work_process(struct work_struct *work)
-
{
-
printk("my_work_process \n");
-
return ;
-
}
-
-
static void do_timer_process(unsigned long arg)
-
{
-
//3.将工作提交到工作队列
-
queue_work(my_work_queue, &my_work);
-
-
timer.expires = jiffies +8;
-
add_timer(&timer);
-
}
-
static int __init scull_init(void)
-
{
-
printk(KERN_ALERT "enter scull dirver\n");
-
//创建工作队列
-
// 1. 填充一个work_struct
-
// 2. 显式的创建一个工作队列
-
INIT_WORK(&my_work, my_work_process);
-
my_work_queue = create_workqueue("my_work");
-
-
//setup timer
-
setup_timer(&timer, do_timer_process, (unsigned long)"Hello");
-
timer.expires = jiffies +8;
-
add_timer(&timer);
-
return 0;
-
}
-
static void __exit scull_exit(void)
-
{
-
printk(KERN_ALERT "goodbye scull dirver\n");
-
return ;
-
}
-
MODULE_LICENSE("GPL");
-
module_init(scull_init);
-
module_exit(scull_exit);
5. 工作队列中参数的传递
-
#include <linux/init.h>
-
#include <linux/module.h>
-
#include <linux/types.h>
-
#include <linux/workqueue.h>
-
#include <linux/slab.h>
-
-
struct timer_list timer;
-
struct _work_s
-
{
-
struct work_struct wk;
-
struct workqueue_struct *wkq;
-
char data[20];
-
};
-
typedef struct _work_s work_s;
-
static void my_work_process(struct work_struct *work)
-
{
-
//利用container_of获取结构体,然后取得参数
-
work_s* my_work= container_of(work, work_s, wk);
-
printk("my_work_process data=%s\n", my_work->data);
-
return ;
-
}
-
-
static void do_timer_process(unsigned long arg)
-
{
-
work_s * my_work = (work_s*)arg; //获取work_s结构体
-
// 3.将工作提交到工作队列
-
queue_work(my_work->wkq, &my_work->wk);
-
timer.expires = jiffies +8;
-
add_timer(&timer);
-
}
-
static int __init scull_init(void)
-
{
-
work_s* my_work = NULL;
-
my_work = kzalloc(sizeof(work_s), GFP_KERNEL);
-
printk(KERN_ALERT "enter scull dirver\n");
-
//创建工作队列
-
// 1. 填充一个work_struct
-
// 2. 显式的创建一个工作队列
-
INIT_WORK(&my_work->wk, my_work_process);
-
my_work->wkq= create_workqueue("my_work");
-
memcpy(my_work->data, "hello", 6);
-
//setup timer
-
//将结构体当作参数传递出去
-
setup_timer(&timer, do_timer_process, (unsigned long)my_work);
-
timer.expires = jiffies +8;
-
add_timer(&timer);
-
return 0;
-
}
-
static void __exit scull_exit(void)
-
{
-
printk(KERN_ALERT "goodbye scull dirver\n");
-
return ;
-
}
-
MODULE_LICENSE("GPL");
-
module_init(scull_init);
-
module_exit(scull_exit);
阅读(1511) | 评论(0) | 转发(0) |