Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions src/comparator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,42 @@ 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
}

pub fn output_polarity(mut self, inverted: bool) -> Self {
self.inverted = inverted;
/// 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, 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,
Expand All @@ -116,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<C> {
fn setup(s: impl stasis::EntitlementLock<Resource = Self>, comp: &mut C);
Expand Down Expand Up @@ -451,7 +483,26 @@ macro_rules! impl_comparator {
}

impl<ED: EnabledState> 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_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 set by [Comparator::set_polarity] or similar
pub fn output(&self) -> bool {
self.regs.csr().read().value().bit_is_set()
}
Expand Down
Loading