use super::super::{
BusOperation, Error, Lps22hh, RegisterOperation, SensorOperation, bisync, register::OnState,
};
use bitfield_struct::bitfield;
use derive_more::TryFrom;
use st_mem_bank_macro::register;
#[repr(u8)]
#[derive(Clone, Copy)]
pub enum Reg {
InterruptCfg = 0x0B,
ThsPL = 0x0C,
ThsPH = 0x0D,
IfCtrl = 0x0E,
WhoAmI = 0x0F,
CtrlReg1 = 0x10,
CtrlReg2 = 0x11,
CtrlReg3 = 0x12,
FifoCtrl = 0x13,
FifoWtm = 0x14,
RefPL = 0x15,
RefPH = 0x16,
RpdsL = 0x18,
RpdsH = 0x19,
IntSource = 0x24,
FifoStatus1 = 0x25,
FifoStatus2 = 0x26,
Status = 0x27,
PressOutXl = 0x28,
PressOutL = 0x29,
PressOutH = 0x2A,
TempOutL = 0x2B,
TempOutH = 0x2C,
FifoDataOutPressXl = 0x78,
FifoDataOutPressL = 0x79,
FifoDataOutPressH = 0x7A,
FifoDataOutTempL = 0x7B,
FifoDataOutTempH = 0x7C,
}
#[register(address = Reg::InterruptCfg, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct InterruptCfg {
#[bits(2)]
pub pe: u8,
#[bits(1)]
pub lir: u8,
#[bits(1)]
pub diff_en: u8,
#[bits(1)]
pub reset_az: u8,
#[bits(1)]
pub autozero: u8,
#[bits(1)]
pub reset_arp: u8,
#[bits(1)]
pub autorefp: u8,
}
#[register(address = Reg::ThsPL, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u16, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u16, order = Lsb))]
pub struct ThsP {
#[bits(15)]
pub ths: u16,
#[bits(1, access = RO)]
pub not_used_01: u8,
}
#[register(address = Reg::IfCtrl, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct IfCtrl {
#[bits(1)]
pub i2c_disable: u8,
#[bits(1)]
pub i3c_disable: u8,
#[bits(1)]
pub pd_dis_int1: u8,
#[bits(1)]
pub sdo_pu_en: u8,
#[bits(1)]
pub sda_pu_en: u8,
#[bits(2, access = RO)]
pub not_used_01: u8,
#[bits(1)]
pub int_en_i3c: u8,
}
#[register(address = Reg::WhoAmI, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct WhoAmI {
#[bits(8, access = RO)]
pub who_am_i: u8,
}
#[register(address = Reg::CtrlReg1, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct CtrlReg1 {
#[bits(1)]
pub sim: u8,
#[bits(1)]
pub bdu: u8,
#[bits(2)]
pub lpfp_cfg: u8,
#[bits(3)]
pub odr: u8,
#[bits(1, access = RO)]
pub not_used_01: u8,
}
#[register(address = Reg::CtrlReg2, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct CtrlReg2 {
#[bits(1)]
pub one_shot: u8,
#[bits(1)]
pub low_noise_en: u8,
#[bits(1)]
pub swreset: u8,
#[bits(1, access = RO)]
pub not_used_01: u8,
#[bits(1)]
pub if_add_inc: u8,
#[bits(1)]
pub pp_od: u8,
#[bits(1)]
pub int_h_l: u8,
#[bits(1)]
pub boot: u8,
}
#[register(address = Reg::CtrlReg3, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct CtrlReg3 {
#[bits(2)]
pub int_s: u8,
#[bits(1)]
pub drdy: u8,
#[bits(1)]
pub int_f_ovr: u8,
#[bits(1)]
pub int_f_wtm: u8,
#[bits(1)]
pub int_f_full: u8,
#[bits(2, access = RO)]
pub not_used_01: u8,
}
#[register(address = Reg::FifoCtrl, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct FifoCtrl {
#[bits(3)]
pub f_mode: u8,
#[bits(1)]
pub stop_on_wtm: u8,
#[bits(4, access = RO)]
pub not_used_01: u8,
}
#[register(address = Reg::FifoWtm, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct FifoWtm {
#[bits(7)]
pub wtm: u8,
#[bits(1, access = RO)]
pub not_used_01: u8,
}
#[register(address = Reg::RefPL, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u16, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u16, order = Lsb))]
pub struct RefP {
#[bits(16)]
pub ref_p: i16,
}
#[register(address = Reg::RpdsL, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u16, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u16, order = Lsb))]
pub struct Rpds {
#[bits(16)]
pub rpds: i16,
}
#[register(address = Reg::IntSource, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct IntSource {
#[bits(1)]
pub ph: u8,
#[bits(1)]
pub pl: u8,
#[bits(1)]
pub ia: u8,
#[bits(4, access = RO)]
pub not_used_01: u8,
#[bits(1)]
pub boot_on: u8,
}
#[register(address = Reg::FifoStatus1, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u16, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u16, order = Lsb))]
pub struct FifoStatusReg {
#[bits(8)]
pub fss: u8,
#[bits(5, access = RO)]
pub not_used_01: u8,
#[bits(1)]
pub fifo_full_ia: u8,
#[bits(1)]
pub fifo_ovr_ia: u8,
#[bits(1)]
pub fifo_wtm_ia: u8,
}
impl From<FifoStatusReg> for FifoStatus1 {
fn from(value: FifoStatusReg) -> Self {
let bytes = value.into_bits().to_le_bytes();
Self::from_bits(bytes[0])
}
}
impl From<FifoStatusReg> for FifoStatus2 {
fn from(value: FifoStatusReg) -> Self {
let bytes = value.into_bits().to_le_bytes();
Self::from_bits(bytes[1])
}
}
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct FifoStatus1 {
#[bits(8)]
pub fss: u8,
}
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct FifoStatus2 {
#[bits(5, access = RO)]
pub not_used_01: u8,
#[bits(1)]
pub fifo_full_ia: u8,
#[bits(1)]
pub fifo_ovr_ia: u8,
#[bits(1)]
pub fifo_wtm_ia: u8,
}
#[register(address = Reg::Status, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u8, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u8, order = Lsb))]
pub struct Status {
#[bits(1)]
pub p_da: u8,
#[bits(1)]
pub t_da: u8,
#[bits(2, access = RO)]
pub not_used_01: u8,
#[bits(1)]
pub p_or: u8,
#[bits(1)]
pub t_or: u8,
#[bits(2, access = RO)]
pub not_used_02: u8,
}
#[register(address = Reg::PressOutXl, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u32, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u32, order = Lsb))]
pub struct PressOut {
#[offset_before(8)]
#[bits(32)]
pub pressure: u32,
}
#[register(address = Reg::TempOutL, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u16, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u16, order = Lsb))]
pub struct TempOut {
#[bits(16)]
pub temperature: i16,
}
#[register(address = Reg::FifoDataOutPressXl, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u32, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u32, order = Lsb))]
pub struct FifoDataOutPress {
#[offset_before(8)]
#[bits(32)]
pub pressure: u32,
}
#[register(address = Reg::FifoDataOutTempL, access_type = "Lps22hh<B, OnState>")]
#[cfg_attr(feature = "bit_order_msb", bitfield(u16, order = Msb))]
#[cfg_attr(not(feature = "bit_order_msb"), bitfield(u16, order = Lsb))]
pub struct FifoDataOutTemp {
#[bits(16)]
pub temperature: i16,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum Odr {
#[default]
PowerDown = 0x00,
OneShoot = 0x08,
_1hz = 0x01,
_10hz = 0x02,
_25hz = 0x03,
_50hz = 0x04,
_75hz = 0x05,
_1hzLowNoise = 0x11,
_10hzLowNoise = 0x12,
_25hzLowNoise = 0x13,
_50hzLowNoise = 0x14,
_75hzLowNoise = 0x15,
_100hz = 0x06,
_200hz = 0x07,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum LpfpCfg {
#[default]
OdrDiv2 = 0,
OdrDiv9 = 1,
OdrDiv20 = 2,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum I2cMode {
#[default]
Enable = 0,
Disable = 1,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum I3cMode {
#[default]
Enable = 0x00,
EnableIntPinEnable = 0x10,
Disable = 0x11,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum PullUp {
#[default]
Disconnect = 0,
Connect = 1,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum Sim {
#[default]
Spi4Wire = 0,
Spi3Wire = 1,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum Lir {
#[default]
Pulsed = 0,
Latched = 1,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum PpOd {
#[default]
PushPull = 0,
OpenDrain = 1,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum IntHL {
#[default]
ActiveHigh = 0,
ActiveLow = 1,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum Pe {
#[default]
NoThreshold = 0,
Positive = 1,
Negative = 2,
Both = 3,
}
#[repr(u8)]
#[derive(Clone, Copy, Default, TryFrom)]
#[try_from(repr)]
pub enum FMode {
#[default]
BypassMode = 0,
FifoMode = 1,
StreamMode = 2,
DynamicStreamMode = 3,
BypassToFifoMode = 5,
BypassToStreamMode = 6,
StreamToFifoMode = 7,
}
#[derive(Default)]
pub struct PinIntRoute {
pub drdy_pres: bool,
pub fifo_th: bool,
pub fifo_ovr: bool,
pub fifo_full: bool,
}