SpiDev I/O
- SpiDev.readbytes(n)
Read the given number of bytes from the SPI device.
Zeroes are written to the device while the given number of bytes are received.
- Parameters
n (int) – An integer indicating the number of bytes to read from the SPI device.
- Returns
A list of the bytes received from the SPI device.
- Return type
list
- Raises
StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.
Example
>>> import quanser.communications.spidev as spidev >>> >>> spi = spidev.SpiDev() >>> spi.open(0, 4) # Bus 0, Device 4 >>> spi.max_speed_hz = 5000000 # 5 MHz >>> spi.mode = 0b00 # device uses SPI mode 0 >>> try: >>> response = spi.readbytes(6) # read 6 bytes from the device >>> finally: >>> spi.close()
- SpiDev.writebytes(values)
Write the values to the SPI device.
If the number of bytes exceeds buffer_size then an error will occur. The values received are discarded.
- Parameters
values (list) – A list of bytes to write to the SPI device.
- Raises
StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.
Example
>>> import quanser.communications.spidev as spidev >>> >>> spi = spidev.SpiDev() >>> spi.open(0, 4) # Bus 0, Device 4 >>> spi.max_speed_hz = 5000000 # 5 MHz >>> spi.mode = 0b00 # device uses SPI mode 0 >>> try: >>> spi.writebytes([10, 20, 30, 40, 50]) # write 5 bytes to the device >>> finally: >>> spi.close()
- SpiDev.writebytes2(values)
Write the values to the SPI device.
If the number of bytes exceeds buffer_size then it will break up the writes into separate chunks of buffer_size each until all the values are written. The chip select is released between each chunk.
- Parameters
values (list) – A list of bytes to write to the SPI device.
- Raises
StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.
Example
>>> import quanser.communications.spidev as spidev >>> >>> spi = spidev.SpiDev() >>> spi.open(0, 4) # Bus 0, Device 4 >>> spi.max_speed_hz = 5000000 # 5 MHz >>> spi.mode = 0b00 # device uses SPI mode 0 >>> try: >>> spi.writebytes2([10, 20, 30, 40, 50]) # write 5 bytes to the device >>> finally: >>> spi.close()
- SpiDev.xfer(values, speed_hz=None, delay_usec=0, bits_per_word=None)
Performs an SPI transaction.
If the number of bytes exceeds buffer_size then an error will occur.
- Parameters
values (list) – A list of bytes to write to the SPI device.
speed_hz (int) – An integer indicating the baud rate in Hz to use for the transaction. If this argument is omitted then the max_speed_hz attribute value will be used, which defaults to 10000000 (10 MHz).
delay_usec (int) – Currently ignored in this implementation. Defaults to zero.
bits_per_word (int) – An integer indicating the number of bits per word, or word length, for the transaction. If this argument is omitted then the bits_per_word attribute value will be used, which defaults to 8.
- Returns
A list of the bytes received from the SPI device during the transaction.
- Return type
list
- Raises
StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.
Example
>>> import quanser.communications.spidev as spidev >>> >>> spi = spidev.SpiDev() >>> spi.open(0, 4) # Bus 0, Device 4 >>> spi.max_speed_hz = 5000000 # 5 MHz >>> spi.mode = 0b00 # device uses SPI mode 0 >>> try: >>> response = spi.xfer([10, 20, 30, 40, 50]) # write 5 bytes to the device and receive 5 bytes >>> finally: >>> spi.close()
- SpiDev.xfer2(values, speed_hz=None, delay_usec=0, bits_per_word=None)
Performs an SPI transaction.
If the number of bytes exceeds buffer_size then an error will occur.
- Parameters
values (list) – A list of bytes to write to the SPI device.
speed_hz (int) – An integer indicating the baud rate in Hz to use for the transaction. If this argument is omitted then the max_speed_hz attribute value will be used, which defaults to 10000000 (10 MHz).
delay_usec (int) – Currently ignored in this implementation. Defaults to zero.
bits_per_word (int) – An integer indicating the number of bits per word, or word length, for the transaction. If this argument is omitted then the bits_per_word attribute value will be used, which defaults to 8.
- Returns
A list of the bytes received from the SPI device during the transaction.
- Return type
list
- Raises
StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.
Examples
Here is a simple example performing a single transfer.
>>> import quanser.communications.spidev as spidev >>> >>> spi = spidev.SpiDev() >>> spi.open(0, 4) # Bus 0, Device 4 >>> spi.max_speed_hz = 5000000 # 5 MHz >>> spi.mode = 0b00 # device uses SPI mode 0 >>> try: >>> response = spi.xfer2([10, 20, 30, 40, 50]) # write 5 bytes to the device and receive 5 bytes >>> finally: >>> spi.close()
Here is a complete example for the PmodGyro or L3G4200D connected to the SPI lines on the user header of a Quanser Mechatronic Sensors Trainer USB device. For the chip select, the SPI CS line is used instead of one of the digital I/O lines.
>>> import quanser.communications.spidev as spidev >>> import time >>> >>> # Create SPI object >>> spi = spidev.SpiDev() >>> spi.open(0, 4) # Bus 0, Device 4 (adjust if needed) >>> spi.max_speed_hz = 5000000 >>> spi.mode = 0b00 # L3G4200D uses SPI mode 0 >>> >>> def write_register(reg, value): >>> spi.xfer2([reg & 0x7F, value]) # MSB=0 for write >>> >>> def read_register(reg): >>> return spi.xfer2([0x80 | reg, 0x00])[1] # MSB=1 for read >>> >>> def read_registers(reg, length): >>> return spi.xfer2([0xC0 | reg] + [0x00]*length)[1:] # MSB=1, auto-increment >>> >>> # L3G4200D register addresses >>> CTRL_REG1 = 0x20 >>> OUT_X_L = 0x28 >>> >>> try: >>> # Initialize gyro: normal mode, all axes enabled, 100Hz >>> write_register(CTRL_REG1, 0b00001111) >>> >>> time.sleep(0.1) >>> >>> while True: >>> data = read_registers(OUT_X_L, 6) >>> x = (data[1] << 8) | data[0] >>> y = (data[3] << 8) | data[2] >>> z = (data[5] << 8) | data[4] >>> >>> # Convert to signed 16-bit >>> if x & 0x8000: x -= 65536 >>> if y & 0x8000: y -= 65536 >>> if z & 0x8000: z -= 65536 >>> >>> print(f"X={x}, Y={y}, Z={z}") >>> time.sleep(0.1) >>> >>> except KeyboardInterrupt: >>> print("Interrupted by the user.") >>> except Exception as ex: >>> print("An error occurred reading from the sensor. (" + str(ex) + ")") >>> finally: >>> spi.close()
- SpiDev.xfer3(values, speed_hz=None, delay_usec=0, bits_per_word=None)
Performs an SPI transaction.
If the number of bytes exceeds buffer_size then it will break up the reads and writes into separate chunks of buffer_size each until all the values are transferred. The chip select is released between each chunk.
- Parameters
values (list) – A list of bytes to write to the SPI device.
speed_hz (int) – An integer indicating the baud rate in Hz to use for the transaction. If this argument is omitted then the max_speed_hz attribute value will be used, which defaults to 10000000 (10 MHz).
delay_usec (int) – Currently ignored in this implementation. Defaults to zero.
bits_per_word (int) – An integer indicating the number of bits per word, or word length, for the transaction. If this argument is omitted then the bits_per_word attribute value will be used, which defaults to 8.
- Returns
A list of the bytes received from the SPI device during the transaction.
- Return type
list
- Raises
StreamError – Raised on an error in the underlying Quanser Stream API. A suitable error message may be retrieved using get_error_message.
Example
>>> import quanser.communications.spidev as spidev >>> >>> spi = spidev.SpiDev() >>> spi.open(0, 4) # Bus 0, Device 4 >>> spi.max_speed_hz = 5000000 # 5 MHz >>> spi.mode = 0b00 # device uses SPI mode 0 >>> try: >>> response = spi.xfer3([10, 20, 30, 40, 50]) # write 5 bytes to the device and receive 5 bytes >>> finally: >>> spi.close()