Skip to content

Commit cc99237

Browse files
committed
CompOutput
1 parent 84f63b5 commit cc99237

File tree

5 files changed

+76
-196
lines changed

5 files changed

+76
-196
lines changed

examples/comp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rt::entry;
1616
#[entry]
1717
fn main() -> ! {
1818
use hal::comparator::{refint_input, ComparatorExt, ComparatorSplit, Config, Hysteresis};
19-
use hal::gpio::GpioExt;
19+
use hal::gpio::{GpioExt, PushPull};
2020
use hal::rcc::RccExt;
2121
use hal::stm32;
2222
use stm32g4xx_hal as hal;
@@ -45,10 +45,10 @@ fn main() -> ! {
4545
.output_inverted(),
4646
&rcc.clocks,
4747
);
48-
let led2 = gpioa.pa12.into_push_pull_output();
48+
let led2 = gpioa.pa12;
4949
// Configure PA12 to the comparator's alternate function so it gets
5050
// changed directly by the comparator.
51-
comp2.output_pin(led2);
51+
comp2.output_pin::<PushPull>(led2);
5252
let _comp2 = comp2.enable().lock();
5353

5454
loop {

examples/comp_w_dac.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn main() -> ! {
1313
use hal::comparator::{self, ComparatorExt, ComparatorSplit};
1414
use hal::dac::{Dac1IntSig1, DacExt, DacOut};
1515
use hal::delay::SYSTDelayExt;
16-
use hal::gpio::GpioExt;
16+
use hal::gpio::{GpioExt, PushPull};
1717
use hal::rcc::RccExt;
1818
use hal::stasis::Freeze;
1919
use hal::stm32;
@@ -44,10 +44,10 @@ fn main() -> ! {
4444
&rcc.clocks,
4545
);
4646

47-
let led2 = gpioa.pa0.into_push_pull_output();
47+
let led2 = gpioa.pa0;
4848
// Configure PA12 to the comparator's alternate function so it gets
4949
// changed directly by the comparator.
50-
comp.output_pin(led2);
50+
comp.output_pin::<PushPull>(led2);
5151
let _comp1 = comp.enable().lock();
5252

5353
enum Direction {

src/comparator.rs

Lines changed: 33 additions & 183 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::marker::PhantomData;
99

1010
use crate::dac;
1111
use crate::exti::{Event as ExtiEvent, ExtiExt};
12-
use crate::gpio::{self, Analog, OpenDrain, Output, PushPull, SignalEdge};
12+
use crate::gpio::{self, alt::CompOutput, Analog, SignalEdge};
1313

1414
use crate::rcc::{Clocks, Rcc};
1515
use crate::stasis;
@@ -157,28 +157,13 @@ positive_input_pin!(COMP2, PA7, PA3);
157157
positive_input_pin!(COMP3, PA0, PC1);
158158
positive_input_pin!(COMP4, PB0, PE7);
159159

160-
#[cfg(any(
161-
feature = "stm32g473",
162-
feature = "stm32g483",
163-
feature = "stm32g474",
164-
feature = "stm32g484"
165-
))]
160+
#[cfg(feature = "comp5")]
166161
positive_input_pin!(COMP5, PB13, PD12);
167162

168-
#[cfg(any(
169-
feature = "stm32g473",
170-
feature = "stm32g483",
171-
feature = "stm32g474",
172-
feature = "stm32g484"
173-
))]
163+
#[cfg(feature = "comp6")]
174164
positive_input_pin!(COMP6, PB11, PD11);
175165

176-
#[cfg(any(
177-
feature = "stm32g473",
178-
feature = "stm32g483",
179-
feature = "stm32g474",
180-
feature = "stm32g484"
181-
))]
166+
#[cfg(feature = "comp7")]
182167
positive_input_pin!(COMP7, PB14, PD14);
183168

184169
macro_rules! negative_input_pin_helper {
@@ -207,12 +192,7 @@ negative_input_pin! {
207192
COMP4: gpio::PE8<Analog>, gpio::PB2<Analog>,
208193
}
209194

210-
#[cfg(any(
211-
feature = "stm32g473",
212-
feature = "stm32g483",
213-
feature = "stm32g474",
214-
feature = "stm32g484"
215-
))]
195+
#[cfg(feature = "comp7")]
216196
negative_input_pin! {
217197
COMP5: gpio::PB10<Analog>, gpio::PD13<Analog>,
218198
COMP6: gpio::PD10<Analog>, gpio::PB15<Analog>,
@@ -273,12 +253,7 @@ macro_rules! refint_input {
273253

274254
refint_input!(COMP1, COMP2, COMP3, COMP4,);
275255

276-
#[cfg(any(
277-
feature = "stm32g473",
278-
feature = "stm32g483",
279-
feature = "stm32g474",
280-
feature = "stm32g484"
281-
))]
256+
#[cfg(feature = "comp7")]
282257
refint_input!(COMP5, COMP6, COMP7,);
283258

