S3C6410 看门狗驱动程序代码

#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/types.h>
#include <linux/timer.h>
#include <linux/miscdevice.h>
#include <linux/watchdog.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/interrupt.h>
#include <linux/clk.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/cpufreq.h>
#include <linux/slab.h>

#include <mach/map.h>

#undef S3C_VA_WATCHDOG
#define S3C_VA_WATCHDOG (0)

#include <plat/regs-watchdog.h>

static void __iomem *dog_addr; 

#define set_time 0x01
#define stop_time 0x02
 
//(*(volatile unsigned long *)(adc_addr + name))

/*#define dog_con  (*(volatile unsigned long *)(dog_addr + S3C2410_WTCON))
#define dog_dat  (*(volatile unsigned long *)(dog_addr + S3C2410_WTDAT))
#define dog_cnt  (*(volatile unsigned long *)(dog_addr + S3C2410_WTCNT))*/

static int yzh_watchdog_open(struct inode *inode, struct file *filp)
{
 int temp;
 int tmp;
 
 temp = readl(dog_addr + S3C2410_WTCON);
 temp |= S3C2410_WTCON_RSTEN|S3C2410_WTCON_ENABLE|S3C2410_WTCON_DIV128|S3C2410_WTCON_PRESCALE(0x55);
 writel(temp,dog_addr + S3C2410_WTCON); 

 tmp = readl(dog_addr + S3C2410_WTCON);
 tmp &= ~(S3C2410_WTCON_INTEN);
 writel(tmp,dog_addr + S3C2410_WTCON); 
  
 printk("the dog has opened \n\r");
 return 0;   
}

int yzh_watchdog_release(struct inode *inode, struct file *filp)
{
 int tmp = 0;
 
 tmp = readl(dog_addr + S3C2410_WTCON);
 tmp &= ~(S3C2410_WTCON_ENABLE);
 writel(tmp,dog_addr + S3C2410_WTCON);

 return 0;
}

long yzh_watchdog_unlocked_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
 int temp = 0x55;
 int tmp;
  
 switch(cmd)
 {
  case set_time:
    writel(temp,dog_addr + S3C2410_WTDAT);
    break;
  case stop_time:
    tmp = readl(dog_addr + S3C2410_WTCON);
    tmp &= ~(S3C2410_WTCON_ENABLE);
    writel(tmp,dog_addr + S3C2410_WTCON);
    break;
  default:
   break;
 }
 return 0;

}

struct file_operations yzh_watchdog_fops = {
 .open = yzh_watchdog_open,
 .release = yzh_watchdog_release,
 .unlocked_ioctl = yzh_watchdog_unlocked_ioctl,
};

struct miscdevice yzh_watchdog_misc = {
 .minor = 255, 
 .name = "watchdog_myself",
 .fops = &yzh_watchdog_fops,
};

int yzh_watchdog_init(void)
{
 int ret;
 dog_addr = ioremap(S3C_PA_WDT,0x20); 
 
 ret = misc_register(&yzh_watchdog_misc);
 printk("the yzh_watchdog has init %d\n\r",ret);
 return ret;
}

void yzh_watchdog_exit(void)
{
 int ret;

 iounmap(dog_addr);
 ret = misc_deregister(&yzh_watchdog_misc);
 printk("the yzh_watchdog has uninstall %d\n\r",ret);
}

module_init(yzh_watchdog_init);
module_exit(yzh_watchdog_exit);

MODULE_LICENSE("GPL");
  

 

 

测试代码:

    #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 <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>

#define set_time 0x01
#define stop_time 0x02

int main(void)
{
 int fd;
 int i;

 fd = open("/dev/watchdog_myself",0);
 if(fd < 0)
 {
  printf("the dog open is fail %d\n\r",fd);
 }
 printf("the dog is open is ok %d\n\r",fd);
 
 for(i=0;i<10;i++)
 {
  ioctl(fd,0x01,&i);
  printf("tesing \n\r");
  sleep(1); 
 }
 
 close(fd);
 return 0;
}

转载于:https://my.oschina.net/rabbityzh/blog/182104

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值