主题 : linux 定时器问题 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 7794
精华: 0
发帖: 26
金钱: 160 两
威望: 44 点
综合积分: 52 分
注册时间: 2009-07-29
最后登录: 2017-09-13
楼主  发表于: 2010-01-22 14:42

 linux 定时器问题

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <mach/hardware.h>
#include <linux/cdev.h>
#include <linux/mm.h>
#include <linux/interrupt.h>
#include <linux/poll.h>
#include <asm/uaccess.h>
#include <asm/ioctl.h>
#include <mach/regs-gpio.h>
#include <mach/regs-irq.h>

#include <plat/regs-timer.h>
#include <asm/io.h>

#include <linux/irq.h>

#include <linux/platform_device.h>
#define DEVICE_NAME "timer"

struct cdev *p_cdev; //声明一个指向字符设备结构体的指针
#define timer_irq IRQ_TIMER0
//#define DEVICE_MAJOR major
//#define DEVICE_MINOR 0  //次设备号一般为0

static irqreturn_t timer_interrupt(void)
{
    printk("Timer0 interrupt occured!\n");
    return IRQ_HANDLED;
}

static int timer_open(struct inode *inode,struct file *filp)
{
    printk("open ok!\n");
    int ret;
    unsigned long Ftclk,Fpclk=50000000; //s3c2440a+--?+Fpclk+?0MHz
    unsigned int tcfg0,tcfg1,tcon;

    tcfg0 = inl(S3C2410_TCFG0);
    tcfg1 = inl(S3C2410_TCFG1);
    tcon = inl(S3C2410_TCON);

    outl((tcfg0 &= ~0xff) | 255,S3C2410_TCFG0);
    outl((tcfg1 &= ~0xf) | 3,S3C2410_TCFG1);  
    Ftclk=Fpclk/(255+1)/16;  
    outl(Ftclk,S3C2410_TCNTB(0));  
    outl(0,S3C2410_TCMPB(0));  

    outl(tcon | S3C2410_TCON_T0MANUALUPD,S3C2410_TCON);
    tcon = inl(S3C2410_TCON) & ~S3C2410_TCON_T0MANUALUPD;
    outl(tcon | (S3C2410_TCON_T0START|S3C2410_TCON_T0RELOAD),S3C2410_TCON);  

    ret=request_irq(timer_irq,&timer_interrupt, IRQF_DISABLED, DEVICE_NAME,NULL);
    if(ret<0){
        printk("Register IRQ_TIMER0 failed!\n");
        return ret;
    }
}

static int timer_close(struct inode *inode,struct file *filp)
{
    free_irq(timer_irq,NULL);
    return 0;
}

static struct file_operations timer_fops={
    .owner=THIS_MODULE,
    .open=timer_open,
    .release=timer_close,
};



static struct miscdevice misc = {
    .minor = MISC_DYNAMIC_MINOR,
    .name = DEVICE_NAME,
    .fops = &timer_fops,
};

static int __init dev_init(void)
{
    int ret;

    ret = misc_register(&misc);

    printk (DEVICE_NAME"\tinitialized\n");

    return ret;
}

static void __exit dev_exit(void)
{
    misc_deregister(&misc);
}

MODULE_LICENSE("GPL");
MODULE_AUTHOR("HJW");
module_init(dev_init);
module_exit(dev_exit);
测试程序如下:#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <fcntl.h>
#include <errno.h>

int main()
{
int fd;


    if ((fd = open("/dev/timer", O_RDWR)) < 0)
  
        printf("Open /dev/timer failed!\n");
while(1);

close(fd);
  
  return 0;
}

以上就是定时器0的驱动程序,但是我打开的时候什么都没有输出。(我在open中写了printk("open ok!\n"); 在中断中写了printk("Timer0 interrupt occured!\n");)
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2010-01-22 15:32
确保/dev/timer的major和minor号与/sys/devices/virtual/misc/timer/dev里的内容一致
"If you have an apple and I have an apple and we exchange apples, then you and I will
still each have one apple. But if you have an idea and I have an idea and we exchange
these ideas, then each of us will have two ideas."
级别: 新手上路
UID: 7794
精华: 0
发帖: 26
金钱: 160 两
威望: 44 点
综合积分: 52 分
注册时间: 2009-07-29
最后登录: 2017-09-13
2楼  发表于: 2010-01-22 21:54

 回 1楼(kasim) 的帖子

什么意思?