Skip to content

Commit bef0dc5

Browse files
authored
Add per-byte timeout budget for rp2 I2C (#5189)
* Add per-byte timeout budget for rp2 I2C * run goimports
1 parent e79cdc1 commit bef0dc5

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/machine/machine_rp2_i2c.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ func (i2c *I2C) deinit() (resetVal uint32) {
280280

281281
// tx performs blocking write followed by read to I2C bus.
282282
func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
283-
const timeout_us = 4_000
284-
deadline := ticks() + timeout_us
285283
if addr >= 0x80 || isReservedI2CAddr(addr) {
286284
return errInvalidTgtAddr
287285
}
@@ -292,6 +290,14 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
292290
return nil
293291
}
294292

293+
// Base 4ms for small register pokes.
294+
// Add per-byte budget. 100us/byte is conservative at 400kHz and still ok at 100kHz for modest sizes.
295+
timeout_us := uint64(4_000) + uint64(txlen+rxlen)*100
296+
// Cap so it doesn't go insane:
297+
timeout_us = min(timeout_us, 500_000)
298+
299+
deadline := ticks() + timeout_us
300+
295301
err = i2c.disable()
296302
if err != nil {
297303
return err

0 commit comments

Comments
 (0)