all: rewrite sleep function

time.Sleep now compiles on all systems, so lets use that.
Additionally, do a few improvements in time unit handling for the
scheduler. This should lead to somewhat longer sleep durations without
wrapping (on some platforms).

Some examples got smaller, some got bigger. In particular, code using
the scheduler got bigger and the blinky1 example got smaller (especially
on Arduino: 380 -> 314 bytes).
This commit is contained in:
Ayke van Laethem
2018-09-15 01:08:31 +02:00
parent 1ac67cf8de
commit 2a20c0c7f0
12 changed files with 75 additions and 72 deletions
+11 -9
View File
@@ -8,13 +8,11 @@ import (
const BOARD = "arduino"
const Microsecond = 1
type timeUnit uint32
var currentTime uint64
var currentTime timeUnit
func init() {
currentTime = 0
}
const tickMicros = 1024 * 16384
// Watchdog timer periods. These can be off by a large margin (hence the jump
// between 64ms and 125ms which is not an exact double), so don't rely on this
@@ -53,9 +51,13 @@ func putchar(c byte) {
//
// TODO: not very accurate. Improve accuracy by calibrating on startup and every
// once in a while.
func sleep(d Duration) {
currentTime += uint64(d)
d /= 16384 // about 16ms
//go:linkname sleep time.Sleep
func sleep(d int64) {
sleepTicks(timeUnit(d / tickMicros))
}
func sleepTicks(d timeUnit) {
currentTime += d
for d != 0 {
sleepWDT(WDT_PERIOD_16MS)
d -= 1
@@ -90,7 +92,7 @@ func sleepWDT(period uint8) {
*avr.SMCR = 0
}
func monotime() uint64 {
func ticks() timeUnit {
return currentTime
}