1 /*!
2 \file gd32f3x0_fwdgt.c
3 \brief FWDGT 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_fwdgt.h"
38
39 /*!
40 \brief enable write access to FWDGT_PSC and FWDGT_RLD
41 \param[in] none
42 \param[out] none
43 \retval none
44 */
fwdgt_write_enable(void)45 void fwdgt_write_enable(void)
46 {
47 FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
48 }
49
50 /*!
51 \brief disable write access to FWDGT_PSC,FWDGT_RLD and FWDGT_WND
52 \param[in] none
53 \param[out] none
54 \retval none
55 */
fwdgt_write_disable(void)56 void fwdgt_write_disable(void)
57 {
58 FWDGT_CTL = FWDGT_WRITEACCESS_DISABLE;
59 }
60
61 /*!
62 \brief start the free watchdog timer counter
63 \param[in] none
64 \param[out] none
65 \retval none
66 */
fwdgt_enable(void)67 void fwdgt_enable(void)
68 {
69 FWDGT_CTL = FWDGT_KEY_ENABLE;
70 }
71
72 /*!
73 \brief configure the free watchdog timer counter window value
74 \param[in] window_value: specify window value(0x0000 - 0x0FFF)
75 \param[out] none
76 \retval ErrStatus: ERROR or SUCCESS
77 */
fwdgt_window_value_config(uint16_t window_value)78 ErrStatus fwdgt_window_value_config(uint16_t window_value)
79 {
80 uint32_t time_index = FWDGT_WND_TIMEOUT;
81 uint32_t flag_status = RESET;
82
83 /* enable write access to FWDGT_WND */
84 FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
85
86 /* wait until the WUD flag to be reset */
87 do{
88 flag_status = FWDGT_STAT & FWDGT_STAT_WUD;
89 }while((--time_index > 0U) && (RESET != flag_status));
90
91 if (RESET != flag_status){
92 return ERROR;
93 }
94
95 FWDGT_WND = WND_WND(window_value);
96
97 return SUCCESS;
98 }
99
100 /*!
101 \brief reload the counter of FWDGT
102 \param[in] none
103 \param[out] none
104 \retval none
105 */
fwdgt_counter_reload(void)106 void fwdgt_counter_reload(void)
107 {
108 FWDGT_CTL = FWDGT_KEY_RELOAD;
109 }
110
111
112 /*!
113 \brief configure counter reload value, and prescaler divider value
114 \param[in] reload_value: specify reload value(0x0000 - 0x0FFF)
115 \param[in] prescaler_div: FWDGT prescaler value
116 only one parameter can be selected which is shown as below:
117 \arg FWDGT_PSC_DIV4: FWDGT prescaler set to 4
118 \arg FWDGT_PSC_DIV8: FWDGT prescaler set to 8
119 \arg FWDGT_PSC_DIV16: FWDGT prescaler set to 16
120 \arg FWDGT_PSC_DIV32: FWDGT prescaler set to 32
121 \arg FWDGT_PSC_DIV64: FWDGT prescaler set to 64
122 \arg FWDGT_PSC_DIV128: FWDGT prescaler set to 128
123 \arg FWDGT_PSC_DIV256: FWDGT prescaler set to 256
124 \param[out] none
125 \retval ErrStatus: ERROR or SUCCESS
126 */
fwdgt_config(uint16_t reload_value,uint8_t prescaler_div)127 ErrStatus fwdgt_config(uint16_t reload_value, uint8_t prescaler_div)
128 {
129 uint32_t timeout = FWDGT_PSC_TIMEOUT;
130 uint32_t flag_status = RESET;
131
132 /* enable write access to FWDGT_PSC,and FWDGT_RLD */
133 FWDGT_CTL = FWDGT_WRITEACCESS_ENABLE;
134
135 /* wait until the PUD flag to be reset */
136 do{
137 flag_status = FWDGT_STAT & FWDGT_STAT_PUD;
138 }while((--timeout > 0U) && (RESET != flag_status));
139
140 if (RESET != flag_status){
141 return ERROR;
142 }
143
144 /* configure FWDGT */
145 FWDGT_PSC = (uint32_t)prescaler_div;
146
147 timeout = FWDGT_RLD_TIMEOUT;
148 /* wait until the RUD flag to be reset */
149 do{
150 flag_status = FWDGT_STAT & FWDGT_STAT_RUD;
151 }while((--timeout > 0U) && (RESET != flag_status));
152
153 if (RESET != flag_status){
154 return ERROR;
155 }
156
157 FWDGT_RLD = RLD_RLD(reload_value);
158
159 /* reload the counter */
160 FWDGT_CTL = FWDGT_KEY_RELOAD;
161
162 return SUCCESS;
163 }
164
165 /*!
166 \brief get flag state of FWDGT
167 \param[in] flag: flag to get
168 only one parameter can be selected which is shown as below:
169 \arg FWDGT_FLAG_PUD: a write operation to FWDGT_PSC register is on going
170 \arg FWDGT_FLAG_RUD: a write operation to FWDGT_RLD register is on going
171 \arg FWDGT_FLAG_WUD: a write operation to FWDGT_WND register is on going
172 \param[out] none
173 \retval FlagStatus: SET or RESET
174 */
fwdgt_flag_get(uint16_t flag)175 FlagStatus fwdgt_flag_get(uint16_t flag)
176 {
177 if(FWDGT_STAT & flag){
178 return SET;
179 }
180 return RESET;
181 }
182