由于LINUX下应用层不能直接访问寄存器,只要通过驱动程序作为桥梁
// 驱动程序:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifdef MODULE
#define __MODULE__
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#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
#include
#include
#include
#include
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");
}
}
上一篇:s3c2410时钟信号:FCLK、HCLK和PCLK
下一篇:ARM 系列 -- FS2410 开发板上启用 MMU 实现虚拟内存管理
推荐阅读最新更新时间:2024-03-16 15:22