1 /*!
2     \file    gd32e10x_fwdgt.c
3     \brief   FWDGT driver
4 
5     \version 2017-12-26, V1.0.0, firmware for GD32E10x
6     \version 2020-09-30, V1.1.0, firmware for GD32E10x
7     \version 2020-12-31, V1.2.0, firmware for GD32E10x
8     \version 2022-06-30, V1.3.0, firmware for GD32E10x
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 "gd32e10x_fwdgt.h"
39 
40 /*!
41     \brief      enable write access to FWDGT_PSC and FWDGT_RLD
42     \param[in]  none
43     \param[out] none
44     \retval     none
45 */
fwdgt_write_enable(void)46 void fwdgt_write_enable(void)
47 {
48     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
49 }
50 
51 /*!
52     \brief      disable write access to FWDGT_PSC and FWDGT_RLD
53     \param[in]  none
54     \param[out] none
55     \retval     none
56 */
fwdgt_write_disable(void)57 void fwdgt_write_disable(void)
58 {
59     FWDGT_CTL = FWDGT_WRITEACCESS_DISABLE;
60 }
61 
62 /*!
63     \brief      start the free watchdog timer counter
64     \param[in]  none
65     \param[out] none
66     \retval     none
67 */
fwdgt_enable(void)68 void fwdgt_enable(void)
69 {
70     FWDGT_CTL = FWDGT_KEY_ENABLE;
71 }
72 
73 /*!
74     \brief      configure the free watchdog timer counter prescaler value
75     \param[in]  prescaler_value: specify prescaler value
76                 only one parameter can be selected which is shown as below:
77       \arg        FWDGT_PSC_DIV4: FWDGT prescaler set to 4
78       \arg        FWDGT_PSC_DIV8: FWDGT prescaler set to 8
79       \arg        FWDGT_PSC_DIV16: FWDGT prescaler set to 16
80       \arg        FWDGT_PSC_DIV32: FWDGT prescaler set to 32
81       \arg        FWDGT_PSC_DIV64: FWDGT prescaler set to 64
82       \arg        FWDGT_PSC_DIV128: FWDGT prescaler set to 128
83       \arg        FWDGT_PSC_DIV256: FWDGT prescaler set to 256
84     \param[out] none
85     \retval     ErrStatus: ERROR or SUCCESS
86 */
fwdgt_prescaler_value_config(uint16_t prescaler_value)87 ErrStatus fwdgt_prescaler_value_config(uint16_t prescaler_value)
88 {
89     uint32_t timeout = FWDGT_PSC_TIMEOUT;
90     uint32_t flag_status = RESET;
91 
92     /* enable write access to FWDGT_PSC */
93     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
94 
95     /* wait until the PUD flag to be reset */
96     do {
97         flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
98     } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
99 
100     if((uint32_t)RESET != flag_status) {
101         return ERROR;
102     }
103 
104     /* configure FWDGT */
105     FWDGT_PSC = (uint32_t)prescaler_value;
106 
107     return SUCCESS;
108 }
109 
110 /*!
111     \brief      configure the free watchdog timer counter reload value
112     \param[in]  reload_value: specify reload value(0x0000 - 0x0FFF)
113     \param[out] none
114     \retval     ErrStatus: ERROR or SUCCESS
115 */
fwdgt_reload_value_config(uint16_t reload_value)116 ErrStatus fwdgt_reload_value_config(uint16_t reload_value)
117 {
118     uint32_t timeout = FWDGT_RLD_TIMEOUT;
119     uint32_t flag_status = RESET;
120 
121     /* enable write access to FWDGT_RLD */
122     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
123 
124     /* wait until the RUD flag to be reset */
125     do {
126         flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
127     } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
128 
129     if((uint32_t)RESET != flag_status) {
130         return ERROR;
131     }
132 
133     FWDGT_RLD = RLD_RLD(reload_value);
134 
135     return SUCCESS;
136 }
137 
138 /*!
139     \brief      reload the counter of FWDGT
140     \param[in]  none
141     \param[out] none
142     \retval     none
143 */
fwdgt_counter_reload(void)144 void fwdgt_counter_reload(void)
145 {
146     FWDGT_CTL = FWDGT_KEY_RELOAD;
147 }
148 
149 /*!
150     \brief      configure counter reload value, and prescaler divider value
151     \param[in]  reload_value: specify reload value(0x0000 - 0x0FFF)
152     \param[in]  prescaler_div: FWDGT prescaler value
153                 only one parameter can be selected which is shown as below:
154       \arg        FWDGT_PSC_DIV4: FWDGT prescaler set to 4
155       \arg        FWDGT_PSC_DIV8: FWDGT prescaler set to 8
156       \arg        FWDGT_PSC_DIV16: FWDGT prescaler set to 16
157       \arg        FWDGT_PSC_DIV32: FWDGT prescaler set to 32
158       \arg        FWDGT_PSC_DIV64: FWDGT prescaler set to 64
159       \arg        FWDGT_PSC_DIV128: FWDGT prescaler set to 128
160       \arg        FWDGT_PSC_DIV256: FWDGT prescaler set to 256
161     \param[out] none
162     \retval     ErrStatus: ERROR or SUCCESS
163 */
fwdgt_config(uint16_t reload_value,uint8_t prescaler_div)164 ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
165 {
166     uint32_t timeout = FWDGT_PSC_TIMEOUT;
167     uint32_t flag_status = RESET;
168 
169     /* enable write access to FWDGT_PSC,and FWDGT_RLD */
170     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
171 
172     /* wait until the PUD flag to be reset */
173     do {
174         flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
175     } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
176 
177     if((uint32_t)RESET != flag_status) {
178         return ERROR;
179     }
180 
181     /* configure FWDGT */
182     FWDGT_PSC = (uint32_t)prescaler_div;
183 
184     timeout = FWDGT_RLD_TIMEOUT;
185     /* wait until the RUD flag to be reset */
186     do {
187         flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
188     } while((--timeout > 0U) && ((uint32_t)RESET != flag_status));
189 
190     if((uint32_t)RESET != flag_status) {
191         return ERROR;
192     }
193 
194     FWDGT_RLD = RLD_RLD(reload_value);
195 
196     /* reload the counter */
197     FWDGT_CTL = FWDGT_KEY_RELOAD;
198 
199     return SUCCESS;
200 }
201 
202 /*!
203     \brief      get flag state of FWDGT
204     \param[in]  flag: flag to get
205                 only one parameter can be selected which is shown as below:
206       \arg        FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
207       \arg        FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
208     \param[out] none
209     \retval     FlagStatus: SET or RESET
210 */
fwdgt_flag_get(uint16_t flag)211 FlagStatus fwdgt_flag_get(uint16_t flag)
212 {
213     if(FWDGT_STAT & flag) {
214         return SET;
215     }
216 
217     return RESET;
218 }
219