diff options
| author | huntc <[email protected]> | 2021-10-11 09:38:35 +1100 |
|---|---|---|
| committer | huntc <[email protected]> | 2021-10-11 09:38:35 +1100 |
| commit | 8c9e50b37871368c31ed23208329fbe28bcc0a7e (patch) | |
| tree | 7c292f298c113b5663a6cf13ab72eab535662692 | |
| parent | b043778f750f60f1361bb56f4b57e8ad56a13dc3 (diff) | |
Conflates the negative and positive types as they are the same, and renames pin to input as they can be more than pins
| -rw-r--r-- | embassy-nrf/src/saadc.rs | 92 |
1 files changed, 26 insertions, 66 deletions
diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index b09b909d1..96116b36a 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs | |||
| @@ -15,8 +15,7 @@ use pac::{saadc, SAADC}; | |||
| 15 | pub use saadc::{ | 15 | pub use saadc::{ |
| 16 | ch::{ | 16 | ch::{ |
| 17 | config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time}, | 17 | config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time}, |
| 18 | pseln::PSELN_A as NegativeChannel, | 18 | pselp::PSELP_A as InputChannel, // We treat the positive and negative channels with the same enum values to keep our type tidy and given they are the same |
| 19 | pselp::PSELP_A as PositiveChannel, | ||
| 20 | }, | 19 | }, |
| 21 | oversample::OVERSAMPLE_A as Oversample, | 20 | oversample::OVERSAMPLE_A as Oversample, |
| 22 | resolution::VAL_A as Resolution, | 21 | resolution::VAL_A as Resolution, |
| @@ -69,40 +68,40 @@ pub struct ChannelConfig<'d> { | |||
| 69 | /// Acquisition time in microseconds. | 68 | /// Acquisition time in microseconds. |
| 70 | pub time: Time, | 69 | pub time: Time, |
| 71 | /// Positive channel to sample | 70 | /// Positive channel to sample |
| 72 | p_channel: PositiveChannel, | 71 | p_channel: InputChannel, |
| 73 | /// An optional negative channel to sample | 72 | /// An optional negative channel to sample |
| 74 | n_channel: Option<NegativeChannel>, | 73 | n_channel: Option<InputChannel>, |
| 75 | 74 | ||
| 76 | phantom: PhantomData<&'d ()>, | 75 | phantom: PhantomData<&'d ()>, |
| 77 | } | 76 | } |
| 78 | 77 | ||
| 79 | impl<'d> ChannelConfig<'d> { | 78 | impl<'d> ChannelConfig<'d> { |
| 80 | /// Default configuration for single ended channel sampling. | 79 | /// Default configuration for single ended channel sampling. |
| 81 | pub fn single_ended(pin: impl Unborrow<Target = impl PositivePin> + 'd) -> Self { | 80 | pub fn single_ended(input: impl Unborrow<Target = impl Input> + 'd) -> Self { |
| 82 | unborrow!(pin); | 81 | unborrow!(input); |
| 83 | Self { | 82 | Self { |
| 84 | reference: Reference::INTERNAL, | 83 | reference: Reference::INTERNAL, |
| 85 | gain: Gain::GAIN1_6, | 84 | gain: Gain::GAIN1_6, |
| 86 | resistor: Resistor::BYPASS, | 85 | resistor: Resistor::BYPASS, |
| 87 | time: Time::_10US, | 86 | time: Time::_10US, |
| 88 | p_channel: pin.channel(), | 87 | p_channel: input.channel(), |
| 89 | n_channel: None, | 88 | n_channel: None, |
| 90 | phantom: PhantomData, | 89 | phantom: PhantomData, |
| 91 | } | 90 | } |
| 92 | } | 91 | } |
| 93 | /// Default configuration for differential channel sampling. | 92 | /// Default configuration for differential channel sampling. |
| 94 | pub fn differential( | 93 | pub fn differential( |
| 95 | ppin: impl Unborrow<Target = impl PositivePin> + 'd, | 94 | p_input: impl Unborrow<Target = impl Input> + 'd, |
| 96 | npin: impl Unborrow<Target = impl NegativePin> + 'd, | 95 | n_input: impl Unborrow<Target = impl Input> + 'd, |
| 97 | ) -> Self { | 96 | ) -> Self { |
| 98 | unborrow!(ppin, npin); | 97 | unborrow!(p_input, n_input); |
| 99 | Self { | 98 | Self { |
| 100 | reference: Reference::VDD1_4, | 99 | reference: Reference::VDD1_4, |
| 101 | gain: Gain::GAIN1_6, | 100 | gain: Gain::GAIN1_6, |
| 102 | resistor: Resistor::BYPASS, | 101 | resistor: Resistor::BYPASS, |
| 103 | time: Time::_10US, | 102 | time: Time::_10US, |
| 104 | p_channel: ppin.channel(), | 103 | p_channel: p_input.channel(), |
| 105 | n_channel: Some(npin.channel()), | 104 | n_channel: Some(n_input.channel()), |
| 106 | phantom: PhantomData, | 105 | phantom: PhantomData, |
| 107 | } | 106 | } |
| 108 | } | 107 | } |
| @@ -131,8 +130,10 @@ impl<'d, const N: usize> OneShot<'d, N> { | |||
| 131 | 130 | ||
| 132 | for (i, cc) in channel_configs.iter().enumerate() { | 131 | for (i, cc) in channel_configs.iter().enumerate() { |
| 133 | r.ch[i].pselp.write(|w| w.pselp().variant(cc.p_channel)); | 132 | r.ch[i].pselp.write(|w| w.pselp().variant(cc.p_channel)); |
| 134 | if let Some(npin) = cc.n_channel.as_ref() { | 133 | if let Some(n_channel) = cc.n_channel { |
| 135 | r.ch[i].pseln.write(|w| w.pseln().variant(*npin)); | 134 | r.ch[i] |
| 135 | .pseln | ||
| 136 | .write(|w| unsafe { w.pseln().bits(n_channel as u8) }); | ||
| 136 | } | 137 | } |
| 137 | r.ch[i].config.write(|w| { | 138 | r.ch[i].config.write(|w| { |
| 138 | w.refsel().variant(cc.reference); | 139 | w.refsel().variant(cc.reference); |
| @@ -225,34 +226,17 @@ impl<'d, const N: usize> Drop for OneShot<'d, N> { | |||
| 225 | } | 226 | } |
| 226 | } | 227 | } |
| 227 | 228 | ||
| 228 | /// A pin that can be used as the positive end of a ADC differential in the SAADC periperhal. | 229 | /// An input that can be used as either or negative end of a ADC differential in the SAADC periperhal. |
| 229 | pub trait PositivePin { | 230 | pub trait Input { |
| 230 | fn channel(&self) -> PositiveChannel; | 231 | fn channel(&self) -> InputChannel; |
| 231 | } | 232 | } |
| 232 | 233 | ||
| 233 | macro_rules! positive_pin_mappings { | 234 | macro_rules! input_mappings { |
| 234 | ( $($ch:ident => $pin:ident,)*) => { | 235 | ( $($ch:ident => $input:ident,)*) => { |
| 235 | $( | 236 | $( |
| 236 | impl PositivePin for crate::peripherals::$pin { | 237 | impl Input for crate::peripherals::$input { |
| 237 | fn channel(&self) -> PositiveChannel { | 238 | fn channel(&self) -> InputChannel { |
| 238 | PositiveChannel::$ch | 239 | InputChannel::$ch |
| 239 | } | ||
| 240 | } | ||
| 241 | )* | ||
| 242 | }; | ||
| 243 | } | ||
| 244 | |||
| 245 | /// A pin that can be used as the negative end of a ADC differential in the SAADC periperhal. | ||
| 246 | pub trait NegativePin { | ||
| 247 | fn channel(&self) -> NegativeChannel; | ||
| 248 | } | ||
| 249 | |||
| 250 | macro_rules! negative_pin_mappings { | ||
| 251 | ( $($ch:ident => $pin:ident,)*) => { | ||
| 252 | $( | ||
| 253 | impl NegativePin for crate::peripherals::$pin { | ||
| 254 | fn channel(&self) -> NegativeChannel { | ||
| 255 | NegativeChannel::$ch | ||
| 256 | } | 240 | } |
| 257 | } | 241 | } |
| 258 | )* | 242 | )* |
| @@ -260,33 +244,9 @@ macro_rules! negative_pin_mappings { | |||
| 260 | } | 244 | } |
| 261 | 245 | ||
| 262 | // TODO the variant names are unchecked | 246 | // TODO the variant names are unchecked |
| 263 | // the pins are copied from nrf hal | 247 | // the inputs are copied from nrf hal |
| 264 | #[cfg(feature = "9160")] | ||
| 265 | positive_pin_mappings! { | ||
| 266 | ANALOGINPUT0 => P0_13, | ||
| 267 | ANALOGINPUT1 => P0_14, | ||
| 268 | ANALOGINPUT2 => P0_15, | ||
| 269 | ANALOGINPUT3 => P0_16, | ||
| 270 | ANALOGINPUT4 => P0_17, | ||
| 271 | ANALOGINPUT5 => P0_18, | ||
| 272 | ANALOGINPUT6 => P0_19, | ||
| 273 | ANALOGINPUT7 => P0_20, | ||
| 274 | } | ||
| 275 | |||
| 276 | #[cfg(not(feature = "9160"))] | ||
| 277 | positive_pin_mappings! { | ||
| 278 | ANALOGINPUT0 => P0_02, | ||
| 279 | ANALOGINPUT1 => P0_03, | ||
| 280 | ANALOGINPUT2 => P0_04, | ||
| 281 | ANALOGINPUT3 => P0_05, | ||
| 282 | ANALOGINPUT4 => P0_28, | ||
| 283 | ANALOGINPUT5 => P0_29, | ||
| 284 | ANALOGINPUT6 => P0_30, | ||
| 285 | ANALOGINPUT7 => P0_31, | ||
| 286 | } | ||
| 287 | |||
| 288 | #[cfg(feature = "9160")] | 248 | #[cfg(feature = "9160")] |
| 289 | negative_pin_mappings! { | 249 | input_mappings! { |
| 290 | ANALOGINPUT0 => P0_13, | 250 | ANALOGINPUT0 => P0_13, |
| 291 | ANALOGINPUT1 => P0_14, | 251 | ANALOGINPUT1 => P0_14, |
| 292 | ANALOGINPUT2 => P0_15, | 252 | ANALOGINPUT2 => P0_15, |
| @@ -298,7 +258,7 @@ negative_pin_mappings! { | |||
| 298 | } | 258 | } |
| 299 | 259 | ||
| 300 | #[cfg(not(feature = "9160"))] | 260 | #[cfg(not(feature = "9160"))] |
| 301 | negative_pin_mappings! { | 261 | input_mappings! { |
| 302 | ANALOGINPUT0 => P0_02, | 262 | ANALOGINPUT0 => P0_02, |
| 303 | ANALOGINPUT1 => P0_03, | 263 | ANALOGINPUT1 => P0_03, |
| 304 | ANALOGINPUT2 => P0_04, | 264 | ANALOGINPUT2 => P0_04, |
