aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/machine/machine_nrf5x.go
diff options
context:
space:
mode:
Diffstat (limited to 'src/machine/machine_nrf5x.go')
-rw-r--r--src/machine/machine_nrf5x.go17
1 files changed, 16 insertions, 1 deletions
diff --git a/src/machine/machine_nrf5x.go b/src/machine/machine_nrf5x.go
index 8c012b49e..4c731036b 100644
--- a/src/machine/machine_nrf5x.go
+++ b/src/machine/machine_nrf5x.go
@@ -72,12 +72,17 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) (err error) {
if len(r) == 0 {
// Stop the I2C transaction after the write.
- i2c.signalStop()
+ err = i2c.signalStop()
} else {
// The last byte read has already stopped the transaction, via
// TWI_SHORTS_BB_STOP. But we still need to wait until we receive the
// STOPPED event.
+ tries := 0
for i2c.Bus.EVENTS_STOPPED.Get() == 0 {
+ tries++
+ if tries >= i2cTimeout {
+ return errI2CSignalStopTimeout
+ }
}
i2c.Bus.EVENTS_STOPPED.Set(0)
}
@@ -87,12 +92,17 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) (err error) {
// writeByte writes a single byte to the I2C bus and waits for confirmation.
func (i2c *I2C) writeByte(data byte) error {
+ tries := 0
i2c.Bus.TXD.Set(uint32(data))
for i2c.Bus.EVENTS_TXDSENT.Get() == 0 {
if e := i2c.Bus.EVENTS_ERROR.Get(); e != 0 {
i2c.Bus.EVENTS_ERROR.Set(0)
return errI2CBusError
}
+ tries++
+ if tries >= i2cTimeout {
+ return errI2CWriteTimeout
+ }
}
i2c.Bus.EVENTS_TXDSENT.Set(0)
return nil
@@ -100,11 +110,16 @@ func (i2c *I2C) writeByte(data byte) error {
// readByte reads a single byte from the I2C bus when it is ready.
func (i2c *I2C) readByte() (byte, error) {
+ tries := 0
for i2c.Bus.EVENTS_RXDREADY.Get() == 0 {
if e := i2c.Bus.EVENTS_ERROR.Get(); e != 0 {
i2c.Bus.EVENTS_ERROR.Set(0)
return 0, errI2CBusError
}
+ tries++
+ if tries >= i2cTimeout {
+ return 0, errI2CReadTimeout
+ }
}
i2c.Bus.EVENTS_RXDREADY.Set(0)
return byte(i2c.Bus.RXD.Get()), nil