mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-05 03:27:48 +00:00
90b42799a2
This makes it possible to assign I2C objects (machine.I2C0, machine.I2C1, etc.) without needing to take a pointer. This is important especially in the future when I2C may be driven using DMA and the machine.I2C type needs to store some state.
48 lines
1.7 KiB
Go
48 lines
1.7 KiB
Go
// +build atmega nrf sam stm32,!stm32l0 fe310 k210
|
|
|
|
package machine
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
// TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
|
|
const (
|
|
TWI_FREQ_100KHZ = 100000
|
|
TWI_FREQ_400KHZ = 400000
|
|
)
|
|
|
|
var (
|
|
errI2CWriteTimeout = errors.New("I2C timeout during write")
|
|
errI2CReadTimeout = errors.New("I2C timeout during read")
|
|
errI2CBusReadyTimeout = errors.New("I2C timeout on bus ready")
|
|
errI2CSignalStartTimeout = errors.New("I2C timeout on signal start")
|
|
errI2CSignalReadTimeout = errors.New("I2C timeout on signal read")
|
|
errI2CSignalStopTimeout = errors.New("I2C timeout on signal stop")
|
|
errI2CAckExpected = errors.New("I2C error: expected ACK not NACK")
|
|
errI2CBusError = errors.New("I2C bus error")
|
|
)
|
|
|
|
// WriteRegister transmits first the register and then the data to the
|
|
// peripheral device.
|
|
//
|
|
// Many I2C-compatible devices are organized in terms of registers. This method
|
|
// is a shortcut to easily write to such registers. Also, it only works for
|
|
// devices with 7-bit addresses, which is the vast majority.
|
|
func (i2c *I2C) WriteRegister(address uint8, register uint8, data []byte) error {
|
|
buf := make([]uint8, len(data)+1)
|
|
buf[0] = register
|
|
copy(buf[1:], data)
|
|
return i2c.Tx(uint16(address), buf, nil)
|
|
}
|
|
|
|
// ReadRegister transmits the register, restarts the connection as a read
|
|
// operation, and reads the response.
|
|
//
|
|
// Many I2C-compatible devices are organized in terms of registers. This method
|
|
// is a shortcut to easily read such registers. Also, it only works for devices
|
|
// with 7-bit addresses, which is the vast majority.
|
|
func (i2c *I2C) ReadRegister(address uint8, register uint8, data []byte) error {
|
|
return i2c.Tx(uint16(address), []byte{register}, data)
|
|
}
|