1 /*!
2 \file gd32vf103_bkp.c
3 \brief BKP driver
4
5 \version 2019-06-05, V1.0.0, firmware for GD32VF103
6 \version 2020-08-04, V1.1.0, firmware for GD32VF103
7 */
8
9 /*
10 Copyright (c) 2020, GigaDevice Semiconductor Inc.
11
12 Redistribution and use in source and binary forms, with or without modification,
13 are permitted provided that the following conditions are met:
14
15 1. Redistributions of source code must retain the above copyright notice, this
16 list of conditions and the following disclaimer.
17 2. Redistributions in binary form must reproduce the above copyright notice,
18 this list of conditions and the following disclaimer in the documentation
19 and/or other materials provided with the distribution.
20 3. Neither the name of the copyright holder nor the names of its contributors
21 may be used to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
28 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
29 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGE.
34 */
35
36 #include "gd32vf103_bkp.h"
37
38 /* BKP register bits offset */
39 #define BKP_TAMPER_BITS_OFFSET ((uint32_t)8U)
40
41 /*!
42 \brief reset BKP registers
43 \param[in] none
44 \param[out] none
45 \retval none
46 */
bkp_deinit(void)47 void bkp_deinit(void)
48 {
49 /* reset BKP domain register*/
50 rcu_bkp_reset_enable();
51 rcu_bkp_reset_disable();
52 }
53
54 /*!
55 \brief write BKP data register
56 \param[in] register_number: refer to bkp_data_register_enum
57 only one parameter can be selected which is shown as below:
58 \arg BKP_DATA_x(x = 0..41): bkp data register number x
59 \param[in] data: the data to be write in BKP data register
60 \param[out] none
61 \retval none
62 */
bkp_data_write(bkp_data_register_enum register_number,uint16_t data)63 void bkp_data_write(bkp_data_register_enum register_number, uint16_t data)
64 {
65 if((register_number >= BKP_DATA_10) && (register_number <= BKP_DATA_41)){
66 BKP_DATA10_41((uint32_t)register_number - 1U) = data;
67 }else if((register_number >= BKP_DATA_0) && (register_number <= BKP_DATA_9)){
68 BKP_DATA0_9((uint32_t)register_number - 1U) = data;
69 }else{
70 /* illegal parameters */
71 }
72 }
73
74 /*!
75 \brief read BKP data register
76 \param[in] register_number: refer to bkp_data_register_enum
77 only one parameter can be selected which is shown as below:
78 \arg BKP_DATA_x(x = 0..41): bkp data register number x
79 \param[out] none
80 \retval data of BKP data register
81 */
bkp_data_read(bkp_data_register_enum register_number)82 uint16_t bkp_data_read(bkp_data_register_enum register_number)
83 {
84 uint16_t data = 0U;
85
86 /* get the data from the BKP data register */
87 if((register_number >= BKP_DATA_10) && (register_number <= BKP_DATA_41)){
88 data = BKP_DATA10_41((uint32_t)register_number - 1U);
89 }else if((register_number >= BKP_DATA_0) && (register_number <= BKP_DATA_9)){
90 data = BKP_DATA0_9((uint32_t)register_number - 1U);
91 }else{
92 /* illegal parameters */
93 }
94 return data;
95 }
96
97 /*!
98 \brief enable RTC clock calibration output
99 \param[in] none
100 \param[out] none
101 \retval none
102 */
bkp_rtc_calibration_output_enable(void)103 void bkp_rtc_calibration_output_enable(void)
104 {
105 BKP_OCTL |= (uint16_t)BKP_OCTL_COEN;
106 }
107
108 /*!
109 \brief disable RTC clock calibration output
110 \param[in] none
111 \param[out] none
112 \retval none
113 */
bkp_rtc_calibration_output_disable(void)114 void bkp_rtc_calibration_output_disable(void)
115 {
116 BKP_OCTL &= (uint16_t)~BKP_OCTL_COEN;
117 }
118
119 /*!
120 \brief enable RTC alarm or second signal output
121 \param[in] none
122 \param[out] none
123 \retval none
124 */
bkp_rtc_signal_output_enable(void)125 void bkp_rtc_signal_output_enable(void)
126 {
127 BKP_OCTL |= (uint16_t)BKP_OCTL_ASOEN;
128 }
129
130 /*!
131 \brief disable RTC alarm or second signal output
132 \param[in] none
133 \param[out] none
134 \retval none
135 */
bkp_rtc_signal_output_disable(void)136 void bkp_rtc_signal_output_disable(void)
137 {
138 BKP_OCTL &= (uint16_t)~BKP_OCTL_ASOEN;
139 }
140
141 /*!
142 \brief select RTC output
143 \param[in] outputsel: RTC output selection
144 only one parameter can be selected which is shown as below:
145 \arg RTC_OUTPUT_ALARM_PULSE: RTC alarm pulse is selected as the RTC output
146 \arg RTC_OUTPUT_SECOND_PULSE: RTC second pulse is selected as the RTC output
147 \param[out] none
148 \retval none
149 */
bkp_rtc_output_select(uint16_t outputsel)150 void bkp_rtc_output_select(uint16_t outputsel)
151 {
152 uint16_t ctl = 0U;
153
154 /* configure BKP_OCTL_ROSEL with outputsel */
155 ctl = BKP_OCTL;
156 ctl &= (uint16_t)~BKP_OCTL_ROSEL;
157 ctl |= outputsel;
158 BKP_OCTL = ctl;
159 }
160
161 /*!
162 \brief set RTC clock calibration value
163 \param[in] value: RTC clock calibration value
164 \arg 0x00 - 0x7F
165 \param[out] none
166 \retval none
167 */
bkp_rtc_calibration_value_set(uint8_t value)168 void bkp_rtc_calibration_value_set(uint8_t value)
169 {
170 uint16_t ctl;
171
172 /* configure BKP_OCTL_RCCV with value */
173 ctl = BKP_OCTL;
174 ctl &= (uint16_t)~BKP_OCTL_RCCV;
175 ctl |= (uint16_t)OCTL_RCCV(value);
176 BKP_OCTL = ctl;
177 }
178
179 /*!
180 \brief enable tamper detection
181 \param[in] none
182 \param[out] none
183 \retval none
184 */
bkp_tamper_detection_enable(void)185 void bkp_tamper_detection_enable(void)
186 {
187 BKP_TPCTL |= (uint16_t)BKP_TPCTL_TPEN;
188 }
189
190 /*!
191 \brief disable tamper detection
192 \param[in] none
193 \param[out] none
194 \retval none
195 */
bkp_tamper_detection_disable(void)196 void bkp_tamper_detection_disable(void)
197 {
198 BKP_TPCTL &= (uint16_t)~BKP_TPCTL_TPEN;
199 }
200
201 /*!
202 \brief set tamper pin active level
203 \param[in] level: tamper active level
204 only one parameter can be selected which is shown as below:
205 \arg TAMPER_PIN_ACTIVE_HIGH: the tamper pin is active high
206 \arg TAMPER_PIN_ACTIVE_LOW: the tamper pin is active low
207 \param[out] none
208 \retval none
209 */
bkp_tamper_active_level_set(uint16_t level)210 void bkp_tamper_active_level_set(uint16_t level)
211 {
212 uint16_t ctl = 0U;
213
214 /* configure BKP_TPCTL_TPAL with level */
215 ctl = BKP_TPCTL;
216 ctl &= (uint16_t)~BKP_TPCTL_TPAL;
217 ctl |= level;
218 BKP_TPCTL = ctl;
219 }
220
221 /*!
222 \brief enable tamper interrupt
223 \param[in] none
224 \param[out] none
225 \retval none
226 */
bkp_interrupt_enable(void)227 void bkp_interrupt_enable(void)
228 {
229 BKP_TPCS |= (uint16_t)BKP_TPCS_TPIE;
230 }
231
232 /*!
233 \brief disable tamper interrupt
234 \param[in] none
235 \param[out] none
236 \retval none
237 */
bkp_interrupt_disable(void)238 void bkp_interrupt_disable(void)
239 {
240 BKP_TPCS &= (uint16_t)~BKP_TPCS_TPIE;
241 }
242
243 /*!
244 \brief get tamper flag state
245 \param[in] none
246 \param[out] none
247 \retval FlagStatus: SET or RESET
248 */
bkp_flag_get(void)249 FlagStatus bkp_flag_get(void)
250 {
251 if(BKP_TPCS & BKP_FLAG_TAMPER){
252 return SET;
253 }else{
254 return RESET;
255 }
256 }
257
258 /*!
259 \brief clear tamper flag state
260 \param[in] none
261 \param[out] none
262 \retval none
263 */
bkp_flag_clear(void)264 void bkp_flag_clear(void)
265 {
266 BKP_TPCS |= (uint16_t)(BKP_FLAG_TAMPER >> BKP_TAMPER_BITS_OFFSET);
267 }
268
269 /*!
270 \brief get tamper interrupt flag state
271 \param[in] none
272 \param[out] none
273 \retval FlagStatus: SET or RESET
274 */
bkp_interrupt_flag_get(void)275 FlagStatus bkp_interrupt_flag_get(void)
276 {
277 if(BKP_TPCS & BKP_INT_FLAG_TAMPER){
278 return SET;
279 }else{
280 return RESET;
281 }
282 }
283
284 /*!
285 \brief clear tamper interrupt flag state
286 \param[in] none
287 \param[out] none
288 \retval none
289 */
bkp_interrupt_flag_clear(void)290 void bkp_interrupt_flag_clear(void)
291 {
292 BKP_TPCS |= (uint16_t)(BKP_INT_FLAG_TAMPER >> BKP_TAMPER_BITS_OFFSET);
293 }
294