mirror of https://github.com/eldruin/ds323x-rs
Generalize array reading and writing
parent
ccca20aa4a
commit
16c4b46843
|
@ -66,10 +66,11 @@ where
|
||||||
|
|
||||||
/// Read the year [2000-2100].
|
/// Read the year [2000-2100].
|
||||||
pub fn get_year(&mut self) -> Result<u16, Error<E>> {
|
pub fn get_year(&mut self) -> Result<u16, Error<E>> {
|
||||||
let mut data = [0; 2];
|
let mut data = [0; 3];
|
||||||
self.iface.read_two_registers(Register::MONTH, &mut data)?;
|
data[0] = Register::MONTH;
|
||||||
let century = data[0] & BitFlags::CENTURY;
|
self.iface.read_data(&mut data)?;
|
||||||
let year = packed_bcd_to_decimal(data[1]);
|
let century = data[1] & BitFlags::CENTURY;
|
||||||
|
let year = packed_bcd_to_decimal(data[2]);
|
||||||
if century != 0 {
|
if century != 0 {
|
||||||
Ok(2100 + (year as u16))
|
Ok(2100 + (year as u16))
|
||||||
}
|
}
|
||||||
|
@ -167,13 +168,15 @@ where
|
||||||
let data = self.iface.read_register(Register::MONTH)?;
|
let data = self.iface.read_register(Register::MONTH)?;
|
||||||
let month_bcd = data & !BitFlags::CENTURY;
|
let month_bcd = data & !BitFlags::CENTURY;
|
||||||
if year > 2099 {
|
if year > 2099 {
|
||||||
let data = [ BitFlags::CENTURY | month_bcd,
|
let mut data = [ Register::MONTH,
|
||||||
|
BitFlags::CENTURY | month_bcd,
|
||||||
decimal_to_packed_bcd((year - 2100) as u8) ];
|
decimal_to_packed_bcd((year - 2100) as u8) ];
|
||||||
self.iface.write_two_registers(Register::MONTH, &data)
|
self.iface.write_data(&mut data)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
let data = [ month_bcd, decimal_to_packed_bcd((year - 2000) as u8) ];
|
let mut data = [ Register::MONTH, month_bcd,
|
||||||
self.iface.write_two_registers(Register::MONTH, &data)
|
decimal_to_packed_bcd((year - 2000) as u8) ];
|
||||||
|
self.iface.write_data(&mut data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,8 +25,8 @@ pub trait WriteData {
|
||||||
type Error;
|
type Error;
|
||||||
/// Write to an u8 register
|
/// Write to an u8 register
|
||||||
fn write_register(&mut self, register: u8, data: u8) -> Result<(), Error<Self::Error>>;
|
fn write_register(&mut self, register: u8, data: u8) -> Result<(), Error<Self::Error>>;
|
||||||
/// Write to two consecutive u8 registers
|
/// Write data. The first element corresponds to the starting address.
|
||||||
fn write_two_registers(&mut self, first_register: u8, data: &[u8; 2]) -> Result<(), Error<Self::Error>>;
|
fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I2C, E> WriteData for I2cInterface<I2C>
|
impl<I2C, E> WriteData for I2cInterface<I2C>
|
||||||
|
@ -41,8 +41,7 @@ where
|
||||||
.map_err(Error::Comm)
|
.map_err(Error::Comm)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_two_registers(&mut self, first_register: u8, data: &[u8; 2]) -> Result<(), Error<Self::Error>> {
|
fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>> {
|
||||||
let payload: [u8; 3] = [first_register, data[0], data[1]];
|
|
||||||
self.i2c
|
self.i2c
|
||||||
.write(DEVICE_ADDRESS, &payload)
|
.write(DEVICE_ADDRESS, &payload)
|
||||||
.map_err(Error::Comm)
|
.map_err(Error::Comm)
|
||||||
|
@ -67,10 +66,9 @@ where
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_two_registers(&mut self, first_register: u8, data: &[u8; 2]) -> Result<(), Error<E>> {
|
fn write_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>> {
|
||||||
self.cs.set_low();
|
self.cs.set_low();
|
||||||
|
payload[0] += 0x80;
|
||||||
let payload: [u8; 3] = [first_register + 0x80, data[0], data[1]];
|
|
||||||
let result = self.spi
|
let result = self.spi
|
||||||
.write(&payload)
|
.write(&payload)
|
||||||
.map_err(Error::Comm);
|
.map_err(Error::Comm);
|
||||||
|
@ -87,8 +85,8 @@ pub trait ReadData {
|
||||||
type Error;
|
type Error;
|
||||||
/// Read an u8 register
|
/// Read an u8 register
|
||||||
fn read_register(&mut self, register: u8) -> Result<u8, Error<Self::Error>>;
|
fn read_register(&mut self, register: u8) -> Result<u8, Error<Self::Error>>;
|
||||||
/// Read two u8 registers
|
/// Read some data. The first element corresponds to the starting address.
|
||||||
fn read_two_registers(&mut self, register: u8, data: &mut [u8; 2]) -> Result<(), Error<Self::Error>>;
|
fn read_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I2C, E> ReadData for I2cInterface<I2C>
|
impl<I2C, E> ReadData for I2cInterface<I2C>
|
||||||
|
@ -104,9 +102,10 @@ where
|
||||||
.and(Ok(data[0]))
|
.and(Ok(data[0]))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_two_registers(&mut self, register: u8, data: &mut [u8; 2]) -> Result<(), Error<Self::Error>> {
|
fn read_data(&mut self, payload: &mut [u8]) -> Result<(), Error<Self::Error>> {
|
||||||
|
let len = payload.len();
|
||||||
self.i2c
|
self.i2c
|
||||||
.write_read(DEVICE_ADDRESS, &[register], &mut data[..])
|
.write_read(DEVICE_ADDRESS, &[payload[0]], &mut payload[1..=(len-1)])
|
||||||
.map_err(Error::Comm)
|
.map_err(Error::Comm)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,19 +129,13 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_two_registers(&mut self, register: u8, data: &mut [u8; 2]) -> Result<(), Error<Self::Error>> {
|
fn read_data(&mut self, mut payload: &mut [u8]) -> Result<(), Error<Self::Error>> {
|
||||||
self.cs.set_low();
|
self.cs.set_low();
|
||||||
let mut payload = [register, 0, 0];
|
|
||||||
let result = self.spi
|
let result = self.spi
|
||||||
.transfer(&mut payload)
|
.transfer(&mut payload)
|
||||||
.map_err(Error::Comm);
|
.map_err(Error::Comm);
|
||||||
self.cs.set_high();
|
self.cs.set_high();
|
||||||
match result {
|
result?;
|
||||||
Ok(result) => { data[0] = result[1];
|
|
||||||
data[1] = result[2];
|
|
||||||
Ok(())
|
Ok(())
|
||||||
},
|
|
||||||
Err(e) => Err(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue