The scripts and the results mostly speak for themselves. smbus
returns good data when I use the standard I2C GPIO pins, but the pigpio
bit banging I2C returns what looks like random data.
What might I try to debug this?
note: I didn't forget to move the SDL and SCA leads to the appropriate pins. Using 3.3V to supply the DS3231 Real Time Clock.
smbus works. When I convert these back from BCD to ints, I get a reasonable time object, with seconds increasing at 1 Hz.
import smbus
import pigpio
from binascii import hexlify
ADDR = 0x68
start = 0x00
nbytes = 7
baud = 9600
bus = smbus.SMBus(1)
pi = pigpio.pi()
for i in range(3):
seven_bytes = bus.read_i2c_block_data(ADDR, start, nbytes)
print "seven_bytes: ", seven_bytes
pi.stop()
""" RETURNS
seven_bytes: [24, 9, 21, 1, 19, 3, 24]
seven_bytes: [24, 9, 21, 1, 19, 3, 24]
seven_bytes: [24, 9, 21, 1, 19, 3, 24]
"""
pigpio bb returns random bytes.
import pigpio
from binascii import hexlify
ADDR = 0x68
start = 0x00 # unused
nbytes = 7
baud = 9600 # have tried several
SDA = 4
SCL = 17
pi = pigpio.pi()
pi.set_pull_up_down(SDA, pigpio.PUD_UP)
pi.set_pull_up_down(SCL, pigpio.PUD_UP)
pi.bb_i2c_open(SDA, SCL, baud)
for i in range(3):
count, data = pi.bb_i2c_zip(SDA, [4, ADDR, 2, 6, nbytes, 3, 0])
print "count, hexlify(data): ", count, hexlify(data)
pi.bb_i2c_close(SDA)
pi.stop()
""" RETURNS
count, hexlify(data): 7 004184bba180b9
count, hexlify(data): 7 1c8b001ac01218
count, hexlify(data): 7 15011303180041
"""
edit 1: per @Joan's comment:
pigs, the socket interface to pigpio also returns random bytes. The following are the pigs equivalents to bb_i2c_open, bb_i2c_zip, and bb_i2c_close. All bytes are non-repeating.
pi@raspberrypi:~ $ pigs bi2co 4 17 9600
pi@raspberrypi:~ $ pigs bi2cz 4 4 0x68 2 6 7 3 0
7 187 161 128 185 28 139 0
pi@raspberrypi:~ $ pigs bi2cz 4 4 0x68 2 6 7 3 0
7 25 128 71 25 22 1 19
pi@raspberrypi:~ $ pigs bi2cz 4 4 0x68 2 6 7 3 0
7 3 24 0 65 132 187 161
pi@raspberrypi:~ $ pigs bi2cc 4
Or reading only once and close/open just to see what happens:
pi@raspberrypi:~ $ pigs bi2co 4 17 9600
pi@raspberrypi:~ $ pigs bi2cz 4 4 0x68 2 6 7 3 0
7 65 132 187 161 128 185 28
pi@raspberrypi:~ $ pigs bi2cc 4
pi@raspberrypi:~ $ pigs bi2co 4 17 9600
pi@raspberrypi:~ $ pigs bi2cz 4 4 0x68 2 6 7 3 0
7 139 0 25 192 80 7 22
pi@raspberrypi:~ $ pigs bi2cc 4
pi@raspberrypi:~ $ pigs bi2co 4 17 9600
pi@raspberrypi:~ $ pigs bi2cz 4 4 0x68 2 6 7 3 0
7 1 19 3 24 0 65 132
pi@raspberrypi:~ $ pigs bi2cc 4
edit 2: per @Joan's answer (not finished posting results, test in progress):
Using smbus:
seven_bytes: [1, 2, 3, 4, 5, 6, 7]
seven_bytes: [8, 9, 10, 11, 12, 13, 14]
seven_bytes: [15, 16, 16, 16, 16, 16, 16]
Using pigpio:
count, data: 7
hexlify(data): 01020304050607
count, data: 7
hexlify(data): 08090a0b0c0d0e
count, data: 7
hexlify(data): 0f101010101010
pigs
? Oh I found the link, let me give it a try!bi2co
, then those two, thenbi2cz
,bi2cc
?