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.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************************/
20 
21 #include "mxc_device.h"
22 #include "rtc_regs.h"
23 #include "rtc.h"
24 #include "mxc_sys.h"
25 #include "mxc_pins.h"
26 #include "mxc_delay.h"
27 #include "gpio_regs.h"
28 #include "mxc_errors.h"
29 #include "rtc_reva.h"
30 
31 /* ***** Functions ***** */
32 
MXC_RTC_EnableInt(uint32_t mask)33 int MXC_RTC_EnableInt(uint32_t mask)
34 {
35     return MXC_RTC_RevA_EnableInt((mxc_rtc_reva_regs_t *)MXC_RTC, mask);
36 }
37 
MXC_RTC_DisableInt(uint32_t mask)38 int MXC_RTC_DisableInt(uint32_t mask)
39 {
40     return MXC_RTC_RevA_DisableInt((mxc_rtc_reva_regs_t *)MXC_RTC, mask);
41 }
42 
MXC_RTC_SetTimeofdayAlarm(uint32_t ras)43 int MXC_RTC_SetTimeofdayAlarm(uint32_t ras)
44 {
45     return MXC_RTC_RevA_SetTimeofdayAlarm((mxc_rtc_reva_regs_t *)MXC_RTC, ras);
46 }
47 
MXC_RTC_SetSubsecondAlarm(uint32_t rssa)48 int MXC_RTC_SetSubsecondAlarm(uint32_t rssa)
49 {
50     return MXC_RTC_RevA_SetSubsecondAlarm((mxc_rtc_reva_regs_t *)MXC_RTC, rssa);
51 }
52 
MXC_RTC_Start(void)53 int MXC_RTC_Start(void)
54 {
55     return MXC_RTC_RevA_Start((mxc_rtc_reva_regs_t *)MXC_RTC);
56 }
57 
MXC_RTC_Stop(void)58 int MXC_RTC_Stop(void)
59 {
60     return MXC_RTC_RevA_Stop((mxc_rtc_reva_regs_t *)MXC_RTC);
61 }
62 
MXC_RTC_Init(uint32_t sec,uint8_t ssec)63 int MXC_RTC_Init(uint32_t sec, uint8_t ssec)
64 {
65     // Enable clock
66     MXC_GCR->clk_ctrl |= MXC_F_GCR_CLK_CTRL_X32K_EN;
67 
68     return MXC_RTC_RevA_Init((mxc_rtc_reva_regs_t *)MXC_RTC, sec, (ssec & MXC_F_RTC_SSEC_RTSS));
69 }
70 
MXC_RTC_SquareWave(mxc_rtc_reva_sqwave_en_t sqe,mxc_rtc_freq_sel_t ft)71 int MXC_RTC_SquareWave(mxc_rtc_reva_sqwave_en_t sqe, mxc_rtc_freq_sel_t ft)
72 {
73     MXC_GPIO_Config(&gpio_cfg_32kcal);
74 
75     return MXC_RTC_RevA_SquareWave((mxc_rtc_reva_regs_t *)MXC_RTC, sqe, ft);
76 }
77 
MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t fq)78 int MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t fq)
79 {
80     MXC_GPIO_Config(&gpio_cfg_32kcal);
81 
82     return MXC_RTC_RevA_SquareWave((mxc_rtc_reva_regs_t *)MXC_RTC, MXC_RTC_REVA_SQUARE_WAVE_ENABLED,
83                                    fq);
84 }
85 
MXC_RTC_SquareWaveStop(void)86 int MXC_RTC_SquareWaveStop(void)
87 {
88     return MXC_RTC_RevA_SquareWave((mxc_rtc_reva_regs_t *)MXC_RTC,
89                                    MXC_RTC_REVA_SQUARE_WAVE_DISABLED, 0);
90 }
91 
MXC_RTC_Trim(int8_t trm)92 int MXC_RTC_Trim(int8_t trm)
93 {
94     return MXC_RTC_RevA_Trim((mxc_rtc_reva_regs_t *)MXC_RTC, trm);
95 }
96 
MXC_RTC_GetFlags(void)97 int MXC_RTC_GetFlags(void)
98 {
99     return MXC_RTC_RevA_GetFlags((mxc_rtc_reva_regs_t *)MXC_RTC);
100 }
101 
MXC_RTC_ClearFlags(int flags)102 int MXC_RTC_ClearFlags(int flags)
103 {
104     return MXC_RTC_RevA_ClearFlags((mxc_rtc_reva_regs_t *)MXC_RTC, flags);
105 }
106 
MXC_RTC_GetSubSecond(void)107 int MXC_RTC_GetSubSecond(void)
108 {
109     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_RDY; // Ensure valid data is in SSEC register
110     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {}
111 
112     return MXC_RTC_RevA_GetSubSecond((mxc_rtc_reva_regs_t *)MXC_RTC);
113 }
114 
MXC_RTC_GetSecond(void)115 int MXC_RTC_GetSecond(void)
116 {
117     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_RDY; // Ensure valid data is in SEC register
118     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {}
119 
120     return MXC_RTC_RevA_GetSecond((mxc_rtc_reva_regs_t *)MXC_RTC);
121 }
122 
MXC_RTC_GetSubSeconds(uint32_t * ssec)123 int MXC_RTC_GetSubSeconds(uint32_t *ssec)
124 {
125     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_RDY; // Ensure valid data is in SSEC register
126     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {}
127 
128     return MXC_RTC_RevA_GetSubSeconds((mxc_rtc_reva_regs_t *)MXC_RTC, ssec);
129 }
130 
MXC_RTC_GetSeconds(uint32_t * sec)131 int MXC_RTC_GetSeconds(uint32_t *sec)
132 {
133     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_RDY; // Ensure valid data is in SEC register
134     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_RDY)) {}
135 
136     return MXC_RTC_RevA_GetSeconds((mxc_rtc_reva_regs_t *)MXC_RTC, sec);
137 }
138 
MXC_RTC_GetTime(uint32_t * sec,uint32_t * subsec)139 int MXC_RTC_GetTime(uint32_t *sec, uint32_t *subsec)
140 {
141     return MXC_RTC_RevA_GetTime((mxc_rtc_reva_regs_t *)MXC_RTC, sec, subsec);
142 }
143 
MXC_RTC_GetBusyFlag(void)144 int MXC_RTC_GetBusyFlag(void)
145 {
146     return MXC_RTC_RevA_GetBusyFlag((mxc_rtc_reva_regs_t *)MXC_RTC);
147 }
148 
MXC_RTC_TrimCrystal(mxc_tmr_regs_t * tmr)149 int MXC_RTC_TrimCrystal(mxc_tmr_regs_t *tmr)
150 {
151     /* MAX32660 does not have a clock source which can
152     be used as the reference clock for the trim function */
153     return E_NOT_SUPPORTED;
154 }
155