Bus

Contains modules for interfacing with the PSLab’s I2C, SPI, and UART buses.

(SPI and UART still TODO)

I2C

Control the PSLab’s I2C bus and devices connected on the bus.

Examples

Set I2C bus speed to 400 kbit/s:

>>> from pslab.bus.i2c import I2CMaster, I2CSlave
>>> bus = I2CMaster()
>>> bus.configure(frequency=4e5)

Scan for connected devices:

>>> bus.scan()
[96, 104]

Connect to the PSLab’s built-in DS1307 RTC:

>>> rtc = I2CSlave(address=104)
class pslab.bus.i2c.I2CMaster(device: Optional[pslab.serial_handler.SerialHandler] = None)[source]

Bases: object

I2C bus controller.

Handles slave independent functionality with the I2C port.

Parameters

device (SerialHandler, optional) – Serial connection to PSLab device. If not provided, a new one will be created.

configure(frequency: float)[source]

Configure bus frequency.

Parameters

frequency (float) – Frequency of SCL in Hz.

scan() → List[int][source]

Scan I2C port for connected devices.

Returns

addrs – List of 7-bit addresses on which slave devices replied.

Return type

list of int

class pslab.bus.i2c.I2CSlave(address: int, device: Optional[pslab.serial_handler.SerialHandler] = None)[source]

Bases: object

I2C slave device.

Parameters
  • address (int) – 7-bit I2C device address.

  • device (SerialHandler, optional) – Serial interface for communicating with the PSLab device. If not provided, a new one will be created.

address

7-bit I2C device address.

Type

int

ping() → bool[source]

Ping the I2C slave.

Returns

response – True is slave responded, False otherwise.

Return type

bool

read(bytes_to_read: int, register_address: int = 0) → bytearray[source]

Read data from I2C device.

This method relies on the slave device auto incrementing its internal pointer after each read. Most devices do, but it is not part of the I2C standard. Refer to the device’s documentation.

If the slave device does not auto increment, use one of read_byte(), read_int(), or read_long() instead, depending on how wide the device registers are.

Parameters
  • bytes_to_read (int) – Number of bytes to read from slave device.

  • register_address (int, optional) – Slave device internal memory address to read from. The default value is 0x0.

Returns

data – Read data as a bytearray.

Return type

bytearray

read_byte(register_address: int = 0) → int[source]

Read a single byte from the I2C slave.

Parameters

register_address (int, optional) – Slave device internal memory address to read from. The default value is 0x0.

Returns

data – A byte interpreted as a uint8.

Return type

int

read_int(register_address: int = 0) → int[source]

Read a two byte value from the I2C slave.

Parameters

register_address (int, optional) – Slave device internal memory address to read from. The default value is 0x0.

Returns

data – Two bytes interpreted as a uint16.

Return type

int

read_long(register_address: int = 0) → int[source]

Read a four byte value from the I2C slave.

Parameters

register_address (int, optional) – Slave device internal memory address to read from. The default value is 0x0.

Returns

data – Four bytes interpreted as a uint32.

Return type

int

write(bytes_to_write: bytearray, register_address: int = 0)[source]

Write data to I2C slave.

This method relies on the slave device auto incrementing its internal pointer after each write. Most devices do, but it is not part of the I2C standard. Refer to the device’s documentation.

If the slave device does not auto increment, use one of write_byte(), write_int(), or write_long() instead, depending on how wide the device registers are.

Parameters
  • bytes_to_write (bytearray) – Data to write to the slave.

  • register_address (int) – Slave device internal memory address to write to. The default value is 0x0.

write_byte(data: int, register_address: int = 0)[source]

Write a single byte to the I2C slave.

Parameters
  • data (int) – An integer that fits in a uint8.

  • register_address (int, optional) – Slave device internal memory address to write to. The default value is 0x0.

write_int(data: int, register_address: int = 0)[source]

Write a two byte value to the I2C slave.

Parameters
  • data (int) – An integer that fits in a uint16.

  • register_address (int, optional) – Slave device internal memory address to write to. The default value is 0x0.

write_long(data: int, register_address: int = 0)[source]

Write a four byte value to the I2C slave.

Parameters
  • data (int) – An integer that fits in a uint32.

  • register_address (int, optional) – Slave device internal memory address to write to. The default value is 0x0.