drv_wdt.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-03-04 stevetong459 first version
  9. */
  10. #include <board.h>
  11. #include <sys/time.h>
  12. #ifdef RT_USING_WDT
  13. #define DBG_TAG "drv.wdt"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #ifndef LSI_VALUE
  17. #define LSI_VALUE ((uint32_t)40000)
  18. #endif
  19. #define DRV_WDT_TIME_OUT 0xFFFF
  20. typedef struct
  21. {
  22. struct rt_watchdog_device wdt;
  23. rt_uint32_t min_threshold;
  24. rt_uint32_t max_threshold;
  25. rt_uint32_t current_threshold;
  26. } apm32_wdt_t;
  27. static apm32_wdt_t wdt_config;
  28. static rt_err_t _iwdt_init(rt_watchdog_t *wdt)
  29. {
  30. rt_uint32_t counter = 0;
  31. RCM_EnableLSI();
  32. while (!RCM_ReadStatusFlag(RCM_FLAG_LSIRDY))
  33. {
  34. if (++counter > DRV_WDT_TIME_OUT)
  35. {
  36. LOG_E("LSI clock open failed.");
  37. return -RT_ERROR;
  38. }
  39. }
  40. wdt_config.min_threshold = 1;
  41. wdt_config.max_threshold = (0xfff << 8) / LSI_VALUE;
  42. LOG_I("threshold section [%u, %d]", \
  43. wdt_config.min_threshold,
  44. wdt_config.max_threshold);
  45. while (IWDT_ReadStatusFlag(IWDT_FLAG_PSCU))
  46. {
  47. if (++counter > DRV_WDT_TIME_OUT)
  48. {
  49. LOG_E("watchdog prescaler init failed.");
  50. return -RT_ERROR;
  51. }
  52. }
  53. IWDT_EnableWriteAccess();
  54. IWDT_ConfigDivider(IWDT_DIVIDER_256);
  55. IWDT_DisableWriteAccess();
  56. return RT_EOK;
  57. }
  58. /**
  59. * @brief This function will control watchdog device.
  60. *
  61. * @param wdt is a pointer to i2c config class.
  62. *
  63. * @return RT_EOK indicates successful , other value indicates failed.
  64. */
  65. static rt_err_t _iwdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
  66. {
  67. volatile rt_uint32_t param, counter = 0;
  68. switch (cmd)
  69. {
  70. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  71. IWDT_Refresh();
  72. break;
  73. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  74. param = *(rt_uint32_t *) arg;
  75. if ((param > wdt_config.max_threshold) || \
  76. (param < wdt_config.min_threshold))
  77. {
  78. LOG_E("invalid param@%u.", param);
  79. return -RT_ERROR;
  80. }
  81. else
  82. {
  83. wdt_config.current_threshold = param;
  84. }
  85. while (IWDT_ReadStatusFlag(IWDT_FLAG_CNTU))
  86. {
  87. if (++counter > DRV_WDT_TIME_OUT)
  88. {
  89. LOG_E("Update watchdog reload value complete.");
  90. return -RT_ERROR;
  91. }
  92. }
  93. IWDT_Refresh();
  94. IWDT_EnableWriteAccess();
  95. IWDT_ConfigReload(param * LSI_VALUE >> 8);
  96. IWDT_DisableWriteAccess();
  97. break;
  98. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  99. *(rt_uint32_t *)arg = wdt_config.current_threshold;
  100. break;
  101. case RT_DEVICE_CTRL_WDT_START:
  102. IWDT_Enable();
  103. IWDT_Refresh();
  104. break;
  105. default:
  106. LOG_W("This command is not supported.");
  107. return -RT_ERROR;
  108. }
  109. return RT_EOK;
  110. }
  111. static struct rt_watchdog_ops _wdt_ops =
  112. {
  113. _iwdt_init,
  114. _iwdt_control,
  115. };
  116. static int rt_hw_wdt_init(void)
  117. {
  118. wdt_config.wdt.ops = &_wdt_ops;
  119. /* register watchdog device */
  120. if (rt_hw_watchdog_register(&wdt_config.wdt, "wdt", \
  121. RT_DEVICE_FLAG_DEACTIVATE, RT_NULL) != RT_EOK)
  122. {
  123. LOG_E("wdt device register failed.");
  124. return -RT_ERROR;
  125. }
  126. LOG_D("wdt device register success.");
  127. return RT_EOK;
  128. }
  129. INIT_BOARD_EXPORT(rt_hw_wdt_init);
  130. #endif