http://wenku.baidu.com/link?url=-9_IHk-7BFRSAxPAeutaQ8Ifhs0Rs9Qg3yAG9LxsqYqRauQRBwjVJ_xnmQ6R-CKbwVDSVmbr4vDJcDyUpbj_Y2RD75J1iLjQonsHywjECRW
-
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include <linux/fs.h>
- #include <linux/init.h>
- #include <linux/delay.h>
- #include <asm/irq.h>
- #include <linux/irq.h>
- #include <linux/interrupt.h>
- #include <asm/uaccess.h>
- #include <linux/timer.h>
- #include <linux/sched.h>
-
- #include <linux/clk.h>
- #include <plat/regs-timer.h>
- #include <asm/io.h>
- #include <linux/miscdevice.h>
- #include <mach/regs-irq.h>
-
- #include <linux/wait.h>
- #define DEVICE_NAME "timer_test"
- #define BUTTON_MAJOR 240
-
- #define TIMER_IOCTL_SET_FREQ 1
- #define TIMER_IOCTL_STOP 0
-
- static struct semaphore lock;
-
- struct button_irq_desc{
- int irq;
- unsigned long flags;
- int num;
- char *name;
- };
- static struct button_irq_desc button_irqs[] = {
- {IRQ_EINT0,IRQF_TRIGGER_FALLING,1,"KEY1"},
- {IRQ_EINT1,IRQF_TRIGGER_FALLING,2,"KEY2"},
- {IRQ_EINT2,IRQF_TRIGGER_FALLING,3,"KEY3"},
- {IRQ_EINT4,IRQF_TRIGGER_FALLING,4,"KEY4"},
- };
- static volatile char key_values[4]={'0','0','0','0'};
- static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
- static volatile int ev_press =0;
-
- static void timer_open(void);
- static void timer_stop(void);
- static inline int timer_interrupt_pending(void);
- static unsigned long get_timer_offset(void);
- static volatile bool value=false;
- static volatile unsigned long count = 0;
- static volatile unsigned long freq_value = 500;
- static volatile unsigned long tcnt_value = 50625;
- static irqreturn_t timer_interrupt_function(int irq,void *dev_id)
- {
- count++;
-
- return IRQ_HANDLED;
- }
- static void timer_second_manage(unsigned long second_value)
- {
- static volatile unsigned long ss_value1 = 0;
- static volatile unsigned long ms_value2 = 0;
- static volatile unsigned long us_value3 = 0;
-
- us_value3 =
- (second_value*1000/freq_value)*10000/tcnt_value/10;
-
- ms_value2 = (count%freq_value)*1000/freq_value + us_value3/1000;
- ss_value1 = count/freq_value + ms_value2/1000;
-
- printk("++++++++++++++++++++++++
");
- printk("used time:%lu n
",count);
- printk("the second_value :%lu n
",second_value);
- printk("used time:%lu.%03lu %03lu s
",ss_value1,
- ms_value2%1000,us_value3%1000);
- printk("++++++++++++++++++++++++
");
-
- }
- static irqreturn_t button_interrupt(int irq,void *dev_id){
- unsigned long second_value = 0;
- int num= ((struct button_irq_desc *)dev_id)->num;
- switch(num){
- case 2:
- value=false;
- timer_stop();
- printk("key1 press
");
- break;
- case 4:
- if(!value) {
- value=true; count=0;
- timer_open();
- }else {
- timer_stop();
- second_value=get_timer_offset();
- timer_second_manage(second_value);
- value=false;
- }
- printk("key2 press
");
- break;
- case 3:
- value=false;
- timer_stop();
- printk("key3 press
");
- break;
- case 1:
- value=false;
- timer_stop();
- printk("key4 press
");
- break;
- default:
- printk("num error,nothing to do!
");
-
- }
-
-
- return IRQ_HANDLED;
- }
-
- static void timer_set_freq(unsigned long freq)
- {
- unsigned long tcon;
- unsigned long tcnt;
- unsigned long tcfg0;
- unsigned long tcfg1;
-
- struct clk *clk_p;
- unsigned long pclk;
-
- tcon = __raw_readl(S3C2410_TCON);
- tcfg0 = __raw_readl(S3C2410_TCFG0);
- tcfg1 = __raw_readl(S3C2410_TCFG1);
-
- tcfg0 &= ~(255<<0);
-
- tcfg0 |= 0;
-
- tcfg1 &= ~(15<<0);
- tcfg1 |= (0<<0);
-
- __raw_writel(tcfg0,S3C2410_TCFG0);
- __raw_writel(tcfg1,S3C2410_TCFG1);
-
- clk_p = clk_get(NULL,"pclk");
- pclk = clk_get_rate(clk_p);
- printk("the pclk is : %ld
",pclk);
-
- tcnt = (pclk/2)/freq;
- tcnt_value = tcnt;
- printk("the tcnt is %ld
",tcnt);
- __raw_writel(tcnt,S3C2410_TCNTB(0));
- __raw_writel(0,S3C2410_TCMPB(0));
-
-
- tcon &= ~0x1e;
- tcon |=0xb;
-
- __raw_writel(tcon,S3C2410_TCON);
- tcon &= ~2;
- __raw_writel(tcon,S3C2410_TCON);
-
- }
- static void timer_open(void)
- {
- unsigned long tcon = 0;
-
- tcon = __raw_readl(S3C2410_TCON);
- tcon |= 1;
- __raw_writel(tcon,S3C2410_TCON);
-
-
- }
- static void timer_stop(void)
- {
- unsigned long tcon;
- tcon = __raw_readl(S3C2410_TCON);
- tcon &= ~1;
- __raw_writel(tcon,S3C2410_TCON);
-
- }
- #define SRCPND_TIMER0 (1<<(IRQ_TIMER0 - IRQ_TIMER0))
- static inline int timer_interrupt_pending(void)
- {
- return __raw_readl(S3C2410_SRCPND) & SRCPND_TIMER0;
- }
- static unsigned long get_timer_offset (void)
- {
- unsigned long tdone;
- unsigned long tval;
-
- tdone = (tcnt_value -__raw_readl(S3C2410_TCNTO(0)));
- if(timer_interrupt_pending()) {
- tval = __raw_readl(S3C2410_TCNTO(0));
- tdone = tcnt_value - tval;
- if(!tval)
- tdone += tcnt_value;
- }
- return tdone;
-
- }
- static struct irqaction timer_irq = {
- .name = "S3C2410 Timer Tick",
- .flags = IRQF_DISABLED |IRQF_TIMER|IRQF_IRQPOLL,
- .handler = timer_interrupt_function,
- .dev_id = NULL
- };
-
- static int button_open(struct inode *inode,struct file *file)
- {
-
- int i,err;
- if(down_trylock(&lock)){
- printk("down_trylock failed!
");
- return -EBUSY;
- }
- if(setup_irq(IRQ_TIMER0,&timer_irq)){
- printk("setup_irq failed!
");
- return -EBUSY;
- }
-
- for(i=0;i<sizeof(button_irqs)/sizeof(button_irqs[0]);i++) {
- err=request_irq(button_irqs[i].irq,button_interrupt,button_irqs[i].flags,
- button_irqs[i].name,(void*)&(button_irqs[i]));
- if(err)
- break;
- }
- if(err) {
- for(--i;i>=0;i--)
- free_irq(button_irqs[i].irq,(void *)&button_irqs[i].num);
- printk("request_irq error!
");
- return -1;
- }
-
- disable_irq(IRQ_EINT0);
- disable_irq(IRQ_EINT1);
- disable_irq(IRQ_EINT2);
- disable_irq(IRQ_EINT4);
-
- set_irq_type(IRQ_EINT0,IRQ_TYPE_LEVEL_LOW);
- set_irq_type(IRQ_EINT1,IRQ_TYPE_EDGE_RISING);
- set_irq_type(IRQ_EINT2,IRQ_TYPE_EDGE_FALLING);
-
- set_irq_type(IRQ_EINT4,IRQ_TYPE_EDGE_FALLING);
-
- enable_irq(IRQ_EINT0);
- enable_irq(IRQ_EINT1);
- enable_irq(IRQ_EINT2);
- enable_irq(IRQ_EINT4);
-
- return 0;
- }
-
- static int button_close(struct inode *inode,struct file *file)
- {
- int i;
- for(i=0;i<sizeof(button_irqs)/sizeof(button_irqs[0]);i++) {
- free_irq(button_irqs[i].irq,(void *)&button_irqs[i]);
- }
- remove_irq(IRQ_TIMER0,&timer_irq);
- up(&lock);
- return 0;
- }
-
- static int button_read(struct file *filp,char __user *buff,
- size_t count,loff_t *offp)
- {
- unsigned long err;
- if(filp->f_flags & O_NONBLOCK)
- return -EAGAIN;
- else {
- wait_event_interruptible(button_waitq,ev_press);
- }
- ev_press=0;
- err=copy_to_user(buff,(const void*)key_values,
- min(sizeof(key_values),count));
- return err ? -EFAULT : min(sizeof(key_values),count);
- }
- static int timer_ioctl(struct inode *inode,struct file *file,
- unsigned int cmd,unsigned long arg)
- {
- switch (cmd) {
- case TIMER_IOCTL_SET_FREQ:
- printk("timer_ioctl_set_freq
");
- if(arg==0)
- return -EINVAL;
- timer_set_freq(arg);
- freq_value = arg;
- break;
- case TIMER_IOCTL_STOP:
- printk("timer_ioctl_stop
");
- timer_stop();
- break;
- }
- return 0;
- }
-
- static struct file_operations button_fops = {
- .owner = THIS_MODULE,
- .open = button_open,
- .read = button_read,
- .release = button_close,
- .ioctl = timer_ioctl,
- };
- static struct miscdevice misc = {
- .minor = MISC_DYNAMIC_MINOR,
- .name = DEVICE_NAME,
- .fops = &button_fops,
- };
- static int __init button_init(void)
- {
- init_MUTEX(&lock);
-
-
- if(misc_register(&misc)<0) {
- printk(DEVICE_NAME"can't register major number!
");
- return -1;
- }
- printk(DEVICE_NAME"register sucess!
");
-
- return 0;
- }
- static void __exit button_exit(void)
- {
- timer_stop();
-
- misc_deregister(&misc);
- }
-
- module_init(button_init);
- module_exit(button_exit);
-
- MODULE_LICENSE("GPL");
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/ioctl.h>
- #define msleep(x) usleep(x*1000);
- #define TIMER_IOCTL_SET_FREQ 1
- #define TIMER_IOCTL_STOP 0
- int main(int argc,char **argv)
- {
- int fd;
- int ioarg=500;
- int press_cnt[4]={0};
- if((fd=open("/dev/timer_test",0))<0){
- printf("Can't open /dev/timer_test~!");
- return -1;
- }
- printf("sleep begin...
");
- ioctl(fd,TIMER_IOCTL_SET_FREQ,ioarg);
-
- while(1)
- usleep(11111);
- ioctl(fd,TIMER_IOCTL_STOP);
- printf("sleep end...
");
- close(fd);
- return 0;
-
- }
- ifneq ($(KERNELRELEASE),)
- obj-m := button_ker.o
- else
- KDIR :=/home/kernel/linux-2.6.32.2
- all:
- make -C $(KDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
-
- arm-linux-gcc button_app.c -o button_app
- clean:
- rm -f *.ko *.o *.mod.o *.mod.c *.symvers modul*