aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--embassy-stm32/src/tsc/enums.rs23
-rw-r--r--embassy-stm32/src/tsc/mod.rs22
2 files changed, 38 insertions, 7 deletions
diff --git a/embassy-stm32/src/tsc/enums.rs b/embassy-stm32/src/tsc/enums.rs
index 56df4173a..bc8e9d2f5 100644
--- a/embassy-stm32/src/tsc/enums.rs
+++ b/embassy-stm32/src/tsc/enums.rs
@@ -90,9 +90,28 @@ impl Into<u32> for TscIOPin {
90 } 90 }
91} 91}
92 92
93/// Spread Spectrum Deviation
94#[derive(Copy, Clone)]
95pub struct SSDeviation(u8);
96impl SSDeviation {
97 /// Create new deviation value, acceptable inputs are 1-128
98 pub fn new(val: u8) -> Result<Self, ()> {
99 if val == 0 || val > 128 {
100 return Err(());
101 }
102 Ok(Self(val - 1))
103 }
104}
105
106impl Into<u8> for SSDeviation {
107 fn into(self) -> u8 {
108 self.0
109 }
110}
111
93/// Charge transfer pulse cycles 112/// Charge transfer pulse cycles
94#[allow(missing_docs)] 113#[allow(missing_docs)]
95#[derive(Copy, Clone)] 114#[derive(Copy, Clone, PartialEq)]
96pub enum ChargeTransferPulseCycle { 115pub enum ChargeTransferPulseCycle {
97 _1, 116 _1,
98 _2, 117 _2,
@@ -137,7 +156,7 @@ impl Into<u8> for ChargeTransferPulseCycle {
137 156
138/// Prescaler divider 157/// Prescaler divider
139#[allow(missing_docs)] 158#[allow(missing_docs)]
140#[derive(Copy, Clone)] 159#[derive(Copy, Clone, PartialEq)]
141pub enum PGPrescalerDivider { 160pub enum PGPrescalerDivider {
142 _1, 161 _1,
143 _2, 162 _2,
diff --git a/embassy-stm32/src/tsc/mod.rs b/embassy-stm32/src/tsc/mod.rs
index 91cf7187a..8da0e87ab 100644
--- a/embassy-stm32/src/tsc/mod.rs
+++ b/embassy-stm32/src/tsc/mod.rs
@@ -6,7 +6,6 @@
6pub mod enums; 6pub mod enums;
7 7
8use crate::gpio::AnyPin; 8use crate::gpio::AnyPin;
9use crate::pac::tsc::regs;
10use crate::{pac::tsc::Tsc as Regs, rcc::RccPeripheral}; 9use crate::{pac::tsc::Tsc as Regs, rcc::RccPeripheral};
11use crate::{peripherals, Peripheral}; 10use crate::{peripherals, Peripheral};
12use embassy_hal_internal::{into_ref, PeripheralRef}; 11use embassy_hal_internal::{into_ref, PeripheralRef};
@@ -98,7 +97,7 @@ pub struct Config {
98 /// Enable/disable of spread spectrum feature 97 /// Enable/disable of spread spectrum feature
99 pub spread_spectrum: bool, 98 pub spread_spectrum: bool,
100 /// Adds variable number of periods of the SS clk to pulse high state 99 /// Adds variable number of periods of the SS clk to pulse high state
101 pub spread_spectrum_deviation: u8, 100 pub spread_spectrum_deviation: SSDeviation,
102 /// Selects AHB clock divider used to generate SS clk 101 /// Selects AHB clock divider used to generate SS clk
103 pub spread_spectrum_prescaler: bool, 102 pub spread_spectrum_prescaler: bool,
104 /// Selects AHB clock divider used to generate pulse generator clk 103 /// Selects AHB clock divider used to generate pulse generator clk
@@ -127,7 +126,7 @@ impl Default for Config {
127 ct_pulse_high_length: ChargeTransferPulseCycle::_1, 126 ct_pulse_high_length: ChargeTransferPulseCycle::_1,
128 ct_pulse_low_length: ChargeTransferPulseCycle::_1, 127 ct_pulse_low_length: ChargeTransferPulseCycle::_1,
129 spread_spectrum: false, 128 spread_spectrum: false,
130 spread_spectrum_deviation: 0, 129 spread_spectrum_deviation: SSDeviation::new(0).unwrap(),
131 spread_spectrum_prescaler: false, 130 spread_spectrum_prescaler: false,
132 pulse_generator_prescaler: PGPrescalerDivider::_1, 131 pulse_generator_prescaler: PGPrescalerDivider::_1,
133 max_count_value: MaxCount::_255, 132 max_count_value: MaxCount::_255,
@@ -255,9 +254,22 @@ impl<'d, T: Instance> Tsc<'d, T> {
255 w.set_ctph(config.ct_pulse_high_length.into()); 254 w.set_ctph(config.ct_pulse_high_length.into());
256 w.set_ctpl(config.ct_pulse_low_length.into()); 255 w.set_ctpl(config.ct_pulse_low_length.into());
257 w.set_sse(config.spread_spectrum); 256 w.set_sse(config.spread_spectrum);
258 w.set_ssd(config.spread_spectrum_deviation); 257 // Prevent invalid configuration for pulse generator prescaler
258 if config.ct_pulse_low_length == ChargeTransferPulseCycle::_1
259 && (config.pulse_generator_prescaler == PGPrescalerDivider::_1
260 || config.pulse_generator_prescaler == PGPrescalerDivider::_2)
261 {
262 w.set_pgpsc(PGPrescalerDivider::_4.into());
263 } else if config.ct_pulse_low_length == ChargeTransferPulseCycle::_2
264 && config.pulse_generator_prescaler == PGPrescalerDivider::_1
265 {
266 w.set_pgpsc(PGPrescalerDivider::_2.into());
267 } else {
268 w.set_pgpsc(config.pulse_generator_prescaler.into());
269 }
270 w.set_ssd(config.spread_spectrum_deviation.into());
259 w.set_sspsc(config.spread_spectrum_prescaler); 271 w.set_sspsc(config.spread_spectrum_prescaler);
260 w.set_pgpsc(config.pulse_generator_prescaler.into()); 272
261 w.set_mcv(config.max_count_value.into()); 273 w.set_mcv(config.max_count_value.into());
262 w.set_syncpol(config.synchro_pin_polarity); 274 w.set_syncpol(config.synchro_pin_polarity);
263 w.set_am(config.acquisition_mode); 275 w.set_am(config.acquisition_mode);