@@ -27,8 +27,9 @@ type Config struct{}
2727
2828// Errors list
2929var (
30- errNoPresence = errors .New ("Error: OneWire. No devices on the bus." )
31- errReadAddress = errors .New ("Error: OneWire. Read address error: CRC mismatch." )
30+ errNoPresence = errors .New ("Error: OneWire. No devices on the bus." )
31+ errTooManyDevices = errors .New ("Error: OneWire. Too many devices on the bus." )
32+ errReadAddress = errors .New ("Error: OneWire. Read address error: CRC mismatch." )
3233)
3334
3435// New creates a new GPIO 1-Wire connection.
@@ -46,7 +47,7 @@ func (d *Device) Configure(config Config) {}
4647func (d Device ) Reset () error {
4748 d .p .Configure (machine.PinConfig {Mode : machine .PinOutput })
4849 time .Sleep (480 * time .Microsecond )
49- d .p .Configure (machine.PinConfig {Mode : machine .PinInput })
50+ d .p .Configure (machine.PinConfig {Mode : machine .PinInputPullup })
5051 time .Sleep (70 * time .Microsecond )
5152 precence := d .p .Get ()
5253 time .Sleep (410 * time .Microsecond )
@@ -61,11 +62,11 @@ func (d Device) WriteBit(data uint8) {
6162 d .p .Configure (machine.PinConfig {Mode : machine .PinOutput })
6263 if data & 1 == 1 { // Send '1'
6364 time .Sleep (5 * time .Microsecond )
64- d .p .Configure (machine.PinConfig {Mode : machine .PinInput })
65+ d .p .Configure (machine.PinConfig {Mode : machine .PinInputPullup })
6566 time .Sleep (60 * time .Microsecond )
6667 } else { // Send '0'
6768 time .Sleep (60 * time .Microsecond )
68- d .p .Configure (machine.PinConfig {Mode : machine .PinInput })
69+ d .p .Configure (machine.PinConfig {Mode : machine .PinInputPullup })
6970 time .Sleep (5 * time .Microsecond )
7071 }
7172}
@@ -82,7 +83,7 @@ func (d Device) Write(data uint8) {
8283func (d Device ) ReadBit () (data uint8 ) {
8384 d .p .Configure (machine.PinConfig {Mode : machine .PinOutput })
8485 time .Sleep (3 * time .Microsecond )
85- d .p .Configure (machine.PinConfig {Mode : machine .PinInput })
86+ d .p .Configure (machine.PinConfig {Mode : machine .PinInputPullup })
8687 time .Sleep (8 * time .Microsecond )
8788 if d .p .Get () {
8889 data = 1
@@ -111,7 +112,7 @@ func (d Device) ReadAddress() ([]uint8, error) {
111112 for i := 0 ; i < 8 ; i ++ {
112113 romid [i ] = d .Read ()
113114 }
114- if d .Сrc8 (romid , 7 ) != romid [ 7 ] {
115+ if d .Сrc8 (romid ) != 0 {
115116 return nil , errReadAddress
116117 }
117118 return romid , nil
@@ -187,15 +188,22 @@ func (d Device) Search(cmd uint8) ([][]uint8, error) {
187188 }
188189 d .WriteBit (bit )
189190 }
191+ if d .Сrc8 (lastAddress ) != 0 {
192+ continue
193+ }
194+
190195 lastFork = lastZero
191196 copy (romIDs [romIndex ], lastAddress )
192197 romIndex ++
198+ if romIndex >= 32 {
199+ return romIDs , errTooManyDevices
200+ }
193201 }
194202 return romIDs [:romIndex :romIndex ], nil
195203}
196204
197205// Crc8 compute a Dallas Semiconductor 8 bit CRC.
198- func (d Device ) Сrc8 (buffer []uint8 , size int ) (crc uint8 ) {
206+ func (_ Device ) Сrc8 (buffer []uint8 ) (crc uint8 ) {
199207 // Dow-CRC using polynomial X^8 + X^5 + X^4 + X^0
200208 // Tiny 2x16 entry CRC table created by Arjen Lentz
201209 // 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) {
205213 0x00 , 0x9D , 0x23 , 0xBE , 0x46 , 0xDB , 0x65 , 0xF8 ,
206214 0x8C , 0x11 , 0xAF , 0x32 , 0xCA , 0x57 , 0xE9 , 0x74 ,
207215 }
208- for i := 0 ; i < size ; i ++ {
216+ for i := 0 ; i < len ( buffer ) ; i ++ {
209217 crc = buffer [i ] ^ crc // just re-using crc as intermediate
210218 crc = crc8_table [crc & 0x0f ] ^ crc8_table [16 + ((crc >> 4 )& 0x0f )]
211219 }
0 commit comments