1 /* Includes ------------------------------------------------------------------*/ 2 #include "air32f10x_rtc.h" 3 4 /** @defgroup RTC 5 * @brief RTC driver modules 6 * @{ 7 */ 8 9 /** @defgroup RTC_Private_TypesDefinitions 10 * @{ 11 */ 12 /** 13 * @} 14 */ 15 16 /** @defgroup RTC_Private_Defines 17 * @{ 18 */ 19 #define RTC_LSB_MASK ((uint32_t)0x0000FFFF) /*!< RTC LSB Mask */ 20 #define PRLH_MSB_MASK ((uint32_t)0x000F0000) /*!< RTC Prescaler MSB Mask */ 21 22 /** 23 * @} 24 */ 25 26 /** @defgroup RTC_Private_Macros 27 * @{ 28 */ 29 30 /** 31 * @} 32 */ 33 34 /** @defgroup RTC_Private_Variables 35 * @{ 36 */ 37 38 /** 39 * @} 40 */ 41 42 /** @defgroup RTC_Private_FunctionPrototypes 43 * @{ 44 */ 45 46 /** 47 * @} 48 */ 49 50 /** @defgroup RTC_Private_Functions 51 * @{ 52 */ 53 54 /** 55 * @brief Enables or disables the specified RTC interrupts. 56 * @param RTC_IT: specifies the RTC interrupts sources to be enabled or disabled. 57 * This parameter can be any combination of the following values: 58 * @arg RTC_IT_OW: Overflow interrupt 59 * @arg RTC_IT_ALR: Alarm interrupt 60 * @arg RTC_IT_SEC: Second interrupt 61 * @param NewState: new state of the specified RTC interrupts. 62 * This parameter can be: ENABLE or DISABLE. 63 * @retval None 64 */ RTC_ITConfig(uint16_t RTC_IT,FunctionalState NewState)65void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState) 66 { 67 /* Check the parameters */ 68 assert_param(IS_RTC_IT(RTC_IT)); 69 assert_param(IS_FUNCTIONAL_STATE(NewState)); 70 71 if (NewState != DISABLE) 72 { 73 RTC->CRH |= RTC_IT; 74 } 75 else 76 { 77 RTC->CRH &= (uint16_t)~RTC_IT; 78 } 79 } 80 81 /** 82 * @brief Enters the RTC configuration mode. 83 * @param None 84 * @retval None 85 */ RTC_EnterConfigMode(void)86void RTC_EnterConfigMode(void) 87 { 88 /* Set the CNF flag to enter in the Configuration Mode */ 89 RTC->CRL |= RTC_CRL_CNF; 90 } 91 92 /** 93 * @brief Exits from the RTC configuration mode. 94 * @param None 95 * @retval None 96 */ RTC_ExitConfigMode(void)97void RTC_ExitConfigMode(void) 98 { 99 /* Reset the CNF flag to exit from the Configuration Mode */ 100 RTC->CRL &= (uint16_t)~((uint16_t)RTC_CRL_CNF); 101 } 102 103 /** 104 * @brief Gets the RTC counter value. 105 * @param None 106 * @retval RTC counter value. 107 */ RTC_GetCounter(void)108uint32_t RTC_GetCounter(void) 109 { 110 uint16_t tmp = 0; 111 tmp = RTC->CNTL; 112 return (((uint32_t)RTC->CNTH << 16 ) | tmp) ; 113 } 114 115 /** 116 * @brief Sets the RTC counter value. 117 * @param CounterValue: RTC counter new value. 118 * @retval None 119 */ RTC_SetCounter(uint32_t CounterValue)120void RTC_SetCounter(uint32_t CounterValue) 121 { 122 RTC_EnterConfigMode(); 123 /* Set RTC COUNTER MSB word */ 124 RTC->CNTH = CounterValue >> 16; 125 /* Set RTC COUNTER LSB word */ 126 RTC->CNTL = (CounterValue & RTC_LSB_MASK); 127 RTC_ExitConfigMode(); 128 } 129 130 /** 131 * @brief Sets the RTC prescaler value. 132 * @param PrescalerValue: RTC prescaler new value. 133 * @retval None 134 */ RTC_SetPrescaler(uint32_t PrescalerValue)135void RTC_SetPrescaler(uint32_t PrescalerValue) 136 { 137 /* Check the parameters */ 138 assert_param(IS_RTC_PRESCALER(PrescalerValue)); 139 140 RTC_EnterConfigMode(); 141 /* Set RTC PRESCALER MSB word */ 142 RTC->PRLH = (PrescalerValue & PRLH_MSB_MASK) >> 16; 143 /* Set RTC PRESCALER LSB word */ 144 RTC->PRLL = (PrescalerValue & RTC_LSB_MASK); 145 RTC_ExitConfigMode(); 146 } 147 148 /** 149 * @brief Sets the RTC alarm value. 150 * @param AlarmValue: RTC alarm new value. 151 * @retval None 152 */ RTC_SetAlarm(uint32_t AlarmValue)153void RTC_SetAlarm(uint32_t AlarmValue) 154 { 155 RTC_EnterConfigMode(); 156 /* Set the ALARM MSB word */ 157 RTC->ALRH = AlarmValue >> 16; 158 /* Set the ALARM LSB word */ 159 RTC->ALRL = (AlarmValue & RTC_LSB_MASK); 160 RTC_ExitConfigMode(); 161 } 162 163 /** 164 * @brief Gets the RTC divider value. 165 * @param None 166 * @retval RTC Divider value. 167 */ RTC_GetDivider(void)168uint32_t RTC_GetDivider(void) 169 { 170 uint32_t tmp = 0x00; 171 tmp = ((uint32_t)RTC->DIVH & (uint32_t)0x000F) << 16; 172 tmp |= RTC->DIVL; 173 return tmp; 174 } 175 176 /** 177 * @brief Waits until last write operation on RTC registers has finished. 178 * @note This function must be called before any write to RTC registers. 179 * @param None 180 * @retval None 181 */ RTC_WaitForLastTask(void)182void RTC_WaitForLastTask(void) 183 { 184 /* Loop until RTOFF flag is set */ 185 while ((RTC->CRL & RTC_FLAG_RTOFF) == (uint16_t)RESET) 186 { 187 } 188 } 189 190 /** 191 * @brief Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) 192 * are synchronized with RTC APB clock. 193 * @note This function must be called before any read operation after an APB reset 194 * or an APB clock stop. 195 * @param None 196 * @retval None 197 */ RTC_WaitForSynchro(void)198void RTC_WaitForSynchro(void) 199 { 200 /* Clear RSF flag */ 201 RTC->CRL &= (uint16_t)~RTC_FLAG_RSF; 202 /* Loop until RSF flag is set */ 203 while ((RTC->CRL & RTC_FLAG_RSF) == (uint16_t)RESET) 204 { 205 } 206 } 207 208 /** 209 * @brief Checks whether the specified RTC flag is set or not. 210 * @param RTC_FLAG: specifies the flag to check. 211 * This parameter can be one the following values: 212 * @arg RTC_FLAG_RTOFF: RTC Operation OFF flag 213 * @arg RTC_FLAG_RSF: Registers Synchronized flag 214 * @arg RTC_FLAG_OW: Overflow flag 215 * @arg RTC_FLAG_ALR: Alarm flag 216 * @arg RTC_FLAG_SEC: Second flag 217 * @retval The new state of RTC_FLAG (SET or RESET). 218 */ RTC_GetFlagStatus(uint16_t RTC_FLAG)219FlagStatus RTC_GetFlagStatus(uint16_t RTC_FLAG) 220 { 221 FlagStatus bitstatus = RESET; 222 223 /* Check the parameters */ 224 assert_param(IS_RTC_GET_FLAG(RTC_FLAG)); 225 226 if ((RTC->CRL & RTC_FLAG) != (uint16_t)RESET) 227 { 228 bitstatus = SET; 229 } 230 else 231 { 232 bitstatus = RESET; 233 } 234 return bitstatus; 235 } 236 237 /** 238 * @brief Clears the RTC's pending flags. 239 * @param RTC_FLAG: specifies the flag to clear. 240 * This parameter can be any combination of the following values: 241 * @arg RTC_FLAG_RSF: Registers Synchronized flag. This flag is cleared only after 242 * an APB reset or an APB Clock stop. 243 * @arg RTC_FLAG_OW: Overflow flag 244 * @arg RTC_FLAG_ALR: Alarm flag 245 * @arg RTC_FLAG_SEC: Second flag 246 * @retval None 247 */ RTC_ClearFlag(uint16_t RTC_FLAG)248void RTC_ClearFlag(uint16_t RTC_FLAG) 249 { 250 /* Check the parameters */ 251 assert_param(IS_RTC_CLEAR_FLAG(RTC_FLAG)); 252 253 /* Clear the corresponding RTC flag */ 254 RTC->CRL &= (uint16_t)~RTC_FLAG; 255 } 256 257 /** 258 * @brief Checks whether the specified RTC interrupt has occurred or not. 259 * @param RTC_IT: specifies the RTC interrupts sources to check. 260 * This parameter can be one of the following values: 261 * @arg RTC_IT_OW: Overflow interrupt 262 * @arg RTC_IT_ALR: Alarm interrupt 263 * @arg RTC_IT_SEC: Second interrupt 264 * @retval The new state of the RTC_IT (SET or RESET). 265 */ RTC_GetITStatus(uint16_t RTC_IT)266ITStatus RTC_GetITStatus(uint16_t RTC_IT) 267 { 268 ITStatus bitstatus = RESET; 269 /* Check the parameters */ 270 assert_param(IS_RTC_GET_IT(RTC_IT)); 271 272 bitstatus = (ITStatus)(RTC->CRL & RTC_IT); 273 if (((RTC->CRH & RTC_IT) != (uint16_t)RESET) && (bitstatus != (uint16_t)RESET)) 274 { 275 bitstatus = SET; 276 } 277 else 278 { 279 bitstatus = RESET; 280 } 281 return bitstatus; 282 } 283 284 /** 285 * @brief Clears the RTC's interrupt pending bits. 286 * @param RTC_IT: specifies the interrupt pending bit to clear. 287 * This parameter can be any combination of the following values: 288 * @arg RTC_IT_OW: Overflow interrupt 289 * @arg RTC_IT_ALR: Alarm interrupt 290 * @arg RTC_IT_SEC: Second interrupt 291 * @retval None 292 */ RTC_ClearITPendingBit(uint16_t RTC_IT)293void RTC_ClearITPendingBit(uint16_t RTC_IT) 294 { 295 /* Check the parameters */ 296 assert_param(IS_RTC_IT(RTC_IT)); 297 298 /* Clear the corresponding RTC pending bit */ 299 RTC->CRL &= (uint16_t)~RTC_IT; 300 } 301 302 /** 303 * @} 304 */ 305 306 /** 307 * @} 308 */ 309 310 /** 311 * @} 312 */ 313