A40i Linux3.10开发板移植高精度定时器hrtimer驱动

目录

整编内核

修改Makefile文件

编译内核

生成.ko文件

应用层调用


这里使用整个编译内核的方式编译.ko文件。

整编内核

编写一个hrtimer_demo.c的驱动程序源码如下:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/uaccess.h>
 
#include <linux/ioctl.h>

//定义设备类型(幻数)
#define IOC_MAGIC 'x'

#define STOP_TIMER _IO(IOC_MAGIC,0)
#define START_TIMER _IO(IOC_MAGIC,1)
#define READ_TIMER_COUNT _IOR(IOC_MAGIC,2,int)
#define SET_TRIGGER_COUNT _IOW(IOC_MAGIC,3,int)
#define SET_TRIGGER_PERIOD _IOW(IOC_MAGIC,4,int)

#define DEVICE_NAME "hrtimer"
 
static struct hrtimer hr_timer;
static ktime_t kt;
static int timer_count = 0;
static int trigger_count = 10;  // 默认触发次数
int data = 0;
 
static enum hrtimer_restart timer_callback(struct hrtimer *timer)
{
    timer_count++;
    printk(KERN_INFO "Timer callback, count: %d\n", timer_count);
    
    if (timer_count >= trigger_count) {
        printk(KERN_INFO "Timer triggered %d times, stopping\n", trigger_count);
        return HRTIMER_NORESTART;
    }
    
    hrtimer_forward_now(timer, kt);
    return HRTIMER_RESTART;
}
 
static int hrtimer_read(struct file *file, char *buf, size_t count, loff_t *ppos)
{
    char tmp[32];
    int len;
    
    len = snprintf(tmp, sizeof(tmp), "Timer count: %d\n", timer_count);
    if (count >= len) {
        if (copy_to_user(buf, tmp, len) != 0) {
            return -EFAULT;
        }
        return len;
    }
    
    return -EINVAL;
}
 
static int hrtimer_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd) {
        case STOP_TIMER:  // 停止定时器
            hrtimer_cancel(&hr_timer);
            printk(KERN_INFO "Hrtimer stopped\n");
            break;
        case START_TIMER:  // 重启定时器
            hrtimer_start(&hr_timer, kt, HRTIMER_MODE_REL);
            printk(KERN_INFO "Hrtimer restarted\n");
            break;
        case READ_TIMER_COUNT:  // 
	    copy_to_user((int __user *)arg, &trigger_count, sizeof(int));
            printk(KERN_INFO "Read trigger_count\n");
            break;
        case SET_TRIGGER_COUNT:  // 设置触发次数
	    copy_from_user(&data, (int __user *)arg, sizeof(int));
            if (data <= 0) {
                printk(KERN_INFO "Trigger count set to %d failed\n", data);
                return -EINVAL;
            }
            trigger_count = data;
            printk(KERN_INFO "Trigger count set to %d\n", trigger_count);
            break;
        case SET_TRIGGER_PERIOD:  // 设置触发周期
	    copy_from_user(&data, (int __user *)arg, sizeof(int));
            if (data <= 0) {
                printk(KERN_INFO "Trigger period set to %ld seconds failed\n", data);
                return -EINVAL;
            }
            kt = ktime_set(data, 0);
            printk(KERN_INFO "Trigger period set to %ld seconds\n", data);
            break;
        default:
            return -EINVAL;
    }
    
    return 0;
}
 
static struct file_operations hrtimer_fops = {
    .read = hrtimer_read,
    .unlocked_ioctl = hrtimer_ioctl,
};
 
static struct miscdevice hrtimer_miscdev = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &hrtimer_fops,
};
 