284259
macro_rules! dac_input_helper {
@@ -312,49 +287,19 @@ dac_input!(COMP3: Dac1Ch1, 0b101);
312287
dac_input!(COMP4: Dac3Ch2, 0b100);
313288
dac_input!(COMP4: Dac1Ch1, 0b101);
314289

315-
#[cfg(any(
316-
feature = "stm32g473",
317-
feature = "stm32g483",
318-
feature = "stm32g474",
319-
feature = "stm32g484"
320-
))]
290+
#[cfg(feature = "comp5")]
321291
dac_input!(COMP5: Dac4Ch1, 0b100);
322-
#[cfg(any(
323-
feature = "stm32g473",
324-
feature = "stm32g483",
325-
feature = "stm32g474",
326-
feature = "stm32g484"
327-
))]
292+
#[cfg(feature = "comp5")]
328293
dac_input!(COMP5: Dac1Ch2, 0b101);
329294

330-
#[cfg(any(
331-
feature = "stm32g473",
332-
feature = "stm32g483",
333-
feature = "stm32g474",
334-
feature = "stm32g484"
335-
))]
295+
#[cfg(feature = "comp6")]
336296
dac_input!(COMP6: Dac4Ch2, 0b100);
337-
#[cfg(any(
338-
feature = "stm32g473",
339-
feature = "stm32g483",
340-
feature = "stm32g474",
341-
feature = "stm32g484"
342-
))]
297+
#[cfg(feature = "comp6")]
343298
dac_input!(COMP6: Dac2Ch1, 0b101);
344299

345-
#[cfg(any(
346-
feature = "stm32g473",
347-
feature = "stm32g483",
348-
feature = "stm32g474",
349-
feature = "stm32g484"
350-
))]
300+
#[cfg(feature = "comp7")]
351301
dac_input!(COMP7: Dac4Ch1, 0b100);
352-
#[cfg(any(
353-
feature = "stm32g473",
354-
feature = "stm32g483",
355-
feature = "stm32g474",
356-
feature = "stm32g484"
357-
))]
302+
#[cfg(feature = "comp7")]
358303
dac_input!(COMP7: Dac2Ch1, 0b101);
359304

360305
pub struct Comparator<C, ED> {
@@ -400,14 +345,10 @@ macro_rules! impl_comparator {
400345
let voltage_scaler_delay = clocks.sys_clk.raw() / (1_000_000 / 200); // 200us
401346
cortex_m::asm::delay(voltage_scaler_delay);
402347
self.csr().modify(|_, w| unsafe {
403-
w.hyst()
404-
.bits(config.hysteresis as u8)
405-
.scalen()
406-
.bit(NP::USE_VREFINT)
407-
.brgen()
408-
.bit(NP::USE_RESISTOR_DIVIDER)
409-
.pol()
410-
.bit(config.inverted)
348+
w.hyst().bits(config.hysteresis as u8);
349+
w.scalen().bit(NP::USE_VREFINT);
350+
w.brgen().bit(NP::USE_RESISTOR_DIVIDER);
351+
w.pol().bit(config.inverted)
411352
});
412353

413354
Comparator {
@@ -492,60 +433,37 @@ macro_rules! impl_comparator {
492433
pub fn unpend(&self, exti: &EXTI) {
493434
exti.unpend($Event);
494435
}
495-
496-
/// Configures a GPIO pin to output the signal of the comparator
497-
///
498-
/// Multiple GPIO pins may be configured as the output simultaneously.
499-
pub fn output_pin<P: OutputPin<$COMP>>(&self, pin: P) {
500-
pin.setup();
501-
}
502436
}
503437
};
504438
}
505439

440+
impl<COMP: CompOutput, ED> Comparator<COMP, ED> {
441+
/// Configures a GPIO pin to output the signal of the comparator
442+
///
443+
/// Multiple GPIO pins may be configured as the output simultaneously.
444+
pub fn output_pin<Otype>(&self, pin: impl Into<COMP::Out<Otype>>) {
445+
let _pin = pin.into();
446+
}
447+
}
448+
506449
impl_comparator!(COMP1, comp1, ExtiEvent::COMP1);
507450
impl_comparator!(COMP2, comp2, ExtiEvent::COMP2);
508451
impl_comparator!(COMP3, comp1, ExtiEvent::COMP3);
509452
impl_comparator!(COMP4, comp2, ExtiEvent::COMP4);
510453

511-
#[cfg(any(
512-
feature = "stm32g473",
513-
feature = "stm32g483",
514-
feature = "stm32g474",
515-
feature = "stm32g484"
516-
))]
454+
#[cfg(feature = "comp5")]
517455
impl_comparator!(COMP5, comp1, ExtiEvent::COMP5);
518456

