# SPI

## Code Example

    from periphery import SPI

    # Open spidev1.0 with mode 0 and max speed 1MHz
    spi = SPI("/dev/spidev1.0", 0, 1000000)

    data_out = [0xaa, 0xbb, 0xcc, 0xdd]
    data_in = spi.transfer(data_out)

    print("shifted out [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".format(*data_out))
    print("shifted in  [0x{:02x}, 0x{:02x}, 0x{:02x}, 0x{:02x}]".format(*data_in))

    spi.close()

## API

class periphery.SPI(devpath, mode, max_speed, bit_order='msb', bits_per_word=8, extra_flags=0)

  Bases: `object`

  Instantiate a SPI object and open the spidev device at the specified path with the specified SPI mode, max speed in hertz, and the defaults of “msb” bit order and 8 bits per word.

  Parameters

    - devpath (str) – spidev device path.

    - mode (int) – SPI mode, can be 0, 1, 2, 3.

    - max_speed (int, float) – maximum speed in Hertz.

    - bit_order (str) – bit order, can be “msb” or “lsb”.

    - bits_per_word (int) – bits per word.

    - extra_flags (int) – extra spidev flags to be bitwise-ORed with the SPI mode.

  Returns

    SPI object.

  Return type

    SPI

  Raises

    - SPIError – if an I/O or OS error occurs.

    - TypeError – if devpath, mode, max_speed, bit_order, bits_per_word, or extra_flags types are invalid.

    - ValueError – if mode, bit_order, bits_per_word, or extra_flags values are invalid.

  transfer(data)

    Shift out data and return shifted in data.

    Parameters

      data (bytes, bytearray, list) – a byte array or list of 8-bit integers to shift out.

    Returns

      data shifted in.

    Return type

      bytes, bytearray, list

    Raises

      - SPIError – if an I/O or OS error occurs.

      - TypeError – if data type is invalid.

      - ValueError – if data is not valid bytes.

  close()

    Close the spidev SPI device.

    Raises

      SPIError – if an I/O or OS error occurs.

  property fd

    Get the file descriptor of the underlying spidev device.

    Type

      int

  property devpath

    Get the device path of the underlying spidev device.

    Type

      str

  property mode

    Get or set the SPI mode. Can be 0, 1, 2, 3.

    Raises

      - SPIError – if an I/O or OS error occurs.

      - TypeError – if mode type is not int.

      - ValueError – if mode value is invalid.

    Type

      int

  property max_speed

    Get or set the maximum speed in Hertz.

    Raises

      - SPIError – if an I/O or OS error occurs.

      - TypeError – if max_speed type is not int or float.

    Type

      int, float

  property bit_order

    Get or set the SPI bit order. Can be “msb” or “lsb”.

    Raises

      - SPIError – if an I/O or OS error occurs.

      - TypeError – if bit_order type is not str.

      - ValueError – if bit_order value is invalid.

    Type

      str

  property bits_per_word

    Get or set the SPI bits per word.

    Raises

      - SPIError – if an I/O or OS error occurs.

      - TypeError – if bits_per_word type is not int.

      - ValueError – if bits_per_word value is invalid.

    Type

      int

  property extra_flags

    Get or set the spidev extra flags. Extra flags are bitwise-ORed with the SPI mode.

    Raises

      - SPIError – if an I/O or OS error occurs.

      - TypeError – if extra_flags type is not int.

      - ValueError – if extra_flags value is invalid.

    Type

      int

class periphery.SPIError

  Bases: `OSError`

  Base class for SPI errors.