1 /**
2  * @file    rtc.h
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_MAX32660_RTC_H_
28 #define LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_RTC_H_
29 
30 /* **** Includes **** */
31 #include <stdint.h>
32 #include "mxc_device.h"
33 #include "rtc_regs.h"
34 #include "mxc_sys.h"
35 
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * @defgroup rtc Real Time Clock (RTC)
42  * @ingroup periphlibs
43  * @{
44  */
45 
46 #define MXC_RTC_MAX_SSEC (0xFFF + 1) // 0xFFF = max ssec counter value
47 #define MXC_RTC_TRIM_TMR_IRQ MXC_F_TMR_INTR_IRQ
48 
49 /* **** Definitions **** */
50 /**
51  * @brief   Bitmasks for each of the RTC's Frequency.
52  */
53 typedef enum {
54     MXC_RTC_F_1HZ = MXC_S_RTC_CTRL_FT_FREQ1HZ, ///< 1Hz (Compensated)
55     MXC_RTC_F_512HZ = MXC_S_RTC_CTRL_FT_FREQ512HZ, ///< 512Hz (Compensated)
56     MXC_RTC_F_4KHZ = MXC_S_RTC_CTRL_FT_FREQ4KHZ, ///< 4Khz
57     MXC_RTC_F_32KHZ = 32, ///< 32Khz
58 } mxc_rtc_freq_sel_t;
59 
60 /**
61  * @brief   Bitmasks for each of the RTC's interrupt enables.
62  */
63 typedef enum {
64     MXC_RTC_INT_EN_LONG = MXC_F_RTC_CTRL_ADE, ///< Long-interval alarm interrupt enable
65     MXC_RTC_INT_EN_SHORT = MXC_F_RTC_CTRL_ASE, ///< Short-interval alarm interrupt enable
66     MXC_RTC_INT_EN_READY = MXC_F_RTC_CTRL_RDYE, ///< Timer ready interrupt enable
67 } mxc_rtc_int_en_t;
68 
69 /**
70  * @brief     Bitmasks for each of the RTC's interrupt flags.
71  */
72 typedef enum {
73     MXC_RTC_INT_FL_LONG = MXC_F_RTC_CTRL_ALDF, ///< Long-interval alarm interrupt flag
74     MXC_RTC_INT_FL_SHORT = MXC_F_RTC_CTRL_ALSF, ///< Short-interval alarm interrupt flag
75     MXC_RTC_INT_FL_READY = MXC_F_RTC_CTRL_RDY, ///< Timer ready interrupt flag
76 } mxc_rtc_int_fl_t;
77 
78 /**
79  * @brief     Set Time-of-Day alarm value and enable Interrupt
80  * @param     ras    20-bit value 0-0xFFFFF
81  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
82  */
83 int MXC_RTC_SetTimeofdayAlarm(uint32_t ras);
84 
85 /**
86  * @brief     Set Sub-Second alarm value and enable interrupt,
87  * @brief     this is to be called after the init_rtc() function
88  * @param     rssa   32-bit value 0-0xFFFFFFFF
89  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
90  */
91 int MXC_RTC_SetSubsecondAlarm(uint32_t rssa);
92 
93 /**
94  * @brief     Start the Real Time Clock (Blocking function)
95  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
96  */
97 int MXC_RTC_Start(void);
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     Set Trim register value (Blocking function)
127  * @param     trm    set the RTC Trim (8-bit, +/- 127)
128  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
129  */
130 int MXC_RTC_Trim(int8_t trm);
131 
132 /**
133  * @brief     Enable Interurpts (Blocking function)
134  * @param     mask   The bitwise OR of interrupts to enable.
135  *                   See #mxc_rtc_int_en_t for available choices.
136  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
137  */
138 int MXC_RTC_EnableInt(uint32_t mask);
139 
140 /**
141  * @brief     Disable Interurpts (Blocking function)
142  * @param     mask   The mask of interrupts to disable.
143  *                   See #mxc_rtc_int_en_t for available choices.
144  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
145  */
146 int MXC_RTC_DisableInt(uint32_t mask);
147 
148 /**
149  * @brief     Gets interrupt flags.
150  * @retval    The bitwise OR of any interrupts flags that are
151  *            currently set. See \ref mxc_rtc_int_fl_t for the list
152  *            of possible flags.
153  */
154 int MXC_RTC_GetFlags(void);
155 
156 /**
157  * @brief     Clear interrupt flags.
158  * @param     flags The bitwise OR of the interrupts flags to cleear.
159  *            See #mxc_rtc_int_fl_t for the list of possible flags.
160  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
161  */
162 int MXC_RTC_ClearFlags(int flags);
163 
164 /**
165  * @brief     Get SubSecond or E_BUSY, see /ref MXC_ERROR_CODES
166  * @retval    Returns subsecond value
167  */
168 #ifdef __GNUC__
169 __attribute__((deprecated("Use MXC_RTC_GetSubSeconds() instead.")))
170 #endif
171 int MXC_RTC_GetSubSecond(void);
172 
173 /**
174  * @brief     This function stores the current value of the sub-seconds counter into a
175  *            pointer if the RTC is not busy. If the RTC is busy, an error is returned.
176  * @param     ssec   Pointer to the variable to store the current sub-seconds value.
177  * @retval    E_NO_ERROR if successful, otherwise an error code (see /ref MXC_ERROR_CODES).
178  */
179 int MXC_RTC_GetSubSeconds(uint32_t *ssec);
180 
181 /**
182  * @brief     Get Second or E_BUSY, see /ref MXC_ERROR_CODES
183  * @retval    returns second value
184  */
185 #ifdef __GNUC__
186 __attribute__((deprecated("Use MXC_RTC_GetSeconds() instead.")))
187 #endif
188 int MXC_RTC_GetSecond(void);
189 
190 /**
191  * @brief     This function stores the current value of the seconds counter into a
192  *            pointer if the RTC is not busy. If the RTC is busy, an error is returned.
193  * @param     sec   Pointer to the variable to store the current seconds value.
194  * @retval    E_NO_ERROR if successful, otherwise an error code (see /ref MXC_ERROR_CODES).
195  */
196 int MXC_RTC_GetSeconds(uint32_t *sec);
197 
198 /**
199  * @brief     Get the current second and sub-second counts.
200  * @param     sec pointer to store seconds value
201  * @param     subsec pointer to store subseconds value
202  * @retval    returns Success or Fail, see \ref MXC_Error_Codes
203  */
204 int MXC_RTC_GetTime(uint32_t *sec, uint32_t *subsec);
205 
206 /**
207  * @brief     Get RTC busy flag.
208  * @retval    returns Success or E_BUSY, see /ref MXC_ERROR_CODES
209  */
210 int MXC_RTC_GetBusyFlag(void);
211 
212 /**@} end of group rtc */
213 #ifdef __cplusplus
214 }
215 #endif
216 
217 #endif // LIBRARIES_PERIPHDRIVERS_INCLUDE_MAX32660_RTC_H_
218