Skip to content

Commit 169d5f1

Browse files
aykevldeadprogram
authored andcommitted
nrf: fix bug in SPI.Tx
There was what appears to be a race condition in the Tx function. While it would work fine in many cases, when there were interrupts (such as when using BLE), the function would just hang waiting for `EVENTS_READY` to arrive. I think what was happening was that the `spi.Bus.RXD.Get()` would start the next transfer, which would complete (and generate an event) before `EVENTS_READY` was reset to 0. The fix is easy: clear `EVENTS_READY` before doing something that can trigger an event. I believe I've seen this bug before on the PineTime but I couldn't find the issue back then.
1 parent 9ed5eae commit 169d5f1

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/machine/machine_nrf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,13 @@ func (spi SPI) Tx(w, r []byte) error {
443443
spi.Bus.TXD.Set(uint32(b))
444444
for spi.Bus.EVENTS_READY.Get() == 0 {
445445
}
446-
_ = spi.Bus.RXD.Get()
447446
spi.Bus.EVENTS_READY.Set(0)
447+
_ = spi.Bus.RXD.Get()
448448
}
449449
for spi.Bus.EVENTS_READY.Get() == 0 {
450450
}
451-
_ = spi.Bus.RXD.Get()
452451
spi.Bus.EVENTS_READY.Set(0)
452+
_ = spi.Bus.RXD.Get()
453453

454454
default:
455455
// write/read

0 commit comments

Comments
 (0)