新闻  |   论坛  |   博客  |   在线研讨会
linux加载定时器中断模块
0750long | 2010-02-09 21:43:32    阅读:5490   发布文章

linux加载定时器中断模块

 

/*转:http://www.arm9home.net/read.php?tid-3113-keyword-%B6%A8%CA%B1%C6%F7.html
*/
#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; //éù?÷ò??????ò×?·?éè±??á11ì?μ?????
#define timer_irq IRQ_TIMER0
//#define DEVICE_MAJOR major
//#define DEVICE_MINOR 0  //??éè±?o?ò?°??a0
 
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+?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); //+++??+++++£?+?????TCNT|-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;
    }
 
}
 
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);


//测试程序
//2aê?3ìDòè???£o
#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(int argc, char **argv)
{
    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;
}

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
推荐文章
最近访客