1 /*!
2     \file    gd32fxxx_fwdgt.c
3     \brief   FWDGT driver
4 
5     \version 2022-01-30, V1.0.0, firmware for GD32A50x
6 */
7 
8 /*
9     Copyright (c) 2022, GigaDevice Semiconductor Inc.
10 
11     Redistribution and use in source and binary forms, with or without modification,
12 are permitted provided that the following conditions are met:
13 
14     1. Redistributions of source code must retain the above copyright notice, this
15        list of conditions and the following disclaimer.
16     2. Redistributions in binary form must reproduce the above copyright notice,
17        this list of conditions and the following disclaimer in the documentation
18        and/or other materials provided with the distribution.
19     3. Neither the name of the copyright holder nor the names of its contributors
20        may be used to endorse or promote products derived from this software without
21        specific prior written permission.
22 
23     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 OF SUCH DAMAGE.
33 */
34 
35 #include "gd32a50x_fwdgt.h"
36 
37 /*!
38     \brief      enable write access to FWDGT_PSC, FWDGT_RLD and FWDGT_WND
39     \param[in]  none
40     \param[out] none
41     \retval     none
42 */
fwdgt_write_enable(void)43 void fwdgt_write_enable(void)
44 {
45     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
46 }
47 
48 /*!
49     \brief      disable write access to FWDGT_PSC, FWDGT_RLD and FWDGT_WND
50     \param[in]  none
51     \param[out] none
52     \retval     none
53 */
fwdgt_write_disable(void)54 void fwdgt_write_disable(void)
55 {
56     FWDGT_CTL = FWDGT_WRITEACCESS_DISABLE;
57 }
58 
59 /*!
60     \brief      start the FWDGT counter
61     \param[in]  none
62     \param[out] none
63     \retval     none
64 */
fwdgt_enable(void)65 void fwdgt_enable(void)
66 {
67     FWDGT_CTL = FWDGT_KEY_ENABLE;
68 }
69 
70 /*!
71     \brief      configure the FWDGT counter prescaler value
72     \param[in]  prescaler_value: specify prescaler value
73                 only one parameter can be selected which is shown as below:
74       \arg        FWDGT_PSC_DIV4: FWDGT prescaler set to 4
75       \arg        FWDGT_PSC_DIV8: FWDGT prescaler set to 8
76       \arg        FWDGT_PSC_DIV16: FWDGT prescaler set to 16
77       \arg        FWDGT_PSC_DIV32: FWDGT prescaler set to 32
78       \arg        FWDGT_PSC_DIV64: FWDGT prescaler set to 64
79       \arg        FWDGT_PSC_DIV128: FWDGT prescaler set to 128
80       \arg        FWDGT_PSC_DIV256: FWDGT prescaler set to 256
81     \param[out] none
82     \retval     ErrStatus: ERROR or SUCCESS
83 */
fwdgt_prescaler_value_config(uint16_t prescaler_value)84 ErrStatus fwdgt_prescaler_value_config(uint16_t prescaler_value)
85 {
86     uint32_t timeout = FWDGT_PSC_TIMEOUT;
87     uint32_t flag_status = 0U;
88 
89     /* enable write access to FWDGT_PSC */
90     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
91 
92     /* wait until the PUD flag to be reset */
93     do{
94         flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
95     } while((--timeout > (uint32_t)0) && (0U != flag_status));
96 
97     if(0U != flag_status){
98         return ERROR;
99     }
100 
101     /* configure FWDGT */
102     FWDGT_PSC = (uint32_t)prescaler_value;
103 
104     return SUCCESS;
105 }
106 
107 /*!
108     \brief      configure the FWDGT counter reload value
109     \param[in]  reload_value: specify reload value(0x0000 - 0x0FFF)
110     \param[out] none
111     \retval     ErrStatus: ERROR or SUCCESS
112 */
fwdgt_reload_value_config(uint16_t reload_value)113 ErrStatus fwdgt_reload_value_config(uint16_t reload_value)
114 {
115     uint32_t timeout = FWDGT_RLD_TIMEOUT;
116     uint32_t flag_status = 0U;
117 
118     /* enable write access to FWDGT_RLD */
119     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
120 
121     /* wait until the RUD flag to be reset */
122     do{
123         flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
124     }while((--timeout > 0U) && (0U != flag_status));
125 
126     if (0U != flag_status){
127         return ERROR;
128     }
129 
130     FWDGT_RLD = RLD_RLD(reload_value);
131 
132     return SUCCESS;
133 }
134 
135 /*!
136     \brief      configure the FWDGT counter window value
137     \param[in]  window_value: specify window value(0x0000 - 0x0FFF)
138     \param[out] none
139     \retval     ErrStatus: ERROR or SUCCESS
140 */
fwdgt_window_value_config(uint16_t window_value)141 ErrStatus fwdgt_window_value_config(uint16_t window_value)
142 {
143     uint32_t time_index = FWDGT_WND_TIMEOUT;
144     uint32_t flag_status = 0U;
145 
146     /* enable write access to FWDGT_WND */
147     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
148 
149     /* wait until the WUD flag to be reset */
150     do{
151         flag_status = FWDGT_STAT & FWDGT_STAT_WUD;
152     }while((--time_index > 0U) && (0U != flag_status));
153 
154     if (0U != flag_status){
155         return ERROR;
156     }
157 
158     FWDGT_WND = WND_WND(window_value);
159 
160     return SUCCESS;
161 }
162 
163 /*!
164     \brief      reload the counter of FWDGT
165     \param[in]  none
166     \param[out] none
167     \retval     none
168 */
fwdgt_counter_reload(void)169 void fwdgt_counter_reload(void)
170 {
171     FWDGT_CTL = FWDGT_KEY_RELOAD;
172 }
173 
174 /*!
175     \brief      configure counter reload value, and prescaler divider value
176     \param[in]  reload_value: specify reload value(0x0000 - 0x0FFF)
177     \param[in]  prescaler_div: FWDGT prescaler value
178                 only one parameter can be selected which is shown as below:
179       \arg        FWDGT_PSC_DIV4: FWDGT prescaler set to 4
180       \arg        FWDGT_PSC_DIV8: FWDGT prescaler set to 8
181       \arg        FWDGT_PSC_DIV16: FWDGT prescaler set to 16
182       \arg        FWDGT_PSC_DIV32: FWDGT prescaler set to 32
183       \arg        FWDGT_PSC_DIV64: FWDGT prescaler set to 64
184       \arg        FWDGT_PSC_DIV128: FWDGT prescaler set to 128
185       \arg        FWDGT_PSC_DIV256: FWDGT prescaler set to 256
186     \param[out] none
187     \retval     ErrStatus: ERROR or SUCCESS
188 */
fwdgt_config(uint16_t reload_value,uint8_t prescaler_div)189 ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
190 {
191     uint32_t timeout = FWDGT_PSC_TIMEOUT;
192     uint32_t flag_status = 0U;
193 
194     /* enable write access to FWDGT_PSC,and FWDGT_RLD */
195     FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
196 
197     /* wait until the PUD flag to be reset */
198     do{
199         flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
200     }while((--timeout > 0U) && (0U != flag_status));
201 
202     if (0U != flag_status){
203         return ERROR;
204     }
205 
206     /* configure FWDGT */
207     FWDGT_PSC = (uint32_t)prescaler_div;
208 
209     timeout = FWDGT_RLD_TIMEOUT;
210     /* wait until the RUD flag to be reset */
211     do{
212         flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
213     }while((--timeout > 0U) && (0U != flag_status));
214 
215     if (0U != flag_status){
216         return ERROR;
217     }
218 
219     FWDGT_RLD = RLD_RLD(reload_value);
220 
221     /* reload the counter */
222     FWDGT_CTL = FWDGT_KEY_RELOAD;
223 
224     return SUCCESS;
225 }
226 
227 /*!
228     \brief      get flag state of FWDGT
229     \param[in]  flag: flag to get
230                 only one parameter can be selected which is shown as below:
231       \arg        FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
232       \arg        FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
233       \arg        FWDGT_FLAG_WUD: a write operation to FWDGT_WND register is on going
234     \param[out] none
235     \retval     FlagStatus: SET or RESET
236 */
fwdgt_flag_get(uint16_t flag)237 FlagStatus fwdgt_flag_get(uint16_t flag)
238 {
239     if (0U != (FWDGT_STAT & flag)){
240         return SET;
241     }
242     return RESET;
243 }
244