1 /*********************COPYRIGHT(C) 2019 WCH. A11 rights reserved*********************** 2 * File Name : ch32f10x_rtc.c 3 * Author : WCH 4 * Version : V1.0.0 5 * Date : 2019/10/15 6 * Description : This file provides all the RTC firmware functions. 7 ***************************************************************************************/ 8 9 #include "ch32f10x_rtc.h" 10 11 /* RTC_Private_Defines */ 12 #define RTC_LSB_MASK ((uint32_t)0x0000FFFF) /*!< RTC LSB Mask */ 13 #define PRLH_MSB_MASK ((uint32_t)0x000F0000) /*!< RTC Prescaler MSB Mask */ 14 15 16 /******************************************************************************** 17 * Function Name : RTC_ITConfig 18 * Description : Enables or disables the specified RTC interrupts. 19 * Input : RTC_IT: specifies the RTC interrupts sources to be enabled or disabled. 20 * RTC_IT_OW: Overflow interrupt 21 * RTC_IT_ALR: Alarm interrupt 22 * RTC_IT_SEC: Second interrupt 23 * Return : NewState: new state of the specified RTC interrupts(ENABLE or DISABLE). 24 *********************************************************************************/ RTC_ITConfig(uint16_t RTC_IT,FunctionalState NewState)25void RTC_ITConfig(uint16_t RTC_IT, FunctionalState NewState) 26 { 27 if (NewState != DISABLE) 28 { 29 RTC->CTLRH |= RTC_IT; 30 } 31 else 32 { 33 RTC->CTLRH &= (uint16_t)~RTC_IT; 34 } 35 } 36 37 /******************************************************************************** 38 * Function Name : RTC_EnterConfigMode 39 * Description : Enters the RTC configuration mode. 40 * Input : None 41 * Return : None 42 *********************************************************************************/ RTC_EnterConfigMode(void)43void RTC_EnterConfigMode(void) 44 { 45 RTC->CTLRL |= RTC_CTLRL_CNF; 46 } 47 48 49 /******************************************************************************** 50 * Function Name : RTC_ExitConfigMode 51 * Description : Exits from the RTC configuration mode. 52 * Input : None 53 * Return : None 54 *********************************************************************************/ RTC_ExitConfigMode(void)55void RTC_ExitConfigMode(void) 56 { 57 RTC->CTLRL &= (uint16_t)~((uint16_t)RTC_CTLRL_CNF); 58 } 59 60 61 /******************************************************************************** 62 * Function Name : RTC_GetCounter 63 * Description : Gets the RTC counter value 64 * Input : None 65 * Return : RTC counter value 66 *********************************************************************************/ RTC_GetCounter(void)67uint32_t RTC_GetCounter(void) 68 { 69 uint16_t tmp = 0; 70 tmp = RTC->CNTL; 71 return (((uint32_t)RTC->CNTH << 16 ) | tmp) ; 72 } 73 74 75 /******************************************************************************** 76 * Function Name : RTC_SetCounter 77 * Description : Sets the RTC counter value. 78 * Input : CounterValue: RTC counter new value. 79 * Return : None 80 *********************************************************************************/ RTC_SetCounter(uint32_t CounterValue)81void RTC_SetCounter(uint32_t CounterValue) 82 { 83 RTC_EnterConfigMode(); 84 RTC->CNTH = CounterValue >> 16; 85 RTC->CNTL = (CounterValue & RTC_LSB_MASK); 86 RTC_ExitConfigMode(); 87 } 88 89 90 /******************************************************************************** 91 * Function Name : RTC_SetPrescaler 92 * Description : Sets the RTC prescaler value 93 * Input : PrescalerValue: RTC prescaler new value 94 * Return : None 95 *********************************************************************************/ RTC_SetPrescaler(uint32_t PrescalerValue)96void RTC_SetPrescaler(uint32_t PrescalerValue) 97 { 98 RTC_EnterConfigMode(); 99 RTC->PSCRH = (PrescalerValue & PRLH_MSB_MASK) >> 16; 100 RTC->PSCRL = (PrescalerValue & RTC_LSB_MASK); 101 RTC_ExitConfigMode(); 102 } 103 104 105 /******************************************************************************** 106 * Function Name : RTC_SetAlarm 107 * Description : Sets the RTC alarm value 108 * Input : AlarmValue: RTC alarm new value 109 * Return : None 110 *********************************************************************************/ RTC_SetAlarm(uint32_t AlarmValue)111void RTC_SetAlarm(uint32_t AlarmValue) 112 { 113 RTC_EnterConfigMode(); 114 RTC->ALRMH = AlarmValue >> 16; 115 RTC->ALRML = (AlarmValue & RTC_LSB_MASK); 116 RTC_ExitConfigMode(); 117 } 118 119 120 /******************************************************************************** 121 * Function Name : RTC_GetDivider 122 * Description : Gets the RTC divider value 123 * Input : None 124 * Return : RTC Divider value 125 *********************************************************************************/ RTC_GetDivider(void)126uint32_t RTC_GetDivider(void) 127 { 128 uint32_t tmp = 0x00; 129 tmp = ((uint32_t)RTC->DIVH & (uint32_t)0x000F) << 16; 130 tmp |= RTC->DIVL; 131 return tmp; 132 } 133 134 /******************************************************************************** 135 * Function Name : RTC_WaitForLastTask 136 * Description : Waits until last write operation on RTC registers has finished 137 * Input : None 138 * Return : None 139 *********************************************************************************/ RTC_WaitForLastTask(void)140void RTC_WaitForLastTask(void) 141 { 142 while ((RTC->CTLRL & RTC_FLAG_RTOFF) == (uint16_t)RESET) 143 { 144 } 145 } 146 147 148 /******************************************************************************** 149 * Function Name : RTC_WaitForSynchro 150 * Description : Waits until the RTC registers (RTC_CNT, RTC_ALR and RTC_PRL) 151 * are synchronized with RTC APB clock 152 * Input : None 153 * Return : None 154 *********************************************************************************/ RTC_WaitForSynchro(void)155void RTC_WaitForSynchro(void) 156 { 157 RTC->CTLRL &= (uint16_t)~RTC_FLAG_RSF; 158 while ((RTC->CTLRL & RTC_FLAG_RSF) == (uint16_t)RESET) 159 { 160 } 161 } 162 163 164 /******************************************************************************** 165 * Function Name : RTC_GetFlagStatus 166 * Description : Checks whether the specified RTC flag is set or not 167 * Input : RTC_FLAG: specifies the flag to check 168 * RTC_FLAG_RTOFF: RTC Operation OFF flag 169 * RTC_FLAG_RSF: Registers Synchronized flag 170 * RTC_FLAG_OW: Overflow flag 171 * RTC_FLAG_ALR: Alarm flag 172 * RTC_FLAG_SEC: Second flag 173 * Return : The new state of RTC_FLAG (SET or RESET) 174 *********************************************************************************/ RTC_GetFlagStatus(uint16_t RTC_FLAG)175FlagStatus RTC_GetFlagStatus(uint16_t RTC_FLAG) 176 { 177 FlagStatus bitstatus = RESET; 178 if ((RTC->CTLRL & RTC_FLAG) != (uint16_t)RESET) 179 { 180 bitstatus = SET; 181 } 182 else 183 { 184 bitstatus = RESET; 185 } 186 return bitstatus; 187 } 188 189 /******************************************************************************** 190 * Function Name : RTC_ClearFlag 191 * Description : Clears the RTC's pending flags 192 * Input : RTC_FLAG: specifies the flag to clear 193 * RTC_FLAG_RSF: Registers Synchronized flag 194 * RTC_FLAG_OW: Overflow flag 195 * RTC_FLAG_ALR: Alarm flag 196 * RTC_FLAG_SEC: Second flag 197 * Return : None 198 *********************************************************************************/ RTC_ClearFlag(uint16_t RTC_FLAG)199void RTC_ClearFlag(uint16_t RTC_FLAG) 200 { 201 RTC->CTLRL &= (uint16_t)~RTC_FLAG; 202 } 203 204 205 /******************************************************************************** 206 * Function Name : RTC_GetITStatus 207 * Description : Checks whether the specified RTC interrupt has occurred or not 208 * Input : RTC_IT: specifies the RTC interrupts sources to check 209 * RTC_FLAG_OW: Overflow interrupt 210 * RTC_FLAG_ALR: Alarm interrupt 211 * RTC_FLAG_SEC: Second interrupt 212 * Return : The new state of the RTC_IT (SET or RESET) 213 *********************************************************************************/ RTC_GetITStatus(uint16_t RTC_IT)214ITStatus RTC_GetITStatus(uint16_t RTC_IT) 215 { 216 ITStatus bitstatus = RESET; 217 218 bitstatus = (ITStatus)(RTC->CTLRL & RTC_IT); 219 if (((RTC->CTLRH & RTC_IT) != (uint16_t)RESET) && (bitstatus != (uint16_t)RESET)) 220 { 221 bitstatus = SET; 222 } 223 else 224 { 225 bitstatus = RESET; 226 } 227 return bitstatus; 228 } 229 230 /******************************************************************************** 231 * Function Name : RTC_ClearITPendingBit 232 * Description : Clears the RTC's interrupt pending bits 233 * Input : RTC_IT: specifies the interrupt pending bit to clear 234 * RTC_FLAG_OW: Overflow interrupt 235 * RTC_FLAG_ALR: Alarm interrupt 236 * RTC_FLAG_SEC: Second interrupt 237 * Return : None 238 *********************************************************************************/ RTC_ClearITPendingBit(uint16_t RTC_IT)239void RTC_ClearITPendingBit(uint16_t RTC_IT) 240 { 241 RTC->CTLRL &= (uint16_t)~RTC_IT; 242 } 243 244