#![no_std]
#![doc = include_str!("../README.md")]
use device_driver::{AsyncRegisterInterface, FieldsetMetadata, RegisterInterfaceBase};
use embedded_hal_async::{i2c::I2c, spi::SpiDevice};
mod sealed {
pub trait Chip {}
pub trait ChannelMode {}
}
pub trait Chip: sealed::Chip {}
pub struct Tla252x;
pub struct Ads7x38;
pub struct Ads7x28;
impl sealed::Chip for Tla252x {}
impl sealed::Chip for Ads7x38 {}
impl sealed::Chip for Ads7x28 {}
impl Chip for Tla252x {}
impl Chip for Ads7x38 {}
impl Chip for Ads7x28 {}
pub trait HasStats: Chip {}
impl HasStats for Ads7x38 {}
impl HasStats for Ads7x28 {}
pub trait HasRmsZcd: HasStats {}
impl HasRmsZcd for Ads7x28 {}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ZcdGpoValue {
Rise0Fall0 = 0,
Rise0Fall1 = 1,
Rise1Fall0 = 2,
Rise1Fall1 = 3,
}
impl From<u8> for ZcdGpoValue {
fn from(v: u8) -> Self {
match v & 0x3 {
0 => Self::Rise0Fall0,
1 => Self::Rise0Fall1,
2 => Self::Rise1Fall0,
_ => Self::Rise1Fall1,
}
}
}
impl From<ZcdGpoValue> for u8 {
fn from(v: ZcdGpoValue) -> Self {
v as u8
}
}
device_driver::compile!(
options: "--rust-defmt-feature=defmt",
manifest: "device.ddsl",
);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Address {
X10 = 0x10,
X11 = 0x11,
X12 = 0x12,
X13 = 0x13,
X14 = 0x14,
X15 = 0x15,
X16 = 0x16,
X17 = 0x17,
}
const OPCODE_READ: u8 = 0x10;
const OPCODE_WRITE: u8 = 0x08;
const REG_MANUAL_CH_SEL: u8 = 0x11;
pub struct I2cInterface<BUS> {
i2c: BUS,
addr: Address,
}
impl<BUS: I2c> RegisterInterfaceBase for I2cInterface<BUS> {
type Error = BUS::Error;
type AddressType = u8;
}
impl<BUS: I2c> AsyncRegisterInterface for I2cInterface<BUS> {
async fn write_register(
&mut self,
address: u8,
data: &mut [u8],
_metadata: &FieldsetMetadata,
) -> Result<(), Self::Error> {
self.i2c
.write(self.addr as u8, &[OPCODE_WRITE, address, data[0]])
.await
}
async fn read_register(
&mut self,
address: u8,
data: &mut [u8],
_metadata: &FieldsetMetadata,
) -> Result<(), Self::Error> {
self.i2c
.write_read(self.addr as u8, &[OPCODE_READ, address], data)
.await
}
}
pub struct SpiInterface<SPI> {
spi: SPI,
}
impl<SPI: SpiDevice> RegisterInterfaceBase for SpiInterface<SPI> {
type Error = SPI::Error;
type AddressType = u8;
}
impl<SPI: SpiDevice> AsyncRegisterInterface for SpiInterface<SPI> {
async fn write_register(
&mut self,
address: u8,
data: &mut [u8],
_metadata: &FieldsetMetadata,
) -> Result<(), Self::Error> {
self.spi.write(&[OPCODE_WRITE, address, data[0]]).await
}
async fn read_register(
&mut self,
address: u8,
data: &mut [u8],
_metadata: &FieldsetMetadata,
) -> Result<(), Self::Error> {
self.spi.write(&[OPCODE_READ, address, 0x00]).await?;
self.spi.read(&mut data[..1]).await
}
}
#[allow(async_fn_in_trait)]
pub trait CanConvert: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface {
async fn read_conversion_raw(&mut self, channel: u8) -> Result<u16, Self::Error>;
async fn read_sequence_raw(&mut self, out: &mut [u8]) -> Result<(), Self::Error>;
}
impl<BUS: I2c> CanConvert for I2cInterface<BUS> {
async fn read_conversion_raw(&mut self, channel: u8) -> Result<u16, BUS::Error> {
self.write_register(
REG_MANUAL_CH_SEL,
&mut [channel & 0x0F],
&device_driver::FieldsetMetadata::DEFAULT,
)
.await?;
let mut buf = [0u8; 2];
self.i2c.read(self.addr as u8, &mut buf).await?;
Ok((buf[0] as u16) << 8 | buf[1] as u16)
}
async fn read_sequence_raw(&mut self, out: &mut [u8]) -> Result<(), BUS::Error> {
self.i2c.read(self.addr as u8, out).await
}
}
impl<SPI: SpiDevice> CanConvert for SpiInterface<SPI> {
async fn read_conversion_raw(&mut self, channel: u8) -> Result<u16, SPI::Error> {
let mut buf = [(channel & 0x1F) << 3, 0x00];
self.spi.transfer_in_place(&mut buf).await?;
Ok((buf[0] as u16) << 8 | buf[1] as u16)
}
async fn read_sequence_raw(&mut self, out: &mut [u8]) -> Result<(), SPI::Error> {
self.spi.transfer_in_place(out).await
}
}
pub struct Unconfigured;
pub struct AnalogIn;
pub struct DigitalIn;
pub struct DigitalOut<DRIVE>(core::marker::PhantomData<DRIVE>);
pub struct OpenDrain;
pub struct PushPull;
pub trait ChannelMode: sealed::ChannelMode {
const IS_ANALOG: bool;
}
impl sealed::ChannelMode for Unconfigured {}
impl ChannelMode for Unconfigured {
const IS_ANALOG: bool = false;
}
impl sealed::ChannelMode for AnalogIn {}
impl ChannelMode for AnalogIn {
const IS_ANALOG: bool = true;
}
impl sealed::ChannelMode for DigitalIn {}
impl ChannelMode for DigitalIn {
const IS_ANALOG: bool = false;
}
impl<DRIVE> sealed::ChannelMode for DigitalOut<DRIVE> {}
impl<DRIVE> ChannelMode for DigitalOut<DRIVE> {
const IS_ANALOG: bool = false;
}
pub struct Driver<
IF,
CHIP,
C0 = Unconfigured,
C1 = Unconfigured,
C2 = Unconfigured,
C3 = Unconfigured,
C4 = Unconfigured,
C5 = Unconfigured,
C6 = Unconfigured,
C7 = Unconfigured,
> {
pub device: Device<IF>,
_chip: core::marker::PhantomData<CHIP>,
#[allow(clippy::type_complexity)]
_channels: core::marker::PhantomData<(C0, C1, C2, C3, C4, C5, C6, C7)>,
}
impl<BUS: I2c, CHIP: Chip> Driver<I2cInterface<BUS>, CHIP> {
pub fn new(i2c: BUS, addr: Address) -> Self {
Self {
device: Device::new(I2cInterface { i2c, addr }),
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
}
}
}
impl<SPI: SpiDevice, CHIP: Chip> Driver<SpiInterface<SPI>, CHIP> {
pub fn new(spi: SPI) -> Self {
Self {
device: Device::new(SpiInterface { spi }),
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
}
}
}
#[must_use = "staged channel configuration does nothing until `.apply()` is called"]
pub struct ConfigureChannels<
IF,
CHIP,
C0 = Unconfigured,
C1 = Unconfigured,
C2 = Unconfigured,
C3 = Unconfigured,
C4 = Unconfigured,
C5 = Unconfigured,
C6 = Unconfigured,
C7 = Unconfigured,
> {
device: Device<IF>,
pin_cfg_value: u8,
pin_cfg_mask: u8,
gpio_cfg_value: u8,
gpio_cfg_mask: u8,
gpo_drive_value: u8,
gpo_drive_mask: u8,
_chip: core::marker::PhantomData<CHIP>,
#[allow(clippy::type_complexity)]
_channels: core::marker::PhantomData<(C0, C1, C2, C3, C4, C5, C6, C7)>,
}
impl<IF, CHIP: Chip, C0, C1, C2, C3, C4, C5, C6, C7>
Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
{
pub fn configure(self) -> ConfigureChannels<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7> {
ConfigureChannels {
device: self.device,
pin_cfg_value: 0,
pin_cfg_mask: 0,
gpio_cfg_value: 0,
gpio_cfg_mask: 0,
gpo_drive_value: 0,
gpo_drive_mask: 0,
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
}
}
}
impl<
IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
CHIP: Chip,
C0,
C1,
C2,
C3,
C4,
C5,
C6,
C7,
> ConfigureChannels<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
{
pub async fn apply(
mut self,
) -> Result<Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>, IF::Error> {
let (mask, value) = (self.pin_cfg_mask, self.pin_cfg_value);
if mask != 0 {
self.device
.pin_cfg()
.modify_async(|r| r.set_pin_cfg((r.pin_cfg() & !mask) | (value & mask)))
.await?;
}
let (mask, value) = (self.gpio_cfg_mask, self.gpio_cfg_value);
if mask != 0 {
self.device
.gpio_cfg()
.modify_async(|r| r.set_gpio_cfg((r.gpio_cfg() & !mask) | (value & mask)))
.await?;
}
let (mask, value) = (self.gpo_drive_mask, self.gpo_drive_value);
if mask != 0 {
self.device
.gpo_drive_cfg()
.modify_async(|r| r.set_gpo_drive_cfg((r.gpo_drive_cfg() & !mask) | (value & mask)))
.await?;
}
Ok(Driver {
device: self.device,
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
})
}
}
impl<
IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
CHIP: Chip,
C0,
C1,
C2,
C3,
C4,
C5,
C6,
C7,
> Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
{
pub async fn reset(mut self) -> Result<Driver<IF, CHIP>, IF::Error> {
self.device
.general_cfg()
.write_with_zero_async(|r| r.set_rst(true))
.await?;
Ok(Driver {
device: self.device,
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
})
}
pub async fn calibrate(&mut self) -> Result<(), IF::Error> {
self.device
.general_cfg()
.modify_async(|r| r.set_cal(true))
.await?;
Ok(())
}
pub async fn browned_out(&mut self) -> Result<bool, IF::Error> {
Ok(self.device.system_status().read_async().await?.bor())
}
pub async fn clear_bor(&mut self) -> Result<(), IF::Error> {
self.device
.system_status()
.write_with_zero_async(|r| r.set_bor(true))
.await?;
Ok(())
}
pub async fn set_oversampling(&mut self, ratio: OversamplingRatio) -> Result<(), IF::Error> {
self.device
.osr_cfg()
.modify_async(|r| r.set_osr(ratio))
.await?;
Ok(())
}
}
impl<IF: CanConvert, CHIP: Chip, C0, C1, C2, C3, C4, C5, C6, C7>
Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
where
C0: ChannelMode,
C1: ChannelMode,
C2: ChannelMode,
C3: ChannelMode,
C4: ChannelMode,
C5: ChannelMode,
C6: ChannelMode,
C7: ChannelMode,
{
pub const ANALOG_CHANNEL_MASK: u8 = (C0::IS_ANALOG as u8)
| (C1::IS_ANALOG as u8) << 1
| (C2::IS_ANALOG as u8) << 2
| (C3::IS_ANALOG as u8) << 3
| (C4::IS_ANALOG as u8) << 4
| (C5::IS_ANALOG as u8) << 5
| (C6::IS_ANALOG as u8) << 6
| (C7::IS_ANALOG as u8) << 7;
pub async fn read_analog_sequence(&mut self) -> Result<[Option<u16>; 8], IF::Error> {
let mask = Self::ANALOG_CHANNEL_MASK;
let mut results = [None; 8];
if mask == 0 {
return Ok(results);
}
let count = mask.count_ones() as usize;
let mut buf = [0u8; 16];
self.run_analog_sequence(&mut buf[..count * 2]).await?;
for (slot, channel) in (0..8u8).filter(|c| mask & (1 << c) != 0).enumerate() {
let hi = buf[slot * 2] as u16;
let lo = buf[slot * 2 + 1] as u16;
results[channel as usize] = Some(hi << 8 | lo);
}
Ok(results)
}
pub async fn verify_analog_sequence_order(&mut self) -> Result<bool, IF::Error> {
let mask = Self::ANALOG_CHANNEL_MASK;
if mask == 0 {
return Ok(true);
}
let count = mask.count_ones() as usize;
let saved_osr = self.device.osr_cfg().read_async().await?.osr();
self.device
.osr_cfg()
.modify_async(|r| r.set_osr(OversamplingRatio::Osr1))
.await?;
self.device
.data_cfg()
.modify_async(|r| r.set_append_status(AppendStatus::ChannelId))
.await?;
let mut buf = [0u8; 16];
let sequence = self.run_analog_sequence(&mut buf[..count * 2]).await;
self.device
.data_cfg()
.modify_async(|r| r.set_append_status(AppendStatus::None))
.await?;
self.device
.osr_cfg()
.modify_async(|r| r.set_osr(saved_osr))
.await?;
sequence?;
let ordered = (0..8u8)
.filter(|c| mask & (1 << c) != 0)
.enumerate()
.all(|(slot, channel)| buf[slot * 2 + 1] & 0x0F == channel);
Ok(ordered)
}
async fn run_analog_sequence(&mut self, out: &mut [u8]) -> Result<(), IF::Error> {
self.device
.auto_seq_ch_sel()
.write_async(|r| r.set_auto_seq_ch_sel(Self::ANALOG_CHANNEL_MASK))
.await?;
self.device
.sequence_cfg()
.write_async(|r| {
r.set_seq_mode(SeqMode::AutoSequence);
r.set_seq_start(true);
})
.await?;
let read = self.device.interface.read_sequence_raw(out).await;
self.device
.sequence_cfg()
.write_async(|r| {
r.set_seq_mode(SeqMode::Manual);
r.set_seq_start(false);
})
.await?;
read
}
}
impl<
IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
CHIP: HasStats,
C0,
C1,
C2,
C3,
C4,
C5,
C6,
C7,
> Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
{
pub async fn enable_dwc(&mut self) -> Result<(), IF::Error> {
self.device
.general_cfg()
.modify_async(|r| r.set_dwc_en(true))
.await?;
Ok(())
}
pub async fn disable_dwc(&mut self) -> Result<(), IF::Error> {
self.device
.general_cfg()
.modify_async(|r| r.set_dwc_en(false))
.await?;
Ok(())
}
pub async fn configure_alert_pin(
&mut self,
push_pull: bool,
logic: AlertLogic,
) -> Result<(), IF::Error> {
self.device
.alert_pin_cfg()
.write_with_zero_async(|r| {
r.set_alert_drive(push_pull);
r.set_alert_logic(logic);
})
.await?;
Ok(())
}
pub async fn set_alert_channel_mask(&mut self, mask: u8) -> Result<(), IF::Error> {
self.device
.alert_ch_sel()
.write_with_zero_async(|r| r.set_alert_ch_sel(mask))
.await?;
Ok(())
}
pub async fn event_flags(&mut self) -> Result<u8, IF::Error> {
Ok(self.device.event_flag().read_async().await?.event_flag())
}
pub async fn event_high_flags(&mut self) -> Result<u8, IF::Error> {
Ok(self
.device
.event_high_flag()
.read_async()
.await?
.event_high_flag())
}
pub async fn event_low_flags(&mut self) -> Result<u8, IF::Error> {
Ok(self
.device
.event_low_flag()
.read_async()
.await?
.event_low_flag())
}
pub async fn clear_event_high_flags(&mut self, mask: u8) -> Result<(), IF::Error> {
self.device
.event_high_flag()
.write_with_zero_async(|r| r.set_event_high_flag(mask))
.await?;
Ok(())
}
pub async fn clear_event_low_flags(&mut self, mask: u8) -> Result<(), IF::Error> {
self.device
.event_low_flag()
.write_with_zero_async(|r| r.set_event_low_flag(mask))
.await?;
Ok(())
}
}
impl<
IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface,
CHIP: HasRmsZcd,
C0,
C1,
C2,
C3,
C4,
C5,
C6,
C7,
> Driver<IF, CHIP, C0, C1, C2, C3, C4, C5, C6, C7>
{
pub async fn configure_rms(
&mut self,
channel: RmsChannelId,
dc_sub: bool,
samples: RmsSampleCount,
) -> Result<(), IF::Error> {
self.device
.rms_cfg()
.write_with_zero_async(|r| {
r.set_rms_chid(channel);
r.set_rms_dc_sub(dc_sub);
r.set_rms_samples(samples);
})
.await?;
Ok(())
}
pub async fn read_rms(&mut self) -> Result<u16, IF::Error> {
let lsb = self.device.rms_lsb().read_async().await?.value();
let msb = self.device.rms_msb().read_async().await?.value();
Ok(((msb as u16) << 8) | lsb as u16)
}
pub async fn configure_zcd_blanking(
&mut self,
multiplier_8x: bool,
blanking: u8,
) -> Result<(), IF::Error> {
self.device
.zcd_blanking_cfg()
.write_with_zero_async(|r| {
r.set_mult_en(multiplier_8x);
r.set_zcd_blanking(blanking & 0x7F);
})
.await?;
Ok(())
}
pub async fn set_zcd_gpo_ch0_ch3(
&mut self,
ch0: ZcdGpoValue,
ch1: ZcdGpoValue,
ch2: ZcdGpoValue,
ch3: ZcdGpoValue,
) -> Result<(), IF::Error> {
self.device
.gpo_value_zcd_cfg_ch_0_ch_3()
.write_with_zero_async(|r| {
r.set_ch_0(ch0);
r.set_ch_1(ch1);
r.set_ch_2(ch2);
r.set_ch_3(ch3);
})
.await?;
Ok(())
}
pub async fn set_zcd_gpo_ch4_ch7(
&mut self,
ch4: ZcdGpoValue,
ch5: ZcdGpoValue,
ch6: ZcdGpoValue,
ch7: ZcdGpoValue,
) -> Result<(), IF::Error> {
self.device
.gpo_value_zcd_cfg_ch_4_ch_7()
.write_with_zero_async(|r| {
r.set_ch_4(ch4);
r.set_ch_5(ch5);
r.set_ch_6(ch6);
r.set_ch_7(ch7);
})
.await?;
Ok(())
}
pub async fn set_zcd_gpo_update_mask(&mut self, mask: u8) -> Result<(), IF::Error> {
self.device
.gpo_zcd_update_en()
.write_with_zero_async(|r| r.set_gpo_zcd_update_en(mask))
.await?;
Ok(())
}
}
macro_rules! impl_channel {
($n:literal, ($($pre:ident),*), ($($post:ident),*)) => {
pastey::paste! {
impl<IF, CHIP: Chip, [<C $n>] $(, $pre)* $(, $post)*>
ConfigureChannels<IF, CHIP, $($pre,)* [<C $n>], $($post,)*>
{
#[doc = concat!("Stage channel ", stringify!($n), " as an analog input.")]
pub fn [<ch $n _as_analog>](mut self)
-> ConfigureChannels<IF, CHIP, $($pre,)* AnalogIn, $($post,)*>
{
self.pin_cfg_mask |= 1u8 << $n;
self.pin_cfg_value &= !(1u8 << $n);
ConfigureChannels {
device: self.device,
pin_cfg_value: self.pin_cfg_value,
pin_cfg_mask: self.pin_cfg_mask,
gpio_cfg_value: self.gpio_cfg_value,
gpio_cfg_mask: self.gpio_cfg_mask,
gpo_drive_value: self.gpo_drive_value,
gpo_drive_mask: self.gpo_drive_mask,
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
}
}
#[doc = concat!("Stage channel ", stringify!($n), " as a digital input (GPIO).")]
pub fn [<ch $n _as_digital_in>](mut self)
-> ConfigureChannels<IF, CHIP, $($pre,)* DigitalIn, $($post,)*>
{
self.pin_cfg_mask |= 1u8 << $n;
self.pin_cfg_value |= 1u8 << $n;
self.gpio_cfg_mask |= 1u8 << $n;
self.gpio_cfg_value &= !(1u8 << $n);
ConfigureChannels {
device: self.device,
pin_cfg_value: self.pin_cfg_value,
pin_cfg_mask: self.pin_cfg_mask,
gpio_cfg_value: self.gpio_cfg_value,
gpio_cfg_mask: self.gpio_cfg_mask,
gpo_drive_value: self.gpo_drive_value,
gpo_drive_mask: self.gpo_drive_mask,
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
}
}
#[doc = concat!("Stage channel ", stringify!($n), " as a push-pull digital output (GPIO).")]
pub fn [<ch $n _as_digital_out_push_pull>](mut self)
-> ConfigureChannels<IF, CHIP, $($pre,)* DigitalOut<PushPull>, $($post,)*>
{
self.pin_cfg_mask |= 1u8 << $n;
self.pin_cfg_value |= 1u8 << $n;
self.gpio_cfg_mask |= 1u8 << $n;
self.gpio_cfg_value |= 1u8 << $n;
self.gpo_drive_mask |= 1u8 << $n;
self.gpo_drive_value |= 1u8 << $n;
ConfigureChannels {
device: self.device,
pin_cfg_value: self.pin_cfg_value,
pin_cfg_mask: self.pin_cfg_mask,
gpio_cfg_value: self.gpio_cfg_value,
gpio_cfg_mask: self.gpio_cfg_mask,
gpo_drive_value: self.gpo_drive_value,
gpo_drive_mask: self.gpo_drive_mask,
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
}
}
#[doc = concat!("Stage channel ", stringify!($n), " as an open-drain digital output (GPIO).")]
pub fn [<ch $n _as_digital_out_open_drain>](mut self)
-> ConfigureChannels<IF, CHIP, $($pre,)* DigitalOut<OpenDrain>, $($post,)*>
{
self.pin_cfg_mask |= 1u8 << $n;
self.pin_cfg_value |= 1u8 << $n;
self.gpio_cfg_mask |= 1u8 << $n;
self.gpio_cfg_value |= 1u8 << $n;
self.gpo_drive_mask |= 1u8 << $n;
self.gpo_drive_value &= !(1u8 << $n);
ConfigureChannels {
device: self.device,
pin_cfg_value: self.pin_cfg_value,
pin_cfg_mask: self.pin_cfg_mask,
gpio_cfg_value: self.gpio_cfg_value,
gpio_cfg_mask: self.gpio_cfg_mask,
gpo_drive_value: self.gpo_drive_value,
gpo_drive_mask: self.gpo_drive_mask,
_chip: core::marker::PhantomData,
_channels: core::marker::PhantomData,
}
}
}
impl<IF: CanConvert, CHIP: Chip $(, $pre)* $(, $post)*>
Driver<IF, CHIP, $($pre,)* AnalogIn, $($post,)*>
{
pub async fn [<read_ch $n>](&mut self) -> Result<u16, IF::Error> {
self.device.interface.read_conversion_raw($n).await
}
}
impl<IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface, CHIP: HasStats $(, $pre)* $(, $post)*>
Driver<IF, CHIP, $($pre,)* AnalogIn, $($post,)*>
{
#[doc = concat!("Trigger a conversion on channel ", stringify!($n), " without SCL stretching and")]
#[doc = concat!("OSR_DONE, then reads the result from the RECENT_CH", stringify!($n), " registers.")]
pub async fn [<read_ch $n _polled>](&mut self) -> Result<u16, IF::Error> {
self.device.general_cfg().modify_async(|r| r.set_stats_en(true)).await?;
self.device.manual_ch_sel().write_async(|r| {
r.set_manual_chid(ChannelId::[<Ain $n>])
}).await?;
self.device.general_cfg().modify_async(|r| r.set_cnvst(true)).await?;
loop {
if self.device.system_status().read_async().await?.osr_done() {
break;
}
}
self.device.system_status().write_with_zero_async(|r| {
r.set_osr_done(true)
}).await?;
let lsb = self.device.recent_ch_lsb().read_at_async($n).await?.value();
let msb = self.device.recent_ch_msb().read_at_async($n).await?.value();
Ok(((msb as u16) << 8) | lsb as u16)
}
#[doc = concat!("Set the high and low alert thresholds for channel ", stringify!($n), " (12-bit, 0-4095).")]
pub async fn [<set_ch $n _thresholds>](
&mut self,
high: u16,
low: u16,
) -> Result<(), IF::Error> {
self.device
.high_th_ch()
.write_with_zero_at_async($n, |r| r.set_high_threshold_msb((high >> 4) as u8))
.await?;
self.device
.hysteresis_ch()
.modify_at_async($n, |r| r.set_high_threshold_lsb((high & 0xF) as u8))
.await?;
self.device
.low_th_ch()
.write_with_zero_at_async($n, |r| r.set_low_threshold_msb((low >> 4) as u8))
.await?;
self.device
.event_count_ch()
.modify_at_async($n, |r| r.set_low_threshold_lsb((low & 0xF) as u8))
.await?;
Ok(())
}
#[doc = concat!("Set the hysteresis for channel ", stringify!($n), " (4-bit; effective value = `hyst << 3` LSBs).")]
pub async fn [<set_ch $n _hysteresis>](
&mut self,
hyst: u8,
) -> Result<(), IF::Error> {
self.device
.hysteresis_ch()
.modify_at_async($n, |r| r.set_hysteresis(hyst & 0xF))
.await?;
Ok(())
}
#[doc = concat!("Set the event count for channel ", stringify!($n), " (4-bit; alert fires after `count + 1`")]
pub async fn [<set_ch $n _event_count>](
&mut self,
count: u8,
) -> Result<(), IF::Error> {
self.device
.event_count_ch()
.modify_at_async($n, |r| r.set_event_count(count & 0xF))
.await?;
Ok(())
}
}
impl<IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface, CHIP: Chip $(, $pre)* $(, $post)*>
Driver<IF, CHIP, $($pre,)* DigitalIn, $($post,)*>
{
pub async fn [<is_ch $n _high>](&mut self) -> Result<bool, IF::Error> {
Ok(self.device.gpi_value().read_async().await?.gpi_value() & (1u8 << $n) != 0)
}
}
impl<IF: RegisterInterfaceBase<AddressType = u8> + AsyncRegisterInterface, CHIP: Chip, D $(, $pre)* $(, $post)*>
Driver<IF, CHIP, $($pre,)* DigitalOut<D>, $($post,)*>
{
pub async fn [<write_ch $n>](&mut self, high: bool) -> Result<(), IF::Error> {
self.device.gpo_value().modify_async(|r| {
let v = r.gpo_value();
r.set_gpo_value(if high { v | (1u8 << $n) } else { v & !(1u8 << $n) })
}).await?;
Ok(())
}
}
}
}
}
impl_channel!(0, (), (C1, C2, C3, C4, C5, C6, C7));
impl_channel!(1, (C0), (C2, C3, C4, C5, C6, C7));
impl_channel!(2, (C0, C1), (C3, C4, C5, C6, C7));
impl_channel!(3, (C0, C1, C2), (C4, C5, C6, C7));
impl_channel!(4, (C0, C1, C2, C3), (C5, C6, C7));
impl_channel!(5, (C0, C1, C2, C3, C4), (C6, C7));
impl_channel!(6, (C0, C1, C2, C3, C4, C5), (C7));
impl_channel!(7, (C0, C1, C2, C3, C4, C5, C6), ());
macro_rules! chip_alias {
($bus:ident : $(#[$attr:meta])* $name:ident, $chip:ty) => {
pastey::paste! {
$(#[$attr])*
pub type $name<
BUS,
C0 = Unconfigured,
C1 = Unconfigured,
C2 = Unconfigured,
C3 = Unconfigured,
C4 = Unconfigured,
C5 = Unconfigured,
C6 = Unconfigured,
C7 = Unconfigured,
> = Driver<[<$bus:camel Interface>]<BUS>, $chip, C0, C1, C2, C3, C4, C5, C6, C7>;
}
};
}
chip_alias!(i2c: Tla2528, Tla252x);
chip_alias!(spi: Tla2518, Tla252x);
chip_alias!(i2c: Ads7138, Ads7x38);
chip_alias!(spi: Ads7038, Ads7x38);
chip_alias!(spi:
Ads7038H,
Ads7x38
);
chip_alias!(i2c: Ads7128, Ads7x28);
chip_alias!(spi: Ads7028, Ads7x28);