1 /*!
2 \file gd32vf103_wwdgt.c
3 \brief WWDGT driver
4
5 \version 2019-06-05, V1.0.0, firmware for GD32VF103
6 \version 2020-08-04, V1.1.0, firmware for GD32VF103
7 */
8
9 /*
10 Copyright (c) 2020, GigaDevice Semiconductor Inc.
11
12 Redistribution and use in source and binary forms, with or without modification,
13 are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this
16 list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holder nor the names of its contributors
21 may be used to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGE.
34 */
35
36 #include "gd32vf103_wwdgt.h"
37
38 /* write value to WWDGT_CTL_CNT bit field */
39 #define CTL_CNT(regval) (BITS(0,6) & ((uint32_t)(regval) << 0))
40 /* write value to WWDGT_CFG_WIN bit field */
41 #define CFG_WIN(regval) (BITS(0,6) & ((uint32_t)(regval) << 0))
42
43 /*!
44 \brief reset the window watchdog timer configuration
45 \param[in] none
46 \param[out] none
47 \retval none
48 */
wwdgt_deinit(void)49 void wwdgt_deinit(void)
50 {
51 rcu_periph_reset_enable(RCU_WWDGTRST);
52 rcu_periph_reset_disable(RCU_WWDGTRST);
53 }
54
55 /*!
56 \brief start the window watchdog timer counter
57 \param[in] none
58 \param[out] none
59 \retval none
60 */
wwdgt_enable(void)61 void wwdgt_enable(void)
62 {
63 WWDGT_CTL |= WWDGT_CTL_WDGTEN;
64 }
65
66 /*!
67 \brief configure the window watchdog timer counter value
68 \param[in] counter_value: 0x00 - 0x7F
69 \param[out] none
70 \retval none
71 */
wwdgt_counter_update(uint16_t counter_value)72 void wwdgt_counter_update(uint16_t counter_value)
73 {
74 uint32_t reg = 0U;
75
76 reg = (WWDGT_CTL & (~WWDGT_CTL_CNT));
77 reg |= CTL_CNT(counter_value);
78
79 WWDGT_CTL = reg;
80 }
81
82 /*!
83 \brief configure counter value, window value, and prescaler divider value
84 \param[in] counter: 0x00 - 0x7F
85 \param[in] window: 0x00 - 0x7F
86 \param[in] prescaler: wwdgt prescaler value
87 only one parameter can be selected which is shown as below:
88 \arg WWDGT_CFG_PSC_DIV1: the time base of window watchdog counter = (PCLK1/4096)/1
89 \arg WWDGT_CFG_PSC_DIV2: the time base of window watchdog counter = (PCLK1/4096)/2
90 \arg WWDGT_CFG_PSC_DIV4: the time base of window watchdog counter = (PCLK1/4096)/4
91 \arg WWDGT_CFG_PSC_DIV8: the time base of window watchdog counter = (PCLK1/4096)/8
92 \param[out] none
93 \retval none
94 */
wwdgt_config(uint16_t counter,uint16_t window,uint32_t prescaler)95 void wwdgt_config(uint16_t counter, uint16_t window, uint32_t prescaler)
96 {
97 uint32_t reg_cfg = 0U, reg_ctl = 0U;
98
99 /* clear WIN and PSC bits, clear CNT bit */
100 reg_cfg = (WWDGT_CFG &(~(WWDGT_CFG_WIN|WWDGT_CFG_PSC)));
101 reg_ctl = (WWDGT_CTL &(~WWDGT_CTL_CNT));
102
103 /* configure WIN and PSC bits, configure CNT bit */
104 reg_cfg |= CFG_WIN(window);
105 reg_cfg |= prescaler;
106 reg_ctl |= CTL_CNT(counter);
107
108 WWDGT_CTL = reg_ctl;
109 WWDGT_CFG = reg_cfg;
110 }
111
112 /*!
113 \brief enable early wakeup interrupt of WWDGT
114 \param[in] none
115 \param[out] none
116 \retval none
117 */
wwdgt_interrupt_enable(void)118 void wwdgt_interrupt_enable(void)
119 {
120 WWDGT_CFG |= WWDGT_CFG_EWIE;
121 }
122
123 /*!
124 \brief check early wakeup interrupt state of WWDGT
125 \param[in] none
126 \param[out] none
127 \retval FlagStatus: SET or RESET
128 */
wwdgt_flag_get(void)129 FlagStatus wwdgt_flag_get(void)
130 {
131 if(WWDGT_STAT & WWDGT_STAT_EWIF){
132 return SET;
133 }
134
135 return RESET;
136 }
137
138 /*!
139 \brief clear early wakeup interrupt state of WWDGT
140 \param[in] none
141 \param[out] none
142 \retval none
143 */
wwdgt_flag_clear(void)144 void wwdgt_flag_clear(void)
145 {
146 WWDGT_STAT &= (~WWDGT_STAT_EWIF);
147 }
148