I2C FRAM Reading And Writing
We highly encourage everyone to use our new Python package which is packed with features. This library is still supported, but is not recommended for new design.
The example below demonstrates how to read from and write to an I2C FRAM. Beyond that, it's also an excellent example of how to port an open-source Arduino library into a python script that can utilize the Binho Multi-Protocol USB Host Adapter.
This example is uses a 256Kbit I2C FRAM (MB85RC256V / Fujitsu) breakout board from our friends over at Adafruit. You can purchase it here.
1
from binhoHostAdapter import binhoHostAdapter
2
from binhoHostAdapter import binhoUtilities
3
4
MB85RC_DEFAULT_ADDRESS = 0xA0
5
MB85RC_SLAVE_ID = 0xF8
6
7
class Adafruit_FRAM_I2C:
8
9
# CONSTRUCTOR
10
def __init__(self, addr):
11
12
self.framInitialised = False
13
self.i2c_addr = addr
14
15
# PUBLIC FUNCTIONS
16
17
# initializes I2C and configures the chip
18
# call this function before doing anything else
19
def begin(self, adapter):
20
21
self.hostAdapter = adapter
22
23
# Make sure we're actually connected
24
manufacturerID = 0
25
productID = 0
26
deviceInfo = [manufacturerID, productID]
27
28
self.getDeviceID(deviceInfo)
29
30
manufacturerID = deviceInfo[0]
31
productID = deviceInfo[1]
32
33
deviceFound = True
34
35
if manufacturerID != 0x00A:
36
print("Unexpected Manufacturer ID: " + str(deviceInfo[0]))
37
deviceFound = False
38
39
if productID != 0x510:
40
print("Unexpected Product ID: " + str(productID))
41
deviceFound = False
42
43
# Everything seems to be properly initialized and connected
44
self._framInitialised = True;
45
46
return deviceFound
47
48
# Writes a byte at the specific FRAM address
49
def write8(self, framAddr, value):
50
51
self.hostAdapter.startI2C(0, self.i2c_addr)
52
self.hostAdapter.writeByteI2C(0, framAddr >> 8)
53
self.hostAdapter.writeByteI2C(0, framAddr & 0xFF)
54
self.hostAdapter.writeByteI2C(0, value)
55
self.hostAdapter.endI2C(0)
56
57
# Reads an 8 bit value from the specified FRAM address
58
def read8(self, framAddr):
59
60
self.hostAdapter.startI2C(0, self.i2c_addr)
61
self.hostAdapter.writeByteI2C(0, framAddr >> 8)
62
self.hostAdapter.writeByteI2C(0, framAddr & 0xFF)
63
self.hostAdapter.endI2C(0)
64
65
response = self.hostAdapter.readByteI2C(0, self.i2c_addr)
66
67
data = response.split()
68
69
if len(data) == 3:
70
return int(data[2])
71
else:
72
return 0
73
74
# Reads the Manufacturer ID and the Product ID frm the IC
75
def getDeviceID(self, deviceInfo):
76
77
self.hostAdapter.startI2C(0, MB85RC_SLAVE_ID)
78
self.hostAdapter.writeByteI2C(0, 0xA0)
79
self.hostAdapter.endI2C(0, True)
80
81
response = self.hostAdapter.readBytesI2C(0, MB85RC_SLAVE_ID, 3)
82
83
data =response.split()
84
85
if len(data) == 5:
86
deviceInfo[0] = (int(data[2]) << 4) + (int(data[3]) >> 4)
87
deviceInfo[1] = ((int(data[3]) & 0x0F) << 8) + int(data[4])
88
89
90
# Change this to match your COMPort
91
default_commport = "COM27"
92
93
print("I2C FRAM Example using Binho Host Adapter")
94
print("v1.0 -- Jonathan Georgino <[email protected]>")
95
print
96
97
utilities = binhoUtilities.binhoUtilities()
98
devices = utilities.listAvailableDevices()
99
100
if len(devices) == 1:
101
COMPORT = devices[0]
102
print("Found 1 attached adapter @ " + devices[0])
103
print
104
else:
105
COMPORT = default_commport
106
print("Found more than 1 attached adapter, using default port " + COMPORT)
107
print
108
109
print("Opening " + COMPORT + "...")
110
print
111
112
# create the binhoHostAdapter object
113
binhoDevice = binhoHostAdapter.binhoHostAdapter(COMPORT)
114
binhoDevice.setNumericalBase('10')
115
116
binhoDevice.setOperationMode(0, 'I2C')
117
binhoDevice.setPullUpStateI2C(0, '1')
118
binhoDevice.setClockI2C(0, 1000000)
119
120
framMemory = Adafruit_FRAM_I2C(MB85RC_DEFAULT_ADDRESS)
121
122
if framMemory.begin(binhoDevice) == True:
123
124
# found the I2C FRAM
125
126
memData = [0 for x in range(32)]
127
for i in range(32):
128
memData[i] = framMemory.read8(i)
129
130
print(memData)
131
132
for i in range(32):
133
framMemory.write8(i, i*2)
134
135
for i in range(32):
136
memData[i] = framMemory.read8(i)
137
138
print(memData)
139
140
binhoDevice.close()
141
142
else:
143
# couldn't read the FRAM manufacturer and product ID bits
144
print("I2C FRAM not identified... check your connections?")
145
Copied!
For comparison's sake, you can see the Adafruit Arduino library that this example was ported from here.
Last modified 8mo ago
Copy link