目录
整编内核
修改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>
#define DEVICE_NAME "hrtimer"
static struct hrtimer hr_timer;
static ktime_t kt;
static int timer_count = 0;
static int trigger_count = 10; // 默认触发次数
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 0: // 停止定时器
hrtimer_cancel(&hr_timer);
printk(KERN_INFO "Hrtimer stopped\n");
break;
case 1: // 重启定时器
hrtimer_start(&hr_timer, kt, HRTIMER_MODE_REL);
printk(KERN_INFO "Hrtimer 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(arg, 0);
printk(KERN_INFO "Trigger period set to %ld seconds\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);
// 设置定时器的超时时间为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");
将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>
#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;
}