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