Bus

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

class pslab.bus.I2CMaster(device: Optional[pslab.serial_handler.SerialHandler] = None)[source]

Bases: pslab.bus.i2c._I2CPrimitive

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.

Raises

ValueError – If given frequency is not supported by PSLab board.

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.I2CSlave(address: int, device: Optional[pslab.serial_handler.SerialHandler] = None)[source]

Bases: pslab.bus.i2c._I2CPrimitive

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, optional) – 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.

class pslab.bus.SPIMaster(device: Optional[pslab.serial_handler.SerialHandler] = None)[source]

Bases: pslab.bus.spi._SPIPrimitive

SPI bus controller.

Parameters

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

classmethod get_parameters() Tuple[int][source]

Get SPI parameters.

Returns

  • primary_prescaler ({0, 1, 2, 3}) – Primary Prescaler for system clock :const:`CP.CLOCK_RATE`Hz. (0,1,2,3) -> (64:1,16:1,4:1,1:1).

  • secondary_prescaler ({0, 1, 2, 3, 4, 5, 6, 7}) – Secondary prescaler (0,1,..7) -> (8:1,7:1,..1:1).

  • CKE ({0, 1}) – SPIx Clock Edge Select bit. Serial output data changes on transition {0: from Idle clock state to active clock state,

    1: from active clock state to Idle clock state}.

  • CKP ({0, 1}) – Clock Polarity Select bit. Idle state for clock is a {0: low, 1: high} level.

  • SMP ({0, 1}) – Input data is sampled at the {0: end, 1: middle} of data output time.

set_parameters(primary_prescaler: int = 0, secondary_prescaler: int = 2, CKE: int = 1, CKP: int = 0, SMP: int = 1)[source]

Set SPI parameters.

Parameters
  • primary_prescaler ({0, 1, 2, 3}) – Primary Prescaler for system clock :const:`CP.CLOCK_RATE`Hz. (0,1,2,3) -> (64:1,16:1,4:1,1:1).

  • secondary_prescaler ({0, 1, 2, 3, 4, 5, 6, 7}) – Secondary prescaler (0,1,..7) -> (8:1,7:1,..1:1).

  • CKE ({0, 1}) –

    SPIx Clock Edge Select bit. Serial output data changes on transition {0: from Idle clock state to active clock state,

    1: from active clock state to Idle clock state}.

  • CKP ({0, 1}) – Clock Polarity Select bit. Idle state for clock is a {0: low, 1: high} level.

  • SMP ({0, 1}) – Input data is sampled at the {0: end, 1: middle} of data output time.

Raises

ValueError – If any one of arguments is not in its shown range.

class pslab.bus.SPISlave(device: Optional[pslab.serial_handler.SerialHandler] = None)[source]

Bases: pslab.bus.spi._SPIPrimitive

SPI slave device.

Parameters

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

read16() int[source]

Read 16-bit data while transmit zero.

Returns

Data returned by slave device.

Return type

int

read16_bulk(data_to_read: int) List[int][source]

Read 16-bit data array while transmitting zeros.

Parameters

data_to_read (int) – Number of 16-bit data to read from slave device.

Returns

List of 16-bit date returned by slave device.

Return type

list of int

read8() int[source]

Read 8-bit data while transmit zero.

Returns

Data returned by slave device.

Return type

int

read8_bulk(data_to_read: int) List[int][source]

Read 8-bit data array while transmitting zeros.

Parameters

data_to_read (int) – Number of 8-bit data to read from slave device.

Returns

List of 8-bit data returned by slave device.

Return type

list of int

transfer16(data: int) int[source]

Send 16-bit data over SPI and receive 16-bit data from SPI simultaneously.

Parameters

data (int) – Data to transmit.

Returns

data_in – Data returned by slave device.

Return type

int

transfer16_bulk(data: List[int]) List[int][source]

Transfer 16-bit data array over SPI.

Parameters

data (list of int) – List of 16-bit data to transmit.

Returns

data_in – List of 16-bit data returned by slave device.

Return type

list of int

transfer8(data: int) int[source]

Send 8-bit data over SPI and receive 8-bit data from SPI simultaneously.

Parameters

data (int) – Data to transmit.

Returns

data_in – Data returned by slave device.

Return type

int

transfer8_bulk(data: List[int]) List[int][source]

Transfer 8-bit data array over SPI.

Parameters

data (list of int) – List of 8-bit data to transmit.

Returns

data_in – List of 8-bit data returned by slave device.

Return type

list of int

write16(data: int)[source]

Send 16-bit data over SPI.

Parameters

data (int) – Data to transmit.

write16_bulk(data: List[int])[source]

Send 16-bit data array over SPI.

Parameters

data (list of int) – List of 16-bit data to transmit.

write8(data: int)[source]

Send 8-bit data over SPI.

Parameters

data (int) – Data to transmit.

write8_bulk(data: List[int])[source]

Send 8-bit data array over SPI.

Parameters

data (list of int) – List of 8-bit data to transmit.

class pslab.bus.UART(device: Optional[pslab.serial_handler.SerialHandler] = None)[source]

Bases: pslab.bus.uart._UARTPrimitive

UART2 bus.

Parameters

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

configure(baudrate: float)[source]

Configure UART bus baudrate.

Parameters

baudrate (float) –

Raises

ValueError – If given baudrate is not supported by PSLab board.

read_byte() int[source]

Read a single byte from the UART bus.

Returns

data – A Byte interpreted as a uint8 read from the UART bus.

Return type

int

read_int() int[source]

Read a two byte value from the UART bus.

It is a primitive UART method, prefered to use UART.read_int().

Returns

data – Two bytes interpreted as a uint16 read from the UART bus.

Return type

int

write_byte(data: int)[source]

Write a single byte to the UART bus.

Parameters

data (int) – Byte value to write to the UART bus.

write_int(data: int)[source]

Write a single int to the UART bus.

Parameters

data (int) – Int value to write to the UART bus.

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: pslab.bus.i2c._I2CPrimitive

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.

Raises

ValueError – If given frequency is not supported by PSLab board.

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: pslab.bus.i2c._I2CPrimitive

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, optional) – 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.