timer example

This commit is contained in:
zeroway
2016-10-11 14:08:06 +00:00
parent 09beba3c1e
commit 273476e4ed
2 changed files with 71 additions and 0 deletions

33
debug/timer/Makefile Normal file
View File

@ -0,0 +1,33 @@
# Makefile
# Comment/uncomment the following line to disable/enable debugging
# DEBUG = y
# Usage
# make CC=<your_compiler_path> KERNELDIR=<your_kernel_dir>
#
# make CC=/home/zeroway/3288/src/3288_4.4/prebuilts/gcc/linux-x86/arm/arm-eabi-4.6/bin/arm-eabi-gcc KERNELDIR=/home/zeroway/3288/src/3288_4.4/kernel
# Add your debugging flag (or not) to CFLAGS
ifeq ($(DEBUG),y)
DEBFLAGS = -O -g -DSCULL_DEBUG # "-O" is needed to expand inlines
else
DEBFLAGS = -O2
endif
obj-m := mytimer_test.o
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
CC ?= gcc
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules.order Module.symvers
depend .depend dep:
$(CC) $(CFLAGS) -M *.c > .depend
ifeq (.depend,$(wildcard .depend))
include .depend
endif

View File

@ -0,0 +1,38 @@
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/timer.h>
#define EXPIRES_PERIOD (5*HZ)
struct self_define_struct {
struct timer_list timer;
};
struct self_define_struct g_sds;
static void timeout_handler(unsigned long tdata)
{
printk("%s, %d\n", __FUNCTION__, __LINE__);
del_timer(&g_sds.timer);
}
static int mytimer_test_init(void)
{
printk("%s, %d\n", __FUNCTION__, __LINE__);
init_timer(&g_sds.timer);
g_sds.timer.expires = jiffies + EXPIRES_PERIOD;
g_sds.timer.function = timeout_handler;
add_timer(&g_sds.timer);
}
static void mytimer_test_exit(void)
{
printk("%s, %d\n", __FUNCTION__, __LINE__);
}
module_init(mytimer_test_init)
module_exit(mytimer_test_exit)