drv_wdt.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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-10-19 Nations first version
  9. */
  10. #include <rthw.h>
  11. #include <rtthread.h>
  12. #include <rtdbg.h>
  13. #include "rtdevice.h"
  14. #include "board.h"
  15. #ifdef RT_USING_WDT
  16. __IO uint32_t LsiFreq = 40000;
  17. static rt_err_t n32_wdt_close(rt_watchdog_t *wdt)
  18. {
  19. rt_uint32_t level;
  20. level = rt_hw_interrupt_disable();
  21. /* Disable the LSI OSC */
  22. RCC_EnableLsi(DISABLE);
  23. rt_hw_interrupt_enable(level);
  24. return RT_EOK;
  25. }
  26. static rt_err_t n32_wdt_open(rt_watchdog_t *wdt, rt_uint16_t oflag)
  27. {
  28. rt_uint32_t level;
  29. level = rt_hw_interrupt_disable();
  30. /* Enable the LSI OSC */
  31. RCC_EnableLsi(ENABLE);
  32. /* Wait till LSI is ready */
  33. #if defined(SOC_N32G45X) || defined(SOC_N32WB452) || defined(SOC_N32G4FR)
  34. while (RCC_GetFlagStatus(RCC_FLAG_LSIRD) == RESET)
  35. {
  36. }
  37. #elif defined(SOC_N32L43X) || defined(SOC_N32L40X) || defined(SOC_N32G43X)
  38. while (RCC_GetFlagStatus(RCC_CTRLSTS_FLAG_LSIRD) == RESET)
  39. {
  40. }
  41. #endif
  42. /* Reload IWDG counter */
  43. IWDG_ReloadKey();
  44. /* Enable IWDG (the LSI oscillator will be enabled by hardware) */
  45. IWDG_Enable();
  46. rt_hw_interrupt_enable(level);
  47. return RT_EOK;
  48. }
  49. static rt_err_t n32_wdt_init(rt_watchdog_t *wdt)
  50. {
  51. return RT_EOK;
  52. }
  53. static rt_err_t n32_wdt_refresh(rt_watchdog_t *wdt)
  54. {
  55. rt_uint32_t level;
  56. level = rt_hw_interrupt_disable();
  57. /* Reload IWDG counter */
  58. IWDG_ReloadKey();
  59. rt_hw_interrupt_enable(level);
  60. return RT_EOK;
  61. }
  62. /**
  63. * @function control wdt
  64. *
  65. * @param
  66. * wdt whick wdt used
  67. * cmd control wdt options
  68. * args argument of conrtol
  69. * @retval rt_err_t the status of control result
  70. *
  71. *
  72. */
  73. static rt_err_t n32_wdt_control(rt_watchdog_t *wdt, int cmd, void *args)
  74. {
  75. RT_ASSERT(wdt != NULL);
  76. uint16_t reload_value;
  77. uint32_t relv, prediv;
  78. static rt_tick_t last_tick = 0;
  79. relv = IWDG->RELV;
  80. prediv = IWDG->PREDIV;
  81. switch (cmd)
  82. {
  83. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  84. {
  85. *(uint16_t *)args = ((relv * LsiFreq) / prediv);
  86. }
  87. break;
  88. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  89. {
  90. RT_ASSERT(*(uint16_t *)args != 0);
  91. reload_value = *(uint16_t *)args;
  92. if (reload_value > 0xFFF * 32 *1000 / LsiFreq)
  93. {
  94. LOG_W("wdg set timeout parameter too large, please less than %d ms\n", 0xFFF * 32 *1000 / LsiFreq);
  95. return -RT_EINVAL;
  96. }
  97. /* Enable write access to IWDG_PR and IWDG_RLR registers */
  98. IWDG_WriteConfig(IWDG_WRITE_ENABLE);
  99. /* IWDG counter clock: LSI/32 */
  100. IWDG_SetPrescalerDiv(IWDG_PRESCALER_DIV32);
  101. reload_value = ((uint32_t)reload_value * LsiFreq / 1000) / 32;
  102. IWDG_CntReload(reload_value);
  103. IWDG_WriteConfig(IWDG_WRITE_DISABLE);
  104. }
  105. break;
  106. case RT_DEVICE_CTRL_WDT_GET_TIMELEFT:
  107. *(uint16_t *)args = ((relv * LsiFreq) / prediv) - \
  108. (rt_tick_get() - last_tick) / RT_TICK_PER_SECOND;
  109. break;
  110. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  111. {
  112. last_tick = rt_tick_get();
  113. n32_wdt_refresh(wdt);
  114. }
  115. break;
  116. case RT_DEVICE_CTRL_WDT_START:
  117. {
  118. n32_wdt_open(wdt, *(rt_uint32_t *)args);
  119. last_tick = rt_tick_get();
  120. }
  121. break;
  122. case RT_DEVICE_CTRL_WDT_STOP:
  123. {
  124. n32_wdt_close(wdt);
  125. }
  126. break;
  127. default:
  128. return RT_EINVAL;
  129. }
  130. return RT_EOK;
  131. }
  132. static struct rt_watchdog_ops n32_wdt_ops =
  133. {
  134. .init = n32_wdt_init,
  135. .control = n32_wdt_control,
  136. };
  137. static struct rt_watchdog_device n32_wdt_device;
  138. int rt_hw_wdt_init(void)
  139. {
  140. int result = RT_EOK;
  141. /* Disable the LSI OSC */
  142. RCC_EnableLsi(DISABLE);
  143. n32_wdt_device.ops = &n32_wdt_ops;
  144. /* register watchdog device */
  145. result = rt_hw_watchdog_register(&n32_wdt_device, "wdt", RT_DEVICE_FLAG_RDWR, (void *)IWDG);
  146. return result;
  147. }
  148. INIT_DEVICE_EXPORT(rt_hw_wdt_init);
  149. #endif /* BSP_USING_WDT */