Skip to content

Commit 705f474

Browse files
committed
adt7410: add connection test and for that matter connection method
Signed-off-by: deadprogram <[email protected]>
1 parent ce9c93f commit 705f474

File tree

4 files changed

+125
-29
lines changed

4 files changed

+125
-29
lines changed

adt7410/adt7410.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
// Package adt7410 provides a driver for the adt7410 I2C Temperature Sensor.
2+
//
3+
// Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADT7410.pdf
4+
//
15
package adt7410 // import "tinygo.org/x/drivers/adt7410"
26

37
import (
@@ -22,70 +26,67 @@ func (e Error) Error() string {
2226
}
2327

2428
type Device struct {
25-
bus drivers.I2C
26-
buf []byte
27-
addr uint8
29+
bus drivers.I2C
30+
buf []byte
31+
Address uint8
2832
}
2933

30-
// New returns ADT7410 device for the provided I2C bus and address. The ADT7410
31-
// has a default address of 0x48 (1001000). The last 2 bits of the address
34+
// New returns ADT7410 device for the provided I2C bus using default address.
35+
// of 0x48 (1001000). To use multiple ADT7410 devices, the last 2 bits of the address
3236
// can be set using by connecting to the A1 and A0 pins to VDD or GND (for a
3337
// total of up to 4 devices on a I2C bus). Also note that 10k pullups are
3438
// recommended for the SDA and SCL lines.
35-
func New(i2c drivers.I2C, addressBits uint8) *Device {
39+
func New(i2c drivers.I2C) *Device {
3640
return &Device{
37-
bus: i2c,
38-
buf: make([]byte, 2),
39-
addr: Address | (addressBits & 0x3),
41+
bus: i2c,
42+
buf: make([]byte, 2),
43+
Address: Address,
4044
}
4145
}
4246

43-
func (dev *Device) Configure() (err error) {
44-
45-
// verify the chip ID
46-
// TODO: According to datasheet, the check below should work; however
47-
// this does not seem to be working right, but is not exactly
48-
// necessary, so can revisit later to see if there is a bug
49-
//id := dev.ReadByte(RegID) & 0xF8
50-
//if id != 0xC8 {
51-
// err = ErrInvalidID
52-
//}
53-
47+
// Configure the ADT7410 device.
48+
func (d *Device) Configure() (err error) {
5449
// reset the chip
55-
dev.writeByte(RegReset, 0xFF)
50+
d.writeByte(RegReset, 0xFF)
5651
time.Sleep(10 * time.Millisecond)
5752
return
53+
}
5854

55+
// Connected returns whether sensor has been found.
56+
func (d *Device) Connected() bool {
57+
data := []byte{0}
58+
d.bus.ReadRegister(uint8(d.Address), RegID, data)
59+
return data[0]&0xF8 == 0xC8
5960
}
6061

6162
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000)
6263
func (d *Device) ReadTemperature() (temperature int32, err error) {
6364
return (int32(d.readUint16(RegTempValueMSB)) * 1000) / 128, nil
6465
}
6566

66-
// ReadTempC returns the value in the temperature value register, in Celcius
67+
// ReadTempC returns the value in the temperature value register, in Celsius.
6768
func (d *Device) ReadTempC() float32 {
6869
t := d.readUint16(RegTempValueMSB)
6970
return float32(int(t)) / 128.0
7071
}
7172

72-
// ReadTempF returns the value in the temperature value register, in Fahrenheit
73+
// ReadTempF returns the value in the temperature value register, in Fahrenheit.
7374
func (d *Device) ReadTempF() float32 {
7475
return d.ReadTempC()*1.8 + 32.0
7576
}
7677

7778
func (d *Device) writeByte(reg uint8, data byte) {
7879
d.buf[0] = reg
7980
d.buf[1] = data
80-
d.bus.Tx(uint16(d.addr), d.buf, nil)
81+
d.bus.Tx(uint16(d.Address), d.buf, nil)
8182
}
8283

8384
func (d *Device) readByte(reg uint8) byte {
84-
d.bus.ReadRegister(d.addr, reg, d.buf)
85+
d.bus.ReadRegister(d.Address, reg, d.buf)
8586
return d.buf[0]
8687
}
8788

8889
func (d *Device) readUint16(reg uint8) uint16 {
89-
d.bus.ReadRegister(d.addr, reg, d.buf)
90+
d.bus.ReadRegister(d.Address, reg, d.buf)
9091
return uint16(d.buf[0])<<8 | uint16(d.buf[1])
9192
}

adt7410/adt7410_test.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package adt7410
2+
3+
import (
4+
"testing"
5+
6+
qt "github.com/frankban/quicktest"
7+
"tinygo.org/x/drivers/tester"
8+
)
9+
10+
func TestDefaultI2CAddress(t *testing.T) {
11+
c := qt.New(t)
12+
bus := tester.NewI2CBus(c)
13+
dev := New(bus)
14+
c.Assert(dev.Address, qt.Equals, uint8(Address))
15+
}
16+
17+
func TestWhoAmI(t *testing.T) {
18+
c := qt.New(t)
19+
bus := tester.NewI2CBus(c)
20+
fake := tester.NewI2CDevice(c, Address)
21+
fake.SetupRegisters(defaultRegisters())
22+
bus.AddDevice(fake)
23+
24+
dev := New(bus)
25+
c.Assert(dev.Connected(), qt.Equals, true)
26+
27+
fake.SetupRegister(RegID, 0x99)
28+
c.Assert(dev.Connected(), qt.Equals, false)
29+
}
30+
31+
// defaultRegisters returns the default values for all of the device's registers.
32+
// see table 22 on page 27 of the datasheet.
33+
func defaultRegisters() []uint8 {
34+
return []uint8{
35+
RegTempValueMSB: 0,
36+
RegTempValueLSB: 0,
37+
RegStatus: 0,
38+
RegConfig: 0,
39+
RegTHIGHMsbReg: 0x20,
40+
RegTHIGHLsbReg: 0,
41+
RegTLOWMsbReg: 0x05,
42+
RegTLOWLsbReg: 0,
43+
RegTCRITMsbReg: 0x49,
44+
RegTCRITLsbReg: 0x80,
45+
RegTHYSTReg: 0x05,
46+
RegID: 0xC8,
47+
RegReset: 0,
48+
}
49+
}

adt7410/registers.go

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,33 @@
11
package adt7410
22

3+
// 0x00 Temperature value most significant byte 0x00
4+
// 0x01 Temperature value least significant byte 0x00
5+
// 0x02 Status 0x00
6+
// 0x03 Configuration 0x00
7+
// 0x04 THIGH setpoint most significant byte 0x20 (64°C)
8+
// 0x05 THIGH setpoint least significant byte 0x00 (64°C)
9+
// 0x06 TLOW setpoint most significant byte 0x05 (10°C)
10+
// 0x07 TLOW setpoint least significant byte 0x00 (10°C)
11+
// 0x08 TCRIT setpoint most significant byte 0x49 (147°C)
12+
// 0x09 TCRIT setpoint least significant byte 0x80 (147°C)
13+
// 0x0A THYST setpoint 0x05 (5°C)
14+
// 0x0B ID 0xCX
15+
// 0x0C Reserved 0xXX
16+
// 0x0D Reserved 0xXX
17+
// 0x2E Reserved 0xXX
18+
// 0x2F Software reset 0xXX
19+
320
const (
4-
// Default I2C address
21+
// Address is default I2C address.
522
Address = 0x48
23+
// Address1 is for first device, aka the default.
24+
Address1 = Address
25+
// Address2 is for second device.
26+
Address2 = 0x49
27+
// Address3 is for third device.
28+
Address3 = 0x4A
29+
// Address4 is for fourth device.
30+
Address4 = 0x4B
631

732
// Temperature Value MSB Register
833
RegTempValueMSB = 0x0
@@ -16,7 +41,28 @@ const (
1641
// Config Register
1742
RegConfig = 0x3
1843

19-
// ID Register
44+
// THIGH setpoint most significant byte 0x20 (64°C)
45+
RegTHIGHMsbReg = 0x4
46+
47+
// THIGH setpoint least significant byte 0x00 (64°C)
48+
RegTHIGHLsbReg = 0x5
49+
50+
// TLOW setpoint most significant byte 0x05 (10°C)
51+
RegTLOWMsbReg = 0x6
52+
53+
// TLOW setpoint least significant byte 0x00 (10°C)
54+
RegTLOWLsbReg = 0x7
55+
56+
// TCRIT setpoint most significant byte 0x49 (147°C)
57+
RegTCRITMsbReg = 0x8
58+
59+
// TCRIT setpoint least significant byte 0x80 (147°C)
60+
RegTCRITLsbReg = 0x9
61+
62+
// THYST setpoint 0x05 (5°C)
63+
RegTHYSTReg = 0xA
64+
65+
// ID Register (0xCx)
2066
RegID = 0x0B
2167

2268
// Software Reset Register

examples/adt7410/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010

1111
var (
1212
i2c = &machine.I2C0
13-
sensor = adt7410.New(i2c, 0)
13+
sensor = adt7410.New(i2c)
1414
)
1515

1616
func main() {

0 commit comments

Comments
 (0)