github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/machine/i2c.go (about)

     1  //go:build atmega || nrf || sam || stm32 || fe310 || k210 || rp2040 || mimxrt1062 || (esp32c3 && !m5stamp_c3)
     2  
     3  package machine
     4  
     5  import (
     6  	"errors"
     7  )
     8  
     9  // If you are getting a compile error on this line please check to see you've
    10  // correctly implemented the methods on the I2C type. They must match
    11  // the i2cController interface method signatures type to type perfectly.
    12  // If not implementing the I2C type please remove your target from the build tags
    13  // at the top of this file.
    14  var _ interface { // 2
    15  	Configure(config I2CConfig) error
    16  	Tx(addr uint16, w, r []byte) error
    17  	SetBaudRate(br uint32) error
    18  } = (*I2C)(nil)
    19  
    20  // TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
    21  //
    22  // Deprecated: use 100 * machine.KHz or 400 * machine.KHz instead.
    23  const (
    24  	TWI_FREQ_100KHZ = 100000
    25  	TWI_FREQ_400KHZ = 400000
    26  )
    27  
    28  var (
    29  	errI2CWriteTimeout       = errors.New("I2C timeout during write")
    30  	errI2CReadTimeout        = errors.New("I2C timeout during read")
    31  	errI2CBusReadyTimeout    = errors.New("I2C timeout on bus ready")
    32  	errI2CSignalStartTimeout = errors.New("I2C timeout on signal start")
    33  	errI2CSignalReadTimeout  = errors.New("I2C timeout on signal read")
    34  	errI2CSignalStopTimeout  = errors.New("I2C timeout on signal stop")
    35  	errI2CAckExpected        = errors.New("I2C error: expected ACK not NACK")
    36  	errI2CBusError           = errors.New("I2C bus error")
    37  	errI2COverflow           = errors.New("I2C receive buffer overflow")
    38  	errI2COverread           = errors.New("I2C transmit buffer overflow")
    39  	errI2CNotImplemented     = errors.New("I2C operation not yet implemented")
    40  )
    41  
    42  // I2CTargetEvent reflects events on the I2C bus
    43  type I2CTargetEvent uint8
    44  
    45  const (
    46  	// I2CReceive indicates target has received a message from the controller.
    47  	I2CReceive I2CTargetEvent = iota
    48  
    49  	// I2CRequest indicates the controller is expecting a message from the target.
    50  	I2CRequest
    51  
    52  	// I2CFinish indicates the controller has ended the transaction.
    53  	//
    54  	// I2C controllers can chain multiple receive/request messages without
    55  	// relinquishing the bus by doing 'restarts'.  I2CFinish indicates the
    56  	// bus has been relinquished by an I2C 'stop'.
    57  	I2CFinish
    58  )
    59  
    60  // I2CMode determines if an I2C peripheral is in Controller or Target mode.
    61  type I2CMode int
    62  
    63  const (
    64  	// I2CModeController represents an I2C peripheral in controller mode.
    65  	I2CModeController I2CMode = iota
    66  
    67  	// I2CModeTarget represents an I2C peripheral in target mode.
    68  	I2CModeTarget
    69  )
    70  
    71  // WriteRegister transmits first the register and then the data to the
    72  // peripheral device.
    73  //
    74  // Many I2C-compatible devices are organized in terms of registers. This method
    75  // is a shortcut to easily write to such registers. Also, it only works for
    76  // devices with 7-bit addresses, which is the vast majority.
    77  func (i2c *I2C) WriteRegister(address uint8, register uint8, data []byte) error {
    78  	buf := make([]uint8, len(data)+1)
    79  	buf[0] = register
    80  	copy(buf[1:], data)
    81  	return i2c.Tx(uint16(address), buf, nil)
    82  }
    83  
    84  // ReadRegister transmits the register, restarts the connection as a read
    85  // operation, and reads the response.
    86  //
    87  // Many I2C-compatible devices are organized in terms of registers. This method
    88  // is a shortcut to easily read such registers. Also, it only works for devices
    89  // with 7-bit addresses, which is the vast majority.
    90  func (i2c *I2C) ReadRegister(address uint8, register uint8, data []byte) error {
    91  	return i2c.Tx(uint16(address), []byte{register}, data)
    92  }