步骤1:同LED

步骤2:同LED

步骤3:编译

  cd workdir/driver/Linux3.2Drivers/fs210_wdt/

  vim Makefile

  修改同LED

  make

步骤4:将ko文件复制到NFS系统目录中。

  cp fs210_wdt.ko /source/rootfs

  make wdt_test

步骤5:启动开发版

  加载驱动

  insmod fs210_wdt.ko

Disabling lock debugging due to kernel taint
[DEBUG] watchdog device major is 253

  mknod /dev/pwm c 253 0
  chmod 777 /dev/wdt
  ./wdt_test

附:程序源码

  fs210_wdt.c

  1 #include <linux/module.h>
  2 #include <linux/init.h>
  3 #include <linux/fs.h>
  4 #include <linux/cdev.h>
  5 #include <linux/ioctl.h>
  6 #include <asm/uaccess.h>
  7 #include <asm/io.h>
  8 #include <linux/delay.h>
  9 #include <plat/regs-watchdog.h>
 10 //#include <plat/map-base.h>
 11 #include <linux/clk.h>
 12 
 13 #define WATCHDOG_MAGIC 'k'  
 14 #define FEED_DOG _IO(WATCHDOG_MAGIC,1)
 15 
 16 #define WATCHDOG_MAJOR 253
 17 #define DEVICE_NAME "s5pv210_watchdog"
 18 
 19 MODULE_LICENSE("GPL");
 20 MODULE_AUTHOR("farsight");
 21 MODULE_DESCRIPTION("s5pv210 Watchdog");
 22 
 23 #define S5PV210_PA_WTCON 0xE2700000
 24 #define S5PV210_PA_WTCNT 0xE2700008
 25 
 26 void *s5pv210_wtcon;
 27 void *s5pv210_wtcnt;
 28 struct clk    *clk;
 29 
 30 static int watchdog_major = WATCHDOG_MAJOR;
 31 
 32 static struct cdev watchdog_cdev;
 33 
 34 static int watchdog_open(struct inode *inode ,struct file *file)    
 35 {
 36     int ret;
 37     
 38     clk = clk_get(NULL, "watchdog");
 39     if (IS_ERR(clk)) {
 40         printk("failed to get watchdog clock\n");
 41         ret = PTR_ERR(clk);
 42         return ret;
 43     }
 44     clk_enable(clk);
 45 
 46     writel(0x8000, s5pv210_wtcnt);//设置计数寄存器,如果里面内容递减为0时,reset。
 47 
 48     writel(S3C2410_WTCON_ENABLE|S3C2410_WTCON_DIV64|S3C2410_WTCON_RSTEN |
 49              S3C2410_WTCON_PRESCALE(0x80), s5pv210_wtcon);//设置控制寄存器的属性,具体参看datasheet,主要是得到t_watchdog时钟周期。
 50 
 51 
 52     printk(KERN_NOTICE"open the watchdog now!\n");
 53     
 54     return 0;
 55 }
 56 
 57 static int watchdog_release(struct inode *inode,struct file *file)
 58 {
 59     clk_disable(clk);
 60     
 61     return 0;
 62 }
 63 
 64 //喂狗
 65 static long watchdog_ioctl(struct file *file,unsigned int cmd,unsigned long arg)
 66 
 67 {    
 68     switch( cmd )
 69     {
 70     case FEED_DOG:
 71         writel(0x8000, s5pv210_wtcnt); 
 72         break;
 73     }
 74     
 75     return 0;    
 76 }
 77 
 78 //将设备注册到系统之中
 79 static void watchdog_setup_dev(struct cdev *dev,int minor,struct file_operations *fops)
 80 {
 81     int err;
 82     dev_t devno = MKDEV(watchdog_major,minor);
 83     
 84     cdev_init(dev, fops); 
 85     dev->owner = THIS_MODULE;
 86     err = cdev_add(dev, devno, 1);
 87     if (err)
 88     printk(KERN_INFO"Error %d adding watchdog %d\n",err,minor);
 89 }
 90 
 91 static struct file_operations watchdog_ops = {
 92     .owner = THIS_MODULE,
 93     .open = watchdog_open, 
 94     .release = watchdog_release, 
 95     .unlocked_ioctl = watchdog_ioctl,
 96 };
 97 
 98 //注册设备驱动程序,主要完成主设备号的注册
 99 static int __init s5pv210_watchdog_init(void)
100 {
101     int result;
102     
103     dev_t dev = MKDEV(watchdog_major,0);
104     
105     result = register_chrdev_region(dev,1,DEVICE_NAME);
106     
107     if (result < 0)
108     {
109          printk(KERN_WARNING"watchdog:unable to get major %d\n",watchdog_major);        
110            return result;
111     }
112     printk(KERN_NOTICE"[DEBUG] watchdog device major is %d\n",watchdog_major);
113     
114     watchdog_setup_dev(&watchdog_cdev, 0, &watchdog_ops);
115 
116     s5pv210_wtcon = ioremap(S5PV210_PA_WTCON, 4);
117     s5pv210_wtcnt = ioremap(S5PV210_PA_WTCNT, 4);
118 
119     return 0;
120 }
121 //驱动模块卸载
122 static void s5pv210_watchdog_exit(void)
123 {
124     iounmap(s5pv210_wtcon);
125     iounmap(s5pv210_wtcnt);
126     cdev_del(&watchdog_cdev);
127     unregister_chrdev_region(MKDEV(watchdog_major,0),1);
128     printk("watchdog device uninstalled\n");
129 }
130 
131 module_init(s5pv210_watchdog_init);
132 module_exit(s5pv210_watchdog_exit);

  wdt_test.c

 1 #include <stdio.h>
 2 #include <sys/types.h>
 3 #include <unistd.h>
 4 #include <fcntl.h>
 5 #include <time.h>
 6 #include <sys/ioctl.h>
 7 
 8 #define WATCHDOG_MAGIC 'k' 
 9 #define FEED_DOG _IO(WATCHDOG_MAGIC,1)
10 
11 int main(int argc,char **argv)
12 {
13     int fd;
14     int n = 3;
15     //打开看门狗
16     
17     fd = open("/dev/wdt",O_RDWR);
18     if (fd < 0)
19     {
20         printf("cannot open the watchdog device\n");
21         return -1;
22     }
23 
24     //喂狗 ,否则系统重启
25     while (n--)
26     {
27         ioctl(fd,FEED_DOG);
28         printf("feed dog\n");
29         sleep(1);
30     }
31 
32     sleep(10);    
33     printf("close\n");
34     close(fd);
35 
36     return 0;
37 }

 

posted on 2018-08-07 17:31  kingofloong  阅读(733)  评论(0)    收藏  举报