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 <stddef.h>
22 #include "rtc.h"
23 #include "rtc_reva.h"
24 #include "mxc_sys.h"
25 #include "mxc_delay.h"
26 #include "mxc_errors.h"
27 
28 // *****************************************************************************
MXC_RTC_SetTimeofdayAlarm(uint32_t ras)29 int MXC_RTC_SetTimeofdayAlarm(uint32_t ras)
30 {
31     return MXC_RTC_RevA_SetTimeofdayAlarm((mxc_rtc_reva_regs_t *)MXC_RTC, ras);
32 }
33 
34 // *****************************************************************************
MXC_RTC_SetSubsecondAlarm(uint32_t rssa)35 int MXC_RTC_SetSubsecondAlarm(uint32_t rssa)
36 {
37     return MXC_RTC_RevA_SetSubsecondAlarm((mxc_rtc_reva_regs_t *)MXC_RTC, rssa);
38 }
39 
40 // *****************************************************************************
MXC_RTC_Start(void)41 int MXC_RTC_Start(void)
42 {
43     return MXC_RTC_RevA_Start((mxc_rtc_reva_regs_t *)MXC_RTC);
44 }
45 
46 // *****************************************************************************
MXC_RTC_Stop(void)47 int MXC_RTC_Stop(void)
48 {
49     return MXC_RTC_RevA_Stop((mxc_rtc_reva_regs_t *)MXC_RTC);
50 }
51 
52 // *****************************************************************************
MXC_RTC_Init(uint32_t sec,uint8_t ssec)53 int MXC_RTC_Init(uint32_t sec, uint8_t ssec)
54 {
55     MXC_SYS_RTCClockEnable();
56     MXC_SYS_Reset_Periph(MXC_SYS_RESET_RTC);
57 
58     return MXC_RTC_RevA_Init((mxc_rtc_reva_regs_t *)MXC_RTC, sec, (ssec & MXC_F_RTC_SSEC_SSEC));
59 }
60 
61 // *****************************************************************************
MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t fq)62 int MXC_RTC_SquareWaveStart(mxc_rtc_freq_sel_t fq)
63 {
64     MXC_GPIO_Config(&gpio_cfg_rtcsqw);
65     return MXC_RTC_RevA_SquareWave((mxc_rtc_reva_regs_t *)MXC_RTC, MXC_RTC_REVA_SQUARE_WAVE_ENABLED,
66                                    fq);
67 }
68 
69 // *****************************************************************************
MXC_RTC_SquareWaveStop(void)70 int MXC_RTC_SquareWaveStop(void)
71 {
72     return MXC_RTC_RevA_SquareWave((mxc_rtc_reva_regs_t *)MXC_RTC,
73                                    MXC_RTC_REVA_SQUARE_WAVE_DISABLED, 0);
74 }
75 
76 // *****************************************************************************
MXC_RTC_EnableInt(uint32_t mask)77 int MXC_RTC_EnableInt(uint32_t mask)
78 {
79     return MXC_RTC_RevA_EnableInt((mxc_rtc_reva_regs_t *)MXC_RTC, mask);
80 }
81 
82 // *****************************************************************************
MXC_RTC_DisableInt(uint32_t mask)83 int MXC_RTC_DisableInt(uint32_t mask)
84 {
85     return MXC_RTC_RevA_DisableInt((mxc_rtc_reva_regs_t *)MXC_RTC, mask);
86 }
87 
88 // *****************************************************************************
MXC_RTC_Trim(int8_t trm)89 int MXC_RTC_Trim(int8_t trm)
90 {
91     /* MAX32650 does not have RTC_TRIM register */
92     return E_NOT_SUPPORTED;
93 }
94 
95 // *****************************************************************************
MXC_RTC_GetFlags(void)96 int MXC_RTC_GetFlags(void)
97 {
98     return MXC_RTC_RevA_GetFlags((mxc_rtc_reva_regs_t *)MXC_RTC);
99 }
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 
107 // *****************************************************************************
MXC_RTC_GetSubSecond(void)108 int MXC_RTC_GetSubSecond(void)
109 {
110     return MXC_RTC_RevA_GetSubSecond((mxc_rtc_reva_regs_t *)MXC_RTC);
111 }
112 
113 // *****************************************************************************
MXC_RTC_GetSecond(void)114 int MXC_RTC_GetSecond(void)
115 {
116     return MXC_RTC_RevA_GetSecond((mxc_rtc_reva_regs_t *)MXC_RTC);
117 }
118 
119 // *****************************************************************************
MXC_RTC_GetSubSeconds(uint32_t * ssec)120 int MXC_RTC_GetSubSeconds(uint32_t *ssec)
121 {
122     // Ensure valid data is in SSEC register
123     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_READY;
124     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_READY)) {}
125 
126     return MXC_RTC_RevA_GetSubSeconds((mxc_rtc_reva_regs_t *)MXC_RTC, ssec);
127 }
128 
129 // *****************************************************************************
MXC_RTC_GetSeconds(uint32_t * sec)130 int MXC_RTC_GetSeconds(uint32_t *sec)
131 {
132     // Ensure valid data is in SEC register
133     MXC_RTC->ctrl &= ~MXC_F_RTC_CTRL_READY;
134     while (!(MXC_RTC->ctrl & MXC_F_RTC_CTRL_READY)) {}
135 
136     return MXC_RTC_RevA_GetSeconds((mxc_rtc_reva_regs_t *)MXC_RTC, sec);
137 }
138 
139 // *****************************************************************************
MXC_RTC_GetTime(uint32_t * sec,uint32_t * subsec)140 int MXC_RTC_GetTime(uint32_t *sec, uint32_t *subsec)
141 {
142     return MXC_RTC_RevA_GetTime((mxc_rtc_reva_regs_t *)MXC_RTC, sec, subsec);
143 }
144 
145 // *****************************************************************************
MXC_RTC_GetBusyFlag(void)146 int MXC_RTC_GetBusyFlag(void)
147 {
148     return MXC_RTC_RevA_GetBusyFlag((mxc_rtc_reva_regs_t *)MXC_RTC);
149 }
150 
151 // *****************************************************************************
MXC_RTC_TrimCrystal(mxc_tmr_regs_t * tmr)152 int MXC_RTC_TrimCrystal(mxc_tmr_regs_t *tmr)
153 {
154     /* MAX32650 does not have RTC_TRIM register */
155     return E_NOT_SUPPORTED;
156 }
157