1 /*!
2 \file gd32f4xx_wwdgt.c
3 \brief WWDGT driver
4
5 \version 2016-08-15, V1.0.0, firmware for GD32F4xx
6 \version 2018-12-12, V2.0.0, firmware for GD32F4xx
7 \version 2020-09-30, V2.1.0, firmware for GD32F4xx
8 \version 2022-03-09, V3.0.0, firmware for GD32F4xx
9 */
10
11 /*
12 Copyright (c) 2022, GigaDevice Semiconductor Inc.
13
14 Redistribution and use in source and binary forms, with or without modification,
15 are permitted provided that the following conditions are met:
16
17 1. Redistributions of source code must retain the above copyright notice, this
18 list of conditions and the following disclaimer.
19 2. Redistributions in binary form must reproduce the above copyright notice,
20 this list of conditions and the following disclaimer in the documentation
21 and/or other materials provided with the distribution.
22 3. Neither the name of the copyright holder nor the names of its contributors
23 may be used to endorse or promote products derived from this software without
24 specific prior written permission.
25
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
35 OF SUCH DAMAGE.
36 */
37
38 #include "gd32f4xx_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 check early wakeup interrupt state of WWDGT
116 \param[in] none
117 \param[out] none
118 \retval FlagStatus: SET or RESET
119 */
wwdgt_flag_get(void)120 FlagStatus wwdgt_flag_get(void)
121 {
122 if(RESET != (WWDGT_STAT & WWDGT_STAT_EWIF)){
123 return SET;
124 }
125
126 return RESET;
127 }
128
129 /*!
130 \brief clear early wakeup interrupt state of WWDGT
131 \param[in] none
132 \param[out] none
133 \retval none
134 */
wwdgt_flag_clear(void)135 void wwdgt_flag_clear(void)
136 {
137 WWDGT_STAT &= (~WWDGT_STAT_EWIF);
138 }
139
140 /*!
141 \brief enable early wakeup interrupt of WWDGT
142 \param[in] none
143 \param[out] none
144 \retval none
145 */
wwdgt_interrupt_enable(void)146 void wwdgt_interrupt_enable(void)
147 {
148 WWDGT_CFG |= WWDGT_CFG_EWIE;
149 }
150