Browse Source

Fix I2C signalStop in readLastByte for Microbit (#344)

* Fix I2C signalStop in readLastByte for Microbit
pull/348/head
Anthony Elder 6 years ago
committed by Ron Evans
parent
commit
8d3f19bc84
  1. 27
      src/machine/machine_nrf.go

27
src/machine/machine_nrf.go

@ -185,21 +185,20 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
}
}
if len(r) != 0 {
// To trigger suspend task when a byte is received
i2c.Bus.SHORTS = nrf.TWI_SHORTS_BB_SUSPEND
i2c.Bus.TASKS_STARTRX = 1 // re-start transmission for reading
for i := range r { // read each char
if i+1 == len(r) {
// The 'stop' signal must be sent before reading back the last
// byte, so that it will be sent by the I2C peripheral right
// after the last byte has been read.
r[i] = i2c.readLastByte()
} else {
r[i] = i2c.readByte()
// To trigger stop task when last byte is received, set before resume task.
i2c.Bus.SHORTS = nrf.TWI_SHORTS_BB_STOP
}
i2c.Bus.TASKS_RESUME = 1 // re-start transmission for reading
r[i] = i2c.readByte()
}
} else {
// Nothing to read back. Stop the transmission.
i2c.signalStop()
}
i2c.signalStop()
i2c.Bus.SHORTS = nrf.TWI_SHORTS_BB_SUSPEND_Disabled
return nil
}
@ -229,16 +228,6 @@ func (i2c I2C) readByte() byte {
return byte(i2c.Bus.RXD)
}
// readLastByte reads a single byte from the I2C bus, sending a stop signal
// after it has been read.
func (i2c I2C) readLastByte() byte {
for i2c.Bus.EVENTS_RXDREADY == 0 {
}
i2c.Bus.EVENTS_RXDREADY = 0
i2c.signalStop() // signal 'stop' now, so it is sent when reading RXD
return byte(i2c.Bus.RXD)
}
// SPI on the NRF.
type SPI struct {
Bus *nrf.SPI_Type

Loading…
Cancel
Save