1 /*!
2     \file    gd32f3x0_wwdgt.c
3     \brief   WWDGT driver
4 
5     \version 2017-06-06, V1.0.0, firmware for GD32F3x0
6     \version 2019-06-01, V2.0.0, firmware for GD32F3x0
7     \version 2020-09-30, V2.1.0, firmware for GD32F3x0
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 #include "gd32f3x0_wwdgt.h"
38 #include "gd32f3x0_rcu.h"
39 
40 /*!
41     \brief      reset the window watchdog timer configuration
42     \param[in]  none
43     \param[out] none
44     \retval     none
45 */
wwdgt_deinit(void)46 void wwdgt_deinit(void)
47 {
48     rcu_periph_reset_enable(RCU_WWDGTRST);
49     rcu_periph_reset_disable(RCU_WWDGTRST);
50 }
51 
52 /*!
53     \brief      start the window watchdog timer counter
54     \param[in]  none
55     \param[out] none
56     \retval     none
57 */
wwdgt_enable(void)58 void wwdgt_enable(void)
59 {
60     WWDGT_CTL |= WWDGT_CTL_WDGTEN;
61 }
62 
63 /*!
64     \brief      configure the window watchdog timer counter value
65     \param[in]  counter_value: 0x00 - 0x7F
66     \param[out] none
67     \retval     none
68 */
wwdgt_counter_update(uint16_t counter_value)69 void wwdgt_counter_update(uint16_t counter_value)
70 {
71     uint32_t reg = 0x0U;
72 
73     reg = WWDGT_CTL &(~(uint32_t)WWDGT_CTL_CNT);
74     reg |= (uint32_t)(CTL_CNT(counter_value));
75 
76     WWDGT_CTL = (uint32_t)reg;
77 }
78 
79 /*!
80     \brief      configure counter value, window value, and prescaler divider value
81     \param[in]  counter: 0x00 - 0x7F
82     \param[in]  window: 0x00 - 0x7F
83     \param[in]  prescaler: wwdgt prescaler value
84                 only one parameter can be selected which is shown as below:
85       \arg        WWDGT_CFG_PSC_DIV1: the time base of window watchdog counter = (PCLK1/4096)/1
86       \arg        WWDGT_CFG_PSC_DIV2: the time base of window watchdog counter = (PCLK1/4096)/2
87       \arg        WWDGT_CFG_PSC_DIV4: the time base of window watchdog counter = (PCLK1/4096)/4
88       \arg        WWDGT_CFG_PSC_DIV8: the time base of window watchdog counter = (PCLK1/4096)/8
89     \param[out] none
90     \retval     none
91 */
wwdgt_config(uint16_t counter,uint16_t window,uint32_t prescaler)92 void wwdgt_config(uint16_t counter, uint16_t window, uint32_t prescaler)
93 {
94     uint32_t reg_cfg = 0x0U, reg_ctl = 0x0U;
95 
96     /* clear WIN and PSC bits, clear CNT bit */
97     reg_cfg = WWDGT_CFG &(~((uint32_t)WWDGT_CFG_WIN|(uint32_t)WWDGT_CFG_PSC));
98     reg_ctl = WWDGT_CTL &(~(uint32_t)WWDGT_CTL_CNT);
99 
100     /* configure WIN and PSC bits, configure CNT bit */
101     reg_cfg |= (uint32_t)(CFG_WIN(window));
102     reg_cfg |= (uint32_t)(prescaler);
103     reg_ctl |= (uint32_t)(CTL_CNT(counter));
104 
105     WWDGT_CFG = (uint32_t)reg_cfg;
106     WWDGT_CTL = (uint32_t)reg_ctl;
107 }
108 
109 /*!
110     \brief      enable early wakeup interrupt of WWDGT
111     \param[in]  none
112     \param[out] none
113     \retval     none
114 */
wwdgt_interrupt_enable(void)115 void wwdgt_interrupt_enable(void)
116 {
117     WWDGT_CFG |= WWDGT_CFG_EWIE;
118 }
119 
120 /*!
121     \brief      check early wakeup interrupt state of WWDGT
122     \param[in]  none
123     \param[out] none
124     \retval     FlagStatus: SET or RESET
125 */
wwdgt_flag_get(void)126 FlagStatus wwdgt_flag_get(void)
127 {
128   if(WWDGT_STAT & WWDGT_STAT_EWIF){
129         return SET;
130   }
131     return RESET;
132 }
133 
134 /*!
135     \brief      clear early wakeup interrupt state of WWDGT
136     \param[in]  none
137     \param[out] none
138     \retval     none
139 */
wwdgt_flag_clear(void)140 void wwdgt_flag_clear(void)
141 {
142     WWDGT_STAT &= (uint32_t)(~(uint32_t)WWDGT_STAT_EWIF);
143 }
144