程序三:利用jiffies及相关宏,实现定时5秒。
创建文件夹/nfsroot/kern/2012-05-11/03/。
创建文件/nfsroot/kern/2012-05-11/03/test.c,内容如下:
View Code
View Code
#include <linux/module.h>
#include <linux/init.h>
#include <linux/param.h>
#include <linux/jiffies.h>
MODULE_LICENSE("GPL");
static void show_uptime(void)
{
unsigned long now;
int h, m, s;
now = jiffies / HZ;
h = now / (60 * 60);
m = (now % (60 * 60)) / 60 + 5;
s = now % 60;
printk("system up %d:%d:%d\n", h, m, s);
}
static int __init test_init(void)
{
unsigned long pos;
pos = jiffies + 5 * HZ;
if (time_before(jiffies, pos))
{
printk("Please wait ...\n");
}
show_uptime();
while (!time_after(jiffies, pos))
; // nothing, just loop!
show_uptime();
return 0;
}
static void __exit test_exit(void)
{
}
module_init(test_init);
module_exit(test_exit);
创建文件/nfsroot/kern/2012-05-11/03/Makefile,内容如下:
View Code
View Code
obj-m := test.o
KERN := /timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410
all:
make -C $(KERN) M=`pwd` modules
clean:
make -C $(KERN) M=`pwd` modules clean
rm -f modules.order
在主机端编译模块,过程如下:
View Code
View Code
[root@localhost 03]# pwd
/nfsroot/kern/2012-05-11/03
[root@localhost 03]# ls
Makefile test.c
[root@localhost 03]# make
make -C /timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410 M=`pwd` modules
make[1]: Entering directory `/timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410'
CC [M] /nfsroot/kern/2012-05-11/03/test.o
Building modules, stage 2.
MODPOST 1 modules
CC /nfsroot/kern/2012-05-11/03/test.mod.o
LD [M] /nfsroot/kern/2012-05-11/03/test.ko
make[1]: Leaving directory `/timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410'
[root@localhost 03]# ls
Makefile Module.symvers test.ko test.mod.o
modules.order test.c test.mod.c test.o
[root@localhost 03]# modinfo test.ko
filename: test.ko
license: GPL
depends:
vermagic: 2.6.28.6 mod_unload ARMv6
[root@localhost 03]#
在开发板端载入模块、查看输出、卸载模块,过程如下:
View Code
View Code
[root@timkyle 03]# pwd
/kern/2012-05-11/03
[root@timkyle 03]# ls
Makefile modules.order test.ko test.mod.o
Module.symvers test.c test.mod.c test.o
[root@timkyle 03]# modinfo test.ko
filename: test.ko
license: GPL
vermagic: 2.6.28.6 mod_unload ARMv6
[root@timkyle 03]# lsmod
[root@timkyle 03]# insmod test.ko ; uptime
Please wait ...
system up 0:27:27
system up 0:27:32
22:11:02 up 27 min, load average: 0.29, 0.11, 0.02
[root@timkyle 03]# rmmod test
[root@timkyle 03]# lsmod
[root@timkyle 03]#
程序四:获取系统当前的绝对时间(自1970年经过的秒数)。
创建文件夹/nfsroot/kern/2012-05-11/04/。
创建文件/nfsroot/kern/2012-05-11/04/test.c,内容如下:
View Code
View Code
#include <linux/module.h>
#include <linux/init.h>
#include <linux/param.h>
#include <linux/jiffies.h>
#include <linux/time.h>
MODULE_LICENSE("GPL");
static struct timeval tv;
static struct timespec ts;
static int __init test_init(void)
{
do_gettimeofday(&tv);
printk("timeval.tv_sec = %ld; timeval.tv_usec = %ld\n", tv.tv_sec, tv.tv_usec);
ts = current_kernel_time();
printk("timespec.tv_set = %ld; timespec.tv_nsec = %ld\n", ts.tv_sec, ts.tv_nsec);
return 0;
}
static void __exit test_exit(void)
{
}
module_init(test_init);
module_exit(test_exit);
创建文件/nfsroot/kern/2012-05-11/04/Makefile,内容如下:
View Code
View Code
obj-m := test.o
KERN := /timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410
all:
make -C $(KERN) M=`pwd` modules
clean:
make -C $(KERN) M=`pwd` modules clean
rm -f modules.order
在主机端编译模块,过程如下:
View Code
View Code
[root@localhost 04]# pwd
/nfsroot/kern/2012-05-11/04
[root@localhost 04]# ls
Makefile test.c
[root@localhost 04]# make
make -C /timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410 M=`pwd` modules
make[1]: Entering directory `/timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410'
CC [M] /nfsroot/kern/2012-05-11/04/test.o
Building modules, stage 2.
MODPOST 1 modules
CC /nfsroot/kern/2012-05-11/04/test.mod.o
LD [M] /nfsroot/kern/2012-05-11/04/test.ko
make[1]: Leaving directory `/timkyle-dev/my/arm/newnfs/home/linux-2.6.28_smdk6410'
[root@localhost 04]# ls
Makefile Module.symvers test.ko test.mod.o
modules.order test.c test.mod.c test.o
[root@localhost 04]# modinfo test.ko
filename: test.ko
license: GPL
depends:
vermagic: 2.6.28.6 mod_unload ARMv6
[root@localhost 04]#
在开发板端载入模块、查看输出、卸载模块,过程如下:
View Code
View Code
[root@timkyle 04]# pwd
/kern/2012-05-11/04
[root@timkyle 04]# ls
Makefile modules.order test.ko test.mod.o
Module.symvers test.c test.mod.c test.o
[root@timkyle 04]# modinfo test.ko
filename: test.ko
license: GPL
vermagic: 2.6.28.6 mod_unload ARMv6
[root@timkyle 04]# lsmod
[root@timkyle 04]# insmod test.ko
timeval.tv_sec = 1370558041; timeval.tv_usec = 904708
timespec.tv_set = 1370558041; timespec.tv_nsec = 903603000
[root@timkyle 04]# rmmod test
[root@timkyle 04]# lsmod
[root@timkyle 04]#