DancingQuanta / pyusbiss

Python API for USB-ISS Multifunction USB Communications Module
MIT License
2 stars 3 forks source link

Add I2C support #8

Open DancingQuanta opened 6 years ago

gwdehaan commented 5 years ago

I2C

Usage

i2cchannel = i2c.I2C(usbissdev, protocol,speed)

Parameter Description
usbissdev object representing the usbiss device.
protocol (H)ardware or (S)oftware
speed Choose based on the next table and the maximum speed of the connected i2c devices
protocol Speed (kHz)
H 100
H 400
H 1000
S 20
S 50
S 100
S 400

Example

from usbiss import usbiss
from usbiss import i2c

# Comport of the usbiss device
Port = 'COM3'

usbissdev = usbiss.USBISS(Port)
# Open a i2c channel with hardware handshaking and 100kHz
i2cchannel = i2c.I2C(usbissdev, 'H', 100)
# Scan the i2c bus for Devices
devices = i2channel.scan()
print(devices)

Methods

Method Description
scan() Scan the  i2c channel for devices. Returns a list of devices
write_data(data) Write data to the usbiss device
read_data(size) Read size data from the usbiss device. Returns size bytes
decode(data) Convert data to a list of bytes for easy processing

Extra

I2C Tests

Method Description
test_i2c.py Checks all available modi and speeds Scans the i2c channel for devices

Class I2CDevice

Usage

from usbiss import usbiss
from usbiss import i2c

usbissdev = usbiss.USBISS(Port)
# Open a i2c channel with hardware handshaking and 100kHz
i2cchannel = i2c.I2C(usbissdev, 'H', 100)
# Create a pcf8574 device for a PCF8574
Pcf8574 = i2c.I2C(i2cchannel, 72)
# send data to the PCF8574
Pcf8574.writeRaw8(0x0F)

Methods

Method Description
ping() ping the Device, returns True the device is available at the specified Address, else False
readRaw8() Read single byte for non-registered devices, such as the Philips PCF8574 I/O chip.
writeRaw8(value) Write single byte for non-registered devices, such as the Philips PCF8574 I/O chip.
readU8(register) Read an unsigned byte from the specified register.
readS8(register) Read a signed byte from the specified register.
write8(register, value) Write an 8-bit value to the specified register.
readU16(register, little_endian=True) Read an unsigned 16-bit value from the specified register, with the specified endianness (default little endian, or least significant byte first).
readS16(register, little_endian=True) Read a signed 16-bit value from the specified register, with the specified endianness (default little endian, or least significant byte first).
write16(register, value, little_endian=True) Write a 16-bit value to the specified register.
readlist(self, register, length) Read a length number of bytes from the specified register. Results will be returned as a bytearray.
writelist(self, register, data) Write bytes to the specified register.
readU16LE(register) Read an unsigned 16-bit value from the specified register, in little endian byte order.
readU16BE(register) Read an unsigned 16-bit value from the specified register, in big endian byte order.
readS16LE(register) Read a signed 16-bit value from the specified register, in little endian byte order.
readS16BE(register) Read a signed 16-bit value from the specified register, in big endian byte order.
readMem(AddressHighByte, AddressLowByte, length) AddressHighByte, AddressLowByte : Address of the memory location within the EPROM. lenght : no of bytes to receive   Read 2 byte addressed devices, eeproms from 32kbit (4kx8) and up. The maximum number of data bytes requested should not exceed 64 so as not to overflow the USB-ISS's internal buffer.   This function is specific for the USBISS, it is not a part of the Adafruit library.
writeMem(AddressHighByte, AddressLowByte, length, data) AddressHighByte, AddressLowByte : Address of the memory location within the EPROM. lenght : length of the transmitted data data : bytearray of data   Write 2 byte addressed devices, eeproms from 32kbit (4kx8) and up.  The maximum number of data bytes should not exceed 59 so as not to overflow the USB-ISS's 64 byte internal buffer.   This function is specific for the USBISS, it is not a part of the Adafruit library.

I2CDevice tests

Test Description
test_i2c_noregister(SGL)_PCF8574.py Testing the no register commands of the USBISS (I2C_SGL) For this test a PCF8574 is used. Pin 0 and Pin 7 shouldbe connected for the loopbacktest to succeed.
test_i2c_register(A1)_mcp23009.py Testing the register commands of the USBISS (A1) This test a MCP23008 IO Extender. For the loopbacktest (test 6) to succeed pins 6 and 7 should be connected.
test_i2c_eprom(A2)_24lc256.py Connect a 24LC256 EPROM to the i2c bus. The test writes a pattern to the eprom and reads it back.

Not implemented

I2C Mode Description
I2C_AD0 Special mode; No devices avialable to test with
I2C_DIRECT No need at this point in time, current implementation is rather complete for common devices.

References

www.robot-electronics.co.uk/htm/usb_iss_i2c_tech.htm https://github.com/adafruit/Adafruit_Python_GPIO/blob/master/Adafruit_GPIO/FT232H.py