嵌入式Linux系统 “内核定时器”

Linux 下的内核定时器:类似单片机中的定时器中断

1.涉及函数
1. 初始化定时器队列结构 init_timer(&buttons_timer);//
2. 定时器超时函数 buttons_timer.function = buttons_timer_function; //
3.或者初始化定时器和超时函数作为一步(data作为fn的参数) setup_timer(timer, fn, data) //
4. 添加定时器 add_timer(&buttons_timer); //
5. 设置定时器超时时间 1\100 s(修改一次超时时间只会触发一次定时器)mod_timer(&buttons_timer, jiffies+HZ/100); //
6.删除定时器 del_timer(&timer);
注意:一个定时器只能使用一次,要想多次使用,必须在调用处理函数时,修改时间,再次调用。
例程如下:

使用时一般使用宏来例化:
struct timer_list my_timer; 函数外部定义mu_timer的定时器
函数内部使用以下函数来初始化该定时器:
1. init_timer(&my_timer);
2. my_timer.expires = jiffies + HZ;
3. my_timer.function = my_timer_hander;
4 . add_timer(&my_timer);
特别注意第二条:HZ在其他模块中定义为1000.表示表示一秒的定时;若你想设置为1ms;则(my_timer.expires = jiffies + HZ/1000;)
如果要多次执行定时器:
就在my_timer_hander()中;修改定时器的值
mod_timer(&my_timer, jiffies + HZ);//HZ为1秒,在此时间之后继续执行
所使用的头文件为:
#include< Linux /timer.h> (有些系统中没有,不加也可以)
#include<linux/jiffies.h>
下面为一个一秒闪烁一次小等定时器(以字符驱动为例)
基于jZ2440 V3 开发板:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include<linux/jiffies.h>

static struct class *firstdrv_class;
static struct class_device *firstdrv_class_dev;

volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
struct timer_list my_timer;
void my_timer_hander(unsigned long num)
{
static unsigned char val = 0;
mod_timer(&my_timer, jiffies + HZ);
//add_timer(&my_timer);
// printf("jiffies=%d",jiffies);
if(val == 0)
{
*gpfdat |= (1<<4) | (1<<5) | (1<<6);
val = 1;
}
else
{
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
val = 0;
}
}
static int first_drv_open(struct inode *inode, struct file *file)
{

init_timer(&my_timer);
my_timer.expires = jiffies + HZ;
my_timer.function = my_timer_hander;
add_timer(&my_timer);
printk("first_drv_open\n");
/* 配置GPF4,5,6为输出 */
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
}

static struct file_operations first_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = first_drv_open,
};
int major;
static int first_drv_init(void)
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}

static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv"); // 卸载
class_device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
iounmap(gpfcon);
del_timer(&my_timer);
}

module_init(first_drv_init);
module_exit(first_drv_exit);
MODULE_LICENSE("GPL");
下面是测试程序:
static int first_drv_open(struct inode *inode, struct file *file)
{

init_timer(&my_timer);
my_timer.expires = jiffies + HZ;
my_timer.function = my_timer_hander;
add_timer(&my_timer);
printk("first_drv_open\n");
/* 配置GPF4,5,6为输出 */
*gpfcon &= ~((0x3<<(4*2)) | (0x3<<(5*2)) | (0x3<<(6*2)));
*gpfcon |= ((0x1<<(4*2)) | (0x1<<(5*2)) | (0x1<<(6*2)));
return 0;
}

static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
{
/*
int val;

//printk("first_drv_write\n");

copy_from_user(&val, buf, count); // copy_to_user();

if (val == 1)
{
// 点灯
*gpfdat &= ~((1<<4) | (1<<5) | (1<<6));
}
else
{
// 灭灯
*gpfdat |= (1<<4) | (1<<5) | (1<<6);
}
*/
return 0;
}

static struct file_operations first_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = first_drv_open,
.write = first_drv_write,
};


int major;
static int first_drv_init(void)
{
major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核
firstdrv_class = class_create(THIS_MODULE, "firstdrv");
firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
return 0;
}

static void first_drv_exit(void)
{
unregister_chrdev(major, "first_drv"); // 卸载
class_device_unregister(firstdrv_class_dev);
class_destroy(firstdrv_class);
iounmap(gpfcon);
del_timer(&my_timer);
}

module_init(first_drv_init);
module_exit(first_drv_exit);


MODULE_LICENSE("GPL");

测试程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

/* firstdrvtest on
* firstdrvtest off
*/
int main(int argc, char **argv)
{
int fd;
fd = open("/dev/xyz", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
return 0;
}


总结:
可以实现小灯一秒一次闪烁
刚开始不闪烁,以为是驱动问题,后来发现其实时my_timer_hander()中的val变量未被定义为static类型,所以每次进入函数都为0,导致不闪烁。
总体来说,内核定时器是比较简单的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值