1 ////////////////////////////////////////////////////////////////////////////////
2 /// @file     hal_bkp.c
3 /// @author   AE TEAM
4 /// @brief    THIS FILE PROVIDES ALL THE BKP FIRMWARE FUNCTIONS.
5 ////////////////////////////////////////////////////////////////////////////////
6 /// @attention
7 ///
8 /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
9 /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
10 /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
11 /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
12 /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
13 /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
14 ///
15 /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 // Define to prevent recursive inclusion
19 #define _HAL_BKP_C_
20 
21 // Files includes
22 #include "types.h"
23 #include "hal_pwr.h"
24 #include "hal_rcc.h"
25 #include "hal_bkp.h"
26 
27 
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// @addtogroup MM32_Hardware_Abstract_Layer
31 /// @{
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// @addtogroup BKP_HAL
35 /// @{
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @addtogroup BKP_Exported_Functions
39 /// @{
40 
41 ////////////////////////////////////////////////////////////////////////////////
42 /// @brief  Deinitializes the BKP peripheral registers to their default reset
43 ///         values.
44 /// @param  None.
45 /// @retval None.
46 ////////////////////////////////////////////////////////////////////////////////
BKP_DeInit(void)47 void BKP_DeInit(void)
48 {
49     RCC_BackupResetCmd(ENABLE);
50     RCC_BackupResetCmd(DISABLE);
51 }
52 
53 ////////////////////////////////////////////////////////////////////////////////
54 /// @brief  Configures the Tamper Pin active level.
55 /// @param  tamper_pin_level: specifies the Tamper Pin active level.
56 ///         This parameter can be one of the following values:
57 /// @arg    BKP_TamperPinLevel_High: Tamper pin active on high level
58 /// @arg    BKP_TamperPinLevel_Low: Tamper pin active on low level
59 /// @retval None.
60 ////////////////////////////////////////////////////////////////////////////////
BKP_TamperPinLevelConfig(BKPTPAL_Typedef tamper_pin_level)61 void BKP_TamperPinLevelConfig(BKPTPAL_Typedef tamper_pin_level)
62 {
63     BKP->CR = tamper_pin_level;
64 }
65 
66 ////////////////////////////////////////////////////////////////////////////////
67 /// @brief  Enables or disables the Tamper Pin activation.
68 /// @param  state: new state of the Tamper Pin activation.
69 ///         This parameter can be: ENABLE or DISABLE.
70 /// @retval None.
71 ////////////////////////////////////////////////////////////////////////////////
BKP_TamperPinCmd(FunctionalState state)72 void BKP_TamperPinCmd(FunctionalState state)
73 {
74     (state) ? SET_BIT(BKP->CR, BKP_CR_TPE) : CLEAR_BIT(BKP->CR, BKP_CR_TPE);
75 }
76 
77 ////////////////////////////////////////////////////////////////////////////////
78 /// @brief  Enables or disables the Tamper Pin Interrupt.
79 /// @param  state: new state of the Tamper Pin Interrupt.
80 ///         This parameter can be: ENABLE or DISABLE.
81 /// @retval None.
82 ////////////////////////////////////////////////////////////////////////////////
BKP_ITConfig(FunctionalState state)83 void BKP_ITConfig(FunctionalState state)
84 {
85     (state) ? SET_BIT(BKP->CSR, BKP_CSR_TPIE) : CLEAR_BIT(BKP->CSR, BKP_CSR_TPIE);
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 /// @brief  Select the RTC output source to output on the Tamper pin.
90 /// @param  rtc_output_source: specifies the RTC output source.
91 ///         This parameter can be one of the following values:
92 /// @arg    BKP_RTCOutputSource_None: no RTC output on the Tamper pin.
93 /// @arg    BKP_RTCOutputSource_CalibClock: output the RTC clock with frequency
94 ///                                          divided by 64 on the Tamper pin.
95 /// @arg    BKP_RTCOutputSource_Alarm: output the RTC Alarm pulse signal on
96 ///                                     the Tamper pin.
97 /// @arg    BKP_RTCOutputSource_Second: output the RTC Second pulse signal on
98 ///                                      the Tamper pin.
99 /// @retval None.
100 ////////////////////////////////////////////////////////////////////////////////
BKP_RTCOutputConfig(BKPRTCOUTPUTSRC_Typedef rtc_output_source)101 void BKP_RTCOutputConfig(BKPRTCOUTPUTSRC_Typedef rtc_output_source)
102 {
103     MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CCO | BKP_RTCCR_ASOE | BKP_RTCCR_ASOS, rtc_output_source);
104 }
105 
106 ////////////////////////////////////////////////////////////////////////////////
107 /// @brief  Sets RTC Clock Calibration value.
108 /// @param  calibration_value: specifies the RTC Clock Calibration value.
109 ///         This parameter must be a number between 0 and 0x7F.
110 /// @retval None.
111 ////////////////////////////////////////////////////////////////////////////////
BKP_SetRTCCalibrationValue(u8 calibration_value)112 void BKP_SetRTCCalibrationValue(u8 calibration_value)
113 {
114     MODIFY_REG(BKP->RTCCR, BKP_RTCCR_CAL, calibration_value);
115 }
116 
117 ////////////////////////////////////////////////////////////////////////////////
118 /// @brief  Checks whether the Tamper Pin Event flag is set or not.
119 /// @param  None.
120 /// @retval State: The new state of the Tamper Pin Event flag (SET or RESET).
121 ////////////////////////////////////////////////////////////////////////////////
BKP_GetFlagStatus(void)122 FlagStatus BKP_GetFlagStatus(void)
123 {
124     return ((BKP->CSR & BKP_CSR_TEF) ? SET : RESET);
125 }
126 
127 ////////////////////////////////////////////////////////////////////////////////
128 /// @brief  Clears Tamper Pin Event pending flag.
129 /// @param  None.
130 /// @retval None.
131 ////////////////////////////////////////////////////////////////////////////////
BKP_ClearFlag(void)132 void BKP_ClearFlag(void)
133 {
134     SET_BIT(BKP->CSR, BKP_CSR_CTE);
135 }
136 
137 ////////////////////////////////////////////////////////////////////////////////
138 /// @brief  Checks whether the Tamper Pin Interrupt has occurred or not.
139 /// @param  None.
140 /// @retval State: The new state of the Tamper Pin Interrupt (SET or RESET).
141 ////////////////////////////////////////////////////////////////////////////////
BKP_GetITStatus(void)142 ITStatus BKP_GetITStatus(void)
143 {
144     return ((BKP->CSR & BKP_CSR_TIF) ? SET : RESET);
145 }
146 
147 ////////////////////////////////////////////////////////////////////////////////
148 /// @brief  Clears Tamper Pin Interrupt pending bit.
149 /// @param  None.
150 /// @retval None.
151 ////////////////////////////////////////////////////////////////////////////////
BKP_ClearITPendingBit(void)152 void BKP_ClearITPendingBit(void)
153 {
154     SET_BIT(BKP->CSR, BKP_CSR_CTI);
155 }
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 /// @brief  Writes user data to the specified data Backup Register.
159 /// @param  bkp_dr: specifies the data Backup Register.
160 ///         This parameter can be BKP_DRx where x:[1, 10]
161 /// @param  data: data to write
162 /// @retval None.
163 ////////////////////////////////////////////////////////////////////////////////
BKP_WriteBackupRegister(BKPDR_Typedef bkp_dr,u16 data)164 void BKP_WriteBackupRegister(BKPDR_Typedef bkp_dr, u16 data)
165 {
166     *(vu16*)(BKP_BASE + bkp_dr) = data;
167 }
168 
169 ////////////////////////////////////////////////////////////////////////////////
170 /// @brief  Reads data from the specified data Backup Register.
171 /// @param  bkp_dr: specifies the data Backup Register.
172 ///         This parameter can be BKP_DRx where x:[1, 10]
173 /// @retval data: The content of the specified data Backup Register
174 ////////////////////////////////////////////////////////////////////////////////
BKP_ReadBackupRegister(BKPDR_Typedef bkp_dr)175 u16 BKP_ReadBackupRegister(BKPDR_Typedef bkp_dr)
176 {
177     return (*(vu16*)(BKP_BASE + bkp_dr));
178 }
179 ////////////////////////////////////////////////////////////////////////////////
180 //          Extended function interface
181 ////////////////////////////////////////////////////////////////////////////////
182 /// @brief  Initializes the BKP peripheral, enable access to the backup
183 ///         registers.
184 /// @param  None.
185 /// @retval None.
186 ////////////////////////////////////////////////////////////////////////////////
exBKP_Init(void)187 void exBKP_Init(void)
188 {
189     RCC_APB1PeriphClockCmd(RCC_APB1ENR_PWR, ENABLE);
190     //COMMON_EnableIpClock(emCLOCK_PWR);
191     RCC_APB1PeriphClockCmd(RCC_APB1ENR_BKP, ENABLE);
192     //COMMON_EnableIpClock(emCLOCK_BKP);
193 
194     RCC->BDCR |= RCC_BDCR_DBP;
195 }
196 
197 ////////////////////////////////////////////////////////////////////////////////
198 /// @brief  Writes user data to the specified data Backup Register immediately.
199 /// @param  bkp_dr: specifies the data Backup Register.
200 ///         This parameter can be BKP_DRx where x:[1, 10]
201 /// @param  data: data to write
202 /// @retval None.
203 ////////////////////////////////////////////////////////////////////////////////
exBKP_ImmWrite(BKPDR_Typedef bkp_dr,u16 dat)204 void exBKP_ImmWrite(BKPDR_Typedef bkp_dr, u16 dat)
205 {
206     RCC->BDCR |= RCC_BDCR_DBP;
207     *(vu16*)(BKP_BASE + bkp_dr) = dat;
208     RCC->BDCR &= ~RCC_BDCR_DBP;
209 
210 }
211 
212 ////////////////////////////////////////////////////////////////////////////////
213 /// @brief  Reads data from the specified data Backup Register immediately.
214 /// @param  bkp_dr: specifies the data Backup Register.
215 ///         This parameter can be BKP_DRx where x:[1, 10]
216 /// @retval data: The content of the specified data Backup Register
217 ////////////////////////////////////////////////////////////////////////////////
exBKP_ImmRead(BKPDR_Typedef bkp_dr)218 u16 exBKP_ImmRead(BKPDR_Typedef bkp_dr)
219 {
220     u16 dat;
221     RCC->BDCR |= RCC_BDCR_DBP;
222     dat = (*(vu16*)(BKP_BASE + bkp_dr));
223     RCC->BDCR &= ~RCC_BDCR_DBP;
224     return dat;
225 }
226 
227 /// @}
228 
229 /// @}
230 
231 /// @}
232