tinygo-org / bluetooth

Cross-platform Bluetooth API for Go and TinyGo. Supports Linux, macOS, Windows, and bare metal using Nordic SoftDevice or HCI
https://tinygo.org
Other
720 stars 134 forks source link

examples/heartrate: use of rand.Intn appears to freeze the program on nrf51 #53

Open knieriem opened 3 years ago

knieriem commented 3 years ago

In revision 3e2ce45 examples/heartrate got extended, i.a. by implementing a heart rate value that changes over time using rand.Intn.

It seems, though, that in order to run examples/heartrate after this change on a micro:bit, I have to replace rand.Intn by some other code (e.g. a constant, or some increasing integer); then I am able to see the "tick" messages on the serial line, and I'm able to receive the values on a smartphone.

With rand.Intn, on the other hand, the program somehow freezes, printing fatal error:.
GDB shows nrf51.cpu -- clearing lockup after double fault, but I don't know if that is related. rand.Intn uses sync.Mutex, I don't know if this might be a source of the problem.

When I use rand.Intn in a normal program on micro:bit, without softdevice, it works. So, for instance, a small program like

func main() {
    for {
        println("rand", rand.Intn(20))
        time.Sleep(time.Second)
    }
}

works on plain micro:bit, but doesn't work on -target microbit-s110v8 - at least for me.

aykevl commented 3 years ago

I think this is caused by calling the NVIC hardware peripheral directly instead of through the SoftDevice. This is definitely a bug, but fixing it may be somewhat involved.