1 /*!
2     \file    gd32e50x_wwdgt.c
3     \brief   WWDGT driver
4 
5     \version 2020-03-10, V1.0.0, firmware for GD32E50x
6     \version 2020-08-26, V1.1.0, firmware for GD32E50x
7     \version 2021-03-23, V1.2.0, firmware for GD32E50x
8     \version 2021-10-08, V1.2.1, firmware for GD32E50x
9 */
10 
11 /*
12     Copyright (c) 2021, 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 "gd32e50x_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 WWDGT 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 WWDGT 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 WWDGT 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     WWDGT_CTL = (uint32_t)(CTL_CNT(counter_value));
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     WWDGT_CTL = (uint32_t)(CTL_CNT(counter));
95     WWDGT_CFG = (uint32_t)(CFG_WIN(window) | prescaler);
96 }
97 
98 /*!
99     \brief      enable early wakeup interrupt of WWDGT
100     \param[in]  none
101     \param[out] none
102     \retval     none
103 */
wwdgt_interrupt_enable(void)104 void wwdgt_interrupt_enable(void)
105 {
106     WWDGT_CFG |= WWDGT_CFG_EWIE;
107 }
108 
109 /*!
110     \brief      check early wakeup interrupt state of WWDGT
111     \param[in]  none
112     \param[out] none
113     \retval     FlagStatus: SET or RESET
114 */
wwdgt_flag_get(void)115 FlagStatus wwdgt_flag_get(void)
116 {
117     if(RESET != (WWDGT_STAT & WWDGT_STAT_EWIF)){
118         return SET;
119     }
120 
121     return RESET;
122 }
123 
124 /*!
125     \brief      clear early wakeup interrupt state of WWDGT
126     \param[in]  none
127     \param[out] none
128     \retval     none
129 */
wwdgt_flag_clear(void)130 void wwdgt_flag_clear(void)
131 {
132     WWDGT_STAT = (uint32_t)(RESET);
133 }
134