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