Files
tinygo/src/runtime/runtime_avr.go
T
Ayke van Laethem 98f84a497d qemu: signal correct exit code to QEMU
There were a few issues that were causing qemu-system-arm and
qemu-system-riscv to give the wrong exit codes. They are in fact capable
of exiting with 0 or 1 signalled from the running application, but this
functionality wasn't used. This commit changes this in the following
ways:

  * It fixes SemiHosting codes, which were incorrectly written in
    decimal while they should have been written in hexadecimal (oops!).
  * It modifies all the baremetal main functions (aka reset handlers) to
    exit with `exit(0)` instead of `abort()`.
  * It changes `syscall.Exit` to call `exit(code)` instead of `abort()`
    on baremetal targets.
  * It adds these new exit functions where necessary, implemented in a
    way that signals the correct exit status if running under QEMU.

All in all, this means that `tinygo test` doesn't have to look at the
output of a test to determine the outcome. It can simply look at the
exit code.
2021-10-06 09:04:06 +02:00

96 lines
1.5 KiB
Go

// +build avr
package runtime
import (
"device/avr"
"unsafe"
)
const BOARD = "arduino"
type timeUnit uint32
var currentTime timeUnit
// 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
// for accurate time keeping.
const (
WDT_PERIOD_16MS = iota
WDT_PERIOD_32MS
WDT_PERIOD_64MS
WDT_PERIOD_125MS
WDT_PERIOD_250MS
WDT_PERIOD_500MS
WDT_PERIOD_1S
WDT_PERIOD_2S
)
//go:extern _sbss
var _sbss [0]byte
//go:extern _ebss
var _ebss [0]byte
//export main
func main() {
preinit()
run()
exit(0)
}
func preinit() {
// Initialize .bss: zero-initialized global variables.
ptr := unsafe.Pointer(&_sbss)
for ptr != unsafe.Pointer(&_ebss) {
*(*uint8)(ptr) = 0
ptr = unsafe.Pointer(uintptr(ptr) + 1)
}
}
func postinit() {
// Enable interrupts after initialization.
avr.Asm("sei")
}
func init() {
initUART()
}
const tickNanos = 1024 * 16384 // roughly 16ms in nanoseconds
func ticksToNanoseconds(ticks timeUnit) int64 {
return int64(ticks) * tickNanos
}
func nanosecondsToTicks(ns int64) timeUnit {
return timeUnit(ns / tickNanos)
}
// Sleep this number of ticks of 16ms.
//
// TODO: not very accurate. Improve accuracy by calibrating on startup and every
// once in a while.
func sleepTicks(d timeUnit) {
currentTime += d
for d != 0 {
sleepWDT(WDT_PERIOD_16MS)
d -= 1
}
}
func ticks() timeUnit {
return currentTime
}
func exit(code int) {
abort()
}
func abort() {
for {
sleepWDT(WDT_PERIOD_2S)
}
}