1 /********************************** (C) COPYRIGHT  *******************************
2  * File Name          : ch32v10x_bkp.c
3  * Author             : WCH
4  * Version            : V1.0.0
5  * Date               : 2020/04/30
6  * Description        : This file provides all the BKP firmware functions.
7  * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
8  * SPDX-License-Identifier: Apache-2.0
9  *******************************************************************************/
10 #include "ch32v10x_bkp.h"
11 #include "ch32v10x_rcc.h"
12 
13 /* BKP registers bit mask */
14 
15 /* OCTLR register bit mask */
16 #define OCTLR_CAL_MASK    ((uint16_t)0xFF80)
17 #define OCTLR_MASK        ((uint16_t)0xFC7F)
18 
19 /*********************************************************************
20  * @fn      BKP_DeInit
21  *
22  * @brief   Deinitializes the BKP peripheral registers to their default reset values.
23  *
24  * @return  none
25  */
BKP_DeInit(void)26 void BKP_DeInit(void)
27 {
28     RCC_BackupResetCmd(ENABLE);
29     RCC_BackupResetCmd(DISABLE);
30 }
31 
32 /*********************************************************************
33  * @fn      BKP_TamperPinLevelConfig
34  *
35  * @brief   Configures the Tamper Pin active level.
36  *
37  * @param   BKP_TamperPinLevel: specifies the Tamper Pin active level.
38  *            BKP_TamperPinLevel_High - Tamper pin active on high level.
39  *            BKP_TamperPinLevel_Low - Tamper pin active on low level.
40  *
41  * @return  none
42  */
BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)43 void BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)
44 {
45     if(BKP_TamperPinLevel)
46     {
47         BKP->TPCTLR |= (1 << 1);
48     }
49     else
50     {
51         BKP->TPCTLR &= ~(1 << 1);
52     }
53 }
54 
55 /*********************************************************************
56  * @fn      BKP_TamperPinCmd
57  *
58  * @brief   Enables or disables the Tamper Pin activation.
59  *
60  * @param   NewState - ENABLE or DISABLE.
61  *
62  * @return  none
63  */
BKP_TamperPinCmd(FunctionalState NewState)64 void BKP_TamperPinCmd(FunctionalState NewState)
65 {
66     if(NewState)
67     {
68         BKP->TPCTLR |= (1 << 0);
69     }
70     else
71     {
72         BKP->TPCTLR &= ~(1 << 0);
73     }
74 }
75 
76 /*********************************************************************
77  * @fn      BKP_ITConfig
78  *
79  * @brief   Enables or disables the Tamper Pin Interrupt.
80  *
81  * @param   NewState - ENABLE or DISABLE.
82  *
83  * @return  none
84  */
BKP_ITConfig(FunctionalState NewState)85 void BKP_ITConfig(FunctionalState NewState)
86 {
87     if(NewState)
88     {
89         BKP->TPCSR |= (1 << 2);
90     }
91     else
92     {
93         BKP->TPCSR &= ~(1 << 2);
94     }
95 }
96 
97 /*********************************************************************
98  * @fn      BKP_RTCOutputConfig
99  *
100  * @brief   Select the RTC output source to output on the Tamper pin.
101  *
102  * @param   BKP_RTCOutputSource - specifies the RTC output source.
103  *            BKP_RTCOutputSource_None - no RTC output on the Tamper pin.
104  *            BKP_RTCOutputSource_CalibClock - output the RTC clock with
105  *        frequency divided by 64 on the Tamper pin.
106  *            BKP_RTCOutputSource_Alarm - output the RTC Alarm pulse signal
107  *        on the Tamper pin.
108  *            BKP_RTCOutputSource_Second - output the RTC Second pulse
109  *        signal on the Tamper pin.
110  *
111  * @return  none
112  */
BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)113 void BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)
114 {
115     uint16_t tmpreg = 0;
116 
117     tmpreg = BKP->OCTLR;
118     tmpreg &= OCTLR_MASK;
119     tmpreg |= BKP_RTCOutputSource;
120     BKP->OCTLR = tmpreg;
121 }
122 
123 /*********************************************************************
124  * @fn      BKP_SetRTCCalibrationValue
125  *
126  * @brief   Sets RTC Clock Calibration value.
127  *
128  * @param   CalibrationValue - specifies the RTC Clock Calibration value.
129  *            This parameter must be a number between 0 and 0x1F.
130  *
131  * @return  none
132  */
BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)133 void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)
134 {
135     uint16_t tmpreg = 0;
136 
137     tmpreg = BKP->OCTLR;
138     tmpreg &= OCTLR_CAL_MASK;
139     tmpreg |= CalibrationValue;
140     BKP->OCTLR = tmpreg;
141 }
142 
143 /*********************************************************************
144  * @fn      BKP_WriteBackupRegister
145  *
146  * @brief   Writes user data to the specified Data Backup Register.
147  *
148  * @param   BKP_DR - specifies the Data Backup Register.
149  *          Data - data to write.
150  *
151  * @return  none
152  */
BKP_WriteBackupRegister(uint16_t BKP_DR,uint16_t Data)153 void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data)
154 {
155     __IO uint32_t tmp = 0;
156 
157     tmp = (uint32_t)BKP_BASE;
158     tmp += BKP_DR;
159     *(__IO uint32_t *)tmp = Data;
160 }
161 
162 /*********************************************************************
163  * @fn      BKP_ReadBackupRegister
164  *
165  * @brief   Reads data from the specified Data Backup Register.
166  *
167  * @param   BKP_DR - specifies the Data Backup Register.
168  *            This parameter can be BKP_DRx where x=[1, 42].
169  *
170  * @return  none
171  */
BKP_ReadBackupRegister(uint16_t BKP_DR)172 uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR)
173 {
174     __IO uint32_t tmp = 0;
175 
176     tmp = (uint32_t)BKP_BASE;
177     tmp += BKP_DR;
178 
179     return (*(__IO uint16_t *)tmp);
180 }
181 
182 /*********************************************************************
183  * @fn      BKP_GetFlagStatus
184  *
185  * @brief   Checks whether the Tamper Pin Event flag is set or not.
186  *
187  * @return  FlagStatus - SET or RESET.
188  */
BKP_GetFlagStatus(void)189 FlagStatus BKP_GetFlagStatus(void)
190 {
191     if(BKP->TPCSR & (1 << 8))
192     {
193         return SET;
194     }
195     else
196     {
197         return RESET;
198     }
199 }
200 
201 /*********************************************************************
202  * @fn      BKP_ClearFlag
203  *
204  * @brief   Clears Tamper Pin Event pending flag.
205  *
206  * @return  none
207  */
BKP_ClearFlag(void)208 void BKP_ClearFlag(void)
209 {
210     BKP->TPCSR |= BKP_CTE;
211 }
212 
213 /*********************************************************************
214  * @fn      BKP_GetITStatus
215  *
216  * @brief   Checks whether the Tamper Pin Interrupt has occurred or not.
217  *
218  * @return  ITStatus - SET or RESET.
219  */
BKP_GetITStatus(void)220 ITStatus BKP_GetITStatus(void)
221 {
222     if(BKP->TPCSR & (1 << 9))
223     {
224         return SET;
225     }
226     else
227     {
228         return RESET;
229     }
230 }
231 
232 /*********************************************************************
233  * @fn      BKP_ClearITPendingBit
234  *
235  * @brief   Clears Tamper Pin Interrupt pending bit.
236  *
237  * @return  none
238  */
BKP_ClearITPendingBit(void)239 void BKP_ClearITPendingBit(void)
240 {
241     BKP->TPCSR |= BKP_CTI;
242 }
243 
244 
245 
246 
247 
248 
249