1 /*!
2  * @file       apm32e10x_bakpr.c
3  *
4  * @brief      This file provides all the BAKPR firmware functions.
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-12-31
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2023 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 #include "apm32e10x_bakpr.h"
27 #include "apm32e10x_rcm.h"
28 
29 /** @addtogroup APM32E10x_StdPeriphDriver
30   @{
31 */
32 
33 /** @addtogroup BAKPR_Driver
34   * @brief BAKPR driver modules
35   @{
36 */
37 
38 /** @defgroup BAKPR_Functions Functions
39   @{
40 */
41 
42 /*!
43  * @brief      Reset the BAKPR peripheral registers to their default reset values.
44  *
45  * @param      None
46  *
47  * @retval     None
48  */
BAKPR_Reset(void)49 void BAKPR_Reset(void)
50 {
51     RCM_EnableBackupReset();
52     RCM_DisableBackupReset();
53 }
54 
55 /*!
56  * @brief      Deinitializes the BAKPR peripheral registers to their default reset values.
57  *
58  * @param      value: specifies the RTC output source.
59  *                    This parameter can be one of the following values:
60  *                    @arg BAKPR_TAMPER_PIN_LEVEL_HIGH: Tamper pin active on high level
61  *                    @arg BAKPR_TAMPER_PIN_LEVEL_LOW: Tamper pin active on low level
62  *
63  * @retval     None
64  */
BAKPR_ConfigTamperPinLevel(BAKPR_TAMPER_PIN_LEVEL_T value)65 void BAKPR_ConfigTamperPinLevel(BAKPR_TAMPER_PIN_LEVEL_T value)
66 {
67     BAKPR->CTRL_B.TPALCFG = value;
68 }
69 
70 /*!
71  * @brief      Enables the Tamper Pin activation.
72  *
73  * @param      None
74  *
75  * @retval     None
76  */
BAKPR_EnableTamperPin(void)77 void BAKPR_EnableTamperPin(void)
78 {
79     BAKPR->CTRL_B.TPFCFG = ENABLE ;
80 }
81 
82 /*!
83  * @brief      Disables the Tamper Pin activation.
84  *
85  * @param      None
86  *
87  * @retval     None
88  */
BAKPR_DisableTamperPin(void)89 void BAKPR_DisableTamperPin(void)
90 {
91     BAKPR->CTRL_B.TPFCFG = DISABLE ;
92 }
93 
94 /*!
95  * @brief      Enables the Tamper Pin Interrupt.
96  *
97  * @param      None
98  *
99  * @retval     None
100  */
BAKPR_EnableInterrupt(void)101 void BAKPR_EnableInterrupt(void)
102 {
103     BAKPR->CSTS_B.TPIEN = ENABLE ;
104 }
105 
106 /*!
107  * @brief      Disables the Tamper Pin Interrupt.
108  *
109  * @param      None
110  *
111  * @retval     None
112  */
BAKPR_DisableInterrupt(void)113 void BAKPR_DisableInterrupt(void)
114 {
115     BAKPR->CSTS_B.TPIEN = DISABLE ;
116 }
117 
118 /*!
119  * @brief   Select the RTC output source to output on the Tamper pin.
120  *
121  * @param   soure: specifies the RTC output source.
122  *            This parameter can be one of the following values:
123  *            @arg BAKPR_RTC_OUTPUT_SOURCE_NONE             : no RTC output on the Tamper pin.
124  *            @arg BAKPR_RTC_OUTPUT_SOURCE_CALIBRATION_CLOCK: output the RTC clock with frequency divided by 64 on the Tamper pin.
125  *            @arg BAKPR_RTC_OUTPUT_SOURCE_ALARM            : output the RTC Alarm pulse signal on the Tamper pin.
126  *            @arg BAKPR_RTC_OUTPUT_SOURCE_SECOND           : output the RTC Second pulse signal on the Tamper pin.
127  *
128  * @retval  None
129  */
BAKPR_ConfigRTCOutput(BAKPR_RTC_OUTPUT_SOURCE_T soure)130 void BAKPR_ConfigRTCOutput(BAKPR_RTC_OUTPUT_SOURCE_T soure)
131 {
132     if(soure == BAKPR_RTC_OUTPUT_SOURCE_NONE)
133     {
134         BAKPR->CLKCAL = RESET;
135     } else if(soure == BAKPR_RTC_OUTPUT_SOURCE_CALIBRATION_CLOCK)
136     {
137         BAKPR->CLKCAL_B.CALCOEN = BIT_SET;
138     } else if(soure == BAKPR_RTC_OUTPUT_SOURCE_ALARM)
139     {
140         BAKPR->CLKCAL_B.ASPOEN = BIT_SET;
141     } else if(soure == BAKPR_RTC_OUTPUT_SOURCE_SECOND)
142     {
143         BAKPR->CLKCAL_B.ASPOSEL = BIT_SET;
144     }
145 }
146 
147 /*!
148  * @brief      Sets RTC Clock Calibration value.
149  *
150  * @param      calibrationValue: Specifies the calibration value.
151  *                               This parameter must be a number between 0 and 0x7F.
152  *
153  * @retval     None
154  */
BAKPR_ConfigRTCCalibrationValue(uint8_t calibrationValue)155 void BAKPR_ConfigRTCCalibrationValue(uint8_t calibrationValue)
156 {
157     BAKPR->CLKCAL_B.CALVALUE = calibrationValue;
158 }
159 
160 /*!
161  * @brief      Set user data to the specified Data Backup Register.
162  *
163  * @param      bakrData : specifies the Data Backup Register.
164  *                        This parameter can be BAKPR_DATAx where x is between 1 and 42.
165  *
166  * @param      data : data to set
167  *                    This parameter can be a 16bit value.
168  *
169  * @retval     None
170  */
BAKPR_ConfigBackupRegister(BAKPR_DATA_T bakrData,uint16_t data)171 void BAKPR_ConfigBackupRegister(BAKPR_DATA_T bakrData, uint16_t data)
172 {
173     __IOM uint32_t tmp = 0;
174 
175     tmp = (uint32_t)BAKPR_BASE;
176     tmp += bakrData;
177 
178     *(__IOM uint32_t *) tmp = data;
179 }
180 
181 /*!
182  * @brief      Reads user data from the specified Data Backup Register.
183  *
184  * @param      bakrData : specifies the Data Backup Register.
185  *                        This parameter can be BAKPR_DATAx where x is between 1 and 42.
186  *
187  * @retval     The content of the specified Data Backup Register
188  */
BAKPR_ReadBackupRegister(BAKPR_DATA_T bakrData)189 uint16_t BAKPR_ReadBackupRegister(BAKPR_DATA_T bakrData)
190 {
191     __IOM uint32_t tmp = 0;
192 
193     tmp = (uint32_t)BAKPR_BASE;
194     tmp += bakrData;
195 
196     return (*(__IOM uint32_t *) tmp);
197 }
198 
199 /*!
200  * @brief      Read whether the Tamper Pin Event flag is set or not.
201  *
202  * @param      None
203  *
204  * @retval     Tamper Pin Event flag state
205  */
BAKPR_ReadStatusFlag(void)206 uint8_t BAKPR_ReadStatusFlag(void)
207 {
208     return BAKPR->CSTS_B.TEFLG;
209 }
210 
211 /*!
212  * @brief      Clears Tamper Pin Event pending flag.
213  *
214  * @param      None
215  *
216  * @retval     None
217  */
BAKPR_ClearStatusFlag(void)218 void BAKPR_ClearStatusFlag(void)
219 {
220     BAKPR->CSTS_B.TECLR = BIT_SET;
221 }
222 
223 /*!
224  * @brief      Get whether the Tamper Pin Interrupt has occurred or not.
225  *
226  * @param      None
227  *
228  * @retval     Tamper Pin Interrupt State
229  */
BAKPR_ReadIntFlag(void)230 uint8_t BAKPR_ReadIntFlag(void)
231 {
232     return BAKPR->CSTS_B.TIFLG;
233 }
234 
235 /*!
236  * @brief      Clears Tamper Pin Interrupt pending bit.
237  *
238  * @param      None
239  *
240  * @retval     None
241  */
BAKPR_ClearIntFlag(void)242 void BAKPR_ClearIntFlag(void)
243 {
244     BAKPR->CSTS_B.TICLR = BIT_SET;
245 }
246 
247 /**@} end of group BAKPR_Functions */
248 /**@} end of group BAKPR_Driver */
249 /**@} end of group APM32E10x_StdPeriphDriver */
250