diff options
| author | Dario Nieuwenhuis <[email protected]> | 2025-03-26 16:01:37 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2025-03-27 15:18:06 +0100 |
| commit | d41eeeae79388f219bf6a84e2f7bde9f6b532516 (patch) | |
| tree | 678b6fc732216e529dc38e6f65b72a309917ac32 /embassy-nrf/src/saadc.rs | |
| parent | 9edf5b7f049f95742b60b041e4443967d8a6b708 (diff) | |
Remove Peripheral trait, rename PeripheralRef->Peri.
Diffstat (limited to 'embassy-nrf/src/saadc.rs')
| -rw-r--r-- | embassy-nrf/src/saadc.rs | 73 |
1 files changed, 39 insertions, 34 deletions
diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 00e2b7402..92b6fb01f 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs | |||
| @@ -3,11 +3,12 @@ | |||
| 3 | #![macro_use] | 3 | #![macro_use] |
| 4 | 4 | ||
| 5 | use core::future::poll_fn; | 5 | use core::future::poll_fn; |
| 6 | use core::marker::PhantomData; | ||
| 6 | use core::sync::atomic::{compiler_fence, Ordering}; | 7 | use core::sync::atomic::{compiler_fence, Ordering}; |
| 7 | use core::task::Poll; | 8 | use core::task::Poll; |
| 8 | 9 | ||
| 9 | use embassy_hal_internal::drop::OnDrop; | 10 | use embassy_hal_internal::drop::OnDrop; |
| 10 | use embassy_hal_internal::{impl_peripheral, into_ref, PeripheralRef}; | 11 | use embassy_hal_internal::{impl_peripheral, Peri}; |
| 11 | use embassy_sync::waitqueue::AtomicWaker; | 12 | use embassy_sync::waitqueue::AtomicWaker; |
| 12 | pub(crate) use vals::Psel as InputChannel; | 13 | pub(crate) use vals::Psel as InputChannel; |
| 13 | 14 | ||
| @@ -15,7 +16,7 @@ use crate::interrupt::InterruptExt; | |||
| 15 | use crate::pac::saadc::vals; | 16 | use crate::pac::saadc::vals; |
| 16 | use crate::ppi::{ConfigurableChannel, Event, Ppi, Task}; | 17 | use crate::ppi::{ConfigurableChannel, Event, Ppi, Task}; |
| 17 | use crate::timer::{Frequency, Instance as TimerInstance, Timer}; | 18 | use crate::timer::{Frequency, Instance as TimerInstance, Timer}; |
| 18 | use crate::{interrupt, pac, peripherals, Peripheral}; | 19 | use crate::{interrupt, pac, peripherals}; |
| 19 | 20 | ||
| 20 | /// SAADC error | 21 | /// SAADC error |
| 21 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 22 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -87,37 +88,32 @@ pub struct ChannelConfig<'d> { | |||
| 87 | /// Acquisition time in microseconds. | 88 | /// Acquisition time in microseconds. |
| 88 | pub time: Time, | 89 | pub time: Time, |
| 89 | /// Positive channel to sample | 90 | /// Positive channel to sample |
| 90 | p_channel: PeripheralRef<'d, AnyInput>, | 91 | p_channel: AnyInput<'d>, |
| 91 | /// An optional negative channel to sample | 92 | /// An optional negative channel to sample |
| 92 | n_channel: Option<PeripheralRef<'d, AnyInput>>, | 93 | n_channel: Option<AnyInput<'d>>, |
| 93 | } | 94 | } |
| 94 | 95 | ||
| 95 | impl<'d> ChannelConfig<'d> { | 96 | impl<'d> ChannelConfig<'d> { |
| 96 | /// Default configuration for single ended channel sampling. | 97 | /// Default configuration for single ended channel sampling. |
| 97 | pub fn single_ended(input: impl Peripheral<P = impl Input> + 'd) -> Self { | 98 | pub fn single_ended(input: impl Input + 'd) -> Self { |
| 98 | into_ref!(input); | ||
| 99 | Self { | 99 | Self { |
| 100 | reference: Reference::INTERNAL, | 100 | reference: Reference::INTERNAL, |
| 101 | gain: Gain::GAIN1_6, | 101 | gain: Gain::GAIN1_6, |
| 102 | resistor: Resistor::BYPASS, | 102 | resistor: Resistor::BYPASS, |
| 103 | time: Time::_10US, | 103 | time: Time::_10US, |
| 104 | p_channel: input.map_into(), | 104 | p_channel: input.degrade_saadc(), |
| 105 | n_channel: None, | 105 | n_channel: None, |
| 106 | } | 106 | } |
| 107 | } | 107 | } |
| 108 | /// Default configuration for differential channel sampling. | 108 | /// Default configuration for differential channel sampling. |
| 109 | pub fn differential( | 109 | pub fn differential(p_input: impl Input + 'd, n_input: impl Input + 'd) -> Self { |
| 110 | p_input: impl Peripheral<P = impl Input> + 'd, | ||
| 111 | n_input: impl Peripheral<P = impl Input> + 'd, | ||
| 112 | ) -> Self { | ||
| 113 | into_ref!(p_input, n_input); | ||
| 114 | Self { | 110 | Self { |
| 115 | reference: Reference::INTERNAL, | 111 | reference: Reference::INTERNAL, |
| 116 | gain: Gain::GAIN1_6, | 112 | gain: Gain::GAIN1_6, |
| 117 | resistor: Resistor::BYPASS, | 113 | resistor: Resistor::BYPASS, |
| 118 | time: Time::_10US, | 114 | time: Time::_10US, |
| 119 | p_channel: p_input.map_into(), | 115 | p_channel: p_input.degrade_saadc(), |
| 120 | n_channel: Some(n_input.map_into()), | 116 | n_channel: Some(n_input.degrade_saadc()), |
| 121 | } | 117 | } |
| 122 | } | 118 | } |
| 123 | } | 119 | } |
| @@ -133,19 +129,17 @@ pub enum CallbackResult { | |||
| 133 | 129 | ||
| 134 | /// One-shot and continuous SAADC. | 130 | /// One-shot and continuous SAADC. |
| 135 | pub struct Saadc<'d, const N: usize> { | 131 | pub struct Saadc<'d, const N: usize> { |
| 136 | _p: PeripheralRef<'d, peripherals::SAADC>, | 132 | _p: Peri<'d, peripherals::SAADC>, |
| 137 | } | 133 | } |
| 138 | 134 | ||
| 139 | impl<'d, const N: usize> Saadc<'d, N> { | 135 | impl<'d, const N: usize> Saadc<'d, N> { |
| 140 | /// Create a new SAADC driver. | 136 | /// Create a new SAADC driver. |
| 141 | pub fn new( | 137 | pub fn new( |
| 142 | saadc: impl Peripheral<P = peripherals::SAADC> + 'd, | 138 | saadc: Peri<'d, peripherals::SAADC>, |
| 143 | _irq: impl interrupt::typelevel::Binding<interrupt::typelevel::SAADC, InterruptHandler> + 'd, | 139 | _irq: impl interrupt::typelevel::Binding<interrupt::typelevel::SAADC, InterruptHandler> + 'd, |
| 144 | config: Config, | 140 | config: Config, |
| 145 | channel_configs: [ChannelConfig; N], | 141 | channel_configs: [ChannelConfig; N], |
| 146 | ) -> Self { | 142 | ) -> Self { |
| 147 | into_ref!(saadc); | ||
| 148 | |||
| 149 | let r = pac::SAADC; | 143 | let r = pac::SAADC; |
| 150 | 144 | ||
| 151 | let Config { resolution, oversample } = config; | 145 | let Config { resolution, oversample } = config; |
| @@ -284,9 +278,9 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 284 | 278 | ||
| 285 | pub async fn run_task_sampler<F, T: TimerInstance, const N0: usize>( | 279 | pub async fn run_task_sampler<F, T: TimerInstance, const N0: usize>( |
| 286 | &mut self, | 280 | &mut self, |
| 287 | timer: &mut T, | 281 | timer: Peri<'_, T>, |
| 288 | ppi_ch1: &mut impl ConfigurableChannel, | 282 | ppi_ch1: Peri<'_, impl ConfigurableChannel>, |
| 289 | ppi_ch2: &mut impl ConfigurableChannel, | 283 | ppi_ch2: Peri<'_, impl ConfigurableChannel>, |
| 290 | frequency: Frequency, | 284 | frequency: Frequency, |
| 291 | sample_counter: u32, | 285 | sample_counter: u32, |
| 292 | bufs: &mut [[[i16; N]; N0]; 2], | 286 | bufs: &mut [[[i16; N]; N0]; 2], |
| @@ -655,14 +649,18 @@ pub(crate) trait SealedInput { | |||
| 655 | 649 | ||
| 656 | /// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal. | 650 | /// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal. |
| 657 | #[allow(private_bounds)] | 651 | #[allow(private_bounds)] |
| 658 | pub trait Input: SealedInput + Into<AnyInput> + Peripheral<P = Self> + Sized + 'static { | 652 | pub trait Input: SealedInput + Sized { |
| 659 | /// Convert this SAADC input to a type-erased `AnyInput`. | 653 | /// Convert this SAADC input to a type-erased `AnyInput`. |
| 660 | /// | 654 | /// |
| 661 | /// This allows using several inputs in situations that might require | 655 | /// This allows using several inputs in situations that might require |
| 662 | /// them to be the same type, like putting them in an array. | 656 | /// them to be the same type, like putting them in an array. |
| 663 | fn degrade_saadc(self) -> AnyInput { | 657 | fn degrade_saadc<'a>(self) -> AnyInput<'a> |
| 658 | where | ||
| 659 | Self: 'a, | ||
| 660 | { | ||
| 664 | AnyInput { | 661 | AnyInput { |
| 665 | channel: self.channel(), | 662 | channel: self.channel(), |
| 663 | _phantom: core::marker::PhantomData, | ||
| 666 | } | 664 | } |
| 667 | } | 665 | } |
| 668 | } | 666 | } |
| @@ -671,23 +669,36 @@ pub trait Input: SealedInput + Into<AnyInput> + Peripheral<P = Self> + Sized + ' | |||
| 671 | /// | 669 | /// |
| 672 | /// This allows using several inputs in situations that might require | 670 | /// This allows using several inputs in situations that might require |
| 673 | /// them to be the same type, like putting them in an array. | 671 | /// them to be the same type, like putting them in an array. |
| 674 | pub struct AnyInput { | 672 | pub struct AnyInput<'a> { |
| 675 | channel: InputChannel, | 673 | channel: InputChannel, |
| 674 | _phantom: PhantomData<&'a ()>, | ||
| 676 | } | 675 | } |
| 677 | 676 | ||
| 678 | impl_peripheral!(AnyInput); | 677 | impl<'a> AnyInput<'a> { |
| 678 | /// Reborrow into a "child" AnyInput. | ||
| 679 | /// | ||
| 680 | /// `self` will stay borrowed until the child AnyInput is dropped. | ||
| 681 | pub fn reborrow(&mut self) -> AnyInput<'_> { | ||
| 682 | // safety: we're returning the clone inside a new Peripheral that borrows | ||
| 683 | // self, so user code can't use both at the same time. | ||
| 684 | Self { | ||
| 685 | channel: self.channel, | ||
| 686 | _phantom: PhantomData, | ||
| 687 | } | ||
| 688 | } | ||
| 689 | } | ||
| 679 | 690 | ||
| 680 | impl SealedInput for AnyInput { | 691 | impl SealedInput for AnyInput<'_> { |
| 681 | fn channel(&self) -> InputChannel { | 692 | fn channel(&self) -> InputChannel { |
| 682 | self.channel | 693 | self.channel |
| 683 | } | 694 | } |
| 684 | } | 695 | } |
| 685 | 696 | ||
| 686 | impl Input for AnyInput {} | 697 | impl Input for AnyInput<'_> {} |
| 687 | 698 | ||
| 688 | macro_rules! impl_saadc_input { | 699 | macro_rules! impl_saadc_input { |
| 689 | ($pin:ident, $ch:ident) => { | 700 | ($pin:ident, $ch:ident) => { |
| 690 | impl_saadc_input!(@local, crate::peripherals::$pin, $ch); | 701 | impl_saadc_input!(@local, crate::Peri<'_, crate::peripherals::$pin>, $ch); |
| 691 | }; | 702 | }; |
| 692 | (@local, $pin:ty, $ch:ident) => { | 703 | (@local, $pin:ty, $ch:ident) => { |
| 693 | impl crate::saadc::SealedInput for $pin { | 704 | impl crate::saadc::SealedInput for $pin { |
| @@ -696,12 +707,6 @@ macro_rules! impl_saadc_input { | |||
| 696 | } | 707 | } |
| 697 | } | 708 | } |
| 698 | impl crate::saadc::Input for $pin {} | 709 | impl crate::saadc::Input for $pin {} |
| 699 | |||
| 700 | impl From<$pin> for crate::saadc::AnyInput { | ||
| 701 | fn from(val: $pin) -> Self { | ||
| 702 | crate::saadc::Input::degrade_saadc(val) | ||
| 703 | } | ||
| 704 | } | ||
| 705 | }; | 710 | }; |
| 706 | } | 711 | } |
| 707 | 712 | ||
