From 424f6ffadbcb7c1f7df995ab98bcafebc941afc2 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 21 Jul 2022 16:42:46 +0200 Subject: [PATCH 1/2] nrf/saadc: add type-erased AnyInput. --- embassy-nrf/src/saadc.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 7d39e33f8..af1aa8812 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs @@ -119,6 +119,25 @@ impl sealed::Input for VddhDiv5Input { #[cfg(any(feature = "_nrf5340-app", feature = "nrf52833", feature = "nrf52840"))] impl Input for VddhDiv5Input {} +pub struct AnyInput { + channel: InputChannel, +} + +unsafe impl Unborrow for AnyInput { + type Target = AnyInput; + unsafe fn unborrow(self) -> Self::Target { + self + } +} + +impl sealed::Input for AnyInput { + fn channel(&self) -> InputChannel { + self.channel + } +} + +impl Input for AnyInput {} + impl<'d> ChannelConfig<'d> { /// Default configuration for single ended channel sampling. pub fn single_ended(input: impl Unborrow + 'd) -> Self { @@ -670,7 +689,13 @@ pub(crate) mod sealed { } /// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal. -pub trait Input: sealed::Input + Unborrow {} +pub trait Input: sealed::Input + Unborrow + Sized { + fn degrade_saadc(self) -> AnyInput { + AnyInput { + channel: self.channel(), + } + } +} macro_rules! impl_saadc_input { ($pin:ident, $ch:ident) => { From c40d5f6e6ff81fb00507e384ef456d4390107e5e Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 21 Jul 2022 19:47:09 +0200 Subject: [PATCH 2/2] nrf/usb: prevent user code from constructing a PowerUsb directly. PowerUsb must be constructed through `new()` so that it sets up the IRQ. It must have at least one private field, otherwise user code can construct it directly with `PowerUsb{}`. --- embassy-nrf/src/usb.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/embassy-nrf/src/usb.rs b/embassy-nrf/src/usb.rs index 9b6e6e83d..a039427f1 100644 --- a/embassy-nrf/src/usb.rs +++ b/embassy-nrf/src/usb.rs @@ -47,7 +47,9 @@ pub struct Driver<'d, T: Instance, P: UsbSupply> { /// Uses the POWER peripheral to detect when power is available /// for USB. Unsuitable for usage with the nRF softdevice. #[cfg(not(feature = "_nrf5340-app"))] -pub struct PowerUsb {} +pub struct PowerUsb { + _private: (), +} /// Can be used to signal that power is available. Particularly suited for /// use with the nRF softdevice. @@ -70,7 +72,7 @@ impl PowerUsb { regs.intenset .write(|w| w.usbdetected().set().usbremoved().set().usbpwrrdy().set()); - Self {} + Self { _private: () } } #[cfg(not(feature = "_nrf5340-app"))]