From b4090858854bbae6999e6c54ce80de2af28475f7 Mon Sep 17 00:00:00 2001 From: Brian Cully Date: Wed, 11 Sep 2019 12:37:26 -0400 Subject: Minor refactor. Remove let bindings and use `Option.map` instead. --- src/lib.rs | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs index 7df6394..423cb84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -121,27 +121,44 @@ pub struct SAMDHost<'a, F> { devices: DeviceTable, - // need sof 1kHz pad? - _sof_pad: gpio::Pa23, _dm_pad: gpio::Pa24, _dp_pad: gpio::Pa25, + _sof_pad: Option>, host_enable_pin: Option>>, // To get current milliseconds. millis: &'a F, } +pub struct Pins { + dm_pin: gpio::Pa24>, + dp_pin: gpio::Pa25>, + sof_pin: Option>>, + host_enable_pin: Option>>, +} +impl Pins { + pub fn new( + dm_pin: gpio::Pa24>, + dp_pin: gpio::Pa25>, + sof_pin: Option>>, + host_enable_pin: Option>>, + ) -> Self { + Self { + dm_pin, + dp_pin, + sof_pin, + host_enable_pin, + } + } +} + impl<'a, F> SAMDHost<'a, F> where F: Fn() -> usize, { - #[allow(clippy::too_many_arguments)] pub fn new( usb: USB, - sof_pin: gpio::Pa23>, - dm_pin: gpio::Pa24>, - dp_pin: gpio::Pa25>, - host_enable_pin: Option>>, + pins: Pins, port: &mut gpio::Port, clocks: &mut GenericClockController, pm: &mut PM, @@ -149,7 +166,7 @@ where ) -> (Self, impl FnMut()) { let (eventr, mut eventw) = unsafe { EVENTS.split() }; - let mut rc = Self { + let rc = Self { usb, events: eventr, @@ -159,18 +176,14 @@ where devices: DeviceTable::new(), - _sof_pad: sof_pin.into_function_g(port), - _dm_pad: dm_pin.into_function_g(port), - _dp_pad: dp_pin.into_function_g(port), - host_enable_pin: None, + _dm_pad: pins.dm_pin.into_function_g(port), + _dp_pad: pins.dp_pin.into_function_g(port), + _sof_pad: pins.sof_pin.map(|p| p.into_function_g(port)), + host_enable_pin: pins.host_enable_pin.map(|p| p.into_open_drain_output(port)), millis, }; - if let Some(he_pin) = host_enable_pin { - rc.host_enable_pin = Some(he_pin.into_open_drain_output(port)); - } - pm.apbbmask.modify(|_, w| w.usb_().set_bit()); // Set up USB clock from 48MHz source on generic clock 6. -- cgit v1.2.3