Linux内核定时器驱动

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");

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux内核定时器内核用于在未来某个时间点或者特定时间段内调度执行某个函数的一种机制。它是一个软定时器,最终依赖于CPU的硬件定时器实现。对于Linux内核来说,它依赖于系统时钟节拍。内核定时器的处理函数在软中断中执行。它有几个特点:依赖于系统时钟节拍、只执行一次,超时后即退出。如果需要周期性的定时器,需要在超时处理函数中重新开启定时器。在Linux内核编程中常常会使用定时器,例如在驱动程序中使用定时器解决按键消抖、延时等待硬件就绪等问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [【Linux驱动编程】如何使用内核定时器](https://blog.csdn.net/qq_20553613/article/details/106028620)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [【嵌入式Linux驱动开发】十四、了解Linux内核定时器使用流程,实现LED闪烁](https://download.csdn.net/download/weixin_38664427/14883898)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值