Skip to main content

AS5600_Driver/
types.rs

1/// Power consumption modes of the AS5600.
2///
3/// Lower power modes reduce current consumption by increasing the sampling interval.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum PowerMode {
6    /// No power saving, continuous sampling. (Current: ~6.5mA)
7    Nominal = 0b00,
8    /// Low Power Mode 1 (Sampling: 1ms)
9    LPM1 = 0b01,
10    /// Low Power Mode 2 (Sampling: 10ms)
11    LPM2 = 0b10,
12    /// Low Power Mode 3 (Sampling: 100ms)
13    LPM3 = 0b11,
14}
15
16/// Hysteresis settings to suppress noise in the output.
17///
18/// Defines the number of LSBs the position must change before the output is updated.
19#[derive(Debug, Clone, Copy, PartialEq, Eq)]
20pub enum Hysteresis {
21    /// No hysteresis.
22    Off = 0b00,
23    /// 1 LSB hysteresis.
24    Lsb1 = 0b01,
25    /// 2 LSBs hysteresis.
26    Lsb2 = 0b10,
27    /// 3 LSBs hysteresis.
28    Lsb3 = 0b11,
29}
30
31/// Output stage configuration for the OUT pin.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33pub enum OutputStage {
34    /// Ratiometric analog output (0V to VDD).
35    AnalogFull = 0b00,
36    /// Ratiometric analog output (10% to 90% of VDD).
37    AnalogReduced = 0b01,
38    /// Pulse Width Modulation (PWM) output.
39    PWM = 0b10,
40}
41
42/// PWM signal frequency when using PWM output stage.
43#[derive(Debug, Clone, Copy, PartialEq, Eq)]
44pub enum PwmFrequency {
45    /// 115 Hz PWM frequency.
46    Hz115 = 0b00,
47    /// 230 Hz PWM frequency.
48    Hz230 = 0b01,
49    /// 460 Hz PWM frequency.
50    Hz460 = 0b10,
51    /// 920 Hz PWM frequency.
52    Hz920 = 0b11,
53}
54
55/// Slow filter settings for noise reduction.
56///
57/// Higher values mean more averaging and less noise, but higher step response time.
58#[derive(Debug, Clone, Copy, PartialEq, Eq)]
59pub enum SlowFilter {
60    /// 16x averaging.
61    X16 = 0b00,
62    /// 8x averaging.
63    X8 = 0b01,
64    /// 4x averaging.
65    X4 = 0b10,
66    /// 2x averaging.
67    X2 = 0b11,
68}
69
70/// Fast filter threshold for adaptive filtering.
71///
72/// If the position change exceeds this threshold, the slow filter is bypassed
73/// to provide a fast response.
74#[derive(Debug, Clone, Copy, PartialEq, Eq)]
75pub enum FastFilterThreshold {
76    /// Fast filter disabled, only slow filter is used.
77    SlowOnly = 0b000,
78    /// 6 LSB threshold.
79    Lsb6 = 0b001,
80    /// 7 LSB threshold.
81    Lsb7 = 0b010,
82    /// 9 LSB threshold.
83    Lsb9 = 0b011,
84    /// 18 LSB threshold.
85    Lsb18 = 0b100,
86    /// 21 LSB threshold.
87    Lsb21 = 0b101,
88    /// 24 LSB threshold.
89    Lsb24 = 0b110,
90    /// 10 LSB threshold.
91    Lsb10 = 0b111,
92}
93
94/// Status of the magnetic system.
95///
96/// Provides information about magnet detection and field strength.
97#[derive(Debug, Clone, Copy, PartialEq, Eq)]
98pub struct MagnetStatus {
99    /// True if a magnet is detected by the Hall sensors.
100    pub detected: bool,
101    /// True if the magnetic field is too weak (magnet too far).
102    pub too_weak: bool,
103    /// True if the magnetic field is too strong (magnet too close).
104    pub too_strong: bool,
105}
106
107/// Full configuration of the AS5600 chip.
108///
109/// This struct maps to the CONF_HI and CONF_LO registers.
110#[derive(Debug, Clone, Copy)]
111pub struct Configuration {
112    /// Current power mode.
113    pub power_mode: PowerMode,
114    /// Hysteresis setting.
115    pub hysteresis: Hysteresis,
116    /// Output pin functionality.
117    pub output_stage: OutputStage,
118    /// Frequency for PWM output.
119    pub pwm_frequency: PwmFrequency,
120    /// Slow filter averaging factor.
121    pub slow_filter: SlowFilter,
122    /// Threshold for fast filter bypass.
123    pub fast_filter_threshold: FastFilterThreshold,
124    /// Enable/Disable the watchdog timer (auto-low-power after 1 minute of inactivity).
125    pub watchdog: bool,
126}
127
128impl Default for Configuration {
129    fn default() -> Self {
130        Self {
131            power_mode: PowerMode::Nominal,
132            hysteresis: Hysteresis::Lsb1,
133            output_stage: OutputStage::AnalogFull,
134            pwm_frequency: PwmFrequency::Hz115,
135            slow_filter: SlowFilter::X16,
136            fast_filter_threshold: FastFilterThreshold::SlowOnly,
137            watchdog: true,
138        }
139    }
140}