Interface Device

extends Device implements Device
A device connected with SPI.

Interface summary


constructor --path/string --frequency/int --mode/int=
Constructs a device from the given path.

Constructors

constructor --path/string --frequency/int --mode/int=0
Constructs a device from the given path.
This function is only available on Linux.
The given frequency is only aspirational, and the actual frequency might be different. For example, the Raspberry Pi only supports a frequency range of 3.814 kHz to 125 MHz. If the given frequency is outside this range, it will be silently clamped to the nearest valid value.
The mode parameter configures the clock polarity and phase (CPOL and CPHA) for this device.
The possible configurations are:
  • 0 (0b00): CPOL=0, CPHA=0
  • 1 (0b01): CPOL=0, CPHA=1
  • 2 (0b10): CPOL=1, CPHA=0
  • 3 (0b11): CPOL=1, CPHA=1
Pin numbers
This constructor does not take any pin numbers. The pins are tied to the path, and configured outside. On the Raspberry Pi, look at /boot/overlays/README for more information. There, you can find options to move the SPI block, or to change/disable the chip select pins.

Methods

close -> any
Closes this SPI device and releases resources associated with it.

read amount/int -> ByteArray
Reads the amount of bytes from the device.
If the device can't provide amount bytes, the behavior of this operation is undefined and depending on the actual device implementation.

registers --byte-size/int=1 --byte-order/ByteOrder=io.BIG-ENDIAN -> Registers
The byte-size parameter specifies the size of the registers in bytes. For most I2C devices, this is 1, but 2 is common too. If the register size is greater than 1, then the byte-order parameter specifies the byte order of the register address.
Always returns the same object, unless the size of the registers changes or the register byte-order is not the same. The first allocation of the register cached; all subsequent *different* ones will create new objects.

transfer data/ByteArray --from/int=0 --to/int=data.size --read/bool=false --dc/int=0 --command/int=0 --address/int=0 --keep-cs-active/bool=false -> none
Transfers the given data to the device.
If read is true, then the transfer is full-duplex, and the read data replaces the contents of data.
If the device has a dc (data/command) pin, then that pin is set to the value of dc.
If a commands and/or address sections was defined, use command and address to set the values.
When keep-cs-active is true, then the chip select pin is kept active after the transfer. This functionality is only allowed when the bus is reserved for this device. See with-reserved-bus.

with-reserved-bus [block] -> any
Reserves the bus for this device while executing the given block.
If the system supports it, actively reserves the bus for this device. In that case, starts by acquiring the bus. Once that's succeeded, executes the block. Finally, releases the bus before returning.
On some systems, like Linux, it is not possible to reserve the bus. In that case, the programmer is responsible for managing the bus. This function then simply calls the given block.
Reserving the bus can be useful in two contexts:
1. The CS pin is controlled by the user. Since the hardware only supports a limited number of automatic CS pins, it might be necessary to set some CS pins by hand. This should be done after the bus has been reserved.
2. When using the --keep-cs-active flag of the transfer function, the bus must be reserved.

write bytes/ByteArray --keep-cs-active/bool=false -> none
Writes the bytes to the device.
If keep-cs-active is true, then the chip select pin is kept active after the transfer. This functionality is only allowed when the bus is reserved for this device. See with-reserved-bus.