1 | /// Charge transfer pulse cycles |
2 | #[allow (missing_docs)] |
3 | #[derive (Copy, Clone, PartialEq)] |
4 | pub enum ChargeTransferPulseCycle { |
5 | _1, |
6 | _2, |
7 | _3, |
8 | _4, |
9 | _5, |
10 | _6, |
11 | _7, |
12 | _8, |
13 | _9, |
14 | _10, |
15 | _11, |
16 | _12, |
17 | _13, |
18 | _14, |
19 | _15, |
20 | _16, |
21 | } |
22 | |
23 | impl Into<u8> for ChargeTransferPulseCycle { |
24 | fn into(self) -> u8 { |
25 | match self { |
26 | ChargeTransferPulseCycle::_1 => 0, |
27 | ChargeTransferPulseCycle::_2 => 1, |
28 | ChargeTransferPulseCycle::_3 => 2, |
29 | ChargeTransferPulseCycle::_4 => 3, |
30 | ChargeTransferPulseCycle::_5 => 4, |
31 | ChargeTransferPulseCycle::_6 => 5, |
32 | ChargeTransferPulseCycle::_7 => 6, |
33 | ChargeTransferPulseCycle::_8 => 7, |
34 | ChargeTransferPulseCycle::_9 => 8, |
35 | ChargeTransferPulseCycle::_10 => 9, |
36 | ChargeTransferPulseCycle::_11 => 10, |
37 | ChargeTransferPulseCycle::_12 => 11, |
38 | ChargeTransferPulseCycle::_13 => 12, |
39 | ChargeTransferPulseCycle::_14 => 13, |
40 | ChargeTransferPulseCycle::_15 => 14, |
41 | ChargeTransferPulseCycle::_16 => 15, |
42 | } |
43 | } |
44 | } |
45 | |
46 | /// Max count |
47 | #[allow (missing_docs)] |
48 | #[derive (Copy, Clone)] |
49 | pub enum MaxCount { |
50 | _255, |
51 | _511, |
52 | _1023, |
53 | _2047, |
54 | _4095, |
55 | _8191, |
56 | _16383, |
57 | } |
58 | |
59 | impl Into<u8> for MaxCount { |
60 | fn into(self) -> u8 { |
61 | match self { |
62 | MaxCount::_255 => 0, |
63 | MaxCount::_511 => 1, |
64 | MaxCount::_1023 => 2, |
65 | MaxCount::_2047 => 3, |
66 | MaxCount::_4095 => 4, |
67 | MaxCount::_8191 => 5, |
68 | MaxCount::_16383 => 6, |
69 | } |
70 | } |
71 | } |
72 | |
73 | /// Prescaler divider |
74 | #[allow (missing_docs)] |
75 | #[derive (Copy, Clone, PartialEq)] |
76 | pub enum PGPrescalerDivider { |
77 | _1, |
78 | _2, |
79 | _4, |
80 | _8, |
81 | _16, |
82 | _32, |
83 | _64, |
84 | _128, |
85 | } |
86 | |
87 | impl Into<u8> for PGPrescalerDivider { |
88 | fn into(self) -> u8 { |
89 | match self { |
90 | PGPrescalerDivider::_1 => 0, |
91 | PGPrescalerDivider::_2 => 1, |
92 | PGPrescalerDivider::_4 => 2, |
93 | PGPrescalerDivider::_8 => 3, |
94 | PGPrescalerDivider::_16 => 4, |
95 | PGPrescalerDivider::_32 => 5, |
96 | PGPrescalerDivider::_64 => 6, |
97 | PGPrescalerDivider::_128 => 7, |
98 | } |
99 | } |
100 | } |
101 | |
102 | /// Error type for SSDeviation |
103 | #[derive (Debug, Clone, Copy, PartialEq, Eq)] |
104 | pub enum SSDeviationError { |
105 | /// The provided value is too low (0) |
106 | ValueTooLow, |
107 | /// The provided value is too high (greater than 128) |
108 | ValueTooHigh, |
109 | } |
110 | |
111 | /// Spread Spectrum Deviation |
112 | #[derive (Copy, Clone)] |
113 | pub struct SSDeviation(u8); |
114 | impl SSDeviation { |
115 | /// Create new deviation value, acceptable inputs are 1-128 |
116 | pub fn new(val: u8) -> Result<Self, SSDeviationError> { |
117 | if val == 0 { |
118 | return Err(SSDeviationError::ValueTooLow); |
119 | } else if val > 128 { |
120 | return Err(SSDeviationError::ValueTooHigh); |
121 | } |
122 | Ok(Self(val - 1)) |
123 | } |
124 | } |
125 | |
126 | impl Into<u8> for SSDeviation { |
127 | fn into(self) -> u8 { |
128 | self.0 |
129 | } |
130 | } |
131 | |
132 | /// Peripheral configuration |
133 | #[derive (Clone, Copy)] |
134 | pub struct Config { |
135 | /// Duration of high state of the charge transfer pulse |
136 | pub ct_pulse_high_length: ChargeTransferPulseCycle, |
137 | /// Duration of the low state of the charge transfer pulse |
138 | pub ct_pulse_low_length: ChargeTransferPulseCycle, |
139 | /// Enable/disable of spread spectrum feature |
140 | pub spread_spectrum: bool, |
141 | /// Adds variable number of periods of the SS clk to pulse high state |
142 | pub spread_spectrum_deviation: SSDeviation, |
143 | /// Selects AHB clock divider used to generate SS clk |
144 | pub spread_spectrum_prescaler: bool, |
145 | /// Selects AHB clock divider used to generate pulse generator clk |
146 | pub pulse_generator_prescaler: PGPrescalerDivider, |
147 | /// Maximum number of charge transfer pulses that can be generated before error |
148 | pub max_count_value: MaxCount, |
149 | /// Defines config of all IOs when no ongoing acquisition |
150 | pub io_default_mode: bool, |
151 | /// Polarity of sync input pin |
152 | pub synchro_pin_polarity: bool, |
153 | /// Acquisition starts when start bit is set or with sync pin input |
154 | pub acquisition_mode: bool, |
155 | /// Enable max count interrupt |
156 | pub max_count_interrupt: bool, |
157 | } |
158 | |
159 | impl Default for Config { |
160 | fn default() -> Self { |
161 | Self { |
162 | ct_pulse_high_length: ChargeTransferPulseCycle::_1, |
163 | ct_pulse_low_length: ChargeTransferPulseCycle::_1, |
164 | spread_spectrum: false, |
165 | spread_spectrum_deviation: SSDeviation::new(val:1).unwrap(), |
166 | spread_spectrum_prescaler: false, |
167 | pulse_generator_prescaler: PGPrescalerDivider::_1, |
168 | max_count_value: MaxCount::_255, |
169 | io_default_mode: false, |
170 | synchro_pin_polarity: false, |
171 | acquisition_mode: false, |
172 | max_count_interrupt: false, |
173 | } |
174 | } |
175 | } |
176 | |