Skip to content

Commit 6d431e0

Browse files
authored
Add I2C INA219 driver (#705)
INA219: add INA219 driver
1 parent f308f8f commit 6d431e0

File tree

7 files changed

+673
-0
lines changed

7 files changed

+673
-0
lines changed

examples/ina219/main.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"machine"
5+
"time"
6+
7+
"tinygo.org/x/drivers/ina219"
8+
)
9+
10+
func main() {
11+
machine.I2C0.Configure(machine.I2CConfig{})
12+
13+
dev := ina219.New(machine.I2C0)
14+
dev.Configure()
15+
16+
for {
17+
busVoltage, shuntVoltage, current, power, err := dev.Measurements()
18+
if err != nil {
19+
println("Error reading measurements", err)
20+
}
21+
22+
println("Bus Voltage:", busVoltage, "V")
23+
println("Shunt Voltage:", shuntVoltage/100, "mV")
24+
println("Current:", current, "mA")
25+
println("Power:", power, "mW")
26+
27+
time.Sleep(10 * time.Millisecond)
28+
}
29+
}

ina219/config.go

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package ina219
2+
3+
type Config struct {
4+
// BusVoltageRange sets the bus voltage range.
5+
BusVoltageRange BusVoltageRange
6+
7+
// PGA sets the programmable gain amplifier.
8+
PGA PGA
9+
10+
// BusADC sets the bus ADC resolution.
11+
BusADC BusADC
12+
13+
// ShuntADC sets the shunt ADC resolution.
14+
ShuntADC ShuntADC
15+
16+
// Mode sets the operating mode.
17+
Mode Mode
18+
19+
// Calibration sets the calibration value for the expected
20+
// voltage and current values.
21+
Calibration Calibration
22+
23+
// 1000 / uA per bit
24+
CurrentDivider float32
25+
26+
// 1mW per bit
27+
PowerMultiplier float32
28+
}
29+
30+
// RegisterValue returns the register value of the configuration.
31+
func (c *Config) RegisterValue() uint16 {
32+
return c.BusVoltageRange.RegisterValue() |
33+
c.PGA.RegisterValue() |
34+
c.BusADC.RegisterValue() |
35+
c.ShuntADC.RegisterValue() |
36+
c.Mode.RegisterValue()
37+
}
38+
39+
// Generate a new configuration from a register value.
40+
func NewConfig(config int16, calibration int16) Config {
41+
return Config{
42+
BusVoltageRange: BusVoltageRange(config >> 13 & 0x1),
43+
PGA: PGA(config >> 11 & 0x3),
44+
BusADC: BusADC(config >> 7 & 0xF),
45+
ShuntADC: ShuntADC(config >> 3 & 0xF),
46+
Mode: Mode(config & 0x7),
47+
Calibration: Calibration(calibration),
48+
}
49+
}
50+
51+
// Configurations from
52+
// https://github.com/adafruit/Adafruit_INA219/blob/master/Adafruit_INA219.cpp
53+
var (
54+
// Config32V2A is a configuration for a 32V 2A range.
55+
Config32V2A = Config{
56+
BusVoltageRange: Range32V,
57+
PGA: PGA8,
58+
BusADC: ADC12,
59+
ShuntADC: SADC12,
60+
Mode: ModeContShuntBus,
61+
Calibration: Calibration16V400mA,
62+
CurrentDivider: 10.0,
63+
PowerMultiplier: 2.0,
64+
}
65+
66+
// Config32V1A is a configuration for a 32V 1A range.
67+
Config32V1A = Config{
68+
BusVoltageRange: Range32V,
69+
PGA: PGA8,
70+
BusADC: ADC12,
71+
ShuntADC: SADC12,
72+
Mode: ModeContShuntBus,
73+
Calibration: Calibration32V1A,
74+
CurrentDivider: 25.0,
75+
PowerMultiplier: 0.8,
76+
}
77+
78+
// Config16V400mA is a configuration for a 16V 400mA range.
79+
Config16V400mA = Config{
80+
BusVoltageRange: Range16V,
81+
PGA: PGA1,
82+
BusADC: ADC12,
83+
ShuntADC: SADC12,
84+
Mode: ModeContShuntBus,
85+
Calibration: Calibration16V400mA,
86+
CurrentDivider: 20.0,
87+
PowerMultiplier: 1.0,
88+
}
89+
)
90+
91+
// BusVoltageRange is the bus voltage range.
92+
type BusVoltageRange int8
93+
94+
const (
95+
Range16V BusVoltageRange = 0 // 0-16V
96+
Range32V BusVoltageRange = 1 // 0-32V
97+
)
98+
99+
func (r BusVoltageRange) RegisterValue() uint16 {
100+
return uint16(r) << 13
101+
}
102+
103+
// PGA is the programmable gain amplifier.
104+
type PGA int8
105+
106+
const (
107+
PGA1 PGA = 0 // 40mV
108+
PGA2 PGA = 1 // 80mV
109+
PGA4 PGA = 2 // 160mV
110+
PGA8 PGA = 3 // 320mV
111+
)
112+
113+
func (p PGA) RegisterValue() uint16 {
114+
return uint16(p) << 11
115+
}
116+
117+
// BusADC is the bus ADC resolution.
118+
type BusADC int8
119+
120+
const (
121+
ADC9 BusADC = 0 // 9-bit
122+
ADC10 BusADC = 1 // 10-bit
123+
ADC11 BusADC = 2 // 11-bit
124+
ADC12 BusADC = 3 // 12-bit
125+
)
126+
127+
func (b BusADC) RegisterValue() uint16 {
128+
return uint16(b) << 7
129+
}
130+
131+
// ShuntADC is the shunt ADC resolution.
132+
type ShuntADC int8
133+
134+
const (
135+
SADC9 ShuntADC = 0 // 9-bit
136+
SADC10 ShuntADC = 1 // 10-bit
137+
SADC11 ShuntADC = 2 // 11-bit
138+
SADC12 ShuntADC = 3 // 12-bit
139+
)
140+
141+
func (s ShuntADC) RegisterValue() uint16 {
142+
return uint16(s) << 3
143+
}
144+
145+
// Mode is the operating mode.
146+
type Mode int8
147+
148+
const (
149+
ModePowerDown Mode = 0 // power-down
150+
ModeTrigShunt Mode = 1 // triggered shunt voltage
151+
ModeTrigBus Mode = 2 // triggered bus voltage
152+
ModeTrigShuntBus Mode = 3 // triggered shunt and bus voltage
153+
ModeADCOff Mode = 4 // ADC off
154+
ModeContShunt Mode = 5 // continuous shunt voltage
155+
ModeContBus Mode = 6 // continuous bus voltage
156+
ModeContShuntBus Mode = 7 // continuous shunt and bus voltage
157+
)
158+
159+
// ModeTriggered is a mask for triggered modes.
160+
const ModeTriggeredMask Mode = 0x4
161+
162+
// ModeTriggered returns true if the mode is a triggered mode.
163+
func ModeTriggered(m Mode) bool {
164+
return m != ModePowerDown && m&ModeTriggeredMask == 0
165+
}
166+
167+
func (m Mode) RegisterValue() uint16 {
168+
return uint16(m)
169+
}
170+
171+
// Calibration is the calibration register for the INA219. Values from:
172+
// https://github.com/adafruit/Adafruit_INA219/blob/master/Adafruit_INA219.cpp
173+
type Calibration uint16
174+
175+
const (
176+
Calibration32V2A Calibration = 4096
177+
Calibration32V1A Calibration = 10240
178+
Calibration16V400mA Calibration = 8192
179+
)
180+
181+
func (c Calibration) RegisterValue() uint16 {
182+
return uint16(c)
183+
}

ina219/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ina219
2+
3+
type ErrOverflow struct{}
4+
5+
func (e ErrOverflow) Error() string { return "overflow" }
6+
7+
type ErrNotReady struct{}
8+
9+
func (e ErrNotReady) Error() string { return "not ready" }
10+
11+
type ErrConfigMismatch struct{}
12+
13+
func (e ErrConfigMismatch) Error() string { return "config mismatch" }

0 commit comments

Comments
 (0)