Under the assumption that this question is still valid - Here an example how to use the I2C mode. One has to provide/place the correct dll/so in the same folder as the script. For unix systems one has to unload the standard driver. For details see e.g. here unable to open a connection with a FTD 232 device .
class I2C:
START_BIT = 0x01
STOP_BIT = 0x02
BREAK_ON_NACK = 0x04
NACK_LAST_BYTE = 0x08
FAST_TRANSFER_BYTES = 0x10
FAST_TRANSFER_BITS = 0x20
FAST_TRANSFER = 0x30
NO_ADDRESS = 0x40
class InitError(Exception):
pass
class I2CChannelConfig(ctypes.Structure):
_fields_ = [("ClockRate", ctypes.c_int),
("LatencyTimer",ctypes.c_ubyte),
("Options", ctypes.c_uint32)]
def __init__(self, I2C_chn_no, clk=400000):
self.libMPSSE = None
try: self.libMPSSE = ctypes.cdll.LoadLibrary("libMPSSE64.dll")
except OSError: pass
try: self.libMPSSE = ctypes.cdll.LoadLibrary("libMPSSE32.dll")
except OSError: pass
try: self.libMPSSE = ctypes.cdll.LoadLibrary("./libMPSSE.so")
except OSError: pass
if self.libMPSSE is None: raise I2C.InitError("no MPSSE lib found")
chn_count = ctypes.c_uint32()
chn_conf = I2C.I2CChannelConfig(clk,1,0)
self.handle = ctypes.c_void_p()
ret = self.libMPSSE.I2C_GetNumChannels(ctypes.byref(chn_count))
module_logger.debug("I2C_GetNumChannels status: %d number of channels: %d"%(ret,chn_count.value))
if ret: raise I2C.InitError("I2C_GetNumChannels failed with errorcode %d"%ret)
ret = self.libMPSSE.I2C_OpenChannel(I2C_chn_no, ctypes.byref(self.handle))
module_logger.debug("I2C_OpenChannel status: %d handle: %s"%(ret, self.handle))
if ret: raise I2C.InitError("I2C_OpenChannel failed with errorcode %d"%ret)
ret = self.libMPSSE.I2C_InitChannel(self.handle,ctypes.byref(chn_conf))
module_logger.debug("I2C_InitChannel status: %d"%ret)
if ret: raise I2C.InitError("I2C_InitChannel failed with errorcode %d"%ret)
def close(self):
if self.libMPSSE:
ret = self.libMPSSE.I2C_CloseChannel(self.handle)
module_logger.debug("I2C_CloseChannel status: %d"%ret)
del self.libMPSSE
self.libMPSSE = None
def __del__(self):
self.close()
def send(self, I2C_addr, raw):
sent = ctypes.c_int32()
buf = ctypes.create_string_buffer(raw,len(raw))
module_logger.debug("sending via I2C msg len: %d raw:\n%s" % (len(raw), raw))
ret = self.libMPSSE.I2C_DeviceWrite(self.handle, I2C_addr, len(raw), buf, ctypes.byref(sent), I2C.START_BIT|I2C.STOP_BIT)#|I2C_TRANSFER_OPTION.FAST_TRANSFER_BYTES ignores the ACK
module_logger.debug("I2C_DeviceWrite status: %d transfered: %d of %d"%(ret,sent.value,len(raw)))
if ret or sent.value != len(raw):
module_logger.warning("sending via I2C failed with ret code %d. Sent %d byte of %d." % (ret, sent.value, len(raw)))
return False
return True