static int __init hrtimer_demo_init(void)
{
    int ret;
    
    printk(KERN_INFO "Hrtimer module loaded\n");
    
    // 初始化定时器
    hrtimer_init(&hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    
    // 设置定时器的超时时间为1秒
    kt = ktime_set(1, 0);
    
    // 设置定时器的回调函数
    hr_timer.function = timer_callback;
    
    // 启动定时器
    hrtimer_start(&hr_timer, kt, HRTIMER_MODE_REL);
    
    // 注册设备
    ret = misc_register(&hrtimer_miscdev);
    if (ret < 0) {
        printk(KERN_ERR "Failed to register misc device\n");
        return ret;
    }
    
    return 0;
}
 
static void __exit hrtimer_demo_exit(void)
{
    // 注销设备
    misc_deregister(&hrtimer_miscdev);
    
    // 停止定时器
    hrtimer_cancel(&hr_timer);
    
    printk(KERN_INFO "Hrtimer module unloaded\n");
}
 
module_init(hrtimer_demo_init);
module_exit(hrtimer_demo_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Hrtimer Example");

如果需要毫秒级别定时,可以改成如下:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/miscdevice.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/uaccess.h>

#define DEVICE_NAME "hrtimer"

static struct hrtimer hr_timer;
static int trigger_count = 0;
static ktime_t kt;

static enum hrtimer_restart timer_callback(struct hrtimer *timer)
{
    printk(KERN_INFO "Timer callback\n");
    
    // 触发次数减1
    trigger_count--;
    
    // 如果触发次数大于0,继续启动定时器
    if (trigger_count > 0) {
        hrtimer_forward_now(&hr_timer, kt);
        return HRTIMER_RESTART;
    }
    
    return HRTIMER_NORESTART;
}

static ssize_t hrtimer_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
{
    char msg[256];
    int len;
    
    // 将触发次数和触发周期信息写入缓冲区
    len = snprintf(msg, sizeof(msg), "Trigger count: %d\nTrigger period: %lld ms\n", trigger_count, kt.tv64 / 1000000);
    
    // 将缓冲区内容拷贝到用户空间
    if (copy_to_user(buf, msg, len)) {
        return -EFAULT;
    }
    
    return len;
}

static long hrtimer_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
    switch (cmd) {
        case 0:  // 停止定时器
            hrtimer_cancel(&hr_timer);
            printk(KERN_INFO "Timer stopped\n");
            break;
        case 1:  // 重启定时器
            hrtimer_forward_now(&hr_timer, kt);
            printk(KERN_INFO "Timer restarted\n");
            break;
        case 2:  // 设置触发次数
            if (arg <= 0) {
                return -EINVAL;
            }
            trigger_count = arg;
            printk(KERN_INFO "Trigger count set to %d\n", trigger_count);
            break;
        case 3:  // 设置触发周期
            if (arg <= 0) {
                return -EINVAL;
            }
            kt = ktime_set(0, arg * 1000000);
            printk(KERN_INFO "Trigger period set to %ld ms\n", arg);
            break;
        default:
            return -EINVAL;
    }
    
    return 0;
}

static struct file_operations hrtimer_fops = {
    .read = hrtimer_read,
    .unlocked_ioctl = hrtimer_ioctl,
};

static struct miscdevice hrtimer_miscdev = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &hrtimer_fops,
};

