Struct ruspiro_i2c::I2cImpl[][src]

pub struct I2cImpl { /* fields omitted */ }
Expand description

I²C peripheral representation

Implementations

Initialize the I²C bus for further usage. This will require the GPIO pins 2 and 3 to be available for usage. If they have been already occupied before this initialization is called an error will be returned.

Scan for I²C devices currently connected to the I²C bus. The scan will just try to get an acknowledge message from any slave address between 0x00 and 0x7F. If a device is connected this call succeeds/get’s acknowledged and the corresponding address is put into the returned vector.

Example

    let devices = I2C.with_mut(|i2c| i2c.scan()).unwrap();
    for d in devices {
        println!("Device at address: 0x{:X}", d);
    }

Checks if a device with the given address is connected to the I²C bus.

Example

    if I2C.with_mut(|i2c| i2c.check_device(0x68)).is_ok() {
        println!("device at 0x68 connected");
    }

Read a u8 from a device register

Example

    let value = I2C.with_mut(|i2c| i2c.read_register_u8(0x68, 0x20)).unwrap();

Read a u16 from a device register. As usually all I²C register are 8 Bit wide this will only return a valid value if the device supports auto-increment of the actual register while reading

Example

    // read_register_u16 will actually read the registers 0x20 and 0x21 and combine
    // both u8 values into the u16 return value.
    let value = I2C.with_mut(|i2c| i2c.read_register_u16(0x68, 0x20)).unwrap();

Read a u8 array from a device register. As usually all I²C register are 8 Bit wide this will only return a valid value if the device supports auto-increment of the actual register while reading

Example

    let mut buffer: [u8; 4] = [0; 4];
    // the buffer read will actuall read the registers 0x20, 0x21, 0x22, 0x23
    // and put the data into the byte buffer given (if register auto increment is supported
    // by this device)
    let _ = I2C.with_mut(|i2c| i2c.read_register_buff(0x68, 0x20, &mut buffer)).unwrap();

Read a specific field from a 8 Bit device register.

Example

    // define an arbitrary register field with 1 bit size at offset 2
    let field = RegisterField::<u8>::new(1, 2);
    let field_value = I2C.with_mut(|i2c| i2c.read_register_field(0x68, 0x20, field)).unwrap();

Write u8 data to a device without specifying a register. This is helpful for devices that may not provide any registers or have only one register to wrte to. In those cases the device accepts the data without specifying the register in the first place.

Example

    I2C.with_mut(|i2c| i2c.write_u8(0x68, 12)).unwrap();

Write u8 data to a device register

Example

    I2C.with_mut(|i2c| i2c.write_register_u8(0x68, 0x20, 12)).unwrap();

Write u16 data to a device register. As usually all I²C register are 8 Bit wide this will only properly write the value if the device supports auto-increment of the actual register while reading

Example

    // this will actually write 0x12 to register 0x20 and 0xab to register 0x21
    // if the device supports auto increment of registers for writes
    I2C.with_mut(|i2c| i2c.write_register_u16(0x68, 0x20, 0x12ab)).unwrap();

Write a u8 array to a device register. As usually all I²C register are 8 Bit wide this will only properly write the value if the device supports auto-increment of the actual register while reading

Example

    let data: [u8; 3] = [0, 1, 2];
    I2C.with_mut(|i2c| i2c.write_register_buff(0x68, 0x20, &data)).unwrap();

Write a specific register field to a 8 Bit device register.

Example

    // define an arbitrary field with bit size 2 and offset 3
    let field = RegisterField::<u8>::new(2, 3);
    // define the field value
    let field_value = RegisterFieldValue::<u8>::new(field, 0b10);
    let value = I2C.with_mut(|i2c| i2c.write_register_field(0x68, 0x20, field_value)).unwrap();

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.