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.
from binhoHostAdapter import binhoHostAdapterfrom binhoHostAdapter import binhoUtilitiesMB85RC_DEFAULT_ADDRESS =0xA0MB85RC_SLAVE_ID =0xF8classAdafruit_FRAM_I2C:# CONSTRUCTORdef__init__(self,addr): self.framInitialised =False self.i2c_addr = addr# PUBLIC FUNCTIONS# initializes I2C and configures the chip# call this function before doing anything elsedefbegin(self,adapter): self.hostAdapter = adapter# Make sure we're actually connected manufacturerID =0 productID =0 deviceInfo = [manufacturerID, productID] self.getDeviceID(deviceInfo) manufacturerID = deviceInfo[0] productID = deviceInfo[1] deviceFound =Trueif manufacturerID !=0x00A:print("Unexpected Manufacturer ID: "+str(deviceInfo[0])) deviceFound =Falseif productID !=0x510:print("Unexpected Product ID: "+str(productID)) deviceFound =False# Everything seems to be properly initialized and connected self._framInitialised =True;return deviceFound# Writes a byte at the specific FRAM addressdefwrite8(self,framAddr,value): self.hostAdapter.startI2C(0, self.i2c_addr) self.hostAdapter.writeByteI2C(0, framAddr >>8) self.hostAdapter.writeByteI2C(0, framAddr &0xFF) self.hostAdapter.writeByteI2C(0, value) self.hostAdapter.endI2C(0)# Reads an 8 bit value from the specified FRAM addressdefread8(self,framAddr): self.hostAdapter.startI2C(0, self.i2c_addr) self.hostAdapter.writeByteI2C(0, framAddr >>8) self.hostAdapter.writeByteI2C(0, framAddr &0xFF) self.hostAdapter.endI2C(0) response = self.hostAdapter.readByteI2C(0, self.i2c_addr) data = response.split()iflen(data)==3:returnint(data[2])else:return0# Reads the Manufacturer ID and the Product ID frm the ICdefgetDeviceID(self,deviceInfo): self.hostAdapter.startI2C(0, MB85RC_SLAVE_ID) self.hostAdapter.writeByteI2C(0, 0xA0) self.hostAdapter.endI2C(0, True) response = self.hostAdapter.readBytesI2C(0, MB85RC_SLAVE_ID, 3) data =response.split()iflen(data)==5: deviceInfo[0]= (int(data[2])<<4) + (int(data[3])>>4) deviceInfo[1]= ((int(data[3])&0x0F) <<8) +int(data[4])# Change this to match your COMPortdefault_commport ="COM27"print("I2C FRAM Example using Binho Host Adapter")print("v1.0 -- Jonathan Georgino <jonathan@binho.io>")printutilities = binhoUtilities.binhoUtilities()devices = utilities.listAvailableDevices()iflen(devices)==1: COMPORT = devices[0]print("Found 1 attached adapter @ "+ devices[0])printelse: COMPORT = default_commportprint("Found more than 1 attached adapter, using default port "+ COMPORT)printprint("Opening "+ COMPORT +"...")print# create the binhoHostAdapter objectbinhoDevice = binhoHostAdapter.binhoHostAdapter(COMPORT)binhoDevice.setNumericalBase('10')binhoDevice.setOperationMode(0, 'I2C')binhoDevice.setPullUpStateI2C(0, '1')binhoDevice.setClockI2C(0, 1000000)framMemory =Adafruit_FRAM_I2C(MB85RC_DEFAULT_ADDRESS)if framMemory.begin(binhoDevice)==True:# found the I2C FRAM memData = [0for x inrange(32)]for i inrange(32): memData[i]= framMemory.read8(i)print(memData)for i inrange(32): framMemory.write8(i, i*2)for i inrange(32): memData[i]= framMemory.read8(i)print(memData) binhoDevice.close()else:# couldn't read the FRAM manufacturer and product ID bitsprint("I2C FRAM not identified... check your connections?")
For comparison's sake, you can see the Adafruit Arduino library that this example was ported from here.