Skip to content

Commit

Permalink
targets: use built-in timer for sleep timing on TKey
Browse files Browse the repository at this point in the history
Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Nov 28, 2024
1 parent 81feaed commit 7297fc1
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/device/tkey/tkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 10 additions & 6 deletions src/runtime/runtime_tkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package runtime

import (
"device/tkey"
"machine"
"runtime/volatile"
)
Expand All @@ -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()
}

Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions src/runtime/runtime_tkey_baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7297fc1

Please sign in to comment.