drv_wdt.c 4.1 KB

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