1 /**
2  * @file
3  * @brief   Real Time Clock (RTC) functions and prototypes.
4  */
5 
6 /******************************************************************************
7  *
8  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
9  * Analog Devices, Inc.),
10  * Copyright (C) 2023-2024 Analog Devices, Inc.
11  *
12  * Licensed under the Apache License, Version 2.0 (the "License");
13  * you may not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  *     http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  *
24  ******************************************************************************/
25 
26 /* Define to prevent redundant inclusion */
27 #ifndef LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_RTC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_RTC_H_
29 
30 /* **** Includes **** */
31 #include <stdint.h>
32 #include "rtc_regs.h"
33 #include "mxc_sys.h"
34 
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 
39 /**
40  * @defgroup rtc RTC
41  * @ingroup periphlibs
42  * @{
43  */
44 
45 #define MXC_RTC_MAX_SSEC (MXC_F_RTC_SSEC_SSEC + 1)
46 #define MXC_RTC_TRIM_TMR_IRQ MXC_F_TMR_INTR_IRQ
47 
48 /* **** Definitions **** */
49 /**
50  * @brief   Bitmasks for each of the RTC's Frequency.
51  */
52 typedef enum {
53     MXC_RTC_F_1HZ = MXC_S_RTC_CTRL_FREQ_SEL_FREQ1HZ, /**< 1Hz (Compensated)   */
54     MXC_RTC_F_512HZ = MXC_S_RTC_CTRL_FREQ_SEL_FREQ512HZ, /**< 512Hz (Compensated) */
55     MXC_RTC_F_4KHZ = MXC_S_RTC_CTRL_FREQ_SEL_FREQ4KHZ, /**< 4Khz                */
56     MXC_RTC_F_32KHZ = 32, /**< 32Khz               */
57 } mxc_rtc_freq_sel_t;
58 
59 /**
60  * @brief   Bitmasks for each of the RTC's interrupt enables.
61  */
62 typedef enum {
63     MXC_RTC_INT_EN_LONG = MXC_F_RTC_CTRL_TOD_ALARM_EN, ///< Long-interval alarm interrupt enable
64     MXC_RTC_INT_EN_SHORT = MXC_F_RTC_CTRL_SSEC_ALARM_EN, ///< Short-interval alarm interrupt enable
65     MXC_RTC_INT_EN_READY = MXC_F_RTC_CTRL_READY_INT_EN, ///< Timer ready interrupt enable
66 } mxc_rtc_int_en_t;
67 
68 /**
69  * @brief     Bitmasks for each of the RTC's interrupt flags.
70  */
71 typedef enum {
72     MXC_RTC_INT_FL_LONG = MXC_F_RTC_CTRL_TOD_ALARM_FL, ///< Long-interval alarm interrupt flag
73     MXC_RTC_INT_FL_SHORT = MXC_F_RTC_CTRL_SSEC_ALARM_FL, ///< Short-interval alarm interrupt flag
74     MXC_RTC_INT_FL_READY = MXC_F_RTC_CTRL_READY, ///< Timer ready interrupt flag
75 } mxc_rtc_int_fl_t;
76 
77 /**
78  * @brief     Set Time-of-Day alarm value
79  * @param     ras    20-bit value 0-0xFFFFF
80  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
81  */
82 int MXC_RTC_SetTimeofdayAlarm(uint32_t ras);
83 
84 /**
85  * @brief     Set Sub-Second alarm value, this is to be called
86  * @brief     after the init_rtc() function
87  * @param     rssa   32-bit value 0-0xFFFFFFFF
88  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
89  */
90 int MXC_RTC_SetSubsecondAlarm(uint32_t rssa);
91 
92 /**
93  * @brief     Start the Real Time Clock (Blocking function)
94  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
95  */
96 int MXC_RTC_Start(void);
97 
98 /**
99  * @brief     Stop the Real Time Clock (Blocking function)
100  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
101  */
102 int MXC_RTC_Stop(void);
103 
104 /**
105  * @brief     Initialize the sec and ssec registers and enable RTC (Blocking function)
106  * @param     sec set the RTC Sec counter (32-bit)
107  * @param     ssec set the RTC Sub-second counter (8-bit)
108  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
109  */
110 int MXC_RTC_Init(uint32_t sec, uint8_t ssec);
111 
112 /**
113  * @brief     Allow generation of Square Wave on the SQW pin (Blocking function)
114  * @param     fq     Frequency output selection
115  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
116  */
117 int MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t fq);
118 
119 /**
120  * @brief     Stop the generation of square wave (Blocking function)
121  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
122  */
123 int MXC_RTC_SquareWaveStop(void);
124 
125 /**
126  * @brief     Enable Interurpts (Blocking function)
127  * @param     mask   The bitwise OR of interrupts to enable.
128  *                   See #mxc_rtc_int_en_t for available choices.
129  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
130  */
131 int MXC_RTC_EnableInt(uint32_t mask);
132 
133 /**
134  * @brief     Disable Interurpts (Blocking function)
135  * @param     mask   The mask of interrupts to disable.
136  *                   See #mxc_rtc_int_en_t for available choices.
137  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
138  */
139 int MXC_RTC_DisableInt(uint32_t mask);
140 
141 /**
142  * @brief     Gets Interrupt flags.
143  * @return    Interrupts flags that have not been cleared
144  */
145 int MXC_RTC_GetFlags(void);
146 
147 /**
148  * @brief     Clear interrupt flags.
149  * @param     flags the flags to be cleared
150  *            See #mxc_rtc_int_fl_t for the list of possible flags.
151  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
152  */
153 int MXC_RTC_ClearFlags(int flags);
154 
155 /**
156  * @brief     Get SubSecond or E_BUSY, see /ref MXC_ERROR_CODES
157  * @retval    Returns subsecond value
158  */
159 #ifdef __GNUC__
160 __attribute__((deprecated("Use MXC_RTC_GetSubSeconds() instead.")))
161 #endif
162 int MXC_RTC_GetSubSecond(void);
163 
164 /**
165  * @brief     This function stores the current value of the sub-seconds counter into a
166  *            pointer if the RTC is not busy. If the RTC is busy, an error is returned.
167  * @param     ssec   Pointer to the variable to store the current sub-seconds value.
168  * @retval    E_NO_ERROR if successful, otherwise an error code (see /ref MXC_ERROR_CODES).
169  */
170 int MXC_RTC_GetSubSeconds(uint32_t *ssec);
171 
172 /**
173  * @brief     Get Second or E_BUSY, see /ref MXC_ERROR_CODES
174  * @retval    returns second value
175  */
176 #ifdef __GNUC__
177 __attribute__((deprecated("Use MXC_RTC_GetSeconds() instead.")))
178 #endif
179 int MXC_RTC_GetSecond(void);
180 
181 /**
182  * @brief     This function stores the current value of the seconds counter into a
183  *            pointer if the RTC is not busy. If the RTC is busy, an error is returned.
184  * @param     sec   Pointer to the variable to store the current seconds value.
185  * @retval    E_NO_ERROR if successful, otherwise an error code (see /ref MXC_ERROR_CODES).
186  */
187 int MXC_RTC_GetSeconds(uint32_t *sec);
188 
189 /**
190  * @brief     Get the current second and sub-second counts
191  * @param     sec pointer to store seconds value
192  * @param     subsec pointer to store subseconds value
193  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
194  */
195 int MXC_RTC_GetTime(uint32_t *sec, uint32_t *subsec);
196 
197 /**
198  * @brief     Get RTC busy flag.
199  * @retval    returns Success or E_BUSY, see /ref MXC_ERROR_CODES
200  */
201 int MXC_RTC_GetBusyFlag(void);
202 
203 #ifdef __cplusplus
204 }
205 #endif
206 /**@} end of group rtc */
207 
208 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32650_RTC_H_
209