1mod gconf;
9mod gstat;
10mod ifcnt;
11mod slaveconf;
12mod otp_prog;
13mod otp_read;
14mod ioin;
15mod factory_conf;
16mod ihold_irun;
17mod tpowerdown;
18mod tstep;
19mod tpwmthrs;
20mod tcoolthrs;
21mod vactual;
22mod sgthrs;
23mod sg_result;
24mod coolconf;
25mod mscnt;
26mod mscuract;
27mod chopconf;
28mod drv_status;
29mod pwmconf;
30mod pwm_scale;
31mod pwm_auto;
32
33pub use gconf::Gconf;
34pub use gstat::Gstat;
35pub use ifcnt::Ifcnt;
36pub use slaveconf::Slaveconf;
37pub use otp_prog::OtpProg;
38pub use otp_read::OtpRead;
39pub use ioin::Ioin;
40pub use factory_conf::FactoryConf;
41pub use ihold_irun::IholdIrun;
42pub use tpowerdown::Tpowerdown;
43pub use tstep::Tstep;
44pub use tpwmthrs::Tpwmthrs;
45pub use tcoolthrs::Tcoolthrs;
46pub use vactual::Vactual;
47pub use sgthrs::Sgthrs;
48pub use sg_result::SgResult;
49pub use coolconf::Coolconf;
50pub use mscnt::Mscnt;
51pub use mscuract::Mscuract;
52pub use chopconf::Chopconf;
53pub use drv_status::DrvStatus;
54pub use pwmconf::Pwmconf;
55pub use pwm_scale::PwmScale;
56pub use pwm_auto::PwmAuto;
57
58pub trait Register: Sized + Copy + Clone + Default + Into<u32> + From<u32> {
60 const ADDRESS: Address;
62
63 fn address() -> Address {
65 Self::ADDRESS
66 }
67}
68
69pub trait ReadableRegister: Register {}
71
72pub trait WritableRegister: Register {}
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79#[cfg_attr(feature = "defmt", derive(defmt::Format))]
80#[repr(u8)]
81pub enum Address {
82 Gconf = 0x00,
84 Gstat = 0x01,
86 Ifcnt = 0x02,
88 Slaveconf = 0x03,
90 OtpProg = 0x04,
92 OtpRead = 0x05,
94 Ioin = 0x06,
96 FactoryConf = 0x07,
98 IholdIrun = 0x10,
100 Tpowerdown = 0x11,
102 Tstep = 0x12,
104 Tpwmthrs = 0x13,
106 Tcoolthrs = 0x14,
108 Vactual = 0x22,
110 Sgthrs = 0x40,
112 SgResult = 0x41,
114 Coolconf = 0x42,
116 Mscnt = 0x6A,
118 Mscuract = 0x6B,
120 Chopconf = 0x6C,
122 DrvStatus = 0x6F,
124 Pwmconf = 0x70,
126 PwmScale = 0x71,
128 PwmAuto = 0x72,
130}
131
132impl Address {
133 pub fn from_u8(value: u8) -> Option<Self> {
135 match value {
136 0x00 => Some(Address::Gconf),
137 0x01 => Some(Address::Gstat),
138 0x02 => Some(Address::Ifcnt),
139 0x03 => Some(Address::Slaveconf),
140 0x04 => Some(Address::OtpProg),
141 0x05 => Some(Address::OtpRead),
142 0x06 => Some(Address::Ioin),
143 0x07 => Some(Address::FactoryConf),
144 0x10 => Some(Address::IholdIrun),
145 0x11 => Some(Address::Tpowerdown),
146 0x12 => Some(Address::Tstep),
147 0x13 => Some(Address::Tpwmthrs),
148 0x14 => Some(Address::Tcoolthrs),
149 0x22 => Some(Address::Vactual),
150 0x40 => Some(Address::Sgthrs),
151 0x41 => Some(Address::SgResult),
152 0x42 => Some(Address::Coolconf),
153 0x6A => Some(Address::Mscnt),
154 0x6B => Some(Address::Mscuract),
155 0x6C => Some(Address::Chopconf),
156 0x6F => Some(Address::DrvStatus),
157 0x70 => Some(Address::Pwmconf),
158 0x71 => Some(Address::PwmScale),
159 0x72 => Some(Address::PwmAuto),
160 _ => None,
161 }
162 }
163
164 pub fn is_readable(self) -> bool {
166 matches!(
167 self,
168 Address::Gconf
169 | Address::Gstat
170 | Address::Ifcnt
171 | Address::OtpRead
172 | Address::Ioin
173 | Address::FactoryConf
174 | Address::Tstep
175 | Address::SgResult
176 | Address::Mscnt
177 | Address::Mscuract
178 | Address::Chopconf
179 | Address::DrvStatus
180 | Address::Pwmconf
181 | Address::PwmScale
182 | Address::PwmAuto
183 )
184 }
185
186 pub fn is_writable(self) -> bool {
188 matches!(
189 self,
190 Address::Gconf
191 | Address::Gstat
192 | Address::Slaveconf
193 | Address::OtpProg
194 | Address::FactoryConf
195 | Address::IholdIrun
196 | Address::Tpowerdown
197 | Address::Tpwmthrs
198 | Address::Tcoolthrs
199 | Address::Vactual
200 | Address::Sgthrs
201 | Address::Coolconf
202 | Address::Chopconf
203 | Address::Pwmconf
204 )
205 }
206
207 pub fn as_u8(self) -> u8 {
209 self as u8
210 }
211}
212
213impl From<Address> for u8 {
214 fn from(addr: Address) -> u8 {
215 addr as u8
216 }
217}
218
219#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
223#[cfg_attr(feature = "defmt", derive(defmt::Format))]
224#[repr(u8)]
225pub enum MicrostepResolution {
226 #[default]
228 M256 = 0,
229 M128 = 1,
231 M64 = 2,
233 M32 = 3,
235 M16 = 4,
237 M8 = 5,
239 M4 = 6,
241 M2 = 7,
243 M1 = 8,
245}
246
247impl MicrostepResolution {
248 pub fn from_mres(mres: u8) -> Self {
250 match mres {
251 0 => Self::M256,
252 1 => Self::M128,
253 2 => Self::M64,
254 3 => Self::M32,
255 4 => Self::M16,
256 5 => Self::M8,
257 6 => Self::M4,
258 7 => Self::M2,
259 8 => Self::M1,
260 _ => Self::M256,
261 }
262 }
263
264 pub fn to_mres(self) -> u8 {
266 self as u8
267 }
268
269 pub fn microsteps(self) -> u16 {
271 match self {
272 Self::M256 => 256,
273 Self::M128 => 128,
274 Self::M64 => 64,
275 Self::M32 => 32,
276 Self::M16 => 16,
277 Self::M8 => 8,
278 Self::M4 => 4,
279 Self::M2 => 2,
280 Self::M1 => 1,
281 }
282 }
283}
284
285#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
287#[cfg_attr(feature = "defmt", derive(defmt::Format))]
288#[repr(u8)]
289pub enum StandstillMode {
290 #[default]
292 Normal = 0,
293 Freewheeling = 1,
295 StrongBraking = 2,
297 Braking = 3,
299}
300
301impl StandstillMode {
302 pub fn from_bits(value: u8) -> Self {
304 match value & 0x03 {
305 0 => Self::Normal,
306 1 => Self::Freewheeling,
307 2 => Self::StrongBraking,
308 3 => Self::Braking,
309 _ => Self::Normal,
310 }
311 }
312
313 pub fn to_bits(self) -> u8 {
315 self as u8
316 }
317}