mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-08-19 14:14:00 +00:00
onewire improvements
This commit is contained in:
committed by
Ron Evans
parent
07216d3051
commit
109cab4f3b
+2
-2
@@ -19,7 +19,7 @@ type OneWireDevice interface {
|
|||||||
Write(uint8)
|
Write(uint8)
|
||||||
Read() uint8
|
Read() uint8
|
||||||
Select([]uint8) error
|
Select([]uint8) error
|
||||||
Сrc8([]uint8, int) uint8
|
Сrc8([]uint8) uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
// Device wraps a connection to an 1-Wire devices.
|
// Device wraps a connection to an 1-Wire devices.
|
||||||
@@ -69,7 +69,7 @@ func (d Device) ReadTemperatureRaw(romid []uint8) ([]uint8, error) {
|
|||||||
for i := 0; i < 9; i++ {
|
for i := 0; i < 9; i++ {
|
||||||
spb[i] = d.owd.Read()
|
spb[i] = d.owd.Read()
|
||||||
}
|
}
|
||||||
if d.owd.Сrc8(spb, 8) != spb[8] {
|
if d.owd.Сrc8(spb) != 0 {
|
||||||
return nil, errReadTemperature
|
return nil, errReadTemperature
|
||||||
}
|
}
|
||||||
return spb[:2:2], nil
|
return spb[:2:2], nil
|
||||||
|
|||||||
+17
-9
@@ -27,8 +27,9 @@ type Config struct{}
|
|||||||
|
|
||||||
// Errors list
|
// Errors list
|
||||||
var (
|
var (
|
||||||
errNoPresence = errors.New("Error: OneWire. No devices on the bus.")
|
errNoPresence = errors.New("Error: OneWire. No devices on the bus.")
|
||||||
errReadAddress = errors.New("Error: OneWire. Read address error: CRC mismatch.")
|
errTooManyDevices = errors.New("Error: OneWire. Too many devices on the bus.")
|
||||||
|
errReadAddress = errors.New("Error: OneWire. Read address error: CRC mismatch.")
|
||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new GPIO 1-Wire connection.
|
// New creates a new GPIO 1-Wire connection.
|
||||||
@@ -46,7 +47,7 @@ func (d *Device) Configure(config Config) {}
|
|||||||
func (d Device) Reset() error {
|
func (d Device) Reset() error {
|
||||||
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
time.Sleep(480 * time.Microsecond)
|
time.Sleep(480 * time.Microsecond)
|
||||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInput})
|
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||||
time.Sleep(70 * time.Microsecond)
|
time.Sleep(70 * time.Microsecond)
|
||||||
precence := d.p.Get()
|
precence := d.p.Get()
|
||||||
time.Sleep(410 * time.Microsecond)
|
time.Sleep(410 * time.Microsecond)
|
||||||
@@ -61,11 +62,11 @@ func (d Device) WriteBit(data uint8) {
|
|||||||
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
if data&1 == 1 { // Send '1'
|
if data&1 == 1 { // Send '1'
|
||||||
time.Sleep(5 * time.Microsecond)
|
time.Sleep(5 * time.Microsecond)
|
||||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInput})
|
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||||
time.Sleep(60 * time.Microsecond)
|
time.Sleep(60 * time.Microsecond)
|
||||||
} else { // Send '0'
|
} else { // Send '0'
|
||||||
time.Sleep(60 * time.Microsecond)
|
time.Sleep(60 * time.Microsecond)
|
||||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInput})
|
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||||
time.Sleep(5 * time.Microsecond)
|
time.Sleep(5 * time.Microsecond)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -82,7 +83,7 @@ func (d Device) Write(data uint8) {
|
|||||||
func (d Device) ReadBit() (data uint8) {
|
func (d Device) ReadBit() (data uint8) {
|
||||||
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||||
time.Sleep(3 * time.Microsecond)
|
time.Sleep(3 * time.Microsecond)
|
||||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInput})
|
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||||
time.Sleep(8 * time.Microsecond)
|
time.Sleep(8 * time.Microsecond)
|
||||||
if d.p.Get() {
|
if d.p.Get() {
|
||||||
data = 1
|
data = 1
|
||||||
@@ -111,7 +112,7 @@ func (d Device) ReadAddress() ([]uint8, error) {
|
|||||||
for i := 0; i < 8; i++ {
|
for i := 0; i < 8; i++ {
|
||||||
romid[i] = d.Read()
|
romid[i] = d.Read()
|
||||||
}
|
}
|
||||||
if d.Сrc8(romid, 7) != romid[7] {
|
if d.Сrc8(romid) != 0 {
|
||||||
return nil, errReadAddress
|
return nil, errReadAddress
|
||||||
}
|
}
|
||||||
return romid, nil
|
return romid, nil
|
||||||
@@ -187,15 +188,22 @@ func (d Device) Search(cmd uint8) ([][]uint8, error) {
|
|||||||
}
|
}
|
||||||
d.WriteBit(bit)
|
d.WriteBit(bit)
|
||||||
}
|
}
|
||||||
|
if d.Сrc8(lastAddress) != 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
lastFork = lastZero
|
lastFork = lastZero
|
||||||
copy(romIDs[romIndex], lastAddress)
|
copy(romIDs[romIndex], lastAddress)
|
||||||
romIndex++
|
romIndex++
|
||||||
|
if romIndex >= 32 {
|
||||||
|
return romIDs, errTooManyDevices
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return romIDs[:romIndex:romIndex], nil
|
return romIDs[:romIndex:romIndex], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Crc8 compute a Dallas Semiconductor 8 bit CRC.
|
// Crc8 compute a Dallas Semiconductor 8 bit CRC.
|
||||||
func (d Device) Сrc8(buffer []uint8, size int) (crc uint8) {
|
func (_ Device) Сrc8(buffer []uint8) (crc uint8) {
|
||||||
// Dow-CRC using polynomial X^8 + X^5 + X^4 + X^0
|
// Dow-CRC using polynomial X^8 + X^5 + X^4 + X^0
|
||||||
// Tiny 2x16 entry CRC table created by Arjen Lentz
|
// Tiny 2x16 entry CRC table created by Arjen Lentz
|
||||||
// See http://lentz.com.au/blog/calculating-crc-with-a-tiny-32-entry-lookup-table
|
// See http://lentz.com.au/blog/calculating-crc-with-a-tiny-32-entry-lookup-table
|
||||||
@@ -205,7 +213,7 @@ func (d Device) Сrc8(buffer []uint8, size int) (crc uint8) {
|
|||||||
0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
|
0x00, 0x9D, 0x23, 0xBE, 0x46, 0xDB, 0x65, 0xF8,
|
||||||
0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74,
|
0x8C, 0x11, 0xAF, 0x32, 0xCA, 0x57, 0xE9, 0x74,
|
||||||
}
|
}
|
||||||
for i := 0; i < size; i++ {
|
for i := 0; i < len(buffer); i++ {
|
||||||
crc = buffer[i] ^ crc // just re-using crc as intermediate
|
crc = buffer[i] ^ crc // just re-using crc as intermediate
|
||||||
crc = crc8_table[crc&0x0f] ^ crc8_table[16+((crc>>4)&0x0f)]
|
crc = crc8_table[crc&0x0f] ^ crc8_table[16+((crc>>4)&0x0f)]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user