2018-10-31 09:10:41 +00:00
|
|
|
//! Functions exclusive of DS3234
|
2020-07-10 20:24:29 +00:00
|
|
|
use crate::interface::{SpiInterface, WriteData};
|
|
|
|
use crate::{ic, BitFlags, Ds323x, Error, Register, TempConvRate, CONTROL_POR_VALUE};
|
2018-10-31 09:47:10 +00:00
|
|
|
use core::marker::PhantomData;
|
2020-07-10 20:24:29 +00:00
|
|
|
use embedded_hal::{blocking::spi, digital::v2::OutputPin};
|
2018-10-31 09:10:41 +00:00
|
|
|
|
2019-05-16 16:50:11 +00:00
|
|
|
impl<SPI, CS, CommE, PinE> Ds323x<SpiInterface<SPI, CS>, ic::DS3234>
|
2018-10-31 09:10:41 +00:00
|
|
|
where
|
2020-07-10 20:24:29 +00:00
|
|
|
SPI: spi::Transfer<u8, Error = CommE> + spi::Write<u8, Error = CommE>,
|
|
|
|
CS: OutputPin<Error = PinE>,
|
2018-10-31 09:10:41 +00:00
|
|
|
{
|
2018-10-31 09:47:10 +00:00
|
|
|
/// Create a new instance.
|
|
|
|
pub fn new_ds3234(spi: SPI, chip_select: CS) -> Self {
|
2019-05-16 17:18:07 +00:00
|
|
|
const STATUS_POR_VALUE: u8 = BitFlags::OSC_STOP | BitFlags::BB32KHZ | BitFlags::EN32KHZ;
|
2018-10-31 09:47:10 +00:00
|
|
|
Ds323x {
|
|
|
|
iface: SpiInterface {
|
|
|
|
spi,
|
2019-05-16 17:18:07 +00:00
|
|
|
cs: chip_select,
|
2018-10-31 09:47:10 +00:00
|
|
|
},
|
|
|
|
control: CONTROL_POR_VALUE,
|
|
|
|
status: STATUS_POR_VALUE,
|
2019-05-16 17:18:07 +00:00
|
|
|
_ic: PhantomData,
|
2018-10-31 09:47:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Destroy driver instance, return SPI bus instance and CS output pin.
|
|
|
|
pub fn destroy_ds3234(self) -> (SPI, CS) {
|
|
|
|
(self.iface.spi, self.iface.cs)
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:07:15 +00:00
|
|
|
/// Enable the 32kHz output when battery-powered. (enabled per default)
|
2018-10-31 09:10:41 +00:00
|
|
|
///
|
|
|
|
/// Additionally, the 32kHz output needs to be enabled. See
|
2018-10-31 10:17:31 +00:00
|
|
|
/// [`enable_32khz_output()`](#method.enable_32khz_output).
|
2018-10-31 09:10:41 +00:00
|
|
|
///
|
|
|
|
/// Note: This is only available for DS3232 and DS3234 devices.
|
2019-05-16 16:50:11 +00:00
|
|
|
pub fn enable_32khz_output_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
|
2018-10-31 09:10:41 +00:00
|
|
|
let status = self.status | BitFlags::BB32KHZ;
|
|
|
|
self.write_status_without_clearing_alarm(status)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disable the 32kHz output when battery-powered.
|
|
|
|
///
|
|
|
|
/// The 32kHz output will still generate a wave when not battery-powered if
|
2018-10-31 10:17:31 +00:00
|
|
|
/// it enabled. See [`enable_32khz_output()`](#method.enable_32khz_output).
|
2018-10-31 09:10:41 +00:00
|
|
|
///
|
|
|
|
/// Note: This is only available for DS3232 and DS3234 devices.
|
2019-05-16 16:50:11 +00:00
|
|
|
pub fn disable_32khz_output_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
|
2018-10-31 09:10:41 +00:00
|
|
|
let status = self.status & !BitFlags::BB32KHZ;
|
|
|
|
self.write_status_without_clearing_alarm(status)
|
|
|
|
}
|
2018-10-31 09:37:42 +00:00
|
|
|
|
|
|
|
/// Set the temperature conversion rate.
|
|
|
|
///
|
|
|
|
/// Set how often the temperature is measured and applies compensation to
|
|
|
|
/// the oscillator. This can be used to reduce power consumption but sudden
|
|
|
|
/// temperature changes will not be compensated for.
|
|
|
|
///
|
|
|
|
/// Note: This is only available for DS3232 and DS3234 devices.
|
2019-05-16 17:18:07 +00:00
|
|
|
pub fn set_temperature_conversion_rate(
|
|
|
|
&mut self,
|
|
|
|
rate: TempConvRate,
|
|
|
|
) -> Result<(), Error<CommE, PinE>> {
|
2020-05-02 10:17:49 +00:00
|
|
|
let status = match rate {
|
|
|
|
TempConvRate::_64s => self.status & !BitFlags::CRATE1 & !BitFlags::CRATE0,
|
|
|
|
TempConvRate::_128s => self.status & !BitFlags::CRATE1 | BitFlags::CRATE0,
|
|
|
|
TempConvRate::_256s => self.status | BitFlags::CRATE1 & !BitFlags::CRATE0,
|
|
|
|
TempConvRate::_512s => self.status | BitFlags::CRATE1 | BitFlags::CRATE0,
|
|
|
|
};
|
2018-10-31 09:37:42 +00:00
|
|
|
self.write_status_without_clearing_alarm(status)
|
|
|
|
}
|
2018-10-31 10:07:38 +00:00
|
|
|
|
|
|
|
/// Enable the temperature conversions when battery-powered. (enabled per default)
|
|
|
|
///
|
|
|
|
/// Note: This is only available for DS3234 devices.
|
2019-05-16 16:50:11 +00:00
|
|
|
pub fn enable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
|
2018-10-31 10:07:38 +00:00
|
|
|
self.iface.write_register(Register::TEMP_CONV, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Disable the temperature conversions when battery-powered.
|
|
|
|
///
|
|
|
|
/// Note: This is only available for DS3234 devices.
|
2019-05-16 16:50:11 +00:00
|
|
|
pub fn disable_temperature_conversions_on_battery(&mut self) -> Result<(), Error<CommE, PinE>> {
|
2019-05-16 17:18:07 +00:00
|
|
|
self.iface
|
|
|
|
.write_register(Register::TEMP_CONV, BitFlags::TEMP_CONV_BAT)
|
2018-10-31 10:07:38 +00:00
|
|
|
}
|
2018-10-31 09:10:41 +00:00
|
|
|
}
|