drv_iwg.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * File : drv_iwg.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, 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-11-08 ZYH the first version
  13. */
  14. #include <rtthread.h>
  15. #include "board.h"
  16. #include <rtdevice.h>
  17. #include <rthw.h>
  18. #ifdef RT_USING_WDT
  19. IWDG_HandleTypeDef hiwdg;
  20. static rt_err_t drv_init(rt_watchdog_t *wdt)
  21. {
  22. hiwdg.Instance = IWDG;
  23. hiwdg.Init.Prescaler = IWDG_PRESCALER_16;//1s
  24. hiwdg.Init.Reload = 4095;
  25. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  26. {
  27. while (1)
  28. {
  29. }
  30. }
  31. return RT_EOK;
  32. }
  33. static rt_err_t drv_control(rt_watchdog_t *wdt, int cmd, void *arg)
  34. {
  35. switch (cmd)
  36. {
  37. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  38. hiwdg.Init.Reload = (rt_uint32_t)arg;
  39. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  40. {
  41. return RT_ERROR;
  42. }
  43. break;
  44. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  45. HAL_IWDG_Refresh(&hiwdg);
  46. break;
  47. default:
  48. return RT_ERROR;
  49. }
  50. return RT_EOK;
  51. }
  52. static struct rt_watchdog_ops _ops =
  53. {
  54. drv_init,
  55. drv_control
  56. };
  57. static rt_watchdog_t _iwg =
  58. {
  59. .ops = &_ops
  60. };
  61. int rt_iwg_init(void)
  62. {
  63. return rt_hw_watchdog_register(&_iwg, "iwg", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL);
  64. }
  65. INIT_BOARD_EXPORT(rt_iwg_init);
  66. #endif