Skip to content

Commit d308ca5

Browse files
peter-mitsisnashif
authored andcommitted
drivers: timer: adsp: Improve elapsed ticks calculations
It is better to use 64-bit variable types for calculating the number of elapsed ticks than 32-bit variable types. This guards against the propagation of calculation errors should the lower 32-bits of the timer counter roll over multiple times before the timer ISR is serviced. (Such a scenario can easily occur when pausing the system for an extended period of time with a debugging device such as a Lauterbach.) Signed-off-by: Peter Mitsis <peter.mitsis@intel.com>
1 parent ff46de7 commit d308ca5

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

drivers/timer/intel_adsp_timer.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ static void compare_isr(const void *arg)
9696
{
9797
ARG_UNUSED(arg);
9898
uint64_t curr;
99-
uint32_t dticks;
99+
uint64_t dticks;
100100

101101
k_spinlock_key_t key = k_spin_lock(&lock);
102102

103103
curr = count();
104-
dticks = (uint32_t)((curr - last_count) / CYC_PER_TICK);
104+
dticks = (curr - last_count) / CYC_PER_TICK;
105105

106106
/* Clear the triggered bit */
107107
*WCTCS |= DSP_WCT_CS_TT(COMPARATOR_IDX);
@@ -119,7 +119,7 @@ static void compare_isr(const void *arg)
119119

120120
k_spin_unlock(&lock, key);
121121

122-
sys_clock_announce(dticks);
122+
sys_clock_announce((int32_t)dticks);
123123
}
124124

125125
void sys_clock_set_timeout(int32_t ticks, bool idle)
@@ -160,10 +160,10 @@ uint32_t sys_clock_elapsed(void)
160160
return 0;
161161
}
162162
k_spinlock_key_t key = k_spin_lock(&lock);
163-
uint32_t ret = (count32() - (uint32_t)last_count) / CYC_PER_TICK;
163+
uint64_t ret = (count() - last_count) / CYC_PER_TICK;
164164

165165
k_spin_unlock(&lock, key);
166-
return ret;
166+
return (uint32_t)ret;
167167
}
168168

169169
uint32_t sys_clock_cycle_get_32(void)

0 commit comments

Comments
 (0)