nrf: add I2C timeout

This commit adds I2C timeouts for nrf51 and nrf52 (but not yet for
others like nrf52840).

Tested on the PineTime, where I now got a timeout instead of hanging and
resetting due to a watchdog reset.
This commit is contained in:
Ayke van Laethem
2023-07-13 15:24:58 +02:00
committed by Ron Evans
parent a7b205c26c
commit 14ddba8519
2 changed files with 25 additions and 2 deletions
+9 -1
View File
@@ -203,6 +203,8 @@ func (uart *UART) handleInterrupt(interrupt.Interrupt) {
}
}
const i2cTimeout = 0xffff // this is around 29ms on a nrf52
// I2CConfig is used to store config info for I2C.
type I2CConfig struct {
Frequency uint32
@@ -261,11 +263,17 @@ func (i2c *I2C) Configure(config I2CConfig) error {
}
// signalStop sends a stop signal to the I2C peripheral and waits for confirmation.
func (i2c *I2C) signalStop() {
func (i2c *I2C) signalStop() error {
tries := 0
i2c.Bus.TASKS_STOP.Set(1)
for i2c.Bus.EVENTS_STOPPED.Get() == 0 {
tries++
if tries >= i2cTimeout {
return errI2CSignalStopTimeout
}
}
i2c.Bus.EVENTS_STOPPED.Set(0)
return nil
}
var rngStarted = false