1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! COOLCONF - CoolStep and StallGuard2 configuration register (0x42)
use super::{Address, Register, WritableRegister};
/// CoolStep and StallGuard2 configuration register.
///
/// Controls CoolStep adaptive current control and StallGuard filtering.
/// CoolStep automatically adjusts motor current based on load.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct Coolconf(u32);
impl Coolconf {
/// Create with default value (0).
pub fn new() -> Self {
Self(0)
}
/// Get SEMIN (0-15).
///
/// Minimum StallGuard value for CoolStep current increase.
/// - 0: CoolStep disabled
/// - 1-15: If SG_RESULT < SEMIN*32, motor current is increased
pub fn semin(&self) -> u8 {
(self.0 & 0x0F) as u8
}
/// Set SEMIN (0-15).
pub fn set_semin(&mut self, value: u8) -> &mut Self {
self.0 = (self.0 & !0x0F) | ((value as u32) & 0x0F);
self
}
/// Get SEUP (0-3).
///
/// Current increment step width:
/// - 0: +1
/// - 1: +2
/// - 2: +4
/// - 3: +8
pub fn seup(&self) -> u8 {
((self.0 >> 5) & 0x03) as u8
}
/// Set SEUP (0-3).
pub fn set_seup(&mut self, value: u8) -> &mut Self {
self.0 = (self.0 & !(0x03 << 5)) | (((value as u32) & 0x03) << 5);
self
}
/// Get SEMAX (0-15).
///
/// StallGuard hysteresis value for CoolStep current decrease.
/// If SG_RESULT > (SEMIN + SEMAX + 1)*32, motor current is decreased.
pub fn semax(&self) -> u8 {
((self.0 >> 8) & 0x0F) as u8
}
/// Set SEMAX (0-15).
pub fn set_semax(&mut self, value: u8) -> &mut Self {
self.0 = (self.0 & !(0x0F << 8)) | (((value as u32) & 0x0F) << 8);
self
}
/// Get SEDN (0-3).
///
/// Current decrement step width:
/// - 0: Decrement by 32
/// - 1: Decrement by 8
/// - 2: Decrement by 2
/// - 3: Decrement by 1
pub fn sedn(&self) -> u8 {
((self.0 >> 13) & 0x03) as u8
}
/// Set SEDN (0-3).
pub fn set_sedn(&mut self, value: u8) -> &mut Self {
self.0 = (self.0 & !(0x03 << 13)) | (((value as u32) & 0x03) << 13);
self
}
/// Get SEIMIN.
///
/// Minimum current for CoolStep:
/// - false: 1/2 of IRUN
/// - true: 1/4 of IRUN
pub fn seimin(&self) -> bool {
(self.0 >> 15) & 1 != 0
}
/// Set SEIMIN.
pub fn set_seimin(&mut self, value: bool) -> &mut Self {
if value {
self.0 |= 1 << 15;
} else {
self.0 &= !(1 << 15);
}
self
}
/// Get the raw register value.
pub fn raw(&self) -> u32 {
self.0
}
/// Create from raw value.
pub fn from_raw(value: u32) -> Self {
Self(value)
}
}
impl Default for Coolconf {
fn default() -> Self {
Self::new()
}
}
impl Register for Coolconf {
const ADDRESS: Address = Address::Coolconf;
}
impl WritableRegister for Coolconf {}
impl From<u32> for Coolconf {
fn from(value: u32) -> Self {
Self(value)
}
}
impl From<Coolconf> for u32 {
fn from(reg: Coolconf) -> u32 {
reg.0
}
}