1 /*
2  * Copyright 2021 MindMotion Microelectronics Co., Ltd.
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef __HAL_RTC_H__
9 #define __HAL_RTC_H__
10 
11 #include "hal_common.h"
12 #include "hal_rcc.h"
13 
14 /*!
15  * @addtogroup RTC
16  * @{
17  */
18 
19 /*!
20  * @addtogroup RTC_STATUS
21  * @{
22  */
23 #define RTC_STATUS_OPERATION           RTC_CRL_RTOFF_MASK    /*!< Status flag when write operation of RTC register is completed. */
24 #define RTC_STATUS_SYNC                RTC_CRL_RSF_MASK      /*!< Status flag when registers synchronized. */
25 #define RTC_STATUS_CLKRDY              RCC_BDCR_LSERDY_MASK  /*!< Status flag when the clock of LSE is ready. */
26 /*!
27  * @}
28  */
29 
30 /*!
31  * @addtogroup RTC_INT
32  * @{
33  */
34 #define RTC_INT_SEC                   RTC_CRL_SECF_MASK     /*!< Interrupt enable when seconds interrupt generation or overflow of prescaler. After the flag is generated, RTC counter + 1. */
35 #define RTC_INT_ALARM                 RTC_CRL_ALRF_MASK     /*!< Interrupt enable when the alarm interrupt is generated, in other words, the value of the alarm counter is the same as the RTC counter. */
36 #define RTC_INT_OVERFLOW              RTC_CRL_OWF_MASK      /*!< Interrupt enable when overflow interrupt generaton or programmable counter overflow. */
37 /*!
38  * @}
39  */
40 
41 /*!
42  * @brief Define the callback function called when the RTC interrupt is done.
43  */
44 typedef void (*RTC_Callback_1_Type)(void *param);
45 
46 /*!
47  * @brief This type of structure instance is used to set and get time when set time and get current time.
48  */
49 typedef struct
50 {
51     uint8_t    Secs;           /*!< Specify the clock seconds. */
52     uint8_t    Mins;           /*!< Specify the clock minutes. */
53     uint8_t    Hours;          /*!< Specify the clock hours. */
54     uint8_t    Days;           /*!< Specify the clock days. */
55     uint8_t    Months;         /*!< Specify the clock months. */
56     uint16_t   Years;          /*!< Specify the clock years. */
57     uint16_t   HYears;         /*!< Specify term of highest years. */
58     uint16_t   LYears;         /*!< Specify term of lowest years. */
59     uint32_t   Div;            /*!< Specify the number of prescaler. */
60 } RTC_Init_Type;
61 
62 typedef struct
63 {
64     uint32_t   WaitTime;       /*!< Specify the time limit for wait to the flag to be generated. If the required flag is not generated after this time, considered as timeout. */
65     uint32_t   AlarmTime;      /*!< Specify the number of seconds that alarm clock will respond. */
66     RTC_Callback_1_Type SecDoneCallback;  /*!< Callback function, called when the rtc seconds interrupt is done. */
67     RTC_Callback_1_Type AlarmDoneCallback;   /*!< Callback function, called when the rtc alarm interrupt is done. */
68 } RTC_Time_Type;
69 
70 /*!
71  * @brief Initialize the RTC module.
72  *
73  * Open access to BKP, enable clock source and RTC.
74  * RTC starts counting after initialization.
75  *
76  * @return None.
77  */
78 void RTC_Init(void);
79 
80 /*!
81  * @brief Enable the BKP access module.
82  *
83  * @param enable 'true' to enable the access to RTC and BKP, 'false' to disable the access to RTC and BKP.
84  * @return None.
85  */
86 void RTC_EnableAccess(bool enable);
87 
88 /*!
89  * @brief Enable reset of the RTC module.
90  *
91  * The register of BKP is reset by the backup domain and is not reset during power reset or system reset.
92  *
93  * @param enable 'true' to enable the BKP reset, 'false' to disable the BKP reset.
94  * @return None.
95  */
96 void RTC_EnableReset(bool enable);
97 
98 /*!
99  * @brief Enable the RTC moudle.
100  *
101  * @param enable 'true' to enable the module, 'false' to disable the module.
102  * @return None.
103  */
104 void RTC_Enable(bool enable);
105 
106 /*!
107  * @brief Get the current status flags of the RTC module.
108  *
109  * @return Status flags. See to @ref RTC_STATUS.
110  */
111 uint32_t RTC_GetStatus(void);
112 
113 /*!
114  * @brief Clear the status flags of the RTC module.
115  *
116  * @param status The mask code of the indicated flags to be clear. See to @ref RTC_STATUS.
117  * @return None.
118  */
119 void RTC_ClearStatus(uint32_t status);
120 
121 /*!
122  * @brief Get the current clock source status flags of RTC module.
123  *
124  * @return Status flags. See to @ref RTC_STATUS.
125  */
126 uint32_t RTC_GetClockStatus(void);
127 
128 /*!
129  * @brief Enable the configration of the RTC module.
130  *
131  * @param enable 'true' to enable the configration module, 'false' to disable the configration module.
132  * @return None.
133  */
134 void RTC_EnableConf(bool enable);
135 
136 /*!
137  * @brief Put the data into prescaler register of the RTC module.
138  *
139  * @param div Data value to be send into the prescaler register.
140  * @return None.
141  */
142 void RTC_PutPrescalerData(uint32_t div);
143 
144 /*!
145  * @brief Put the data into counter register of the RTC module.
146  *
147  * @param cnt Data value to be send into the counter register.
148  * @return None.
149  */
150 void RTC_PutCounterData(uint32_t cnt);
151 
152 /*!
153  * @brief Put the data into alarm register of the RTC module.
154  *
155  * @param alarm Data value to be send into the alarm register.
156  * @return None.
157  */
158 void RTC_PutAlarmData(uint32_t alarm);
159 
160 /*!
161  * @brief Get the data from counter register of the RTC module.
162  *
163  * @return The data value from counter.
164  */
165 uint32_t RTC_GetCounterData(void);
166 
167 /*!
168  * @brief Get the data from alarm register of the RTC module.
169  *
170  * @return The data value from alarm register.
171  */
172 uint32_t RTC_GetAlarmData(void);
173 
174 /*!
175  * @brief Enabel interrupt of the RTC module.
176  *
177  * @param interrupts Interrupt code masks. See to @ref RTC_INT.
178  * @param enable 'true' to enable the indicated interrupts, 'false' to disable the indicated interrupts.
179  * @return None.
180  */
181 void RTC_EnableInterrupts(uint32_t interrupts, bool enable);
182 
183 /*!
184  * @brief Get the interrupts status flags of the RTC module.
185  *
186  * @return Interrupt status flags. See to @ref RTC_INT.
187  */
188 uint32_t RTC_GetInterruptStatus(void);
189 
190 /*!
191  * @brief Clear the interrupts status flags of the RTC module.
192  *
193  * @param interrupts The mask codes of the indicated interrupt flags to be cleared. See to @ref RTC_INT.
194  * @return None.
195  */
196 void RTC_ClearInterruptStatus(uint32_t interrupts);
197 
198 /*!
199  * @brief Read the current enabled interrupts the RTC module.
200  *
201  * @return The mask codes enabled interrupts. See to @ref RTC_INT.
202  */
203 uint32_t RTC_GetEnabledInterrupts(void);
204 
205 /*!
206  * @brief Setup the initialization time of the RTC module.
207  *
208  * @param init Pointer to the initialization structure. See to @ref RTC_Init_Type.
209  * @param time Pointer to the time structure. See to @ref RTC_Time_Type.
210  * @return 'true' to set time succeess, 'false' to set time failed.
211  */
212 bool RTC_SetTimeBlocking(RTC_Init_Type * init, RTC_Time_Type * time);
213 
214 /*!
215  * @brief Calculate and get current time.
216  *
217  * @param init Pointer to the initialization structure. See to @ref RTC_Init_Type.
218  * @param time Pointer to the time structure. See to @ref RTC_Time_Type.
219  * @return None.
220  */
221 void RTC_CalcTimeBlocking(RTC_Init_Type * init, RTC_Time_Type * time);
222 
223 /*!
224  * @brief Set alarm time of the RTC module.
225  *
226  * @param time Pointer to the time structure. See to @ref RTC_Time_Type.
227  * @return 'true' to set alarm time succeess, 'false' to set alarm time failed.
228  */
229 bool RTC_SetAlarmBlocking(RTC_Time_Type * time);
230 
231 /*!
232  * @brief RTC interrupt handler.
233  *
234  * @param init Pointer to the RTC current time. See to @ref RTC_Init_Type.
235  * @param time Pointer to the RTC interrupt done and interrupt count. See to @ref RTC_Time_Type.
236  * @param interrupts Interrupt status flags. See to @ref RTC_INT.
237  * @return None.
238  */
239 void RTC_TimeHandler(RTC_Init_Type * init, RTC_Time_Type * time, uint32_t interrupts);
240 
241 /*!
242  *@}
243  */
244 
245 #endif /* __HAL_RTC_H__ */
246 
247