github.com/aykevl/tinygo@v0.5.0/src/machine/i2c.go (about)

     1  // +build avr nrf sam stm32f103xx
     2  
     3  package machine
     4  
     5  // TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
     6  const (
     7  	TWI_FREQ_100KHZ = 100000
     8  	TWI_FREQ_400KHZ = 400000
     9  )
    10  
    11  // WriteRegister transmits first the register and then the data to the
    12  // peripheral device.
    13  //
    14  // Many I2C-compatible devices are organized in terms of registers. This method
    15  // is a shortcut to easily write to such registers. Also, it only works for
    16  // devices with 7-bit addresses, which is the vast majority.
    17  func (i2c I2C) WriteRegister(address uint8, register uint8, data []byte) error {
    18  	buf := make([]uint8, len(data)+1)
    19  	buf[0] = register
    20  	copy(buf[1:], data)
    21  	return i2c.Tx(uint16(address), buf, nil)
    22  }
    23  
    24  // ReadRegister transmits the register, restarts the connection as a read
    25  // operation, and reads the response.
    26  //
    27  // Many I2C-compatible devices are organized in terms of registers. This method
    28  // is a shortcut to easily read such registers. Also, it only works for devices
    29  // with 7-bit addresses, which is the vast majority.
    30  func (i2c I2C) ReadRegister(address uint8, register uint8, data []byte) error {
    31  	return i2c.Tx(uint16(address), []byte{register}, data)
    32  }