Initial commit
This commit is contained in:
commit
124e954d3a
11
go.mod
Normal file
11
go.mod
Normal file
@ -0,0 +1,11 @@
|
||||
module git.blackfinn.de/solar/ina3221
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
gotest.tools/v3 v3.5.0
|
||||
periph.io/x/conn/v3 v3.7.0
|
||||
periph.io/x/host/v3 v3.8.2
|
||||
)
|
||||
|
||||
require github.com/google/go-cmp v0.5.9 // indirect
|
9
go.sum
Normal file
9
go.sum
Normal file
@ -0,0 +1,9 @@
|
||||
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
|
||||
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
|
||||
gotest.tools/v3 v3.5.0 h1:Ljk6PdHdOhAb5aDMWXjDLMMhph+BpztA4v1QdqEW2eY=
|
||||
gotest.tools/v3 v3.5.0/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU=
|
||||
periph.io/x/conn/v3 v3.7.0 h1:f1EXLn4pkf7AEWwkol2gilCNZ0ElY+bxS4WE2PQXfrA=
|
||||
periph.io/x/conn/v3 v3.7.0/go.mod h1:ypY7UVxgDbP9PJGwFSVelRRagxyXYfttVh7hJZUHEhg=
|
||||
periph.io/x/host/v3 v3.8.2 h1:ayKUDzgUCN0g8+/xM9GTkWaOBhSLVcVHGTfjAOi8OsQ=
|
||||
periph.io/x/host/v3 v3.8.2/go.mod h1:yFL76AesNHR68PboofSWYaQTKmvPXsQH2Apvp/ls/K4=
|
105
ina3221.go
Normal file
105
ina3221.go
Normal file
@ -0,0 +1,105 @@
|
||||
package ina3221
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"log"
|
||||
|
||||
"periph.io/x/conn/v3/i2c"
|
||||
"periph.io/x/conn/v3/i2c/i2creg"
|
||||
"periph.io/x/conn/v3/mmr"
|
||||
"periph.io/x/host/v3"
|
||||
)
|
||||
|
||||
type Channel uint8
|
||||
|
||||
const (
|
||||
Channel_1 Channel = 1
|
||||
Channel_2 Channel = 3
|
||||
Channel_3 Channel = 5
|
||||
|
||||
current_factor float64 = 40e-6
|
||||
voltage_factor float64 = 8e-3
|
||||
)
|
||||
|
||||
var (
|
||||
logger log.Logger = *log.Default()
|
||||
|
||||
mem_dev mmr.Dev8
|
||||
bus i2c.BusCloser
|
||||
i2c_dev i2c.Dev
|
||||
)
|
||||
|
||||
func start(slave_addr uint16) error {
|
||||
logger.SetPrefix("ina3112: ")
|
||||
logger.Println("Starting")
|
||||
|
||||
var err error
|
||||
_, err = host.Init()
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
}
|
||||
bus, err = i2creg.Open("")
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
}
|
||||
i2c_dev = i2c.Dev{
|
||||
Bus: bus,
|
||||
Addr: slave_addr,
|
||||
}
|
||||
mem_dev = mmr.Dev8{
|
||||
Conn: &i2c_dev,
|
||||
Order: binary.BigEndian,
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func stop() error {
|
||||
return bus.Close()
|
||||
}
|
||||
|
||||
func read_register(address uint8) (uint16, error) {
|
||||
return mem_dev.ReadUint16(address)
|
||||
}
|
||||
|
||||
func shunt_voltage(channel Channel) (float64, error) {
|
||||
res, err := read_register(uint8(channel))
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
return 0.0, err
|
||||
}
|
||||
res >>= 3
|
||||
return float64(res), err
|
||||
}
|
||||
|
||||
func bus_voltage(channel Channel) (float64, error) {
|
||||
res, err := read_register(uint8(channel) + 1)
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
return 0.0, err
|
||||
}
|
||||
res >>= 3
|
||||
return float64(res), err
|
||||
}
|
||||
|
||||
func Current(channel Channel) (float64, error) {
|
||||
res, err := shunt_voltage(channel)
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
return 0.0, err
|
||||
}
|
||||
|
||||
return res * current_factor, err
|
||||
}
|
||||
|
||||
func Voltage(channel Channel) (float64, error) {
|
||||
res, err := bus_voltage(channel)
|
||||
if err != nil {
|
||||
logger.Println(err)
|
||||
return 0.0, err
|
||||
}
|
||||
return res * voltage_factor, err
|
||||
}
|
||||
|
||||
func ManufacturerID() (uint16, error) {
|
||||
return read_register(0xfe)
|
||||
}
|
32
ina3221_test.go
Normal file
32
ina3221_test.go
Normal file
@ -0,0 +1,32 @@
|
||||
package ina3221
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
const (
|
||||
i2c_slave_addr uint16 = 0x40
|
||||
manufacturer_id uint16 = 0x5449
|
||||
)
|
||||
|
||||
func TestMain(t *testing.T) {
|
||||
err := start(i2c_slave_addr)
|
||||
assert.NilError(t, err)
|
||||
|
||||
var id uint16
|
||||
id, err = ManufacturerID()
|
||||
assert.NilError(t, err)
|
||||
assert.Assert(t, id == manufacturer_id)
|
||||
|
||||
for i := 1; i < 4; i++ {
|
||||
_, err = Current(Channel(i))
|
||||
assert.NilError(t, err)
|
||||
_, err = Voltage(Channel(i))
|
||||
assert.NilError(t, err)
|
||||
}
|
||||
|
||||
err = stop()
|
||||
assert.NilError(t, err)
|
||||
}
|
Loading…
Reference in New Issue
Block a user