drv_iwg.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <rthw.h>
  15. #include <rtthread.h>
  16. #include <rtdevice.h>
  17. #include <board.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. RT_ASSERT(0);
  28. }
  29. return RT_EOK;
  30. }
  31. static rt_err_t drv_control(rt_watchdog_t *wdt, int cmd, void *arg)
  32. {
  33. switch (cmd)
  34. {
  35. case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
  36. hiwdg.Init.Reload = (rt_uint32_t)arg;
  37. if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
  38. {
  39. return RT_ERROR;
  40. }
  41. break;
  42. case RT_DEVICE_CTRL_WDT_KEEPALIVE:
  43. HAL_IWDG_Refresh(&hiwdg);
  44. break;
  45. default:
  46. return RT_ERROR;
  47. }
  48. return RT_EOK;
  49. }
  50. static struct rt_watchdog_ops _ops =
  51. {
  52. drv_init,
  53. drv_control
  54. };
  55. static rt_watchdog_t _iwg =
  56. {
  57. .ops = &_ops
  58. };
  59. int rt_iwg_init(void)
  60. {
  61. return rt_hw_watchdog_register(&_iwg, "iwg", RT_DEVICE_FLAG_DEACTIVATE, RT_NULL);
  62. }
  63. INIT_BOARD_EXPORT(rt_iwg_init);
  64. #endif