Skip to content

Commit eed26f2

Browse files
committed
machine: [rp2] discount scheduling delays in I2C timeouts
The `gosched` call introduce arbitrary long delays in general, and in TinyGo particular because the goroutine scheduler is cooperative and doesn't preempt busy (e.g. compute-heavy) goroutines. This change discounts such delays from the rp2xxx I2C timeout logic. I tested this change by simulating a busy goroutine: go func() { for { // Busy. before := time.Now() for time.Since(before) < 100*time.Millisecond { } // Sleep. time.Sleep(100 * time.Millisecond) } }() and testing that I2C transfers would no longer time out.
1 parent 612a38e commit eed26f2

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/machine/machine_rp2_i2c.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,9 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte, timeout_us uint64) (err error) {
338338
return errI2CWriteTimeout // If there was a timeout, don't attempt to do anything else.
339339
}
340340

341+
now := ticks()
341342
gosched()
343+
deadline += ticks() - now
342344
}
343345

344346
abortReason = i2c.getAbortReason()
@@ -361,7 +363,9 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte, timeout_us uint64) (err error) {
361363
return errI2CWriteTimeout
362364
}
363365

366+
now := ticks()
364367
gosched()
368+
deadline += ticks() - now
365369
}
366370
i2c.Bus.IC_CLR_STOP_DET.Get()
367371
}

0 commit comments

Comments
 (0)