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 <stddef.h>
23 #include "mxc_assert.h"
24 #include "tmr.h"
25 #include "tmr_common.h"
26
27 /* **** Functions **** */
28
MXC_TMR_Common_Delay(mxc_tmr_regs_t * tmr,uint32_t us)29 void MXC_TMR_Common_Delay(mxc_tmr_regs_t *tmr, uint32_t us)
30 {
31 // Return immediately if delay is 0
32 if (!us) {
33 return;
34 }
35
36 MXC_TMR_TO_Start(tmr, us);
37
38 while (MXC_TMR_TO_Check(tmr) != E_TIME_OUT) {}
39 }
40
MXC_TMR_Common_TO_Check(mxc_tmr_regs_t * tmr)41 int MXC_TMR_Common_TO_Check(mxc_tmr_regs_t *tmr)
42 {
43 if (MXC_TMR_GetFlags(tmr)) {
44 return E_TIME_OUT;
45 }
46
47 return E_NO_ERROR;
48 }
49
MXC_TMR_Common_TO_Stop(mxc_tmr_regs_t * tmr)50 void MXC_TMR_Common_TO_Stop(mxc_tmr_regs_t *tmr)
51 {
52 MXC_TMR_Stop(tmr);
53 MXC_TMR_SetCount(tmr, 0x0);
54 }
55
MXC_TMR_Common_TO_Clear(mxc_tmr_regs_t * tmr)56 void MXC_TMR_Common_TO_Clear(mxc_tmr_regs_t *tmr)
57 {
58 MXC_TMR_ClearFlags(tmr);
59 MXC_TMR_SetCount(tmr, 0x0);
60 }
61
MXC_TMR_Common_TO_Remaining(mxc_tmr_regs_t * tmr)62 unsigned int MXC_TMR_Common_TO_Remaining(mxc_tmr_regs_t *tmr)
63 {
64 uint32_t remaining_ticks, remaining_time;
65 mxc_tmr_unit_t units;
66
67 remaining_ticks = MXC_TMR_GetCompare(tmr) - MXC_TMR_GetCount(tmr);
68 MXC_TMR_GetTime(tmr, remaining_ticks, &remaining_time, &units);
69
70 switch (units) {
71 case MXC_TMR_UNIT_NANOSEC:
72 default:
73 return (remaining_time / 1000);
74
75 case MXC_TMR_UNIT_MICROSEC:
76 return (remaining_time);
77
78 case MXC_TMR_UNIT_MILLISEC:
79 return (remaining_time * 1000);
80
81 case MXC_TMR_UNIT_SEC:
82 return (remaining_time * 1000000);
83 }
84 }
85
MXC_TMR_Common_SW_Start(mxc_tmr_regs_t * tmr)86 void MXC_TMR_Common_SW_Start(mxc_tmr_regs_t *tmr)
87 {
88 MXC_TMR_TO_Start(tmr, 0xFFFFFFFF);
89 }
90
MXC_TMR_Common_SW_Stop(mxc_tmr_regs_t * tmr)91 unsigned int MXC_TMR_Common_SW_Stop(mxc_tmr_regs_t *tmr)
92 {
93 unsigned int elapsed = MXC_TMR_TO_Elapsed(tmr);
94 MXC_TMR_TO_Stop(tmr);
95 return elapsed;
96 }
97
MXC_TMR_Common_TO_Elapsed(mxc_tmr_regs_t * tmr)98 unsigned int MXC_TMR_Common_TO_Elapsed(mxc_tmr_regs_t *tmr)
99 {
100 uint32_t elapsed;
101 mxc_tmr_unit_t units;
102 MXC_TMR_GetTime(tmr, tmr->cnt, &elapsed, &units);
103
104 switch (units) {
105 case MXC_TMR_UNIT_NANOSEC:
106 default:
107 return (elapsed / 1000);
108
109 case MXC_TMR_UNIT_MICROSEC:
110 return (elapsed);
111
112 case MXC_TMR_UNIT_MILLISEC:
113 return (elapsed * 1000);
114
115 case MXC_TMR_UNIT_SEC:
116 return (elapsed * 1000000);
117 }
118 }
119