在路上...

The development of life
我们一直都在努力,有您的支持,将走得更远...

站内搜索: Google

  :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
由于LINUX下应用层不能直接访问寄存器,只要通过驱动程序作为桥梁
 //  驱动程序:
 #ifndef __KERNEL__
 #define __KERNEL__
#endif
#ifdef MODULE
 #define __MODULE__
#endif
#include <module.h>
#include <linux/fs.h>
#include <linux/iobuf.h>
#include <linux/major.h>
#include <linux/blkdev.h>
#include <linux/capability.h>
#include <linux/smp_lock.h>
#include <asm/uaccess.h>
#include <asm/hardware.h>
#include <asm/io.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/timer.h>
#include <linux/interrupt.h>
#include <asm/irq.h>
#include <asm/arch/irqs.h>
#include <asm/semaphore.h>
#include <linux/major.h>
#include <linux/vmalloc.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>

//#define rWTCON   (*(volatile unsigned *)0x53000000)
//#define rWTDAT   (*(volatile unsigned *)0x53000004)
//#define rWTCNT   (*(volatile unsigned *)0x53000008)

#define WATCHDOG_MAJOR 213
#define DEVICE_NAME  "watchdog"
 
static unsigned int r_WTCON,r_WTDAT,r_WTCNT;

devfs_handle_t devfs_wt;

int wt_open(struct inode *,struct file *);
ssize_t wt_write(struct file *,const char *,size_t,loff_t *);
 

static struct file_operations wt_fops={
    owner:    THIS_MODULE,
    open:      wt_open,
 write:     wt_write,
};
 
static int address_map(void)
{
 r_WTCON=(unsigned long)ioremap(0x53000000,4);  //控制寄存器
 r_WTDAT=(unsigned long)ioremap(0x53000004,4);  //计数器重载值
    r_WTCNT=(unsigned long)ioremap(0x53000008,4);  //计数器
    return 0;
}
int __init wt_init(void)

    address_map();
    devfs_wt=devfs_register(NULL,DEVICE_NAME,DEVFS_FL_DEFAULT,WATCHDOG_MAJOR, 0, S_IFCHR |S_IRUSR |S_IWUSR |S_IRGRP |S_IWGRP, &wt_fops, NULL);
    return 0;
}
int wt_open(struct inode * inode,struct file * filp)
{
    *(unsigned long *)r_WTCNT=1525 *2;  //2秒
    *(unsigned long *)r_WTCON=0xff39;
    //PCLK=50M
    //Prescaler Value               =255
    // Enable Watchdog              =1  使能看门狗定时器
    //Division_factor                =128(Clock Select=128)
    //Interrupt Generation       =0(不产生中断)
    //Reset                            =1(开启Reset Signal)
    return 0;
}
ssize_t wt_write(struct file *filp,const char * buf ,size_t size,loff_t *offp)
{  
    *(unsigned long *)r_WTCNT=1525 *2;  //2秒
    return 0;
}

void __exit wt_exit(void)
{
   iounmap((void *)r_WTCON);
   iounmap((void *)r_WTDAT);
   iounmap((void *)r_WTCNT);
   devfs_unregister(devfs_wt);
}
module_init(wt_init);
module_exit(wt_exit);
MODULE_LICENSE("watchdog");
MODULE_AUTHOR("<email@mail.com>");
MODULE_DESCRIPTION("watchdog driver for sc2410");
//应用程序:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(void)
{
    int wt_fd; 
    wt_fd = open("/dev/watchdog", O_RDWR);
 if(wt_fd<=0)
 {
        printf("open watchdog  device is wrong!\n");
  return 0;
 }
    while (1) {
        sleep(1);
        write(wt_fd,NULL,0);
        printf("feed watchdog!\n");
    }
}
posted on 2009-08-24 14:51  palam  阅读(1023)  评论(0编辑  收藏  举报