
- hrtimer를 이용하여 timer interrupt 발생하는 예 (kernel module)
- 매 1000ms 마다 interrupt 발생
/*
* example_hrtimer.c
*/
#include <linux/types.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#define TEST_HRTIMER_PERIOD_MS (1000)
struct hrtimer test_timer;
ulong test_nr_iteration = 0;
enum hrtimer_restart test_irq_timer_handler(struct hrtimer *timer)
{
BUG_ON(!in_interrupt());
/*
* DO SOMETHING
*/
test_nr_iteration++;
// reset expire time
hrtimer_forward_now(timer, ms_to_ktime(TEST_HRTIMER_PERIOD_MS));
return HRTIMER_RESTART;
}
static int __init example_hrtimer_init(void)
{
// initialize hrtimer struct
hrtimer_init(&test_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
// save hanlder function
test_timer.function = test_irq_timer_handler;
// start hrtimer
hrtimer_start(&test_timer, ms_to_ktime(TEST_HRTIMER_PERIOD_MS), HRTIMER_MODE_REL);
return 0;
}
static void __exit example_hrtimer_exit(void)
{
// cancel hrtimer
hrtimer_cancel(&test_timer);
}
module_init(example_hrtimer_init);
module_exit(example_hrtimer_exit);
MODULE_LICENSE("GPL");
아래는 arm64 kernel을 위한 Makefile
# must set kernel build output directory
export KERNEL_DIR=../out
export CUR_DIR=$(PWD)
TARGET_NAME=example_hrtimer
obj-m += $(TARGET_NAME).o
all:
make ARCH=arm64 LD=aarch64-linux-gnu-ld CC=aarch64-linux-gnu-gcc -C ${KERNEL_DIR} KCFLAGS=$(KCFLAGS) $(EXTRA_CFLAGS) M=$(CUR_DIR) modules
clean:
make ARCH=arm64 -C ${KERNEL_DIR} M=$(PWD) clean
.
'Kernel > Practice' 카테고리의 다른 글
| misc device 例 (0) | 2024.09.21 |
|---|---|
| kwork 사용 例 (0) | 2024.02.02 |
| process 생성/종료 ftrace 얻기 (0) | 2024.02.02 |
| debugfs 생성 ( /sys/kernel/debug/) (0) | 2024.02.02 |
| 함수 ftrace 얻기 (0) | 2024.02.02 |