drv_wwdg.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-18 thread-liu the first version
  9. */
  10. #include <board.h>
  11. #if defined(BSP_USING_WWDG)
  12. #include "drv_config.h"
  13. #include <string.h>
  14. #include <stdlib.h>
  15. //#define DRV_DEBUG
  16. #define LOG_TAG "drv.wwg"
  17. #include <drv_log.h>
  18. #define LED5_PIN GET_PIN(A, 14)
  19. static rt_uint8_t feed_flag = 0;
  20. static WWDG_HandleTypeDef hwwdg1;
  21. void WWDG1_IRQHandler(void)
  22. {
  23. /* enter interrupt */
  24. rt_interrupt_enter();
  25. HAL_WWDG_IRQHandler(&hwwdg1);
  26. /* leave interrupt */
  27. rt_interrupt_leave();
  28. }
  29. void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef* hwwdg)
  30. {
  31. if(hwwdg->Instance==WWDG1)
  32. {
  33. if (feed_flag)
  34. {
  35. HAL_WWDG_Refresh(&hwwdg1);
  36. HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_14);
  37. }
  38. }
  39. }
  40. static void wwdg_init()
  41. {
  42. rt_pin_mode(LED5_PIN, PIN_MODE_OUTPUT);
  43. hwwdg1.Instance = WWDG1;
  44. hwwdg1.Init.Prescaler = WWDG_PRESCALER_8;
  45. hwwdg1.Init.Window = 0X5F;
  46. hwwdg1.Init.Counter = 0x7F;
  47. hwwdg1.Init.EWIMode = WWDG_EWI_ENABLE;
  48. if (HAL_WWDG_Init(&hwwdg1) != HAL_OK)
  49. {
  50. Error_Handler();
  51. }
  52. feed_flag = 1;
  53. }
  54. static void wwdg_control(uint8_t pre_value)
  55. {
  56. if(pre_value > 7)
  57. {
  58. pre_value = 7;
  59. }
  60. hwwdg1.Instance->CFR &= ~(7 << 11); /* clear WDGTB[2:0] */
  61. hwwdg1.Instance->CFR |= pre_value << 11; /* set WDGTB[2:0] */
  62. }
  63. static void wwdg_stop(void)
  64. {
  65. feed_flag = 0;
  66. }
  67. static int wwdg_sample(int argc, char *argv[])
  68. {
  69. if (argc > 1)
  70. {
  71. if (!strcmp(argv[1], "run"))
  72. {
  73. wwdg_init();
  74. }
  75. else if (!strcmp(argv[1], "set"))
  76. {
  77. if (argc > 2)
  78. {
  79. wwdg_control(atoi(argv[2]));
  80. }
  81. }
  82. else if (!strcmp(argv[1], "stop"))
  83. {
  84. wwdg_stop();
  85. }
  86. }
  87. else
  88. {
  89. rt_kprintf("Usage:\n");
  90. rt_kprintf("wwdg_sample run - open wwdg, when feed wwdg in wwdg irq, the LD5 will blink\n");
  91. rt_kprintf("wwdg_sample stop - stop to feed wwdg, system will reset\n");
  92. rt_kprintf("wwdg_sample set - set the wwdg prescaler, wwdg_sample set [0 - 7]\n");
  93. }
  94. return RT_EOK;
  95. }
  96. MSH_CMD_EXPORT(wwdg_sample, window watch dog sample);
  97. #endif