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