519-
#[cfg(any(
520-
feature = "stm32g473",
521-
feature = "stm32g483",
522-
feature = "stm32g474",
523-
feature = "stm32g484"
524-
))]
457+
#[cfg(feature = "comp6")]
525458
impl_comparator!(COMP6, comp2, ExtiEvent::COMP6);
526459

527-
#[cfg(any(
528-
feature = "stm32g473",
529-
feature = "stm32g483",
530-
feature = "stm32g474",
531-
feature = "stm32g484"
532-
))]
460+
#[cfg(feature = "comp7")]
533461
impl_comparator!(COMP7, comp2, ExtiEvent::COMP7);
534462

535-
#[cfg(not(any(
536-
feature = "stm32g473",
537-
feature = "stm32g483",
538-
feature = "stm32g474",
539-
feature = "stm32g484"
540-
)))]
463+
#[cfg(not(feature = "comp7"))]
541464
type Comparators = (COMP1, COMP2, COMP3, COMP4);
542465

543-
#[cfg(any(
544-
feature = "stm32g473",
545-
feature = "stm32g483",
546-
feature = "stm32g474",
547-
feature = "stm32g484"
548-
))]
466+
#[cfg(feature = "comp7")]
549467
type Comparators = (COMP1, COMP2, COMP3, COMP4, COMP5, COMP6, COMP7);
550468

551469
/// Enables the comparator peripheral, and splits the [`COMP`] into independent [`COMP1`] and [`COMP2`]
@@ -562,26 +480,11 @@ pub fn split(_comp: COMP, rcc: &mut Rcc) -> Comparators {
562480
COMP2 { _rb: PhantomData },
563481
COMP3 { _rb: PhantomData },
564482
COMP4 { _rb: PhantomData },
565-
#[cfg(any(
566-
feature = "stm32g473",
567-
feature = "stm32g483",
568-
feature = "stm32g474",
569-
feature = "stm32g484"
570-
))]
483+
#[cfg(feature = "comp5")]
571484
COMP5 { _rb: PhantomData },
572-
#[cfg(any(
573-
feature = "stm32g473",
574-
feature = "stm32g483",
575-
feature = "stm32g474",
576-
feature = "stm32g484"
577-
))]
485+
#[cfg(feature = "comp6")]
578486
COMP6 { _rb: PhantomData },
579-
#[cfg(any(
580-
feature = "stm32g473",
581-
feature = "stm32g483",
582-
feature = "stm32g474",
583-
feature = "stm32g484"
584-
))]
487+
#[cfg(feature = "comp7")]
585488
COMP7 { _rb: PhantomData },
586489
)
587490
}
@@ -596,56 +499,3 @@ impl ComparatorSplit for COMP {
596499
split(self, rcc)
597500
}
598501
}
599-
600-
pub trait OutputPin<COMP> {
601-
fn setup(self);
602-
}
603-
604-
#[allow(unused_macros)] // TODO: add support for more devices
605-
macro_rules! output_pin {
606-
($COMP:ident, $pin:ident, $AF:literal, $mode_t:ident, $into:ident) => {
607-
impl OutputPin<$COMP> for gpio::$pin<Output<$mode_t>> {
608-
fn setup(self) {
609-
self.$into::<$AF>();
610-
}
611-
}
612-
};
613-
($($COMP:ident: $pin:ident, $AF:literal,)+) => {$(
614-
output_pin!($COMP, $pin, $AF, PushPull, into_alternate);
615-
output_pin!($COMP, $pin, $AF, OpenDrain, into_alternate_open_drain);
616-
)+};
617-
}
618-
619-
output_pin! {
620-
COMP1: PA0, 8,
621-
COMP1: PA6, 8,
622-
COMP1: PA11, 8,
623-
COMP1: PB8, 8,
624-
625-
COMP2: PA2, 8,
626-
COMP2: PA7, 8,
627-
COMP2: PA12, 8,
628-
COMP2: PB9, 8,
629-
630-
COMP3: PB7, 8,
631-
COMP3: PB15, 3,
632-
COMP3: PC2, 3,
633-
634-
COMP4: PB1, 8,
635-
COMP4: PB6, 8,
636-
COMP4: PB14, 8,
637-
}
638-
639-
#[cfg(feature = "gpio-g47x")]
640-
output_pin! {
641-
COMP1: PF4, 2,
642-
643-
COMP5: PA9, 8,
644-
COMP5: PC7, 7,
645-
646-
COMP6: PA10, 8,
647-
COMP6: PC6, 7,
648-
649-
COMP7: PA8, 8,
650-
COMP7: PC8, 7,
651-
}

src/gpio/alt.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ pub trait CanCommon {
295295
type Tx;
296296
}
297297

298+
/// Comparator pins
299+
pub trait CompOutput {
300+
/// Output pin
301+
type Out<Otype>;
302+
}
303+
298304
// Serial pins
299305
pub trait SerialAsync {
300306
/// Receive

0 commit comments

Comments
 (0)