1 /**
2 ******************************************************************************
3 * @file    HAL_bkp.c
4 * @author  AE Team
5 * @version V1.1.0
6 * @date    28/08/2019
7 * @brief   This file provides all the BKP firmware functions.
8 ******************************************************************************
9 * @copy
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, MindMotion SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2019 MindMotion</center></h2>
19 */
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "HAL_bkp.h"
23 #include "HAL_rcc.h"
24 
25 /** @addtogroup StdPeriph_Driver
26 * @{
27 */
28 
29 /** @defgroup BKP
30 * @brief BKP driver modules
31 * @{
32 */
33 
34 /** @defgroup BKP_Private_TypesDefinitions
35 * @{
36 */
37 
38 /**
39 * @}
40 */
41 
42 /** @defgroup BKP_Private_Defines
43 * @{
44 */
45 
46 /* ------------ BKP registers bit address in the alias region --------------- */
47 #define BKP_OFFSET        (BKP_BASE - PERIPH_BASE)
48 
49 /* --- CR Register ----*/
50 
51 /* Alias word address of TPAL bit */
52 #define CR_OFFSET         (BKP_OFFSET + 0x30)
53 #define TPAL_BitNumber    0x01
54 #define CR_TPAL_BB        (PERIPH_BB_BASE + (CR_OFFSET * 32) + (TPAL_BitNumber * 4))
55 
56 /* Alias word address of TPE bit */
57 #define TPE_BitNumber     0x00
58 #define CR_TPE_BB         (PERIPH_BB_BASE + (CR_OFFSET * 32) + (TPE_BitNumber * 4))
59 
60 /* --- CSR Register ---*/
61 
62 /* Alias word address of TPIE bit */
63 #define CSR_OFFSET        (BKP_OFFSET + 0x34)
64 #define TPIE_BitNumber    0x02
65 #define CSR_TPIE_BB       (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TPIE_BitNumber * 4))
66 
67 /* Alias word address of TIF bit */
68 #define TIF_BitNumber     0x09
69 #define CSR_TIF_BB        (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TIF_BitNumber * 4))
70 
71 /* Alias word address of TEF bit */
72 #define TEF_BitNumber     0x08
73 #define CSR_TEF_BB        (PERIPH_BB_BASE + (CSR_OFFSET * 32) + (TEF_BitNumber * 4))
74 
75 /* ---------------------- BKP registers bit mask ------------------------ */
76 
77 /* RTCCR register bit mask */
78 #define RTCCR_CAL_Mask    ((uint16_t)0xFF80)
79 #define RTCCR_Mask        ((uint16_t)0xFC7F)
80 
81 /* CSR register bit mask */
82 #define CSR_CTE_Set       ((uint16_t)0x0001)
83 #define CSR_CTI_Set       ((uint16_t)0x0002)
84 
85 /**
86 * @}
87 */
88 
89 
90 /** @defgroup BKP_Private_Macros
91 * @{
92 */
93 
94 /**
95 * @}
96 */
97 
98 /** @defgroup BKP_Private_Variables
99 * @{
100 */
101 
102 /**
103 * @}
104 */
105 
106 /** @defgroup BKP_Private_FunctionPrototypes
107 * @{
108 */
109 
110 /**
111 * @}
112 */
113 
114 /** @defgroup BKP_Private_Functions
115 * @{
116 */
117 
118 /**
119 * @brief  Deinitializes the BKP peripheral registers to their default
120 *   reset values.
121 * @param  None
122 * @retval : None
123 */
BKP_DeInit(void)124 void BKP_DeInit(void)
125 {
126     RCC_BackupResetCmd(ENABLE);
127     RCC_BackupResetCmd(DISABLE);
128 }
129 
130 /**
131 * @brief  Configures the Tamper Pin active level.
132 * @param BKP_TamperPinLevel: specifies the Tamper Pin active level.
133 *   This parameter can be one of the following values:
134 * @arg BKP_TamperPinLevel_High: Tamper pin active on high level
135 * @arg BKP_TamperPinLevel_Low: Tamper pin active on low level
136 * @retval : None
137 */
BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)138 void BKP_TamperPinLevelConfig(uint16_t BKP_TamperPinLevel)
139 {
140     /* Check the parameters */
141     assert_param(IS_BKP_TAMPER_PIN_LEVEL(BKP_TamperPinLevel));
142     *(__IO uint32_t *) CR_TPAL_BB = BKP_TamperPinLevel;
143 }
144 
145 /**
146 * @brief  Enables or disables the Tamper Pin activation.
147 * @param NewState: new state of the Tamper Pin activation.
148 *   This parameter can be: ENABLE or DISABLE.
149 * @retval : None
150 */
BKP_TamperPinCmd(FunctionalState NewState)151 void BKP_TamperPinCmd(FunctionalState NewState)
152 {
153     /* Check the parameters */
154     assert_param(IS_FUNCTIONAL_STATE(NewState));
155     *(__IO uint32_t *) CR_TPE_BB = (uint32_t)NewState;
156 }
157 
158 /**
159 * @brief  Enables or disables the Tamper Pin Interrupt.
160 * @param NewState: new state of the Tamper Pin Interrupt.
161 *   This parameter can be: ENABLE or DISABLE.
162 * @retval : None
163 */
BKP_ITConfig(FunctionalState NewState)164 void BKP_ITConfig(FunctionalState NewState)
165 {
166     /* Check the parameters */
167     assert_param(IS_FUNCTIONAL_STATE(NewState));
168     *(__IO uint32_t *) CSR_TPIE_BB = (uint32_t)NewState;
169 }
170 
171 /**
172 * @brief  Select the RTC output source to output on the Tamper pin.
173 * @param BKP_RTCOutputSource: specifies the RTC output source.
174 *   This parameter can be one of the following values:
175 * @arg BKP_RTCOutputSource_None: no RTC output on the Tamper pin.
176 * @arg BKP_RTCOutputSource_CalibClock: output the RTC clock
177 *   with frequency divided by 64 on the Tamper pin.
178 * @arg BKP_RTCOutputSource_Alarm: output the RTC Alarm pulse
179 *   signal on the Tamper pin.
180 * @arg BKP_RTCOutputSource_Second: output the RTC Second pulse
181 *   signal on the Tamper pin.
182 * @retval : None
183 */
BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)184 void BKP_RTCOutputConfig(uint16_t BKP_RTCOutputSource)
185 {
186     uint16_t tmpreg = 0;
187     /* Check the parameters */
188     assert_param(IS_BKP_RTC_OUTPUT_SOURCE(BKP_RTCOutputSource));
189     tmpreg = BKP->RTCCR;
190     /* Clear CCO, ASOE and ASOS bits */
191     tmpreg &= RTCCR_Mask;
192 
193     /* Set CCO, ASOE and ASOS bits according to BKP_RTCOutputSource value */
194     tmpreg |= BKP_RTCOutputSource;
195     /* Store the new value */
196     BKP->RTCCR = tmpreg;
197 }
198 
199 /**
200 * @brief  Sets RTC Clock Calibration value.
201 * @param CalibrationValue: specifies the RTC Clock Calibration value.
202 *   This parameter must be a number between 0 and 0x7F.
203 * @retval : None
204 */
BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)205 void BKP_SetRTCCalibrationValue(uint8_t CalibrationValue)
206 {
207     uint16_t tmpreg = 0;
208     /* Check the parameters */
209     assert_param(IS_BKP_CALIBRATION_VALUE(CalibrationValue));
210     tmpreg = BKP->RTCCR;
211     /* Clear CAL[6:0] bits */
212     tmpreg &= RTCCR_CAL_Mask;
213     /* Set CAL[6:0] bits according to CalibrationValue value */
214     tmpreg |= CalibrationValue;
215     /* Store the new value */
216     BKP->RTCCR = tmpreg;
217 }
218 
219 /**
220 * @brief  Writes user data to the specified Data Backup Register.
221 * @param BKP_DR: specifies the Data Backup Register.
222 *   This parameter can be BKP_DRx where x:[1, 42]
223 * @param Data: data to write
224 * @retval : None
225 */
BKP_WriteBackupRegister(uint16_t BKP_DR,uint16_t Data)226 void BKP_WriteBackupRegister(uint16_t BKP_DR, uint16_t Data)
227 {
228     /* Check the parameters */
229     assert_param(IS_BKP_DR(BKP_DR));
230     *(__IO uint16_t *) (BKP_BASE + BKP_DR) = Data;
231 }
232 
233 /**
234 * @brief  Reads data from the specified Data Backup Register.
235 * @param BKP_DR: specifies the Data Backup Register.
236 *   This parameter can be BKP_DRx where x:[1, 42]
237 * @retval : The content of the specified Data Backup Register
238 */
BKP_ReadBackupRegister(uint16_t BKP_DR)239 uint16_t BKP_ReadBackupRegister(uint16_t BKP_DR)
240 {
241     /* Check the parameters */
242     assert_param(IS_BKP_DR(BKP_DR));
243     return (*(__IO uint16_t *) (BKP_BASE + BKP_DR));
244 }
245 
246 /**
247 * @brief  Checks whether the Tamper Pin Event flag is set or not.
248 * @param  None
249 * @retval : The new state of the Tamper Pin Event flag (SET or RESET).
250 */
BKP_GetFlagStatus(void)251 FlagStatus BKP_GetFlagStatus(void)
252 {
253     return (FlagStatus)(*(__IO uint32_t *) CSR_TEF_BB);
254 }
255 
256 /**
257 * @brief  Clears Tamper Pin Event pending flag.
258 * @param  None
259 * @retval : None
260 */
BKP_ClearFlag(void)261 void BKP_ClearFlag(void)
262 {
263     /* Set CTE bit to clear Tamper Pin Event flag */
264     BKP->CSR |= CSR_CTE_Set;
265 }
266 
267 /**
268 * @brief  Checks whether the Tamper Pin Interrupt has occurred or not.
269 * @param  None
270 * @retval : The new state of the Tamper Pin Interrupt (SET or RESET).
271 */
BKP_GetITStatus(void)272 ITStatus BKP_GetITStatus(void)
273 {
274     return (ITStatus)(*(__IO uint32_t *) CSR_TIF_BB);
275 }
276 
277 /**
278 * @brief  Clears Tamper Pin Interrupt pending bit.
279 * @param  None
280 * @retval : None
281 */
BKP_ClearITPendingBit(void)282 void BKP_ClearITPendingBit(void)
283 {
284     /* Set CTI bit to clear Tamper Pin Interrupt pending bit */
285     BKP->CSR |= CSR_CTI_Set;
286 }
287 
288 /**
289 * @}
290 */
291 
292 /**
293 * @}
294 */
295 
296 /**
297 * @}
298 */
299 
300 /*-------------------------(C) COPYRIGHT 2019 MindMotion ----------------------*/
301