1 /**
2  * @file
3  * @brief   Real Time Clock (RTC) functions and prototypes.
4  */
5 
6 /* ****************************************************************************
7  * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
23  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Except as contained in this notice, the name of Maxim Integrated
28  * Products, Inc. shall not be used except as stated in the Maxim Integrated
29  * Products, Inc. Branding Policy.
30  *
31  * The mere transfer of this software does not imply any licenses
32  * of trade secrets, proprietary technology, copyrights, patents,
33  * trademarks, maskwork rights, or any other form of intellectual
34  * property whatsoever. Maxim Integrated Products, Inc. retains all
35  * ownership rights.
36  *
37  *
38  * $Date: 2019-10-07 11:05:30 -0500 (Mon, 07 Oct 2019) $
39  * $Revision: 47429 $
40  *************************************************************************** */
41 
42 /* Define to prevent redundant inclusion */
43 #ifndef _RTC_H_
44 #define _RTC_H_
45 
46 /* **** Includes **** */
47 #include <stdint.h>
48 #include "mxc_config.h"
49 #include "rtc_regs.h"
50 #include "mxc_sys.h"
51 
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55 
56 /**
57  * @defgroup rtc RTC
58  * @ingroup periphlibs
59  * @{
60  */
61 
62 /* **** Definitions **** */
63 
64 typedef enum {
65     SQUARE_WAVE_DISABLED, /**< Sq. wave output disabled */
66     SQUARE_WAVE_ENABLED,  /**< Sq. wave output enabled  */
67 } rtc_sqwave_en_t;
68 
69 typedef enum {
70     F_1HZ   = MXC_S_RTC_CTRL_FT_FREQ1HZ,     /**< 1Hz (Compensated)   */
71     F_512HZ = MXC_S_RTC_CTRL_FT_FREQ512HZ,   /**< 512Hz (Compensated) */
72     F_4KHZ  = MXC_S_RTC_CTRL_FT_FREQ4KHZ,    /**< 4Khz                */
73     F_32KHZ = 32,                          /**< 32Khz               */
74 } rtc_freq_sel_t;
75 
76 typedef enum {
77     NOISE_IMMUNE_MODE        = MXC_S_RTC_CTRL_X32KMD_NOISEIMMUNEMODE,
78     QUIET_MODE               = MXC_S_RTC_CTRL_X32KMD_QUIETMODE,
79     QUIET_STOP_WARMUP_MODE   = MXC_S_RTC_CTRL_X32KMD_QUIETINSTOPWITHWARMUP,
80     QUIET_STOP_NOWARMUP_MODE = MXC_S_RTC_CTRL_X32KMD_QUIETINSTOPNOWARMUP,
81 } rtc_osc_mode_t;
82 
83 /**
84  *@brief     Enables Time-of-Day's Alarm Interrupt
85  *@param     rtc    pointer to the rtc register structure
86  *@return    #E_SUCCESS=pass
87  *@return    #E_BAD_STATE=fail
88  *@return    #E_BUSY=Fail
89  */
90 int RTC_EnableTimeofdayInterrupt(mxc_rtc_regs_t *rtc);
91 
92 /**
93  *@brief     Disable Time-of-Day's Alarm Interrupt
94  *@param     rtc    pointer to the rtc register structure
95  *@return    #E_SUCCESS=pass
96  *@return    #E_BAD_STATE=fail
97  *@return    #E_BUSY=Fail
98  */
99 int RTC_DisableTimeofdayInterrupt(mxc_rtc_regs_t *rtc);
100 
101 /**
102  *@brief     Enables Sub-Second's Alarm Interrupt
103  *@param     rtc    pointer to the rtc register structure
104  *@return    #E_SUCCESS=pass
105  *@return    #E_BAD_STATE=fail
106  *@return    #E_BUSY=Fail
107  */
108 int RTC_EnableSubsecondInterrupt(mxc_rtc_regs_t *rtc);
109 
110 /**
111  *@brief     Disable Sub-Second's Alarm Interrupt
112  *@param     rtc    pointer to the rtc register structure
113  *@return    #E_SUCCESS=pass
114  *@return    #E_BAD_STATE=fail
115  *@return    #E_BUSY=Fail
116  */
117 int RTC_DisableSubsecondInterrupt(mxc_rtc_regs_t *rtc);
118 
119 /**
120  *@brief     Set Time-of-Day alarm value and enable Interrupt
121  *@param     rtc    pointer to the rtc register structure
122  *@param     ras    20-bit value 0-0xFFFFF
123  *@return    #E_SUCCESS=pass
124  *@return    #E_BAD_STATE=fail
125  *@return    #E_BUSY=Fail
126  */
127 int RTC_SetTimeofdayAlarm(mxc_rtc_regs_t *rtc, uint32_t ras);
128 
129 /**
130  *@brief     Set Sub-Second alarm value and enable interrupt,
131  *@brief     this is to be called after the init_rtc() function
132  *@param     rtc    pointer to the rtc register structure
133  *@param     rssa   32-bit value 0-0xFFFFFFFF
134  *@return    #E_SUCCESS=pass
135  *@return    #E_BAD_STATE=fail
136  *@return    #E_BUSY=Fail
137  */
138 int RTC_SetSubsecondAlarm(mxc_rtc_regs_t *rtc, uint32_t rssa);
139 
140 /**
141  *@brief     Enable/Start the Real Time Clock
142  *@param     rtc    pointer to the rtc register structure
143  *@return    #E_SUCCESS=Pass
144  *@return    #E_BUSY=Fail
145  */
146 int RTC_EnableRTCE(mxc_rtc_regs_t *rtc);
147 
148 /**
149  *@brief     Disable/Stop the Real Time Clock
150  *@param     rtc    pointer to the rtc register structure
151  *@return    #E_SUCCESS=Pass
152  *@return    #E_BUSY=Fail
153  */
154 int RTC_DisableRTCE(mxc_rtc_regs_t *rtc);
155 
156 /**
157  * @brief Initialize the sec and ssec registers and enable RTC
158  * @param      rtc pointer to the rtc register structure
159  * @param      sec set the RTC Sec counter (32-bit)
160  * @param      ssec set the RTC Sub-second counter (8-bit)
161  * @param      sys_cfg The system configuration
162  * @return #E_SUCCESS=pass
163  * @return #E_BAD_STATE=fail
164  */
165 int RTC_Init(mxc_rtc_regs_t *rtc, uint32_t sec, uint8_t ssec, sys_cfg_rtc_t *sys_cfg);
166 
167 /**
168  * @brief Allow generation of Square Wave on the SQW pin
169  * @param      rtc pointer to the rtc register structure
170  * @param      sqe Enable/Disable square wave output
171  * @param      ft Frequency output selection
172  * @param      x32kmd 32KHz Oscillator mode
173  * @param      sys_cfg The system configuration
174  * @return #E_SUCCESS=Pass
175  * @return #E_BUSY=Fail
176  */
177 int RTC_SquareWave(mxc_rtc_regs_t *rtc, rtc_sqwave_en_t sqe, rtc_freq_sel_t ft,
178                    rtc_osc_mode_t x32kmd, const sys_cfg_rtc_t* sys_cfg);
179 
180 /**
181  *@brief     Set Trim register value
182  *@param     rtc    pointer to the rtc register structure
183  *@param     trm    set the RTC Trim (8-bit, +/- 127)
184  *@return    #E_SUCCESS=Pass
185  *@return    #E_BUSY=Fail
186  */
187 int RTC_Trim(mxc_rtc_regs_t *rtc, int8_t trm);
188 
189 /**
190  *@brief     Check if BUSY bit is 0.
191  *@return    #E_SUCCESS=Pass
192  *@return    #E_BUSY=Fail
193  */
194 int RTC_CheckBusy(void);
195 
196 /**
197  *@brief     Gets Interrupt flags.
198  *@return    Interrupts flags that have not been cleared
199  */
200 int RTC_GetFlags(void);
201 
202 /**
203  *@brief     Clear Interrupt flag.
204  *@param     flags the flags that need to be cleared
205  */
206 int RTC_ClearFlags(int flags);
207 
208 /**
209  *@brief     Get SubSecond
210  *@return    Returns subsecond value
211  */
212 int RTC_GetSubSecond(void);
213 
214 /**
215  * @brief Get Second
216  * @return returns Second value
217  */
218 int RTC_GetSecond(void);
219 
220 /**
221  * @brief Read seconds, then subseconds, and finally seconds.  If RTC ready flag ever gets cleared during this sequence,
222    the RTC is in the middle of updating the counts and the user should come back later and try again.  If the first
223    read of the seconds register doesn't match the next read, then a subsecond overflow condition has happened and
224    another attempt to read the counts should be made.
225  * @param      sec    variable that will be changed to hold second value
226  * @param      subsec variable that will be changed to hold Subsecond value
227  * @return    #E_NO_ERROR=Pass
228  * @return    #E_BUSY=Fail
229  */
230 int RTC_GetTime(uint32_t* sec, uint32_t* subsec);
231 
232 /**
233  *@brief    Check if RTC is already running
234  */
235 int RTC_IsEnabled(void);
236 
237 #ifdef __cplusplus
238 }
239 #endif
240 /**@} end of group rtc */
241 
242 #endif /* _RTC_H_ */
243