(驱动)简单定时器编写练习(2014/8/23)

/*定时点亮LED然后定时熄灭*/

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/delay.h>
#include <linux/jiffies.h>
#include <linux/timer.h>

#include <asm/uaccess.h>
#include <asm/io.h>

volatile unsigned long virt,phys = 0xe0200280;
volatile unsigned long *GPJ2CON,*GPJ2DAT;
struct timer_list time1;
struct timer_list time2;

static void init(void)
{
	virt = (unsigned long)ioremap(phys,0x8);
	GPJ2CON = (unsigned long *)(virt + 0x00);
	GPJ2DAT = (unsigned long *)(virt + 0x04);
	
	*GPJ2CON &= 0xffff0000;
	*GPJ2CON |= 0x00001111;
	
	*GPJ2DAT |= 0x0f;
}

static void led1(unsigned long data)	//点亮LED
{
	 *GPJ2DAT &= 0xf0;	
}

static void led2(unsigned long data)//熄灭LED
{
	*GPJ2DAT |= 0x0f;
}

static __init int test_init(void)
{
	init();
	setup_timer(&time1,led1,NULL);
	time1.expires = jiffies + 1000;
	add_timer(&time1);  //定时器启动,等待触发。

	setup_timer(&time2,led2,NULL);
        time2.expires = jiffies + 2000;
        add_timer(&time2);

	return 0;
}

static __exit void test_exit(void)
{

}

module_init(test_init); 
module_exit(test_exit);  
MODULE_LICENSE("GPL");
MODULE_AUTHOR("henry");
MODULE_DESCRIPTION("This is test demo");


在kernel/drivers/char下的Kconfig里面添加

config LED_TIME 
        tristate "LED TIME"
        default y


kernel/drivers/char下的Makefile里面添加

obj-m       += led-time.o


在kernel下编译:$ make menuconfig

                           $ make modules


把 led-time.ko下载到开发板上:

    $  tftp  -g  -r  led-time.ko 192.168.x.x

装载模块:$  insmod  led-time.ko

卸载模块:$  rmmod  led-time.ko



你可能感兴趣的:(ARM-LINUX驱动学习)