Skip to content

Commit 43cca97

Browse files
visitorckwjserv
authored andcommitted
Fix early timer interrupt causing exception before first task launch
In preemptive scheduling mode, enabling the machine timer interrupt too early can trigger an interrupt before the first task is launched, resulting in an instruction access fault. This happens because hal_timer_enable() enables MTIE before a valid mtimecmp value is set. Set mtimecmp before enabling the timer interrupt to ensure the first interrupt is scheduled in the future, avoiding premature preemption and exceptions in user applications like cpubench.
1 parent b3e756c commit 43cca97

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

arch/riscv/hal.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ void do_trap(uint32_t cause, uint32_t epc)
308308
/* Enables the machine-level timer interrupt source. */
309309
void hal_timer_enable(void)
310310
{
311+
mtimecmp_w(mtime_r() + (F_CPU / F_TIMER));
311312
write_csr(mie, read_csr(mie) | MIE_MTIE);
312313
}
313314

0 commit comments

Comments
 (0)