Created
August 26, 2016 06:55
-
-
Save rxseger/ab3afb98b825aa445ca13df8dab08045 to your computer and use it in GitHub Desktop.
write 16-bit EEPROMs over I2C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# write 16-bit EEPROMs over I2C | |
import sys | |
import smbus | |
import time | |
bus = smbus.SMBus(1) | |
if len(sys.argv) < 2: | |
sys.stderr.write("usage: %s device-address\n" % (sys.argv[0],)) | |
raise SystemExit | |
device_address = int(sys.argv[1], 16) | |
data = sys.stdin.read() | |
def write(device_address, memory_address, data): | |
cmd = memory_address >> 8 | |
bytes = [memory_address & 0xff] + map(ord, data) | |
print "writing to %.2x at %.4x bytes %d" % (device_address, memory_address, len(data)) | |
return bus.write_i2c_block_data(device_address, cmd, bytes) | |
chunks = [] | |
i = 0 | |
while i * 16 < len(data): | |
chunk = data[i * 16:(i + 1) * 16] | |
print write(device_address, i * 16, chunk) | |
time.sleep(0.1) # needs some time to finish write, or fails [Errno 5] Input/output error | |
i += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment