-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ina260: Driver, initial commit for review. #64
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2018 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
// Package ina219 controls a Texas Instruments ina260 current, | ||
// voltage and power monitor IC over an i2c bus. | ||
// | ||
// # Datasheet | ||
// | ||
// http://www.ti.com/lit/ds/symlink/ina260.pdf | ||
package ina260 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ina260_test | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"periph.io/x/conn/v3/i2c/i2creg" | ||
"periph.io/x/devices/v3/ina260" | ||
"periph.io/x/host/v3" | ||
) | ||
|
||
func main() { | ||
if _, err := host.Init(); err != nil { | ||
fmt.Println(err) | ||
} | ||
|
||
busNumber := 0 | ||
bus, err := i2creg.Open(fmt.Sprintf("/dev/i2c-%d", busNumber)) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer bus.Close() | ||
|
||
// Fuel gauge | ||
fuelGauge := ina260.New(bus) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for { | ||
f, err := fuelGauge.Read() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Printf("%f V %f A %f W", f.Voltage, f.Current, f.Power) | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2023 The Periph Authors. All rights reserved. | ||
// Use of this source code is governed under the Apache License, Version 2.0 | ||
// that can be found in the LICENSE file. | ||
|
||
package ina260 | ||
|
||
import ( | ||
"periph.io/x/conn/v3/i2c" | ||
) | ||
|
||
const ( | ||
INA260_CHIP_ID uint8 = 0x40 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not export these. |
||
INA260_CONFIG uint8 = 0x00 // CONFIGURATION REGISTER (R/W) | ||
INA260_CURRENT uint8 = 0x01 // SHUNT VOLTAGE REGISTER (R) | ||
INA260_BUSVOLTAGE uint8 = 0x02 // BUS VOLTAGE REGISTER (R) | ||
INA260_POWER uint8 = 0x03 // POWER REGISTER (R) | ||
INA260_MASK_ENABLE uint8 = 0x06 // MASK ENABLE REGISTER (R/W) | ||
INA260_ALERT_LIMIT uint8 = 0x07 // ALERT LIMIT REGISTER (R/W) | ||
INA260_MFG_UID uint8 = 0xFE // MANUFACTURER UNIQUE ID REGISTER (R) | ||
INA260_DIE_UID uint8 = 0xFF // DIE UNIQUE ID REGISTER (R) | ||
|
||
) | ||
|
||
type Power struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please document exported symbols. |
||
Current float64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use types in https://pkg.go.dev/periph.io/x/conn/[email protected]/physic |
||
Voltage float64 | ||
Power float64 | ||
} | ||
|
||
type ina260 struct { | ||
Conn *i2c.Dev | ||
Power Power | ||
} | ||
|
||
func New(bus i2c.Bus) *ina260 { | ||
|
||
dev := &i2c.Dev{Bus: bus, Addr: uint16(INA260_CHIP_ID)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not an ID, this is an address. I'd put 0x40 directly as is there and would remove the constant. |
||
power := Power{ | ||
Current: 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary. Memory allocation in Go is zero-initialized. |
||
Voltage: 0, | ||
Power: 0, | ||
} | ||
|
||
ina260 := &ina260{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can return this without using a named variable. |
||
Conn: dev, | ||
Power: power, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is unnecessary. |
||
} | ||
|
||
return ina260 | ||
} | ||
|
||
func (i *ina260) Read() (Power, error) { | ||
|
||
var power Power | ||
|
||
reg := []byte{byte(INA260_CURRENT)} | ||
currentBytes := make([]byte, 2) | ||
if err := i.Conn.Tx(reg, currentBytes); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
return power, err | ||
} | ||
|
||
reg = []byte{byte(INA260_BUSVOLTAGE)} | ||
voltageBytes := make([]byte, 2) | ||
if err := i.Conn.Tx(reg, voltageBytes); err != nil { | ||
return power, err | ||
} | ||
|
||
reg = []byte{byte(INA260_POWER)} | ||
powerBytes := make([]byte, 2) | ||
if err := i.Conn.Tx(reg, powerBytes); err != nil { | ||
return power, err | ||
} | ||
|
||
amps := uint16(currentBytes[0])<<8 + uint16(currentBytes[1]) | ||
volts := uint16(voltageBytes[0])<<8 + uint16(voltageBytes[1]) | ||
watts := uint16(powerBytes[0])<<8 + uint16(powerBytes[1]) | ||
|
||
power.Voltage = 0.00125 * float64(volts) | ||
power.Current = 0.00125 * float64(amps) | ||
power.Power = 0.01 * float64(watts) // 10mW/bit | ||
|
||
return power, nil | ||
} | ||
|
||
func ManufacturerId(d *i2c.Dev) (uint16, error) { | ||
reg := []byte{byte(INA260_MFG_UID)} | ||
manufacturerBytes := make([]byte, 2) | ||
if err := d.Tx(reg, manufacturerBytes); err != nil { | ||
return 0, err | ||
} | ||
manufacturerID := uint16(manufacturerBytes[0])<<8 + uint16(manufacturerBytes[1]) | ||
return manufacturerID, nil | ||
} | ||
|
||
func DieId(d *i2c.Dev) (uint16, error) { | ||
reg := []byte{byte(INA260_DIE_UID)} | ||
dieBytes := make([]byte, 2) | ||
if err := d.Tx(reg, dieBytes); err != nil { | ||
return 0, err | ||
} | ||
dieID := uint16(dieBytes[0])<<8 + uint16(dieBytes[1]) | ||
return dieID, nil | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please update.