1 /*!
2  * @file        apm32e10x_rtc.c
3  *
4  * @brief       This file provides all the RTC firmware functions
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-12-31
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2023 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 #include "apm32e10x_rtc.h"
26 
27 /** @addtogroup APM32E10x_StdPeriphDriver
28   @{
29 */
30 
31 /** @addtogroup RTC_Driver
32   * @brief RTC driver modules
33   @{
34 */
35 
36 /** @defgroup RTC_Functions Functions
37   @{
38 */
39 
40 /*!
41  * @brief     Enter RTC configuration mode.
42  *
43  * @param     None
44  *
45  * @retval    None
46  */
RTC_EnableConfigMode(void)47 void RTC_EnableConfigMode(void)
48 {
49     RTC->CSTS_B.CFGMFLG = BIT_SET;
50 }
51 
52 /*!
53  * @brief     Exit RTC configuration mode.
54  *
55  * @param     None
56  *
57  * @retval    None
58  */
RTC_DisableConfigMode(void)59 void RTC_DisableConfigMode(void)
60 {
61     RTC->CSTS_B.CFGMFLG = BIT_RESET;
62 }
63 
64 /*!
65  * @brief     Read the RTC counter value.
66  *
67  * @param     None
68  *
69  * @retval    RTC counter value.
70  */
RTC_ReadCounter(void)71 uint32_t RTC_ReadCounter(void)
72 {
73   uint32_t reg = 0;
74   reg  = (RTC->CNTH_B.CNTH) << 16;
75   reg |= (RTC->CNTL_B.CNTL);
76   return  (reg);
77 }
78 
79 /*!
80  * @brief     Config the RTC counter value.
81  *
82  * @param     value: RTC counter new value.
83  *
84  * @retval    None
85  */
RTC_ConfigCounter(uint32_t value)86 void RTC_ConfigCounter(uint32_t value)
87 {
88     RTC_EnableConfigMode();
89     RTC->CNTH_B.CNTH = value >> 16;
90     RTC->CNTL_B.CNTL = value & 0x0000FFFF;
91     RTC_DisableConfigMode();
92 }
93 
94 /*!
95  * @brief     Config the RTC prescaler value.
96  *
97  * @param     value: RTC prescaler new value.
98  *
99  * @retval    None
100  */
RTC_ConfigPrescaler(uint32_t value)101 void RTC_ConfigPrescaler(uint32_t value)
102 {
103     RTC_EnableConfigMode();
104     RTC->PSCRLDH_B.PSCRLDH = value >> 16;
105     RTC->PSCRLDL_B.PSCRLDL = value & 0x0000FFFF;
106     RTC_DisableConfigMode();
107 }
108 
109 /*!
110  * @brief     Config the RTC alarm value.
111  *
112  * @param     value: RTC alarm new value.
113  *
114  * @retval    None
115  */
RTC_ConfigAlarm(uint32_t value)116 void RTC_ConfigAlarm(uint32_t value)
117 {
118     RTC_EnableConfigMode();
119     RTC->ALRH_B.ALRH = value >> 16;
120     RTC->ALRL_B.ALRL = value & 0x0000FFFF;
121     RTC_DisableConfigMode();
122 }
123 
124 /*!
125  * @brief     Reads the RTC divider value.
126  *
127  * @param     None
128  *
129  * @retval    RTC Divider value.
130  */
RTC_ReadDivider(void)131 uint32_t RTC_ReadDivider(void)
132 {
133   uint32_t reg = 0;
134   reg  = (RTC->PSCH_B.PSCH & 0x000F) << 16 ;
135   reg |= (RTC->PSCL_B.PSCL);
136   return (reg);
137 }
138 
139 /*!
140  * @brief     Waits until last write operation on RTC registers has finished.
141  *
142  * @param     None
143  *
144  * @retval    None
145  */
RTC_WaitForLastTask(void)146 void RTC_WaitForLastTask(void)
147 {
148     while(RTC->CSTS_B.OCFLG == BIT_RESET)
149     {
150     }
151 }
152 
153 /*!
154  * @brief     Waits until the RTC registers
155  *
156  * @param     None
157  *
158  * @retval    None
159  */
RTC_WaitForSynchro(void)160 void RTC_WaitForSynchro(void)
161 {
162     RTC->CSTS_B.RSYNCFLG = BIT_RESET;
163     while(RTC->CSTS_B.RSYNCFLG == BIT_RESET)
164     {
165     }
166 }
167 
168 /*!
169  * @brief     Enable RTC interrupts.
170  *
171  * @param     interrupt: specifies the RTC interrupt sources to be enabled
172  *                  This parameter can be any combination of the following values:
173  *                  @arg RTC_INT_OVR : Overflow interrupt
174  *                  @arg RTC_INT_ALR : Alarm interrupt
175  *                  @arg RTC_INT_SEC : Second interrupt
176  */
RTC_EnableInterrupt(uint16_t interrupt)177 void RTC_EnableInterrupt(uint16_t interrupt)
178 {
179     RTC->CTRL |= interrupt;
180 }
181 
182 /*!
183  * @brief     Disable RTC interrupts.
184  *
185  * @param     interrupt: specifies the RTC interrupt sources to be disabled
186  *                  This parameter can be any combination of the following values:
187  *                  @arg RTC_INT_OVR : Overflow interrupt
188  *                  @arg RTC_INT_ALR : Alarm interrupt
189  *                  @arg RTC_INT_SEC : Second interrupt
190  *
191  * @retval    None
192  */
RTC_DisableInterrupt(uint16_t interrupt)193 void RTC_DisableInterrupt(uint16_t interrupt)
194 {
195     RTC->CTRL &= (uint32_t )~interrupt;
196 }
197 
198 /*!
199  * @brief     Read flag bit
200  *
201  * @param     flag: specifies the flag to check.
202  *                  This parameter can be one of the following values:
203  *                  @arg RTC_FLAG_OC   : RTC Operation Complete flag
204  *                  @arg RTC_FLAG_RSYNC: Registers Synchronized flag
205  *                  @arg RTC_FLAG_OVR  : Overflow flag
206  *                  @arg RTC_FLAG_ALR  : Alarm flag
207  *                  @arg RTC_FLAG_SEC  : Second flag
208  *
209  * @retval    flag bit
210  */
RTC_ReadStatusFlag(RTC_FLAG_T flag)211 uint8_t RTC_ReadStatusFlag(RTC_FLAG_T flag)
212 {
213     return  (RTC->CSTS & flag) ? SET : RESET;
214 }
215 
216 /*!
217  * @brief     Clear flag bit
218  *
219  * @param     flag: specifies the flag to clear.
220  *                  This parameter can be any combination of the following values:
221  *                  @arg RTC_FLAG_OVR : Overflow flag
222  *                  @arg RTC_FLAG_ALR : Alarm flag
223  *                  @arg RTC_FLAG_SEC : Second flag
224  *
225  * @retval    None
226  */
RTC_ClearStatusFlag(uint16_t flag)227 void RTC_ClearStatusFlag(uint16_t flag)
228 {
229     RTC->CSTS &= (uint32_t)~flag;
230 }
231 
232 /*!
233  * @brief     Read interrupt flag bit is set
234  *
235  * @param     flag: specifies the flag to check.
236  *                  This parameter can be any combination of the following values:
237  *                  @arg RTC_INT_OVR : Overflow interrupt
238  *                  @arg RTC_INT_ALR : Alarm interrupt
239  *                  @arg RTC_INT_SEC : Second interrupt
240  *
241  * @retval    None
242  */
RTC_ReadIntFlag(RTC_INT_T flag)243 uint8_t RTC_ReadIntFlag(RTC_INT_T flag)
244 {
245     return (RTC->CSTS & flag) ? SET : RESET;
246 }
247 
248 /*!
249  * @brief     Clear RTC interrupt flag bit
250  *
251  * @param     flag: specifies the flag to clear.
252  *                  This parameter can be one of the following values:
253  *                  @arg RTC_INT_OVR : Overflow interrupt
254  *                  @arg RTC_INT_ALR : Alarm interrupt
255  *                  @arg RTC_INT_SEC : Second interrupt
256  *
257  * @retval    None
258  */
RTC_ClearIntFlag(uint16_t flag)259 void RTC_ClearIntFlag(uint16_t flag)
260 {
261     RTC->CSTS &= (uint32_t)~flag;
262 }
263 
264 /**@} end of group RTC_Functions */
265 /**@} end of group RTC_Driver */
266 /**@} end of group APM32E10x_StdPeriphDriver */
267