am335x 看门狗驱动&看门狗应用例程序

<span style="font-family: Arial, Helvetica, sans-serif;">看门狗应用编程,</span>

看门狗应用编程主要是在超出定时器时间之前喂狗,如果超过时间没有喂狗系统则重启。

1. reboot指令能让系统重启,看门狗超时系统才能重启

参照看门狗驱动编写应用程序:

arch/arm/mach-omap2/omap_hwmod.c 



      
      
  1. static long omap_wdt_ioctl(struct file *file, unsigned int cmd,
  2. unsigned long arg)
  3. {
  4. struct omap_wdt_dev *wdev;
  5. int new_margin;
  6. static const struct watchdog_info ident = {
  7. .identity = "OMAP Watchdog",
  8. .options = WDIOF_SETTIMEOUT,
  9. .firmware_version = 0,
  10. };
  11. wdev = file->private_data;
  12. switch (cmd) {
  13. case WDIOC_GETSUPPORT:
  14. return copy_to_user(( struct watchdog_info __user *)arg, &ident,
  15. sizeof(ident));
  16. case WDIOC_GETSTATUS:
  17. return put_user( 0, ( int __user *)arg);
  18. case WDIOC_GETBOOTSTATUS:
  19. if ( cpu_is_omap16xx())
  20. return put_user(__raw_readw(ARM_SYSST),
  21. ( int __user *)arg);
  22. if ( cpu_is_omap24xx())
  23. return put_user( omap_prcm_get_reset_sources(),
  24. ( int __user *)arg);
  25. case WDIOC_KEEPALIVE:
  26. pm_runtime_get_sync(wdev->dev);
  27. spin_lock(&wdt_lock);
  28. omap_wdt_ping(wdev);
  29. spin_unlock(&wdt_lock);
  30. pm_runtime_put_sync(wdev->dev);
  31. return 0;

     
     
  1. case WDIOC_SETTIMEOUT:
  2. if (get_user(new_margin, ( int __user *)arg))
  3. return -EFAULT;
  4. omap_wdt_adjust_timeout(new_margin);
  5. pm_runtime_get_sync(wdev->dev);
  6. spin_lock(&wdt_lock);
  7. omap_wdt_disable(wdev);
  8. omap_wdt_set_timeout(wdev);
  9. omap_wdt_enable(wdev);
  10. omap_wdt_ping(wdev);
  11. spin_unlock(&wdt_lock);
  12. pm_runtime_put_sync(wdev->dev);
  13. /* Fall */
  14. case WDIOC_GETTIMEOUT:
  15. return put_user(timer_margin, ( int __user *)arg);
  16. default:
  17. return -ENOTTY;
  18. }
  19. }




include/linux/watchdog.h


     
     
  1. #define WATCHDOG_IOCTL_BASE 'W'
  2. struct watchdog_info {
  3. __u32 options; /* Options the card/driver supports */
  4. __u32 firmware_version; /* Firmware version of the card */
  5. __u8 identity[ 32]; /* Identity of the board */
  6. };
  7. #define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)
  8. #define WDIOC_GETSTATUS _IOR(WATCHDOG_IOCTL_BASE, 1, int)
  9. #define WDIOC_GETBOOTSTATUS _IOR(WATCHDOG_IOCTL_BASE, 2, int)
  10. #define WDIOC_GETTEMP _IOR(WATCHDOG_IOCTL_BASE, 3, int)
  11. #define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)
  12. #define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)
  13. #define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)
  14. #define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int)
  15. #define WDIOC_SETPRETIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 8, int)
  16. #define WDIOC_GETPRETIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 9, int)
  17. #define WDIOC_GETTIMELEFT _IOR(WATCHDOG_IOCTL_BASE, 10, int)
  18. #define WDIOF_UNKNOWN -1 /* Unknown flag error */
  19. #define WDIOS_UNKNOWN -1 /* Unknown status error */
  20. #define WDIOF_OVERHEAT 0x0001 /* Reset due to CPU overheat */
  21. #define WDIOF_FANFAULT 0x0002 /* Fan failed */
  22. #define WDIOF_EXTERN1 0x0004 /* External relay 1 */
  23. #define WDIOF_EXTERN2 0x0008 /* External relay 2 */
  24. #define WDIOF_POWERUNDER 0x0010 /* Power bad/power fault */
  25. #define WDIOF_CARDRESET 0x0020 /* Card previously reset the CPU */
  26. #define WDIOF_POWEROVER 0x0040 /* Power over voltage */
  27. #define WDIOF_SETTIMEOUT 0x0080 /* Set timeout (in seconds) */
  28. #define WDIOF_MAGICCLOSE 0x0100 /* Supports magic close char */
  29. #define WDIOF_PRETIMEOUT 0x0200 /* Pretimeout (in seconds), get/set */
  30. #define WDIOF_KEEPALIVEPING 0x8000 /* Keep alive ping reply */
  31. #define WDIOS_DISABLECARD 0x0001 /* Turn off the watchdog timer */
  32. #define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */
  33. #define WDIOS_TEMPPANIC 0x0004 /* Kernel panic on temperature trip */

