diff --git a/src/device/tkey/tkey.go b/src/device/tkey/tkey.go index 81f8f113e9..b4542eb4f0 100644 --- a/src/device/tkey/tkey.go +++ b/src/device/tkey/tkey.go @@ -63,10 +63,8 @@ type TRNG_Type struct { type TIMER_Type struct { _ [32]byte - CTRL volatile.Register16 - _ [2]byte - STATUS volatile.Register16 - _ [2]byte + CTRL volatile.Register32 + STATUS volatile.Register32 PRESCALER volatile.Register32 TIMER volatile.Register32 } diff --git a/src/runtime/runtime_tkey.go b/src/runtime/runtime_tkey.go index 41eb9473e1..5cc5c80801 100644 --- a/src/runtime/runtime_tkey.go +++ b/src/runtime/runtime_tkey.go @@ -5,6 +5,7 @@ package runtime import ( + "device/tkey" "machine" "runtime/volatile" ) @@ -21,6 +22,10 @@ func main() { // initPeripherals configures peripherals the way the runtime expects them. func initPeripherals() { + // prescaler value that results in 0.00001-second timer-ticks. + // given an 18 MHz processor, a millisecond is about 18,000 cycles. + tkey.TIMER.PRESCALER.Set(18_000_000 / 100000) + machine.InitSerial() } @@ -49,12 +54,11 @@ func ticks() timeUnit { // sleepTicks sleeps for at least the duration d. func sleepTicks(d timeUnit) { - target := uint64(ticks() + d) + target := uint32(ticks() + d) - for { - if uint64(ticks()) >= target { - break - } - timestamp.Set(timestamp.Get() + 1) + tkey.TIMER.TIMER.Set(uint32(d)) + tkey.TIMER.CTRL.SetBits(tkey.TK1_MMIO_TIMER_CTRL_START) + for tkey.TIMER.STATUS.Get() != 0 { } + timestamp.Set(target) } diff --git a/src/runtime/runtime_tkey_baremetal.go b/src/runtime/runtime_tkey_baremetal.go index c232fcf619..3d892a9dac 100644 --- a/src/runtime/runtime_tkey_baremetal.go +++ b/src/runtime/runtime_tkey_baremetal.go @@ -4,12 +4,12 @@ package runtime // ticksToNanoseconds converts ticks (at 18MHz) to nanoseconds. func ticksToNanoseconds(ticks timeUnit) int64 { - return int64(ticks) * 1800 + return int64(ticks) * 10000 } // nanosecondsToTicks converts nanoseconds to ticks (at 18MHz). func nanosecondsToTicks(ns int64) timeUnit { - return timeUnit(ns / 1800) + return timeUnit(ns / 10000) } func exit(code int) {