I2C

public final class I2C

I2C (I square C) is a two wire protocol to communicate between different devices. The I2C class allows some operations through I2C protocol, including reading messages from a device and writing messages to a device. Currently the I2C ports support only master mode.

Note

Different I2C devices have different attributes. Please reference the device manual before using the functions below. This class allows the reading and writing of a byte UInt8 or an array of bytes [UInt8].
  • Initialize a specific I2C interface as a master device.

    Usage Example

    // Initialize an I2C interface I2C0.
    let i2cBus = I2C(Id.I2C0)
    

    Declaration

    Swift

    public init(_ id: IdName,
                speed: Speed = .standard)

    Parameters

    id

    REQUIRED The name of the I2C interface.

    speed

    OPTIONAL The clock speed used to control the data transmission.

  • Get the current clock speed of the data transmission.

    Declaration

    Swift

    public func getSpeed() -> Speed

    Return Value

    The current speed: .standard, .fast or .fastPlus.

  • Set the clock speed to change the transmission rate.

    Declaration

    Swift

    public func setSpeed(_ speed: Speed)

    Parameters

    speed

    The clock speed used to control the data transmission.

  • Read one byte from a specified slave device with the given address.

    Declaration

    Swift

    @inline(__always)
    public func readByte(from address: UInt8) -> UInt8

    Parameters

    address

    The address of the slave device the board will communicate with.

    Return Value

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

  • Read an array of data from a specified slave device with the given address.

    Declaration

    Swift

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

    Parameters

    count

    The number of bytes to read.

    address

    The address of the slave device the board will communicate with.

    Return Value

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

  • Write a byte of data to a specified slave device with the given address.

    Declaration

    Swift

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

    Parameters

    byte

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

    address

    The address of the slave device the board will communicate with.

  • Write an array of data to a specified slave device with the given address.

    Declaration

    Swift

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

    Parameters

    data

    A byte array to be sent to the slave device.

    address

    The address of the slave device the board will communicate with.

  • Write an array of bytes to the slave device with the given address and then read the bytes sent from the device.

    Declaration

    Swift

    @inline(__always)
    public func writeRead(_ data: [UInt8], readCount: Int, address: UInt8) -> [UInt8]

    Parameters

    data

    A byte array to be sent to the slave device.

    readCount

    The number of bytes to read.

    address

    The address of the slave device the board will communicate with.

    Return Value

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

  • The clock signal is used to synchronize the data transmission between the devices.There are three available speed grades.

    See more

    Declaration

    Swift

    public enum Speed : Int32