stm32f1_wdg.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * File : stm32f1_wdg.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-2013, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-01-18 aubrcool@qq.com 1st version
  13. */
  14. #include "stm32f10x.h"
  15. #include <rtdevice.h>
  16. #ifdef RT_USING_WDT
  17. static rt_err_t stm32f1_wdg_init(rt_watchdog_t *wdt)
  18. {
  19. return RT_EOK;
  20. }
  21. static rt_err_t stm32f1_wdg_control(rt_watchdog_t *wdt, int cmd, void *arg)
  22. {
  23. rt_uint32_t timeout_ms = 0;
  24. rt_uint32_t timeout_pow = 1;
  25. switch(cmd)
  26. {
  27. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  28. timeout_ms = *((rt_uint32_t*) arg);
  29. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  30. if(timeout_ms >= 13107)
  31. {
  32. if(timeout_ms >= 26214)
  33. {
  34. timeout_ms = 26214;
  35. }
  36. IWDG_SetPrescaler(IWDG_Prescaler_256);
  37. timeout_pow = 256;
  38. }
  39. else if(timeout_ms >= 6553)
  40. {
  41. IWDG_SetPrescaler(IWDG_Prescaler_128);
  42. timeout_pow = 128;
  43. }
  44. else if(timeout_ms >= 3276)
  45. {
  46. IWDG_SetPrescaler(IWDG_Prescaler_64);
  47. timeout_pow = 64;
  48. }
  49. else if(timeout_ms >= 1638)
  50. {
  51. IWDG_SetPrescaler(IWDG_Prescaler_32);
  52. timeout_pow = 32;
  53. }
  54. else if(timeout_ms >= 819)
  55. {
  56. IWDG_SetPrescaler(IWDG_Prescaler_16);
  57. timeout_pow = 16;
  58. }
  59. else if(timeout_ms >= 409)
  60. {
  61. IWDG_SetPrescaler(IWDG_Prescaler_8);
  62. timeout_pow = 8;
  63. }
  64. else
  65. {
  66. IWDG_SetPrescaler(IWDG_Prescaler_4);
  67. timeout_pow = 4;
  68. }
  69. timeout_ms = timeout_ms * 40 / timeout_pow;
  70. if(timeout_ms > 0xFFF)
  71. {
  72. timeout_ms = 0xFFF;
  73. }
  74. IWDG_SetReload(timeout_ms);
  75. IWDG_ReloadCounter();
  76. break;
  77. case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
  78. timeout_pow = IWDG->PR;
  79. if(timeout_pow > 6)
  80. {
  81. timeout_pow = 6;
  82. }
  83. timeout_pow = 1 << (2 + timeout_pow);
  84. timeout_ms = IWDG->RLR;
  85. timeout_ms &= 0xFFF;
  86. *((rt_uint32_t *) arg) = timeout_ms * timeout_pow / 40;
  87. break;
  88. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  89. IWDG_ReloadCounter();
  90. break;
  91. case RT_DEVICE_CTRL_WDT_START:
  92. IWDG_Enable();
  93. break;
  94. default:
  95. return RT_EIO;
  96. }
  97. return RT_EOK;
  98. }
  99. static const struct rt_watchdog_ops stm32f1_wdg_pos =
  100. {
  101. stm32f1_wdg_init,
  102. stm32f1_wdg_control,
  103. };
  104. static rt_watchdog_t stm32f1_wdg;
  105. int rt_hw_wdg_init(void)
  106. {
  107. stm32f1_wdg.ops = &stm32f1_wdg_pos;
  108. rt_hw_watchdog_register(&stm32f1_wdg, "wdg", 0, RT_NULL);
  109. return RT_EOK;
  110. }
  111. INIT_BOARD_EXPORT(rt_hw_wdg_init);
  112. #endif /*RT_USING_WDT*/