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
11 #include <board.h>
12
13 #if defined(BSP_USING_WWDG)
14 #include "drv_config.h"
15 #include <string.h>
16 #include <stdlib.h>
17
18 //#define DRV_DEBUG
19 #define LOG_TAG "drv.wwg"
20 #include <drv_log.h>
21
22 #define LED5_PIN GET_PIN(A, 14)
23
24 static rt_uint8_t feed_flag = 0;
25 static WWDG_HandleTypeDef hwwdg1;
26
WWDG1_IRQHandler(void)27 void WWDG1_IRQHandler(void)
28 {
29 /* enter interrupt */
30 rt_interrupt_enter();
31
32 HAL_WWDG_IRQHandler(&hwwdg1);
33
34 /* leave interrupt */
35 rt_interrupt_leave();
36 }
37
HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef * hwwdg)38 void HAL_WWDG_EarlyWakeupCallback(WWDG_HandleTypeDef* hwwdg)
39 {
40 if(hwwdg->Instance==WWDG1)
41 {
42 if (feed_flag)
43 {
44 HAL_WWDG_Refresh(&hwwdg1);
45 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_14);
46 }
47 }
48 }
49
wwdg_init()50 static void wwdg_init()
51 {
52 rt_pin_mode(LED5_PIN, PIN_MODE_OUTPUT);
53
54 hwwdg1.Instance = WWDG1;
55 hwwdg1.Init.Prescaler = WWDG_PRESCALER_8;
56 hwwdg1.Init.Window = 0X5F;
57 hwwdg1.Init.Counter = 0x7F;
58 hwwdg1.Init.EWIMode = WWDG_EWI_ENABLE;
59
60 if (HAL_WWDG_Init(&hwwdg1) != HAL_OK)
61 {
62 Error_Handler();
63 }
64
65 feed_flag = 1;
66 }
67
wwdg_control(uint8_t pre_value)68 static void wwdg_control(uint8_t pre_value)
69 {
70 if(pre_value > 7)
71 {
72 pre_value = 7;
73 }
74 hwwdg1.Instance->CFR &= ~(7 << 11); /* clear WDGTB[2:0] */
75 hwwdg1.Instance->CFR |= pre_value << 11; /* set WDGTB[2:0] */
76 }
77
wwdg_stop(void)78 static void wwdg_stop(void)
79 {
80 feed_flag = 0;
81 }
82
wwdg_sample(int argc,char * argv[])83 static int wwdg_sample(int argc, char *argv[])
84 {
85 if (argc > 1)
86 {
87 if (!strcmp(argv[1], "run"))
88 {
89 wwdg_init();
90 }
91 else if (!strcmp(argv[1], "set"))
92 {
93 if (argc > 2)
94 {
95 wwdg_control(atoi(argv[2]));
96 }
97 }
98 else if (!strcmp(argv[1], "stop"))
99 {
100 wwdg_stop();
101 }
102 }
103 else
104 {
105 rt_kprintf("Usage:\n");
106 rt_kprintf("wwdg_sample run - open wwdg, when feed wwdg in wwdg irq, the LD5 will blink\n");
107 rt_kprintf("wwdg_sample stop - stop to feed wwdg, system will reset\n");
108 rt_kprintf("wwdg_sample set - set the wwdg prescaler, wwdg_sample set [0 - 7]\n");
109 }
110
111 return RT_EOK;
112 }
113 MSH_CMD_EXPORT(wwdg_sample, window watch dog sample);
114
115 #endif
116