From 58cc39616c627df329ca8a6908114c8897d99b50 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 6 Jul 2025 20:59:02 +0200 Subject: [PATCH 1/2] Allow inverting comparator on the fly --- src/comparator.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/comparator.rs b/src/comparator.rs index e5603f4e..77356a9d 100644 --- a/src/comparator.rs +++ b/src/comparator.rs @@ -93,11 +93,35 @@ impl Config { self } + /// Invert the comparator output + /// + /// NOTE: This does NOT affect the value read by [Comparator::output] + /// + /// The following observers are affected by this setting + /// * Pin output + /// * EXTI interrupt/wakeup + /// * TIMx + /// + /// The following observers are NOT affected by this setting + /// * HRTIM + /// * Software using [Comparator::output] pub fn output_inverted(mut self) -> Self { self.inverted = true; self } + /// Invert the comparator output + /// + /// NOTE: This does NOT affect the value read by [Comparator::output] + /// + /// The following observers are affected by this setting + /// * Pin output + /// * EXTI interrupt/wakeup + /// * TIMx + /// + /// The following observers are NOT affected by this setting + /// * HRTIM + /// * Software using [Comparator::output] pub fn output_polarity(mut self, inverted: bool) -> Self { self.inverted = inverted; self @@ -451,7 +475,25 @@ macro_rules! impl_comparator { } impl Comparator<$COMP, ED> { + /// Invert the comparator output + /// + /// NOTE: This does NOT affect the value read by [Comparator::output] + /// + /// The following observers are affected by this setting + /// * Pin output + /// * EXTI interrupt/wakeup + /// * TIMx + /// + /// The following observers are NOT affected by this setting + /// * HRTIM + /// * Software using [Comparator::output] + pub fn set_inverted(&mut self, inverted: bool) { + self.regs.csr().modify(|_, w| w.pol().bit(inverted)); + } + /// Returns the value of the output of the comparator + /// + /// NOTE: This is taken before any potential inversion pub fn output(&self) -> bool { self.regs.csr().read().value().bit_is_set() } From e368fd87b1e39ea3b13af9a64266c3ad0e75b3c9 Mon Sep 17 00:00:00 2001 From: Albin Hedman Date: Sun, 6 Jul 2025 21:45:04 +0200 Subject: [PATCH 2/2] Use Polarity enum --- src/comparator.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/comparator.rs b/src/comparator.rs index 77356a9d..b09f0fa6 100644 --- a/src/comparator.rs +++ b/src/comparator.rs @@ -122,12 +122,13 @@ impl Config { /// The following observers are NOT affected by this setting /// * HRTIM /// * Software using [Comparator::output] - pub fn output_polarity(mut self, inverted: bool) -> Self { - self.inverted = inverted; + pub fn output_polarity(mut self, polarity: Polarity) -> Self { + self.inverted = matches!(polarity, Polarity::Inverted); self } } +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Copy, Clone, Eq, PartialEq)] pub enum Hysteresis { None = 0b000, @@ -140,6 +141,13 @@ pub enum Hysteresis { H70mV = 0b111, } +#[cfg_attr(feature = "defmt", derive(defmt::Format))] +#[derive(Debug, Copy, Clone)] +pub enum Polarity { + Normal, + Inverted, +} + /// Comparator positive input pub trait PositiveInput { fn setup(s: impl stasis::EntitlementLock, comp: &mut C); @@ -487,13 +495,14 @@ macro_rules! impl_comparator { /// The following observers are NOT affected by this setting /// * HRTIM /// * Software using [Comparator::output] - pub fn set_inverted(&mut self, inverted: bool) { + pub fn set_polarity(&mut self, polarity: Polarity) { + let inverted = matches!(polarity, Polarity::Inverted); self.regs.csr().modify(|_, w| w.pol().bit(inverted)); } /// Returns the value of the output of the comparator /// - /// NOTE: This is taken before any potential inversion + /// NOTE: This is taken before any potential inversion set by [Comparator::set_polarity] or similar pub fn output(&self) -> bool { self.regs.csr().read().value().bit_is_set() }