tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/ina260/ina260.go (about) 1 package ina260 2 3 import ( 4 "tinygo.org/x/drivers" 5 "tinygo.org/x/drivers/internal/legacy" 6 ) 7 8 // Device wraps an I2C connection to an INA260 device. 9 type Device struct { 10 bus drivers.I2C 11 Address uint16 12 } 13 14 // Config holds the configuration of the INA260 device. 15 type Config struct { 16 // One of AVGMODE_XXX 17 AverageMode byte 18 19 // One of CONVTIME_XXXXUSEC 20 VoltConvTime byte 21 22 // One of CONVTIME_XXXXUSEC 23 CurrentConvTime byte 24 25 // Multiple of MODE_XXXX 26 Mode byte 27 } 28 29 // New creates a new INA260 connection. The I2C bus must already be 30 // configured. 31 // 32 // This function only creates the Device object, it does not touch the device. 33 func New(bus drivers.I2C) Device { 34 return Device{ 35 bus: bus, 36 Address: Address, 37 } 38 } 39 40 // Configure sets up the device. 41 // 42 // This only needs to be called to override built-in defaults. By default, 43 // the device starts with: 44 // 45 // * AverageMode = AVGMODE_1 46 // * VoltConvTime = CONVTIME_1100USEC 47 // * CurrentConvTime = CONVTIME_1100USEC 48 // * Mode = MODE_CONTINUOUS | MODE_VOLTAGE | MODE_CURRENT 49 func (d *Device) Configure(cfg Config) { 50 var val uint16 51 52 val = uint16(cfg.AverageMode&0x7) << 9 53 val |= uint16(cfg.VoltConvTime&0x7) << 6 54 val |= uint16(cfg.CurrentConvTime&0x7) << 3 55 val |= uint16(cfg.Mode & 0x7) 56 57 d.WriteRegister(REG_CONFIG, val) 58 } 59 60 // Resets the device, setting all registers to default values 61 func (d *Device) Reset() { 62 d.WriteRegister(REG_CONFIG, 0x8000) 63 } 64 65 // Connected returns whether an INA260 has been found. 66 func (d *Device) Connected() bool { 67 return d.ReadRegister(REG_MANF_ID) == MANF_ID && 68 (d.ReadRegister(REG_DIE_ID)&DEVICE_ID_MASK) == DEVICE_ID 69 } 70 71 // Gets the measured current in µA (max resolution 1.25mA) 72 func (d *Device) Current() int32 { 73 val := d.ReadRegister(REG_CURRENT) 74 75 if val&0x8000 == 0 { 76 return int32(val) * 1250 77 } 78 79 // Two's complement, convert to signed int 80 return -(int32(^val) + 1) * 1250 81 } 82 83 // Gets the measured voltage in µV (max resolution 1.25mV) 84 func (d *Device) Voltage() int32 { 85 val := d.ReadRegister(REG_BUSVOLTAGE) 86 87 if val&0x8000 == 0 { 88 return int32(val) * 1250 89 } 90 91 // Two's complement, convert to signed int 92 return -(int32(^val) + 1) * 1250 93 } 94 95 // Gets the measured power in µW (max resolution 10mW) 96 func (d *Device) Power() int32 { 97 return int32(d.ReadRegister(REG_POWER)) * 10000 98 } 99 100 // Read a register 101 func (d *Device) ReadRegister(reg uint8) uint16 { 102 data := []byte{0, 0} 103 legacy.ReadRegister(d.bus, uint8(d.Address), reg, data) 104 return (uint16(data[0]) << 8) | uint16(data[1]) 105 } 106 107 // Write to a register 108 func (d *Device) WriteRegister(reg uint8, v uint16) { 109 data := []byte{0, 0} 110 data[0] = byte(v >> 8) 111 data[1] = byte(v & 0xff) 112 113 legacy.WriteRegister(d.bus, uint8(d.Address), reg, data) 114 }