watchdog_test.c


     
     
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <errno.h>
  6. #include <pthread.h>
  7. #include <sys/ioctl.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <fcntl.h>
  11. #include <sys/time.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14. #include <getopt.h>
  15. #include <sys/signal.h>
  16. //watchdog
  17. #define WATCHDOG_IOCTL_BASE 'W'
  18. struct watchdog_info {
  19. unsigned int options; /* Options the card/driver supports */
  20. unsigned int firmware_version; /* Firmware version of the card */
  21. char identity[ 32]; /* Identity of the board */
  22. };
  23. #define WDIOC_GETSUPPORT _IOR(WATCHDOG_IOCTL_BASE, 0, struct watchdog_info)
  24. #define WDIOC_GETSTATUS _IOR(WATCHDOG_IOCTL_BASE, 1, int)
  25. #define WDIOC_GETBOOTSTATUS _IOR(WATCHDOG_IOCTL_BASE, 2, int)
  26. #define WDIOC_GETTEMP _IOR(WATCHDOG_IOCTL_BASE, 3, int)
  27. #define WDIOC_SETOPTIONS _IOR(WATCHDOG_IOCTL_BASE, 4, int)
  28. #define WDIOC_KEEPALIVE _IOR(WATCHDOG_IOCTL_BASE, 5, int)
  29. #define WDIOC_SETTIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 6, int)
  30. #define WDIOC_GETTIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 7, int)
  31. #define WDIOC_SETPRETIMEOUT _IOWR(WATCHDOG_IOCTL_BASE, 8, int)
  32. #define WDIOC_GETPRETIMEOUT _IOR(WATCHDOG_IOCTL_BASE, 9, int)
  33. #define WDIOC_GETTIMELEFT _IOR(WATCHDOG_IOCTL_BASE, 10, int)
  34. #define WDIOF_OVERHEAT 0x0001 /* Reset due to CPU overheat */
  35. #define WDIOF_FANFAULT 0x0002 /* Fan failed */
  36. #define WDIOF_EXTERN1 0x0004 /* External relay 1 */
  37. #define WDIOF_EXTERN2 0x0008 /* External relay 2 */
  38. #define WDIOF_POWERUNDER 0x0010 /* Power bad/power fault */
  39. #define WDIOF_CARDRESET 0x0020 /* Card previously reset the CPU */
  40. #define WDIOF_POWEROVER 0x0040 /* Power over voltage */
  41. #define WDIOF_SETTIMEOUT 0x0080 /* Set timeout (in seconds) */
  42. #define WDIOF_MAGICCLOSE 0x0100 /* Supports magic close char */
  43. #define WDIOF_PRETIMEOUT 0x0200 /* Pretimeout (in seconds), get/set */
  44. #define WDIOF_KEEPALIVEPING 0x8000 /* Keep alive ping reply */
  45. #define WDIOS_DISABLECARD 0x0001 /* Turn off the watchdog timer */
  46. #define WDIOS_ENABLECARD 0x0002 /* Turn on the watchdog timer */
  47. #define WDIOS_TEMPPANIC 0x0004 /* Kernel panic on temperature trip */
  48. int wdt_status= 1;
  49. int wdt_fd;
  50. int time_out = 5;
  51. int main(int argc, char *argv[])
  52. {
  53. int new_time;
  54. int i;
  55. int ret, count = 20;
  56. struct watchdog_info wdt_info;
  57. wdt_fd = open( "/dev/watchdog", O_RDWR);
  58. if(wdt_fd == -1)
  59. perror( "Open Watchdog ERROR!\n");
  60. //get watchdog infomation struct
  61. ioctl(wdt_fd, WDIOC_GETSUPPORT, &wdt_info);
  62. printf( "options=%d,id=%s\n", wdt_info.options, wdt_info.identity);
  63. //set timeout
  64. ioctl(wdt_fd, WDIOC_SETTIMEOUT, &time_out);
  65. //read the timeout value
  66. ioctl(wdt_fd, WDIOC_GETTIMEOUT, &time_out);
  67. new_time = time_out;
  68. printf( "time_value=%d\n", new_time);
  69. #if 0
  70. //close the watchdog
  71. i=WDIOS_DISABLECARD;
  72. printf( "%d\n", ioctl(wdt_fd,WDIOC_SETOPTIONS,&i));
  73. //open the watchdog
  74. i=WDIOS_ENABLECARD;
  75. printf( "%d\n", ioctl(wdt_fd,WDIOC_SETOPTIONS,&i));
  76. #endif
  77. while( 1)
  78. {
  79. count--;
  80. //feed time
  81. if(wdt_status == 1)
  82. {
  83. ioctl(wdt_fd,WDIOC_KEEPALIVE, NULL);
  84. //write(wdt_fd,NULL,1); //feed time, too
  85. printf( "Feed time!\n");
  86. }
  87. sleep( 1);
  88. if(count == 0)
  89. {
  90. wdt_status = 0;
  91. printf( "End of feeding time!\n");
  92. }
  93. }
  94. close(wdt_fd);
  95. return 0;
  96. }

imx6编译:

${CC} watchdog_test.c -o watchdog


am335x编译:

arm-linux-gcc watchdog_test.c -o watchdog


参考:http://www.linuxidc.com/Linux/2013-12/94074.htm













    
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值