• «
  • 1
  • 2
  • 3
  • »
  • Pages: 1/3     Go
主题 : linux驱动问题 (TIMER0)定时器 复制链接 | 浏览器收藏 | 打印
级别: 新手上路
UID: 8756
精华: 0
发帖: 34
金钱: 340 两
威望: 170 点
贡献值: 0 点
综合积分: 68 分
注册时间: 2009-09-05
最后登录: 2010-10-11
楼主  发表于: 2009-09-21 19:11

 linux驱动问题 (TIMER0)定时器

代码如下:问题是程序运行起来后,程序就结束不了,而且别的程序也没反应了(如我再在用COMS采集视频,这个驱动运行后COMS也不动了),CTRL+C也结束不了,板子的触摸屏也不动了。现在就只有重启板子了,大家帮我看看是不是程序中什么地方干拢了系统的中断!

#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)
{
    int ret;
    unsigned long Ftclk,Fpclk=50000000; //s3c2440a的默认Fpclk为50MHz
    outl(255,S3C2410_TCFG0); //设置预分频
    outl(3,S3C2410_TCFG1);   //设置分频和模式
    Ftclk=Fpclk/(255+1)/16;  //参考datasheet公式
    outl(Ftclk,S3C2410_TCNTB(0));  //写入定时初值
    outl(0,S3C2410_TCMPB(0));  //写入终点比较值
    outl(S3C2410_TCON_T0MANUALUPD,S3C2410_TCON); //手动刷新一次,将数据装入TCNT和TCMP
    outl(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 <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

int main(void)
{
    int fd;
    fd=open("/dev/timer",0);
    if(fd<0){
        printf("Open /dev/timer failed!\n");
        exit(1);
    }
    else printf("Open device successfully!\n");
    while(1);
    close(fd);
    return 0;
}

代码如下:问题是程序运行起来后,程序就结束不了,而且别的程序也没反应了(如我再在用COMS采集视频,这个驱动运行后COMS也不动了),CTRL+C也结束不了,板子的触摸屏也不动了。现在就只有重启板子了,大家帮我看看是不是程序中什么地方干拢了系统的中断
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
1楼  发表于: 2009-09-22 14:37
看起来你的驱动本身没有问题,我把它编译成内核模块后在2.6.29.4的内核上运行,当测试程序运行后,会每隔1s触发一次Timer0的中断(出现Timer0 interrupt occured!)。
但你的测试程序最好把
复制代码
  1. while(1);
改成
复制代码
  1. while(1) {sleep(99999999);};
,用来释放CPU时间。虽然不能响应CRTL-C,但至少不会影响系统里其他程序。

附件里是我从你贴出来的源代码编译出来的内核模块和测试程序。
附件: pwm_mod.zip (2 K) 下载次数:157
附件: pwm_mod_test.zip (3 K) 下载次数:148
"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: 8756
精华: 0
发帖: 34
金钱: 340 两
威望: 170 点
贡献值: 0 点
综合积分: 68 分
注册时间: 2009-09-05
最后登录: 2010-10-11
2楼  发表于: 2009-09-22 18:28
谢谢版主,我下来试试看,
级别: 新手上路
UID: 8756
精华: 0
发帖: 34
金钱: 340 两
威望: 170 点
贡献值: 0 点
综合积分: 68 分
注册时间: 2009-09-05
最后登录: 2010-10-11
3楼  发表于: 2009-09-22 18:40
哇,还是一样的呢
版主
  这是怎么回事呀,这个问题搞了好几天了!写个定时器的驱动都这么费劲呢!
    大虾们写驱动都不用这样的定时器吗,我知道还有一个timer_list的结构可以实现定时器,但是那个不能实现us级的定时呀,
     我有个驱动里有一个定时器要实现256us周期性的加1,不知道大家还有没有其它的方法实现!

  谢谢大家了
级别: 新手上路
UID: 8756
精华: 0
发帖: 34
金钱: 340 两
威望: 170 点
贡献值: 0 点
综合积分: 68 分
注册时间: 2009-09-05
最后登录: 2010-10-11
4楼  发表于: 2009-09-23 11:56
我是说加了sleep(9999999); 后还是和以前一样呀!
  别的程序也不能运行!是不是板子上以经使用timer0?

重新配置就出问题了!
*無鈳取玳
级别: 论坛版主
UID: 27
精华: 12
发帖: 5398
金钱: 40120 两
威望: 17929 点
贡献值: 71 点
综合积分: 11036 分
注册时间: 2008-01-16
最后登录: 2014-11-22
5楼  发表于: 2009-09-23 22:52
非常抱歉,我太相信qemu模拟mini2440了,居然忽略了写驱动最基本的原则。驱动写的的确有问题,问题在于操作timer0相关的寄存器(TCFG0, TCFG1, TCON)时, “覆盖”了寄存器中原来储存的设定值,而这些设定值是系统timer4正常运行所必需的。正确的写法应该是在写寄存器之前,先将其原来的值读出来,再加上需要赋的值,最后写回寄存器中。
我改了一下驱动源代码,红色部分是我修改的地方:

static int timer_open(struct inode *inode,struct file *filp)
{
    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;  //?++datasheet+?+
    outl(Ftclk,S3C2410_TCNTB(0));  //+++??眧?+
    outl(0,S3C2410_TCMPB(0));  //+++?++惚+++++

    outl(tcon | S3C2410_TCON_T0MANUALUPD,S3C2410_TCON); //+++??+++++,+????隩CNT|-TCMP
    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;
    }
}

注意原本的寄存器的操作顺序是没有问题的。
[ 此帖被kasim在2009-09-23 22:59重新编辑 ]
"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: 8756
精华: 0
发帖: 34
金钱: 340 两
威望: 170 点
贡献值: 0 点
综合积分: 68 分
注册时间: 2009-09-05
最后登录: 2010-10-11
6楼  发表于: 2009-09-24 12:24
果然如你所说,问题解决了,谢谢总版主


结贴!太感谢了!
级别: 新手上路
UID: 7794
精华: 0
发帖: 26
金钱: 160 两
威望: 44 点
贡献值: 0 点
综合积分: 52 分
注册时间: 2009-07-29
最后登录: 2017-09-13
7楼  发表于: 2010-01-21 16:06

 回 5楼(kasim) 的帖子

我下了你修改的定时器程序,发现什么都没有(我在open中写了  printk("open ok "))
但是能看到    "Open device successfully!\n
级别: 新手上路
UID: 10245
精华: 0
发帖: 19
金钱: 110 两
威望: 31 点
贡献值: 0 点
综合积分: 38 分
注册时间: 2009-10-28
最后登录: 2013-01-30
8楼  发表于: 2010-03-10 09:12
非常感谢!我近来也在做这一块,谢谢指点。
你先走吧,我在后面看看.
级别: 新手上路
UID: 19783
精华: 0
发帖: 1
金钱: 5 两
威望: 1 点
贡献值: 0 点
综合积分: 2 分
注册时间: 2010-04-23
最后登录: 2010-04-27
9楼  发表于: 2010-04-23 10:03
不错哦,谢谢分享
  • «
  • 1
  • 2
  • 3
  • »
  • Pages: 1/3     Go