SPI

public final class SPI

SPI is a four wire serial protocol for communication between devices.

  • Initialize a specified interface for SPI communication as a master device.

    Usage Example

    // Initialize a SPI interface SPI0.
    let spi = SPI(Id.SPI0)
    

    Declaration

    Swift

    public init(_ id: IdName,
                speed: Int = 1000000)

    Parameters

    id

    REQUIRED The name of the SPI interface.

    speed

    OPTIONAL The clock speed used to control the data transmission.

  • Get the current clock speed of SPI communication.

    Declaration

    Swift

    public func getSpeed() -> Int

    Return Value

    The current clock speed.

  • Set the speed of SPI communication.

    Declaration

    Swift

    public func setSpeed(_ speed: Int)

    Parameters

    speed

    The clock speed used to control the data transmission.

  • Read a byte of data from the slave device.

    Declaration

    Swift

    @inline(__always)
    public func readByte() -> UInt8

    Return Value

    One 8-bit binary number receiving from the slave device.

  • Read an array of data from the slave device.

    Declaration

    Swift

    @inline(__always)
    public func read(count: Int) -> [UInt8]

    Parameters

    count

    The number of bytes receiving from the slave device.

    Return Value

    An array of 8-bit binary numbers receiving from the slave device.

  • Write a byte of data to the slave device.

    Declaration

    Swift

    @inline(__always)
    public func write(_ byte: UInt8)

    Parameters

    byte

    One 8-bit binary number to be sent to the slave device.

  • Write an array of data to the slave device.

    Declaration

    Swift

    @inline(__always)
    public func write(_ data: [UInt8])

    Parameters

    data

    A byte array to be sent to the slave device.

  • Write raw data to the slave device.

    Declaration

    Swift

    @inline(__always)
    public func write(_ data: UnsafeRawBufferPointer)

    Parameters

    data

    Raw data to be sent to the slave device.