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 /* **** Includes **** */
22 #include <string.h>
23 #include "mxc_assert.h"
24 #include "mxc_sys.h"
25 #include "mxc_errors.h"
26 #include "gpio.h"
27 #include "owm.h"
28 #include "owm_reva.h"
29 
30 /* **** Definitions **** */
31 #define MXC_OWM_CLK_FREQ 1000000 //1-Wire requires 1MHz clock
32 
33 /* **** Globals **** */
34 
35 /* **** Functions **** */
36 
37 /* ************************************************************************* */
MXC_OWM_Init(const mxc_owm_cfg_t * cfg)38 int MXC_OWM_Init(const mxc_owm_cfg_t *cfg)
39 {
40     uint32_t mxc_owm_clk, clk_div = 0;
41 
42     if (cfg == NULL) {
43         return E_NULL_PTR;
44     }
45 
46 #ifndef MSDK_NO_GPIO_CLK_INIT
47     // Set system level configurations
48     MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_OWIRE);
49     MXC_GPIO_Config(&gpio_cfg_owm);
50 #endif
51 
52     // Configure clk divisor to get 1MHz OWM clk
53     mxc_owm_clk = PeripheralClock;
54 
55     if (mxc_owm_clk == 0) {
56         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_OWIRE);
57         return E_UNINITIALIZED;
58     }
59 
60     // Return error if clk doesn't divide evenly to 1MHz
61     if (mxc_owm_clk % MXC_OWM_CLK_FREQ) {
62         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_OWIRE);
63         return E_NOT_SUPPORTED;
64     }
65 
66     clk_div = (mxc_owm_clk / (MXC_OWM_CLK_FREQ));
67 
68     // Can not support lower frequencies
69     if (clk_div == 0) {
70         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_OWIRE);
71         return E_NOT_SUPPORTED;
72     }
73 
74     return MXC_OWM_RevA_Init((mxc_owm_reva_regs_t *)MXC_OWM, cfg);
75 }
76 
77 /* ************************************************************************* */
MXC_OWM_Shutdown(void)78 void MXC_OWM_Shutdown(void)
79 {
80     MXC_OWM_RevA_Shutdown((mxc_owm_reva_regs_t *)MXC_OWM);
81     MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_OWIRE);
82 }
83 
84 /* ************************************************************************* */
MXC_OWM_Reset(void)85 int MXC_OWM_Reset(void)
86 {
87     return MXC_OWM_RevA_Reset((mxc_owm_reva_regs_t *)MXC_OWM);
88 }
89 
90 /* ************************************************************************* */
MXC_OWM_GetPresenceDetect(void)91 int MXC_OWM_GetPresenceDetect(void)
92 {
93     return (!!(MXC_OWM->ctrl_stat & MXC_F_OWM_CTRL_STAT_PRESENCE_DETECT));
94 }
95 
96 /* ************************************************************************* */
MXC_OWM_TouchByte(uint8_t data)97 int MXC_OWM_TouchByte(uint8_t data)
98 {
99     return MXC_OWM_RevA_TouchByte((mxc_owm_reva_regs_t *)MXC_OWM, data);
100 }
101 
102 /* ************************************************************************* */
MXC_OWM_WriteByte(uint8_t data)103 int MXC_OWM_WriteByte(uint8_t data)
104 {
105     return MXC_OWM_RevA_WriteByte(data);
106 }
107 
108 /* ************************************************************************* */
MXC_OWM_ReadByte(void)109 int MXC_OWM_ReadByte(void)
110 {
111     return MXC_OWM_RevA_ReadByte();
112 }
113 
114 /* ************************************************************************* */
MXC_OWM_TouchBit(uint8_t bit)115 int MXC_OWM_TouchBit(uint8_t bit)
116 {
117     return MXC_OWM_RevA_TouchBit((mxc_owm_reva_regs_t *)MXC_OWM, bit);
118 }
119 
120 /* ************************************************************************* */
MXC_OWM_WriteBit(uint8_t bit)121 int MXC_OWM_WriteBit(uint8_t bit)
122 {
123     return MXC_OWM_RevA_WriteBit(bit);
124 }
125 
126 /* ************************************************************************* */
MXC_OWM_ReadBit(void)127 int MXC_OWM_ReadBit(void)
128 {
129     return MXC_OWM_RevA_ReadBit();
130 }
131 
132 /* ************************************************************************* */
MXC_OWM_Write(uint8_t * data,int len)133 int MXC_OWM_Write(uint8_t *data, int len)
134 {
135     return MXC_OWM_RevA_Write((mxc_owm_reva_regs_t *)MXC_OWM, data, len);
136 }
137 
138 /* ************************************************************************* */
MXC_OWM_Read(uint8_t * data,int len)139 int MXC_OWM_Read(uint8_t *data, int len)
140 {
141     return MXC_OWM_RevA_Read((mxc_owm_reva_regs_t *)MXC_OWM, data, len);
142 }
143 
144 /* ************************************************************************* */
MXC_OWM_ReadROM(uint8_t * ROMCode)145 int MXC_OWM_ReadROM(uint8_t *ROMCode)
146 {
147     return MXC_OWM_RevA_ReadROM(ROMCode);
148 }
149 
150 /* ************************************************************************* */
MXC_OWM_MatchROM(uint8_t * ROMCode)151 int MXC_OWM_MatchROM(uint8_t *ROMCode)
152 {
153     return MXC_OWM_RevA_MatchROM(ROMCode);
154 }
155 
156 /* ************************************************************************* */
MXC_OWM_ODMatchROM(uint8_t * ROMCode)157 int MXC_OWM_ODMatchROM(uint8_t *ROMCode)
158 {
159     return MXC_OWM_RevA_ODMatchROM((mxc_owm_reva_regs_t *)MXC_OWM, ROMCode);
160 }
161 
162 /* ************************************************************************* */
MXC_OWM_SkipROM(void)163 int MXC_OWM_SkipROM(void)
164 {
165     return MXC_OWM_RevA_SkipROM();
166 }
167 
168 /* ************************************************************************* */
MXC_OWM_ODSkipROM(void)169 int MXC_OWM_ODSkipROM(void)
170 {
171     return MXC_OWM_RevA_ODSkipROM((mxc_owm_reva_regs_t *)MXC_OWM);
172 }
173 
174 /* ************************************************************************* */
MXC_OWM_Resume(void)175 int MXC_OWM_Resume(void)
176 {
177     return MXC_OWM_RevA_Resume();
178 }
179 
180 /* ************************************************************************* */
MXC_OWM_SearchROM(int newSearch,uint8_t * ROMCode)181 int MXC_OWM_SearchROM(int newSearch, uint8_t *ROMCode)
182 {
183     return MXC_OWM_RevA_SearchROM((mxc_owm_reva_regs_t *)MXC_OWM, newSearch, ROMCode);
184 }
185 
186 /* ************************************************************************* */
MXC_OWM_ClearFlags(uint32_t mask)187 void MXC_OWM_ClearFlags(uint32_t mask)
188 {
189     MXC_OWM_RevA_ClearFlags((mxc_owm_reva_regs_t *)MXC_OWM, mask);
190 }
191 
192 /* ************************************************************************* */
MXC_OWM_GetFlags(void)193 unsigned MXC_OWM_GetFlags(void)
194 {
195     return MXC_OWM_RevA_GetFlags((mxc_owm_reva_regs_t *)MXC_OWM);
196 }
197 
198 /* ************************************************************************* */
MXC_OWM_SetExtPullup(int enable)199 void MXC_OWM_SetExtPullup(int enable)
200 {
201     MXC_OWM_RevA_SetExtPullup((mxc_owm_reva_regs_t *)MXC_OWM, enable);
202 }
203 
204 /* ************************************************************************* */
MXC_OWM_SetOverdrive(int enable)205 void MXC_OWM_SetOverdrive(int enable)
206 {
207     MXC_OWM_RevA_SetOverdrive((mxc_owm_reva_regs_t *)MXC_OWM, enable);
208 }
209 
210 /* ************************************************************************* */
MXC_OWM_EnableInt(int flags)211 void MXC_OWM_EnableInt(int flags)
212 {
213     MXC_OWM_RevA_EnableInt((mxc_owm_reva_regs_t *)MXC_OWM, flags);
214 }
215 
216 /* ************************************************************************* */
MXC_OWM_DisableInt(int flags)217 void MXC_OWM_DisableInt(int flags)
218 {
219     MXC_OWM_RevA_DisableInt((mxc_owm_reva_regs_t *)MXC_OWM, flags);
220 }
221 
222 /* ************************************************************************* */
MXC_OWM_SetForcePresenceDetect(int enable)223 int MXC_OWM_SetForcePresenceDetect(int enable)
224 {
225     return MXC_OWM_RevA_SetForcePresenceDetect((mxc_owm_reva_regs_t *)MXC_OWM, enable);
226 }
227 
228 /* ************************************************************************* */
MXC_OWM_SetInternalPullup(int enable)229 int MXC_OWM_SetInternalPullup(int enable)
230 {
231     return MXC_OWM_RevA_SetInternalPullup((mxc_owm_reva_regs_t *)MXC_OWM, enable);
232 }
233 
234 /* ************************************************************************* */
MXC_OWM_SetExternalPullup(mxc_owm_ext_pu_t ext_pu_mode)235 int MXC_OWM_SetExternalPullup(mxc_owm_ext_pu_t ext_pu_mode)
236 {
237     return MXC_OWM_RevA_SetExternalPullup((mxc_owm_reva_regs_t *)MXC_OWM, ext_pu_mode);
238 }
239 
240 /* ************************************************************************* */
MXC_OWM_SystemClockUpdated(void)241 int MXC_OWM_SystemClockUpdated(void)
242 {
243     return MXC_OWM_RevA_SystemClockUpdated((mxc_owm_reva_regs_t *)MXC_OWM);
244 }
245 
246 /* ************************************************************************* */
MXC_OWM_SetSearchROMAccelerator(int enable)247 int MXC_OWM_SetSearchROMAccelerator(int enable)
248 {
249     return MXC_OWM_RevA_SetSearchROMAccelerator((mxc_owm_reva_regs_t *)MXC_OWM, enable);
250 }
251 
252 /* ************************************************************************* */
MXC_OWM_BitBang_Init(int initialState)253 int MXC_OWM_BitBang_Init(int initialState)
254 {
255     return MXC_OWM_RevA_BitBang_Init((mxc_owm_reva_regs_t *)MXC_OWM, initialState);
256 }
257 
258 /* ************************************************************************* */
MXC_OWM_BitBang_Read(void)259 int MXC_OWM_BitBang_Read(void)
260 {
261     return MXC_OWM_RevA_BitBang_Read((mxc_owm_reva_regs_t *)MXC_OWM);
262 }
263 
264 /* ************************************************************************* */
MXC_OWM_BitBang_Write(int state)265 int MXC_OWM_BitBang_Write(int state)
266 {
267     return MXC_OWM_RevA_BitBang_Write((mxc_owm_reva_regs_t *)MXC_OWM, state);
268 }
269 
270 /* ************************************************************************* */
MXC_OWM_BitBang_Disable(void)271 int MXC_OWM_BitBang_Disable(void)
272 {
273     return MXC_OWM_RevA_BitBang_Disable((mxc_owm_reva_regs_t *)MXC_OWM);
274 }
275