From 7f027ddd330a0851faf917ac4d6c71f043348f6b Mon Sep 17 00:00:00 2001 From: Ron Evans Date: Fri, 22 Feb 2019 09:08:46 +0100 Subject: [PATCH] machine/samd21: correct calculation for runtime ticks() function so that go routine scheduling can function as expected as described in issue #149 Signed-off-by: Ron Evans --- src/runtime/runtime_atsamd21.go | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/runtime/runtime_atsamd21.go b/src/runtime/runtime_atsamd21.go index 80fba9b9c..09a093cdd 100644 --- a/src/runtime/runtime_atsamd21.go +++ b/src/runtime/runtime_atsamd21.go @@ -205,11 +205,7 @@ func initRTC() { // set Mode0 to 32-bit counter (mode 0) with prescaler 1 and GCLK2 is 32KHz/1 sam.RTC_MODE0.CTRL = sam.RegValue16((sam.RTC_MODE0_CTRL_MODE_COUNT32 << sam.RTC_MODE0_CTRL_MODE_Pos) | - (sam.RTC_MODE0_CTRL_PRESCALER_DIV1 << sam.RTC_MODE0_CTRL_PRESCALER_Pos) | - sam.RTC_MODE0_CTRL_MATCHCLR) - waitForSync() - - sam.RTC_MODE0.COMP0 = 0xffffffff + (sam.RTC_MODE0_CTRL_PRESCALER_DIV1 << sam.RTC_MODE0_CTRL_PRESCALER_Pos)) waitForSync() // re-enable RTC @@ -256,8 +252,8 @@ func ticks() timeUnit { sam.RTC_MODE0.READREQ = sam.RTC_MODE0_READREQ_RREQ waitForSync() - rtcCounter := uint64(sam.RTC_MODE0.COUNT) * 30 // each counter tick == 30.5us - offset := (rtcCounter - timerLastCounter) // change since last measurement + rtcCounter := (uint64(sam.RTC_MODE0.COUNT) * 305) / 10 // each counter tick == 30.5us + offset := (rtcCounter - timerLastCounter) // change since last measurement timerLastCounter = rtcCounter timestamp += timeUnit(offset) // TODO: not precise return timestamp @@ -277,7 +273,7 @@ func timerSleep(ticks uint32) { // set compare value cnt := sam.RTC_MODE0.COUNT - sam.RTC_MODE0.COMP0 = sam.RegValue(uint32(cnt) + (ticks / 30)) // each counter tick == 30.5us + sam.RTC_MODE0.COMP0 = sam.RegValue(uint32(cnt) + (ticks * 10 / 305)) // each counter tick == 30.5us waitForSync() // enable IRQ for CMP0 compare