-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c-test.py
87 lines (73 loc) · 2.07 KB
/
i2c-test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env python
# encoding: utf-8
"""
Created by Peter Huewe on 2009-10-26.
Copyright 2009 Peter Huewe <peterhuewe@gmx.de>
Based on the spi testscript from Sean Nelson
This file is part of pyBusPirate.
pyBusPirate is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pyBusPirate is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with pyBusPirate. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
from pyBusPirateLite.I2C import *
""" enter binary mode """
def i2c_write_data(data):
i2c.send_start_bit()
i2c.bulk_trans(len(data),data)
i2c.send_stop_bit()
def i2c_read_bytes(address, numbytes, ret=False):
data_out=[]
i2c.send_start_bit()
i2c.bulk_trans(len(address),address)
while numbytes > 0:
if not ret:
print ord(i2c.read_byte())
else:
data_out.append(ord(i2c.read_byte()))
if numbytes > 1:
i2c.send_ack()
numbytes-=1
i2c.send_nack()
i2c.send_stop_bit()
if ret:
return data_out
if __name__ == '__main__':
i2c = I2C("/dev/ttyUSB0", 115200)
print "Entering binmode: ",
if i2c.BBmode():
print "OK."
else:
print "failed."
sys.exit()
print "Entering raw I2C mode: ",
if i2c.enter_I2C():
print "OK."
else:
print "failed."
sys.exit()
print "Configuring I2C."
if not i2c.cfg_pins(I2CPins.POWER | I2CPins.PULLUPS):
print "Failed to set I2C peripherals."
sys.exit()
if not i2c.set_speed(I2CSpeed._50KHZ):
print "Failed to set I2C Speed."
sys.exit()
i2c.timeout(0.2)
print "Reading EEPROM."
i2c_write_data([0xa0, 0,0, 1, 2,3,4,5,6,7,8,9])
i2c_write_data([0xa0, 0,0])
print i2c_read_bytes([0xa1],5)
print "Reset Bus Pirate to user terminal: "
if i2c.resetBP():
print "OK."
else:
print "failed."
sys.exit()