歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> mini6410 實現 看門狗移植

mini6410 實現 看門狗移植

日期:2017/3/1 10:46:44   编辑:Linux編程
寫在移植前的:

看門狗在嵌入式系統開發中占據重要的地位,管理系統的工作狀態。在這裡本人muge0913在參考別人的基礎上,實現了mini6410看門狗的移植。

在mini6410中看門狗驅動文件為linux2.6.38/drivers/watchdog/s3c2410_wdt.c

在mini6410中linux系統默認看門狗是不開機啟動,但是我們可以向/dev/watchdog寫入數據來啟動或關閉看門狗。

如:echo 0 >/dev/watchdog

echo這個命令啟動的作用是先打開文件,再寫入內容,然後關閉。也就是open->write->release。

運行效果:

一段時間後系統會自動重啟。

如果執行:

echo 0 >/dev/watchdog

echo V >/dev/watchdog

系統側不會重啟。

原因分析:

open函數:

  1. static int s3c2410wdt_open(struct inode *inode, struct file *file)
  2. {
  3. if (test_and_set_bit(0, &open_lock))
  4. return -EBUSY;
  5. if (nowayout)
  6. __module_get(THIS_MODULE);
  7. expect_close = 0;
  8. /* start the timer */
  9. s3c2410wdt_start();
  10. return nonseekable_open(inode, file);
  11. }
release函數:
  1. static int s3c2410wdt_release(struct inode *inode, struct file *file)
  2. {
  3. /*
  4. * Shut off the timer.
  5. * Lock it in if it's a module and we set nowayout
  6. */
  7. if (expect_close == 42)
  8. s3c2410wdt_stop();
  9. else {
  10. dev_err(wdt_dev, "Unexpected close, not stopping watchdog\n");
  11. s3c2410wdt_keepalive();
  12. }
  13. expect_close = 0;
  14. clear_bit(0, &open_lock);
  15. return 0;
  16. }
write函數:
  1. static ssize_t s3c2410wdt_write(struct file *file, const char __user *data,
  2. size_t len, loff_t *ppos)
  3. {
  4. /*
  5. * Refresh the timer.
  6. */
  7. if (len) {
  8. if (!nowayout) {
  9. size_t i;
  10. /* In case it was set long ago */
  11. expect_close = 0;
  12. for (i = 0; i != len; i++) {
  13. char c;
  14. if (get_user(c, data + i))
  15. return -EFAULT;
  16. if (c == 'V')
  17. expect_close = 42;
  18. }
  19. }
  20. s3c2410wdt_keepalive();
  21. }
  22. return len;
  23. }

看門狗只能被一個進程打開,打開函數中先判斷了一下,然後啟動了看門狗;再看write函數,寫入的如果是V則允許關閉看門狗,如果不是V僅僅喂狗一次;最後是release函數,如果允許關閉則關閉看門狗,如果不允許關閉,打印"Unexpectedclose, not stopping watchdog",喂狗一次。此時看門狗並沒有關閉,所以系統會復位的,如果輸入V則看門狗被關閉,這樣系統就不復位了。

Copyright © Linux教程網 All Rights Reserved