[−][src]Struct ruspiro_i2c::I2cImpl
I²C peripheral representation
Implementations
impl I2cImpl
[src]
pub fn initialize(&mut self, core_speed: u32, fast_mode: bool) -> I2cResult<()>
[src]
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.
pub fn scan(&self) -> I2cResult<Vec<u8>>
[src]
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.take_for(|i2c| i2c.scan()).unwrap(); for d in devices { println!("Device at address: 0x{:X}", d); }
pub fn check_device(&self, addr: u8) -> I2cResult<()>
[src]
Checks if a device with the given address is connected to the I²C bus.
Example
if I2C.take_for(|i2c| i2c.check_device(0x68)).is_ok() { println!("device at 0x68 connected"); }
pub fn read_register_u8(&self, device_addr: u8, reg: u8) -> I2cResult<u8>
[src]
Read a u8 from a device register
Example
let value = I2C.take_for(|i2c| i2c.read_register_u8(0x68, 0x20)).unwrap();
pub fn read_register_u16(&self, device_addr: u8, reg: u8) -> I2cResult<u16>
[src]
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.take_for(|i2c| i2c.read_register_u16(0x68, 0x20)).unwrap();
pub fn read_register_buff(
&self,
device_addr: u8,
reg: u8,
buffer: &mut [u8]
) -> I2cResult<usize>
[src]
&self,
device_addr: u8,
reg: u8,
buffer: &mut [u8]
) -> I2cResult<usize>
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.take_for(|i2c| i2c.read_register_buff(0x68, 0x20, &mut buffer)).unwrap();
pub fn read_register_field(
&self,
device_addr: u8,
reg: u8,
field: RegisterField<u8>
) -> I2cResult<RegisterFieldValue<u8>>
[src]
&self,
device_addr: u8,
reg: u8,
field: RegisterField<u8>
) -> I2cResult<RegisterFieldValue<u8>>
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.take_for(|i2c| i2c.read_register_field(0x68, 0x20, field)).unwrap();
pub fn write_u8(&self, device_addr: u8, data: u8) -> I2cResult<()>
[src]
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.take_for(|i2c| i2c.write_u8(0x68, 12)).unwrap();
pub fn write_register_u8(
&self,
device_addr: u8,
reg: u8,
data: u8
) -> I2cResult<()>
[src]
&self,
device_addr: u8,
reg: u8,
data: u8
) -> I2cResult<()>
Write u8 data to a device register
Example
I2C.take_for(|i2c| i2c.write_register_u8(0x68, 0x20, 12)).unwrap();
pub fn write_register_u16(
&self,
device_addr: u8,
reg: u8,
data: u16
) -> I2cResult<()>
[src]
&self,
device_addr: u8,
reg: u8,
data: u16
) -> I2cResult<()>
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.take_for(|i2c| i2c.write_register_u16(0x68, 0x20, 0x12ab)).unwrap();
pub fn write_register_buff(
&self,
device_addr: u8,
reg: u8,
data: &[u8]
) -> I2cResult<()>
[src]
&self,
device_addr: u8,
reg: u8,
data: &[u8]
) -> I2cResult<()>
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.take_for(|i2c| i2c.write_register_buff(0x68, 0x20, &data)).unwrap();
pub fn write_register_field(
&self,
device_addr: u8,
reg: u8,
value: RegisterFieldValue<u8>
) -> I2cResult<()>
[src]
&self,
device_addr: u8,
reg: u8,
value: RegisterFieldValue<u8>
) -> I2cResult<()>
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.take_for(|i2c| i2c.write_register_field(0x68, 0x20, field_value)).unwrap();
Auto Trait Implementations
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,