Skip to content

Commit a8f8835

Browse files
committed
Use array feature of bitfields
1 parent 9cb658e commit a8f8835

File tree

3 files changed

+23
-84
lines changed

3 files changed

+23
-84
lines changed

src/adc.rs

Lines changed: 20 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -689,23 +689,12 @@ where
689689
#[rustfmt::skip]
690690
pub fn channel_sequence(&self, sequence: config::Sequence) -> Option<channel::Id> {
691691
// Set the channel in the right sequence field
692-
match sequence {
693-
config::Sequence::One => self.reg.sqr1().read().sq1().bits().try_into().ok(),
694-
config::Sequence::Two => self.reg.sqr1().read().sq2().bits().try_into().ok(),
695-
config::Sequence::Three => self.reg.sqr1().read().sq3().bits().try_into().ok(),
696-
config::Sequence::Four => self.reg.sqr1().read().sq4().bits().try_into().ok(),
697-
config::Sequence::Five => self.reg.sqr2().read().sq5().bits().try_into().ok(),
698-
config::Sequence::Six => self.reg.sqr2().read().sq6().bits().try_into().ok(),
699-
config::Sequence::Seven => self.reg.sqr2().read().sq7().bits().try_into().ok(),
700-
config::Sequence::Eight => self.reg.sqr2().read().sq8().bits().try_into().ok(),
701-
config::Sequence::Nine => self.reg.sqr2().read().sq9().bits().try_into().ok(),
702-
config::Sequence::Ten => self.reg.sqr3().read().sq10().bits().try_into().ok(),
703-
config::Sequence::Eleven => self.reg.sqr3().read().sq11().bits().try_into().ok(),
704-
config::Sequence::Twelve => self.reg.sqr3().read().sq12().bits().try_into().ok(),
705-
config::Sequence::Thirteen => self.reg.sqr3().read().sq13().bits().try_into().ok(),
706-
config::Sequence::Fourteen => self.reg.sqr3().read().sq14().bits().try_into().ok(),
707-
config::Sequence::Fifteen => self.reg.sqr4().read().sq15().bits().try_into().ok(),
708-
config::Sequence::Sixteen => self.reg.sqr4().read().sq16().bits().try_into().ok(),
692+
let index = channel % 4;
693+
match channel {
694+
0..=3 => self.reg.sqr1().read().sq(index).bits().try_into().ok(),
695+
4..=7 => self.reg.sqr2().read().sq(index).bits().try_into().ok(),
696+
8..=11 => self.reg.sqr3().read().sq(index).bits().try_into().ok(),
697+
12.. => self.reg.sqr4().read().sq(index).bits().try_into().ok(),
709698
}
710699
}
711700

@@ -715,32 +704,11 @@ where
715704
where
716705
Pin: Channel<ADC, ID = channel::Id>,
717706
{
718-
let channel = Pin::channel();
719-
// Set the sample time for the channel
707+
let channel = Pin::channel() as u8;
708+
let index = channel % 10;
720709
match channel {
721-
#[cfg(feature = "gpio-f373")]
722-
channel::Id::Zero => self.adc.smpr1().read().smp0().variant().into(),
723-
channel::Id::One => self.reg.smpr1().read().smp1().variant().into(),
724-
channel::Id::Two => self.reg.smpr1().read().smp2().variant().into(),
725-
channel::Id::Three => self.reg.smpr1().read().smp3().variant().into(),
726-
channel::Id::Four => self.reg.smpr1().read().smp4().variant().into(),
727-
channel::Id::Five => self.reg.smpr1().read().smp5().variant().into(),
728-
channel::Id::Six => self.reg.smpr1().read().smp6().variant().into(),
729-
channel::Id::Seven => self.reg.smpr1().read().smp7().variant().into(),
730-
channel::Id::Eight => self.reg.smpr1().read().smp8().variant().into(),
731-
channel::Id::Nine => self.reg.smpr1().read().smp9().variant().into(),
732-
channel::Id::Ten => self.reg.smpr2().read().smp10().variant().into(),
733-
channel::Id::Eleven => self.reg.smpr2().read().smp11().variant().into(),
734-
channel::Id::Twelve => self.reg.smpr2().read().smp12().variant().into(),
735-
channel::Id::Thirteen => self.reg.smpr2().read().smp13().variant().into(),
736-
channel::Id::Fourteen => self.reg.smpr2().read().smp14().variant().into(),
737-
channel::Id::Fifteen => self.reg.smpr2().read().smp15().variant().into(),
738-
channel::Id::Sixteen => self.reg.smpr2().read().smp16().variant().into(),
739-
channel::Id::Seventeen => self.reg.smpr2().read().smp17().variant().into(),
740-
channel::Id::Eighteen => self.reg.smpr2().read().smp18().variant().into(),
741-
// #[cfg(not(feature = "gpio-f373"))]
742-
// // SAFETY: We know, that channel IDs will not exceed 18, see [`crate::adc::channel`]
743-
// _ => unsafe { unreachable_unchecked() },
710+
0..=9 => self.reg.smpr1().read().smp(index).variant().into(),
711+
10.. => self.reg.smpr2().read().smp(index).variant().into(),
744712
}
745713
}
746714

@@ -1031,23 +999,12 @@ where
1031999
// Set the channel in the right sequence field
10321000
// SAFETY: the channel.into() implementation ensures that those are valid values
10331001
unsafe {
1034-
match sequence {
1035-
config::Sequence::One => self.reg.sqr1().modify(|_, w| w.sq1().bits(channel.into())),
1036-
config::Sequence::Two => self.reg.sqr1().modify(|_, w| w.sq2().bits(channel.into())),
1037-
config::Sequence::Three => self.reg.sqr1().modify(|_, w| w.sq3().bits(channel.into())),
1038-
config::Sequence::Four => self.reg.sqr1().modify(|_, w| w.sq4().bits(channel.into())),
1039-
config::Sequence::Five => self.reg.sqr2().modify(|_, w| w.sq5().bits(channel.into())),
1040-
config::Sequence::Six => self.reg.sqr2().modify(|_, w| w.sq6().bits(channel.into())),
1041-
config::Sequence::Seven => self.reg.sqr2().modify(|_, w| w.sq7().bits(channel.into())),
1042-
config::Sequence::Eight => self.reg.sqr2().modify(|_, w| w.sq8().bits(channel.into())),
1043-
config::Sequence::Nine => self.reg.sqr2().modify(|_, w| w.sq9().bits(channel.into())),
1044-
config::Sequence::Ten => self.reg.sqr3().modify(|_, w| w.sq10().bits(channel.into())),
1045-
config::Sequence::Eleven => self.reg.sqr3().modify(|_, w| w.sq11().bits(channel.into())),
1046-
config::Sequence::Twelve => self.reg.sqr3().modify(|_, w| w.sq12().bits(channel.into())),
1047-
config::Sequence::Thirteen => self.reg.sqr3().modify(|_, w| w.sq13().bits(channel.into())),
1048-
config::Sequence::Fourteen => self.reg.sqr3().modify(|_, w| w.sq14().bits(channel.into())),
1049-
config::Sequence::Fifteen => self.reg.sqr4().modify(|_, w| w.sq15().bits(channel.into())),
1050-
config::Sequence::Sixteen => self.reg.sqr4().modify(|_, w| w.sq16().bits(channel.into())),
1002+
let index = channel % 4;
1003+
match channel {
1004+
0..=3 => self.reg.sqr1().modify(|_, w| w.sq(index).set(channel.into())),
1005+
4..=7 => self.reg.sqr2().modify(|_, w| w.sq(index).set(channel.into())),
1006+
8..=11 => self.reg.sqr3().modify(|_, w| w.sq(index).set(channel.into())),
1007+
12.. => self.reg.sqr4().modify(|_, w| w.sq(index).set(channel.into())),
10511008
};
10521009
}
10531010
}
@@ -1066,30 +1023,12 @@ where
10661023
Pin: Channel<ADC, ID = channel::Id>,
10671024
{
10681025
let channel = Pin::channel();
1026+
1027+
let index = channel % 10;
10691028
// Set the sample time for the channel
10701029
match channel {
1071-
#[cfg(feature = "gpio-f373")]
1072-
channel::Id::Zero => self.adc.smpr1().modify(|_, w| w.smp0().variant(sample_time.into())),
1073-
channel::Id::One => self.reg.smpr1().modify(|_, w| w.smp1().variant(sample_time.into())),
1074-
channel::Id::Two => self.reg.smpr1().modify(|_, w| w.smp2().variant(sample_time.into())),
1075-
channel::Id::Three => self.reg.smpr1().modify(|_, w| w.smp3().variant(sample_time.into())),
1076-
channel::Id::Four => self.reg.smpr1().modify(|_, w| w.smp4().variant(sample_time.into())),
1077-
channel::Id::Five => self.reg.smpr1().modify(|_, w| w.smp5().variant(sample_time.into())),
1078-
channel::Id::Six => self.reg.smpr1().modify(|_, w| w.smp6().variant(sample_time.into())),
1079-
channel::Id::Seven => self.reg.smpr1().modify(|_, w| w.smp7().variant(sample_time.into())),
1080-
channel::Id::Eight => self.reg.smpr1().modify(|_, w| w.smp8().variant(sample_time.into())),
1081-
channel::Id::Nine => self.reg.smpr1().modify(|_, w| w.smp9().variant(sample_time.into())),
1082-
channel::Id::Ten => self.reg.smpr2().modify(|_, w| w.smp10().variant(sample_time.into())),
1083-
channel::Id::Eleven => self.reg.smpr2().modify(|_, w| w.smp11().variant(sample_time.into())),
1084-
channel::Id::Twelve => self.reg.smpr2().modify(|_, w| w.smp12().variant(sample_time.into())),
1085-
channel::Id::Thirteen => self.reg.smpr2().modify(|_, w| w.smp13().variant(sample_time.into())),
1086-
channel::Id::Fourteen => self.reg.smpr2().modify(|_, w| w.smp14().variant(sample_time.into())),
1087-
channel::Id::Fifteen => self.reg.smpr2().modify(|_, w| w.smp15().variant(sample_time.into())),
1088-
channel::Id::Sixteen => self.reg.smpr2().modify(|_, w| w.smp16().variant(sample_time.into())),
1089-
channel::Id::Seventeen => self.reg.smpr2().modify(|_, w| w.smp17().variant(sample_time.into())),
1090-
channel::Id::Eighteen => self.reg.smpr2().modify(|_, w| w.smp18().variant(sample_time.into())),
1091-
// #[cfg(not(feature = "gpio-f373"))]
1092-
// _ => () // Make it a no-op for channels which are not available.
1030+
0..=9 => self.reg.smpr1().modify(|_, w| w.smp(index).variant(sample_time.into())),
1031+
10.. => self.reg.smpr2().modify(|_, w| w.smp(index).variant(sample_time.into())),
10931032
};
10941033
}
10951034

src/adc/channel.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ pub enum Id {
3434
/// 0
3535
// FIXME: Can not configure on feature, because 0 is the reset value.
3636
#[cfg(feature = "stm32f373")]
37-
Zero,
37+
Zero = 0,
3838
/// 1
39-
One,
39+
One = 1,
4040
/// 2
4141
Two,
4242
/// 3

src/adc/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use core::convert::TryFrom;
5454
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5555
#[allow(missing_docs)]
5656
pub enum Sequence {
57-
One,
57+
One = 0,
5858
Two,
5959
Three,
6060
Four,

0 commit comments

Comments
 (0)