drv_wdt.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2020-2021, Bluetrum Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-11-30 greedyhao first version
  9. */
  10. #include <board.h>
  11. #ifdef RT_USING_WDT
  12. #include <drv_wdt.h>
  13. // #define DRV_DEBUG
  14. #define LOG_TAG "drv.wdt"
  15. #include <drv_log.h>
  16. struct ab32_wdt_obj
  17. {
  18. rt_watchdog_t watchdog;
  19. };
  20. static struct ab32_wdt_obj ab32_wdt;
  21. static struct rt_watchdog_ops ops;
  22. static rt_err_t wdt_init(rt_watchdog_t *wdt)
  23. {
  24. return RT_EOK;
  25. }
  26. static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
  27. {
  28. rt_uint32_t tmp = 0;
  29. switch (cmd)
  30. {
  31. /* feed the watchdog */
  32. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  33. WDTCON = 0xa;
  34. break;
  35. /* set watchdog timeout */
  36. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  37. if (arg == RT_NULL) {
  38. LOG_E("The watchdog timeout argument cannot be NULL!");
  39. break;
  40. }
  41. tmp = WDTCON & ~(0x7 << 20);
  42. switch (*((rt_uint32_t *)arg))
  43. {
  44. case AB32_WDT_TIMEOUT_1MS:
  45. LOG_I("The watchdog timeout is set to 1ms");
  46. tmp |= (0xa << 24) | (0x00 << 20);
  47. break;
  48. case AB32_WDT_TIMEOUT_256MS:
  49. LOG_I("The watchdog timeout is set to 256ms");
  50. tmp |= (0xa << 24) | (0x01 << 20);
  51. break;
  52. case AB32_WDT_TIMEOUT_512MS:
  53. LOG_I("The watchdog timeout is set to 512ms");
  54. tmp |= (0xa << 24) | (0x02 << 20);
  55. break;
  56. case AB32_WDT_TIMEOUT_1024MS:
  57. LOG_I("The watchdog timeout is set to 1024ms");
  58. tmp |= (0xa << 24) | (0x03 << 20);
  59. break;
  60. case AB32_WDT_TIMEOUT_2048MS:
  61. LOG_I("The watchdog timeout is set to 2048ms");
  62. tmp |= (0xa << 24) | (0x04 << 20);
  63. break;
  64. case AB32_WDT_TIMEOUT_4096MS:
  65. LOG_I("The watchdog timeout is set to 4096ms");
  66. tmp |= (0xa << 24) | (0x05 << 20);
  67. break;
  68. case AB32_WDT_TIMEOUT_8192MS:
  69. LOG_I("The watchdog timeout is set to 8192ms");
  70. tmp |= (0xa << 24) | (0x06 << 20);
  71. break;
  72. case AB32_WDT_TIMEOUT_16384MS:
  73. LOG_I("The watchdog timeout is set to 16384ms");
  74. tmp |= (0xa << 24) | (0x07 << 20);
  75. break;
  76. default:
  77. LOG_W("The watchdog timeout argument range from 0 to 7!");
  78. tmp = WDTCON;
  79. break;
  80. }
  81. WDTCON = tmp;
  82. LOG_D("WDTCON=%X", WDTCON);
  83. break;
  84. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  85. switch ((WDTCON >> 20) & 0x7)
  86. {
  87. case AB32_WDT_TIMEOUT_1MS:
  88. LOG_D("The watchdog timeout is set to 1ms");
  89. break;
  90. case AB32_WDT_TIMEOUT_256MS:
  91. LOG_D("The watchdog timeout is set to 256ms");
  92. break;
  93. case AB32_WDT_TIMEOUT_512MS:
  94. LOG_D("The watchdog timeout is set to 512ms");
  95. break;
  96. case AB32_WDT_TIMEOUT_1024MS:
  97. LOG_D("The watchdog timeout is set to 1024ms");
  98. break;
  99. case AB32_WDT_TIMEOUT_2048MS:
  100. LOG_D("The watchdog timeout is set to 2048ms");
  101. break;
  102. case AB32_WDT_TIMEOUT_4096MS:
  103. LOG_D("The watchdog timeout is set to 4096ms");
  104. break;
  105. case AB32_WDT_TIMEOUT_8192MS:
  106. LOG_D("The watchdog timeout is set to 8192ms");
  107. break;
  108. case AB32_WDT_TIMEOUT_16384MS:
  109. LOG_D("The watchdog timeout is set to 16384ms");
  110. break;
  111. default:
  112. break;
  113. }
  114. break;
  115. case RT_DEVICE_CTRL_WDT_START:
  116. WDTCON = 0x110;
  117. break;
  118. default:
  119. LOG_W("This command is not supported.");
  120. return -RT_ERROR;
  121. }
  122. return RT_EOK;
  123. }
  124. int rt_wdt_init(void)
  125. {
  126. ops.init = &wdt_init;
  127. ops.control = &wdt_control;
  128. ab32_wdt.watchdog.ops = &ops;
  129. /* register watchdog device */
  130. if (rt_hw_watchdog_register(&ab32_wdt.watchdog, "wdt", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
  131. {
  132. LOG_E("wdt device register failed.");
  133. return -RT_ERROR;
  134. }
  135. LOG_D("wdt device register success.");
  136. return RT_EOK;
  137. }
  138. INIT_BOARD_EXPORT(rt_wdt_init);
  139. #endif /* RT_USING_WDT */