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