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 "tmr.h"
22 #include "tmr_revb.h"
23 #include "tmr_common.h"
24 #include "stdbool.h"
25 
MXC_TMR_Init(mxc_tmr_regs_t * tmr,mxc_tmr_cfg_t * cfg,bool init_pins)26 int MXC_TMR_Init(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg, bool init_pins)
27 {
28     uint8_t tmr_id = MXC_TMR_GET_IDX(tmr);
29     uint8_t clockSource = MXC_TMR_CLK0;
30 
31     if (cfg == NULL) {
32         return E_NULL_PTR;
33     }
34 
35     MXC_ASSERT(tmr_id >= 0);
36 
37     switch (cfg->clock) {
38     case MXC_TMR_EXT_CLK:
39         clockSource = MXC_TMR_CLK1;
40         MXC_GPIO_Config(&gpio_cfg_extclk);
41         break;
42 
43     case MXC_TMR_INRO_CLK:
44         if (tmr_id < 4) { // Timers 0-3 do not support this clock source
45             return E_NOT_SUPPORTED;
46         }
47 
48         clockSource = MXC_TMR_CLK3;
49         MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_INRO);
50         MXC_TMR_RevB_SetClockSourceFreq((mxc_tmr_revb_regs_t *)tmr, INRO_FREQ);
51         break;
52 
53     case MXC_TMR_IBRO_CLK:
54         if (tmr_id > 3) { // Timer 4 do not support this clock source
55             return E_NOT_SUPPORTED;
56         }
57 
58         clockSource = MXC_TMR_CLK2;
59         MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_IBRO);
60         MXC_TMR_RevB_SetClockSourceFreq((mxc_tmr_revb_regs_t *)tmr, IBRO_FREQ);
61         break;
62 
63     case MXC_TMR_ERFO_CLK:
64         if (tmr_id > 3) { // Timer 4 do not support this clock source
65             return E_NOT_SUPPORTED;
66         }
67 
68         clockSource = MXC_TMR_CLK3;
69         MXC_SYS_ClockSourceEnable(MXC_SYS_CLOCK_ERFO);
70         MXC_TMR_RevB_SetClockSourceFreq((mxc_tmr_revb_regs_t *)tmr, ERFO_FREQ);
71         break;
72 
73     default:
74         MXC_TMR_RevB_SetClockSourceFreq((mxc_tmr_revb_regs_t *)tmr, PeripheralClock);
75         break;
76     }
77 
78 #ifndef MSDK_NO_GPIO_CLK_INIT
79     //enable peripheral clock and configure gpio pins
80     switch (tmr_id) {
81     case 0:
82         MXC_SYS_Reset_Periph(MXC_SYS_RESET0_TMR0);
83         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TMR0);
84 
85         if (init_pins) {
86             if (cfg->bitMode != MXC_TMR_BIT_MODE_16B) {
87                 MXC_GPIO_Config(&gpio_cfg_tmr0);
88             }
89         }
90 
91         break;
92 
93     case 1:
94         MXC_SYS_Reset_Periph(MXC_SYS_RESET0_TMR1);
95         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TMR1);
96 
97         if (init_pins) {
98             if (cfg->bitMode != MXC_TMR_BIT_MODE_16B) {
99                 MXC_GPIO_Config(&gpio_cfg_tmr1);
100             }
101         }
102 
103         break;
104 
105     case 2:
106         MXC_SYS_Reset_Periph(MXC_SYS_RESET0_TMR2);
107         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TMR2);
108 
109         if (init_pins) {
110             if (cfg->bitMode != MXC_TMR_BIT_MODE_16B) {
111                 MXC_GPIO_Config(&gpio_cfg_tmr2);
112             }
113         }
114 
115         break;
116 
117     case 3:
118         MXC_SYS_Reset_Periph(MXC_SYS_RESET0_TMR3);
119         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TMR3);
120 
121         if (init_pins) {
122             if (cfg->bitMode != MXC_TMR_BIT_MODE_16B) {
123                 MXC_GPIO_Config(&gpio_cfg_tmr3);
124             }
125         }
126 
127         break;
128 
129     case 4:
130         MXC_SYS_Reset_Periph(MXC_SYS_RESET_TMR4);
131         MXC_SYS_ClockEnable(MXC_SYS_PERIPH_CLOCK_TMR4);
132 
133         if (init_pins) {
134             if (cfg->bitMode != MXC_TMR_BIT_MODE_16B) {
135                 MXC_GPIO_Config(&gpio_cfg_tmr4);
136             } else {
137                 return E_NOT_SUPPORTED;
138                 break;
139             }
140         }
141 
142         break;
143 
144     case 5:
145         return E_NOT_SUPPORTED;
146         break;
147     }
148 #else
149     (void)init_pins;
150 #endif
151 
152     return MXC_TMR_RevB_Init((mxc_tmr_revb_regs_t *)tmr, cfg, clockSource);
153 }
154 
MXC_TMR_Shutdown(mxc_tmr_regs_t * tmr)155 void MXC_TMR_Shutdown(mxc_tmr_regs_t *tmr)
156 {
157     int tmr_id = MXC_TMR_GET_IDX(tmr);
158 
159     MXC_ASSERT(tmr_id >= 0);
160 
161     MXC_TMR_RevB_Shutdown((mxc_tmr_revb_regs_t *)tmr);
162 
163     // System settigns
164     //diasble peripheral clock
165     switch (tmr_id) {
166     case 0:
167         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR0);
168         break;
169 
170     case 1:
171         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR1);
172         break;
173 
174     case 2:
175         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR2);
176         break;
177 
178     case 3:
179         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR3);
180         break;
181 
182     case 4:
183         MXC_SYS_ClockDisable(MXC_SYS_PERIPH_CLOCK_TMR4);
184         break;
185     }
186 }
187 
MXC_TMR_Start(mxc_tmr_regs_t * tmr)188 void MXC_TMR_Start(mxc_tmr_regs_t *tmr)
189 {
190     MXC_TMR_RevB_Start((mxc_tmr_revb_regs_t *)tmr);
191 }
192 
MXC_TMR_Stop(mxc_tmr_regs_t * tmr)193 void MXC_TMR_Stop(mxc_tmr_regs_t *tmr)
194 {
195     MXC_TMR_RevB_Stop((mxc_tmr_revb_regs_t *)tmr);
196 }
197 
MXC_TMR_SetPWM(mxc_tmr_regs_t * tmr,uint32_t pwm)198 int MXC_TMR_SetPWM(mxc_tmr_regs_t *tmr, uint32_t pwm)
199 {
200     return MXC_TMR_RevB_SetPWM((mxc_tmr_revb_regs_t *)tmr, pwm);
201 }
202 
MXC_TMR_GetCompare(mxc_tmr_regs_t * tmr)203 uint32_t MXC_TMR_GetCompare(mxc_tmr_regs_t *tmr)
204 {
205     return MXC_TMR_RevB_GetCompare((mxc_tmr_revb_regs_t *)tmr);
206 }
207 
MXC_TMR_GetCapture(mxc_tmr_regs_t * tmr)208 uint32_t MXC_TMR_GetCapture(mxc_tmr_regs_t *tmr)
209 {
210     return MXC_TMR_RevB_GetCompare((mxc_tmr_revb_regs_t *)tmr);
211 }
212 
MXC_TMR_GetPeriod(mxc_tmr_regs_t * tmr,mxc_tmr_clock_t clock,uint32_t prescalar,uint32_t frequency)213 uint32_t MXC_TMR_GetPeriod(mxc_tmr_regs_t *tmr, mxc_tmr_clock_t clock, uint32_t prescalar,
214                            uint32_t frequency)
215 {
216     uint32_t clockFrequency = PeripheralClock;
217     int tmr_id = MXC_TMR_GET_IDX(tmr);
218 
219     MXC_ASSERT(tmr_id >= 0);
220 
221     if (tmr_id > 3) {
222         switch (clock) {
223         case MXC_TMR_APB_CLK:
224             clockFrequency = (PeripheralClock / 4);
225             break;
226 
227             // MAX32675 does not support the ERTCO.
228 
229         case MXC_TMR_INRO_CLK:
230             clockFrequency = INRO_FREQ;
231             break;
232 
233         default:
234             break;
235         }
236     } else {
237         switch (clock) {
238         case MXC_TMR_APB_CLK:
239             clockFrequency = PeripheralClock;
240             break;
241 
242         case MXC_TMR_IBRO_CLK:
243             clockFrequency = IBRO_FREQ;
244             break;
245 
246         case MXC_TMR_ERFO_CLK:
247             clockFrequency = ERFO_FREQ; // Clock Frequency 16 MHz
248             break;
249 
250         default:
251             break;
252         }
253     }
254 
255     return MXC_TMR_RevB_GetPeriod((mxc_tmr_revb_regs_t *)tmr, clockFrequency, prescalar, frequency);
256 }
257 
MXC_TMR_GetCount(mxc_tmr_regs_t * tmr)258 uint32_t MXC_TMR_GetCount(mxc_tmr_regs_t *tmr)
259 {
260     return MXC_TMR_RevB_GetCount((mxc_tmr_revb_regs_t *)tmr);
261 }
262 
MXC_TMR_ClearFlags(mxc_tmr_regs_t * tmr)263 void MXC_TMR_ClearFlags(mxc_tmr_regs_t *tmr)
264 {
265     MXC_TMR_RevB_ClearFlags((mxc_tmr_revb_regs_t *)tmr);
266 }
267 
MXC_TMR_GetFlags(mxc_tmr_regs_t * tmr)268 uint32_t MXC_TMR_GetFlags(mxc_tmr_regs_t *tmr)
269 {
270     return MXC_TMR_RevB_GetFlags((mxc_tmr_revb_regs_t *)tmr);
271 }
272 
MXC_TMR_EnableInt(mxc_tmr_regs_t * tmr)273 void MXC_TMR_EnableInt(mxc_tmr_regs_t *tmr)
274 {
275     MXC_TMR_RevB_EnableInt((mxc_tmr_revb_regs_t *)tmr);
276 }
277 
MXC_TMR_DisableInt(mxc_tmr_regs_t * tmr)278 void MXC_TMR_DisableInt(mxc_tmr_regs_t *tmr)
279 {
280     MXC_TMR_RevB_DisableInt((mxc_tmr_revb_regs_t *)tmr);
281 }
282 
MXC_TMR_EnableWakeup(mxc_tmr_regs_t * tmr,mxc_tmr_cfg_t * cfg)283 void MXC_TMR_EnableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg)
284 {
285     MXC_TMR_RevB_EnableWakeup((mxc_tmr_revb_regs_t *)tmr, cfg);
286 }
287 
MXC_TMR_DisableWakeup(mxc_tmr_regs_t * tmr,mxc_tmr_cfg_t * cfg)288 void MXC_TMR_DisableWakeup(mxc_tmr_regs_t *tmr, mxc_tmr_cfg_t *cfg)
289 {
290     MXC_TMR_RevB_DisableWakeup((mxc_tmr_revb_regs_t *)tmr, cfg);
291 }
292 
MXC_TMR_SetCompare(mxc_tmr_regs_t * tmr,uint32_t cmp_cnt)293 void MXC_TMR_SetCompare(mxc_tmr_regs_t *tmr, uint32_t cmp_cnt)
294 {
295     MXC_TMR_RevB_SetCompare((mxc_tmr_revb_regs_t *)tmr, cmp_cnt);
296 }
297 
MXC_TMR_SetCount(mxc_tmr_regs_t * tmr,uint32_t cnt)298 void MXC_TMR_SetCount(mxc_tmr_regs_t *tmr, uint32_t cnt)
299 {
300     MXC_TMR_RevB_SetCount((mxc_tmr_revb_regs_t *)tmr, cnt);
301 }
302 
MXC_TMR_Delay(mxc_tmr_regs_t * tmr,uint32_t us)303 void MXC_TMR_Delay(mxc_tmr_regs_t *tmr, uint32_t us)
304 {
305     MXC_TMR_Common_Delay(tmr, us);
306 }
307 
MXC_TMR_TO_Start(mxc_tmr_regs_t * tmr,uint32_t us)308 void MXC_TMR_TO_Start(mxc_tmr_regs_t *tmr, uint32_t us)
309 {
310     MXC_TMR_RevB_TO_Start((mxc_tmr_revb_regs_t *)tmr, us);
311 }
312 
MXC_TMR_TO_Check(mxc_tmr_regs_t * tmr)313 int MXC_TMR_TO_Check(mxc_tmr_regs_t *tmr)
314 {
315     return MXC_TMR_Common_TO_Check(tmr);
316 }
317 
MXC_TMR_TO_Stop(mxc_tmr_regs_t * tmr)318 void MXC_TMR_TO_Stop(mxc_tmr_regs_t *tmr)
319 {
320     MXC_TMR_Common_TO_Stop(tmr);
321 }
322 
MXC_TMR_TO_Clear(mxc_tmr_regs_t * tmr)323 void MXC_TMR_TO_Clear(mxc_tmr_regs_t *tmr)
324 {
325     MXC_TMR_Common_TO_Clear(tmr);
326 }
327 
MXC_TMR_TO_Elapsed(mxc_tmr_regs_t * tmr)328 unsigned int MXC_TMR_TO_Elapsed(mxc_tmr_regs_t *tmr)
329 {
330     return MXC_TMR_Common_TO_Elapsed(tmr);
331 }
332 
MXC_TMR_TO_Remaining(mxc_tmr_regs_t * tmr)333 unsigned int MXC_TMR_TO_Remaining(mxc_tmr_regs_t *tmr)
334 {
335     return MXC_TMR_Common_TO_Remaining(tmr);
336 }
337 
MXC_TMR_SW_Start(mxc_tmr_regs_t * tmr)338 void MXC_TMR_SW_Start(mxc_tmr_regs_t *tmr)
339 {
340     MXC_TMR_Common_SW_Start(tmr);
341 }
342 
MXC_TMR_SW_Stop(mxc_tmr_regs_t * tmr)343 unsigned int MXC_TMR_SW_Stop(mxc_tmr_regs_t *tmr)
344 {
345     return MXC_TMR_Common_SW_Stop(tmr);
346 }
347 
MXC_TMR_GetTime(mxc_tmr_regs_t * tmr,uint32_t ticks,uint32_t * time,mxc_tmr_unit_t * units)348 int MXC_TMR_GetTime(mxc_tmr_regs_t *tmr, uint32_t ticks, uint32_t *time, mxc_tmr_unit_t *units)
349 {
350     return MXC_TMR_RevB_GetTime((mxc_tmr_revb_regs_t *)tmr, ticks, time, units);
351 }
352