mod gconf;
mod gstat;
mod ifcnt;
mod slaveconf;
mod otp_prog;
mod otp_read;
mod ioin;
mod factory_conf;
mod ihold_irun;
mod tpowerdown;
mod tstep;
mod tpwmthrs;
mod tcoolthrs;
mod vactual;
mod sgthrs;
mod sg_result;
mod coolconf;
mod mscnt;
mod mscuract;
mod chopconf;
mod drv_status;
mod pwmconf;
mod pwm_scale;
mod pwm_auto;
pub use gconf::Gconf;
pub use gstat::Gstat;
pub use ifcnt::Ifcnt;
pub use slaveconf::Slaveconf;
pub use otp_prog::OtpProg;
pub use otp_read::OtpRead;
pub use ioin::Ioin;
pub use factory_conf::FactoryConf;
pub use ihold_irun::IholdIrun;
pub use tpowerdown::Tpowerdown;
pub use tstep::Tstep;
pub use tpwmthrs::Tpwmthrs;
pub use tcoolthrs::Tcoolthrs;
pub use vactual::Vactual;
pub use sgthrs::Sgthrs;
pub use sg_result::SgResult;
pub use coolconf::Coolconf;
pub use mscnt::Mscnt;
pub use mscuract::Mscuract;
pub use chopconf::Chopconf;
pub use drv_status::DrvStatus;
pub use pwmconf::Pwmconf;
pub use pwm_scale::PwmScale;
pub use pwm_auto::PwmAuto;
pub trait Register: Sized + Copy + Clone + Default + Into<u32> + From<u32> {
const ADDRESS: Address;
fn address() -> Address {
Self::ADDRESS
}
}
pub trait ReadableRegister: Register {}
pub trait WritableRegister: Register {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum Address {
Gconf = 0x00,
Gstat = 0x01,
Ifcnt = 0x02,
Slaveconf = 0x03,
OtpProg = 0x04,
OtpRead = 0x05,
Ioin = 0x06,
FactoryConf = 0x07,
IholdIrun = 0x10,
Tpowerdown = 0x11,
Tstep = 0x12,
Tpwmthrs = 0x13,
Tcoolthrs = 0x14,
Vactual = 0x22,
Sgthrs = 0x40,
SgResult = 0x41,
Coolconf = 0x42,
Mscnt = 0x6A,
Mscuract = 0x6B,
Chopconf = 0x6C,
DrvStatus = 0x6F,
Pwmconf = 0x70,
PwmScale = 0x71,
PwmAuto = 0x72,
}
impl Address {
pub fn from_u8(value: u8) -> Option<Self> {
match value {
0x00 => Some(Address::Gconf),
0x01 => Some(Address::Gstat),
0x02 => Some(Address::Ifcnt),
0x03 => Some(Address::Slaveconf),
0x04 => Some(Address::OtpProg),
0x05 => Some(Address::OtpRead),
0x06 => Some(Address::Ioin),
0x07 => Some(Address::FactoryConf),
0x10 => Some(Address::IholdIrun),
0x11 => Some(Address::Tpowerdown),
0x12 => Some(Address::Tstep),
0x13 => Some(Address::Tpwmthrs),
0x14 => Some(Address::Tcoolthrs),
0x22 => Some(Address::Vactual),
0x40 => Some(Address::Sgthrs),
0x41 => Some(Address::SgResult),
0x42 => Some(Address::Coolconf),
0x6A => Some(Address::Mscnt),
0x6B => Some(Address::Mscuract),
0x6C => Some(Address::Chopconf),
0x6F => Some(Address::DrvStatus),
0x70 => Some(Address::Pwmconf),
0x71 => Some(Address::PwmScale),
0x72 => Some(Address::PwmAuto),
_ => None,
}
}
pub fn is_readable(self) -> bool {
matches!(
self,
Address::Gconf
| Address::Gstat
| Address::Ifcnt
| Address::OtpRead
| Address::Ioin
| Address::FactoryConf
| Address::Tstep
| Address::SgResult
| Address::Mscnt
| Address::Mscuract
| Address::Chopconf
| Address::DrvStatus
| Address::Pwmconf
| Address::PwmScale
| Address::PwmAuto
)
}
pub fn is_writable(self) -> bool {
matches!(
self,
Address::Gconf
| Address::Gstat
| Address::Slaveconf
| Address::OtpProg
| Address::FactoryConf
| Address::IholdIrun
| Address::Tpowerdown
| Address::Tpwmthrs
| Address::Tcoolthrs
| Address::Vactual
| Address::Sgthrs
| Address::Coolconf
| Address::Chopconf
| Address::Pwmconf
)
}
pub fn as_u8(self) -> u8 {
self as u8
}
}
impl From<Address> for u8 {
fn from(addr: Address) -> u8 {
addr as u8
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum MicrostepResolution {
#[default]
M256 = 0,
M128 = 1,
M64 = 2,
M32 = 3,
M16 = 4,
M8 = 5,
M4 = 6,
M2 = 7,
M1 = 8,
}
impl MicrostepResolution {
pub fn from_mres(mres: u8) -> Self {
match mres {
0 => Self::M256,
1 => Self::M128,
2 => Self::M64,
3 => Self::M32,
4 => Self::M16,
5 => Self::M8,
6 => Self::M4,
7 => Self::M2,
8 => Self::M1,
_ => Self::M256,
}
}
pub fn to_mres(self) -> u8 {
self as u8
}
pub fn microsteps(self) -> u16 {
match self {
Self::M256 => 256,
Self::M128 => 128,
Self::M64 => 64,
Self::M32 => 32,
Self::M16 => 16,
Self::M8 => 8,
Self::M4 => 4,
Self::M2 => 2,
Self::M1 => 1,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum StandstillMode {
#[default]
Normal = 0,
Freewheeling = 1,
StrongBraking = 2,
Braking = 3,
}
impl StandstillMode {
pub fn from_bits(value: u8) -> Self {
match value & 0x03 {
0 => Self::Normal,
1 => Self::Freewheeling,
2 => Self::StrongBraking,
3 => Self::Braking,
_ => Self::Normal,
}
}
pub fn to_bits(self) -> u8 {
self as u8
}
}