You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2019/05/10 21:02:41 UTC

[mynewt-core] 02/03: mcu/fe310: Correct tick calculation

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 9387eab69c2fe706be6cd5070303ec034c08f16c
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Thu May 9 12:06:00 2019 +0200

    mcu/fe310: Correct tick calculation
    
    Next tick time was calculated based on current time taken
    during interrupt handling.
    It could lead to slightly lower tick frequency if handling
    of timer interrupt was delayed.
    Now time comparator is used instead of current time (same pattern
    is used in other MCUs).
---
 hw/mcu/sifive/fe310/src/hal_os_tick.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/hw/mcu/sifive/fe310/src/hal_os_tick.c b/hw/mcu/sifive/fe310/src/hal_os_tick.c
index b04e528..4027dff 100644
--- a/hw/mcu/sifive/fe310/src/hal_os_tick.c
+++ b/hw/mcu/sifive/fe310/src/hal_os_tick.c
@@ -48,12 +48,16 @@ os_tick_init(uint32_t os_ticks_per_sec, int prio)
 void
 timer_interrupt_handler(void)
 {
+    int delta;
+    int ticks;
     uint64_t time = get_timer_value();
-    int delta = (int)(time - last_tick_time);
-    last_tick_time = time;
 
-    int ticks = delta / ticks_per_ostick;
-    set_mtimecmp(time + ticks_per_ostick);
+    delta = (int)(time - last_tick_time);
+    ticks = (int)(delta / ticks_per_ostick);
+
+    last_tick_time += ticks * ticks_per_ostick;
+
+    set_mtimecmp(last_tick_time + ticks_per_ostick);
 
     os_time_advance(ticks);
 }