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