1 /******************************************************************************
2  *
3  * Copyright (C) 2022-2023 Maxim Integrated Products, Inc. (now owned by
4  * Analog Devices, Inc.),
5  * Copyright (C) 2023-2024 Analog Devices, Inc. All Rights Reserved. This software
6  * is proprietary to Analog Devices, Inc. and its licensors.
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  ******************************************************************************/
21 
22 #include "mxc_device.h"
23 #include "rtc_regs.h"
24 #include "rtc.h"
25 #include "mxc_sys.h"
26 #include "mxc_delay.h"
27 #include "gpio_regs.h"
28 #include "mxc_errors.h"
29 #include "mcr_regs.h"
30 #include "rtc_reva.h"
31 
32 /* ***** Functions ***** */
33 
MXC_RTC_EnableInt(uint32_t mask)34 int MXC_RTC_EnableInt(uint32_t mask)
35 {
36     return MXC_RTC_RevA_EnableInt((mxc_rtc_reva_regs_t *)MXC_RTC, mask);
37 }
38 
MXC_RTC_DisableInt(uint32_t mask)39 int MXC_RTC_DisableInt(uint32_t mask)
40 {
41     return MXC_RTC_RevA_DisableInt((mxc_rtc_reva_regs_t *)MXC_RTC, mask);
42 }
43 
MXC_RTC_SetTimeofdayAlarm(uint32_t ras)44 int MXC_RTC_SetTimeofdayAlarm(uint32_t ras)
45 {
46     return MXC_RTC_RevA_SetTimeofdayAlarm((mxc_rtc_reva_regs_t *)MXC_RTC, ras);
47 }
48 
MXC_RTC_SetSubsecondAlarm(uint32_t rssa)49 int MXC_RTC_SetSubsecondAlarm(uint32_t rssa)
50 {
51     return MXC_RTC_RevA_SetSubsecondAlarm((mxc_rtc_reva_regs_t *)MXC_RTC, rssa);
52 }
53 
MXC_RTC_Start(void)54 int MXC_RTC_Start(void)
55 {
56     return MXC_RTC_RevA_Start((mxc_rtc_reva_regs_t *)MXC_RTC);
57 }
58 
MXC_RTC_Stop(void)59 int MXC_RTC_Stop(void)
60 {
61     return MXC_RTC_RevA_Stop((mxc_rtc_reva_regs_t *)MXC_RTC);
62 }
63 
MXC_RTC_Init(uint32_t sec,uint16_t ssec)64 int MXC_RTC_Init(uint32_t sec, uint16_t ssec)
65 {
66     MXC_MCR->ctrl |= MXC_F_MCR_CLKCTRL_ERTCO_EN;
67     MXC_MCR->clkctrl &= ~(MXC_F_MCR_CLKCTRL_ERTCO_PD);
68 
69     return MXC_RTC_RevA_Init((mxc_rtc_reva_regs_t *)MXC_RTC, sec, (ssec & MXC_F_RTC_SSEC_SSEC));
70 }
71 
MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t ft)72 int MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t ft)
73 {
74     MXC_GPIO_Config(&gpio_cfg_rtcsqw);
75 
76     return MXC_RTC_RevA_SquareWave((mxc_rtc_reva_regs_t *)MXC_RTC, MXC_RTC_REVA_SQUARE_WAVE_ENABLED,
77                                    ft);
78 }
79 
MXC_RTC_SquareWaveStop(void)80 int MXC_RTC_SquareWaveStop(void)
81 {
82     return MXC_RTC_RevA_SquareWave((mxc_rtc_reva_regs_t *)MXC_RTC,
83                                    MXC_RTC_REVA_SQUARE_WAVE_DISABLED, MXC_RTC_F_32KHZ);
84 }
85 
MXC_RTC_Trim(int8_t trm)86 int MXC_RTC_Trim(int8_t trm)
87 {
88     return MXC_RTC_RevA_Trim((mxc_rtc_reva_regs_t *)MXC_RTC, trm);
89 }
90 
MXC_RTC_GetFlags(void)91 int MXC_RTC_GetFlags(void)
92 {
93     return MXC_RTC_RevA_GetFlags((mxc_rtc_reva_regs_t *)MXC_RTC);
94 }
95 
MXC_RTC_ClearFlags(int flags)96 int MXC_RTC_ClearFlags(int flags)
97 {
98     return MXC_RTC_RevA_ClearFlags((mxc_rtc_reva_regs_t *)MXC_RTC, flags);
99 }
100 
MXC_RTC_GetSubSecond(void)101 int MXC_RTC_GetSubSecond(void)
102 {
103     return MXC_RTC_RevA_GetSubSecond((mxc_rtc_reva_regs_t *)MXC_RTC);
104 }
105 
MXC_RTC_GetSecond(void)106 int MXC_RTC_GetSecond(void)
107 {
108     return MXC_RTC_RevA_GetSecond((mxc_rtc_reva_regs_t *)MXC_RTC);
109 }
110 
MXC_RTC_GetSubSeconds(uint32_t * ssec)111 int MXC_RTC_GetSubSeconds(uint32_t *ssec)
112 {
113     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_RDY; // Ensure valid data is in SSEC register
114     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {}
115 
116     return MXC_RTC_RevA_GetSubSeconds((mxc_rtc_reva_regs_t *)MXC_RTC, ssec);
117 }
118 
MXC_RTC_GetSeconds(uint32_t * sec)119 int MXC_RTC_GetSeconds(uint32_t *sec)
120 {
121     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_RDY; // Ensure valid data is in SEC register
122     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {}
123 
124     return MXC_RTC_RevA_GetSeconds((mxc_rtc_reva_regs_t *)MXC_RTC, sec);
125 }
126 
MXC_RTC_GetTime(uint32_t * sec,uint32_t * subsec)127 int MXC_RTC_GetTime(uint32_t *sec, uint32_t *subsec)
128 {
129     return MXC_RTC_RevA_GetTime((mxc_rtc_reva_regs_t *)MXC_RTC, sec, subsec);
130 }
131 
MXC_RTC_GetBusyFlag(void)132 int MXC_RTC_GetBusyFlag(void)
133 {
134     return MXC_RTC_RevA_GetBusyFlag((mxc_rtc_reva_regs_t *)MXC_RTC);
135 }
136 
MXC_RTC_TrimCrystal(mxc_tmr_regs_t * tmr)137 int MXC_RTC_TrimCrystal(mxc_tmr_regs_t *tmr)
138 {
139     if (MXC_TMR_GET_IDX(tmr) < 0 ||
140         MXC_TMR_GET_IDX(tmr) > 3) { // Timer must support ERFO as clock source
141         return E_BAD_PARAM;
142     }
143 
144     mxc_tmr_cfg_t
145         tmr_cfg; // Configure timer to trigger each interrupt NUM_PERIOD number of times within a second
146     tmr_cfg.pres = MXC_TMR_PRES_1;
147     tmr_cfg.mode = MXC_TMR_MODE_CONTINUOUS;
148     tmr_cfg.cmp_cnt = ERFO_FREQ / MXC_RTC_REVA_TRIM_PERIODS;
149     tmr_cfg.pol = 0;
150     MXC_TMR_Init(tmr, &tmr_cfg);
151 
152     return MXC_RTC_RevA_TrimCrystal((mxc_rtc_reva_regs_t *)MXC_RTC, tmr);
153 }
154