Description
@eldruin has created a Rust driver for the VEML60300 ambient light sensor that uses the embedded-hal traits.
At the lowest level of the driver are two functions write_register()
and read_register()
defined as follows:
fn write_register(&mut self, register: u8, value: u16) -> Result<(), Error<E>> {
self.i2c
.write(self.address, &[register, value as u8, (value >> 8) as u8])
.map_err(Error::I2C)
}
fn read_register(&mut self, register: u8) -> Result<u16, Error<E>> {
let mut data = [0; 2];
self.i2c
.write_read(self.address, &[register], &mut data)
.map_err(Error::I2C)
.and(Ok(u16::from(data[0]) | u16::from(data[1]) << 8))
}
In testing the code the function that writes to the registers in the VEML6030 is working as it is configuring the ambient light sensor and enabling it to collect samples.
When we try to read the raw data from the ambient light sensor using a function called read_raw()
which simply calls the read_register()
function with the correct register offset it always returns 0.
Within the test application if we replace the call to the drivers read_raw()
function with the following:
let mut dev = LinuxI2CDevice::new("/dev/i2c-2", 0x10).unwrap();
let raw = dev.smbus_read_word_data(0x04).unwrap();
then correct data is being returned. If we split the write then read as:
dev.write(&[0x04]).unwrap();
let mut data = [0; 2];
dev.read(&mut data).unwrap();
let raw = u16::from(data[0]) | (u16::from(data[1]) << 8);
then the value of raw
is, again, always 0.
The code is all cross compiled for an NXP i.MX6 running a custom Yocto Linux build with kernel version 4.14.98.
-Andy.