static int __init hrtimer_demo_init(void)
{
    int ret;
    
    printk(KERN_INFO "Hrtimer module loaded\n");
    
    // 初始化定时器
    hrtimer_init(&hr_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
    
    // 设置定时器的超时时间为2毫秒
    kt = ktime_set(0, 2000000);
    
    // 设置定时器的回调函数
    hr_timer.function = timer_callback;
    
    // 启动定时器
    hrtimer_start(&hr_timer, kt, HRTIMER_MODE_REL);
    
    // 注册设备
    ret = misc_register(&hrtimer_miscdev);
    if (ret < 0) {
        printk(KERN_ERR "Failed to register misc device\n");
        return ret;
    }
    
    return 0;
}

static void __exit hrtimer_demo_exit(void)
{
    // 注销设备
    misc_deregister(&hrtimer_miscdev);
    
    // 停止定时器
    hrtimer_cancel(&hr_timer);
    
    printk(KERN_INFO "Hrtimer module unloaded\n");
}

module_init(hrtimer_demo_init);
module_exit(hrtimer_demo_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Hrtimer Example");

将hrtimer_demo.c程序拷贝到linux-3.10/drivers/char/目录下

修改Makefile文件

修改linux-3.10/drivers/char/目录下Makefile文件,在Makefile中增加如下代码: 

obj-m += hrtimer_demo.o

编译内核

lu@Computer:~/workplace/A40i/lichee$ ./build.sh -m kernel

生成.ko文件

应用层调用

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include <linux/ioctl.h>
//定义设备类型(幻数)
#define IOC_MAGIC 'x'

#define STOP_TIMER _IO(IOC_MAGIC,0)
#define START_TIMER _IO(IOC_MAGIC,1)
#define READ_TIMER_COUNT _IOR(IOC_MAGIC,2,int)
#define SET_TRIGGER_COUNT _IOW(IOC_MAGIC,3,int)
#define SET_TRIGGER_PERIOD _IOW(IOC_MAGIC,4,int)

#define DEVICE_PATH "/dev/hrtimer"


int main()
{
    int fd;
    char buf[256] = {0};
    
    // 打开设备文件
    fd = open(DEVICE_PATH, O_RDWR);
    if (fd < 0) {
        perror("Failed to open device");
        return -1;
    }
    
    printf("fd=%d\n", fd);

    // 读取设备文件
    if (read(fd, buf, sizeof(buf)) < 0) {
        perror("Failed to read device");
        close(fd);
        return -1;
    }
    
    printf("Read from device: %s\n", buf);

    // 读取 
    int timer_count = 0;
    if (ioctl(fd, READ_TIMER_COUNT, &timer_count) < 0) {
        perror("Failed to stop timer");
        close(fd);
        return -1;
    }
    printf("READ_TIMER_COUNT=%d\n", timer_count);
    
    // 停止定时器
    if (ioctl(fd, STOP_TIMER) < 0) {
        perror("Failed to stop timer");
        close(fd);
        return -1;
    }
    
    printf("Timer stopped\n");
    
    // 设置触发次数为5
    int my_count = 5;
    if (ioctl(fd, SET_TRIGGER_COUNT, &my_count) < 0) {
        perror("Failed to set trigger count");
        close(fd);
        return -1;
    }
    
    printf("Trigger count set to 5\n");
    
    // 设置触发周期为2秒
    int my_period = 2;
    if (ioctl(fd, SET_TRIGGER_PERIOD, &my_period) < 0) {
        perror("Failed to set trigger period");
        close(fd);
        return -1;
    }
    
    printf("Trigger period set to 2 seconds\n");

    // 重启定时器
    if (ioctl(fd, START_TIMER) < 0) {
        perror("Failed to restart timer");
        close(fd);
        return -1;
    }
    
    printf("Timer restarted\n");
    
    
    // 关闭设备文件
    close(fd);
    
    return 0;
}

如果是毫秒级定时器调用

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>

#define DEVICE_PATH "/dev/hrtimer"

int stop_timer(int fd)
{
    if (ioctl(fd, 0) < 0) {
        perror("Failed to stop timer");
        return -1;
    }

    printf("Timer stopped\n");

    return 0;
}

int restart_timer(int fd)
{
    if (ioctl(fd, 1) < 0) {
        perror("Failed to restart timer");
        return -1;
    }

    printf("Timer restarted\n");

    return 0;
}

int set_trigger_count(int fd, int count)
{
    if (ioctl(fd, 2, count) < 0) {
        perror("Failed to set trigger count");
        return -1;
    }

    printf("Trigger count set to %d\n", count);

    return 0;
}

int set_trigger_period(int fd, int period)
{
    if (ioctl(fd, 3, period) < 0) {
        perror("Failed to set trigger period");
        return -1;
    }

    printf("Trigger period set to %d ms\n", period);

    return 0;
}

int main()
{
    int fd;
    char buf[256];

    // 打开设备文件
    fd = open(DEVICE_PATH, O_RDWR);
    if (fd < 0) {
        perror("Failed to open device");
        return -1;
    }

    // 读取设备文件
    if (read(fd, buf, sizeof(buf)) < 0) {
        perror("Failed to read device");
        close(fd);
        return -1;
    }

    printf("Read from device: %s\n", buf);

    // 停止定时器
    if (stop_timer(fd) < 0) {
        close(fd);
        return -1;
    }

    // 设置触发次数为5
    if (set_trigger_count(fd, 5) < 0) {
        close(fd);
        return -1;
    }

    // 设置触发周期为2毫秒
    if (set_trigger_period(fd, 2) < 0) {
        close(fd);
        return -1;
    }

    // 重启定时器
    if (restart_timer(fd) < 0) {
        close(fd);
        return -1;
    }

    // 关闭设备文件
    close(fd);

    return 0;
}

Linux字符设备驱动(设备文件,用户空间与内核空间进行数据交互,ioctl接口)_字符设备文件_星 野的博客-优快云博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值