#[non_exhaustive]
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Interface {
Jtag = 0,
Swd = 1,
Bdm3 = 2,
Fine = 3,
Pic32Icsp = 4,
Spi = 5,
C2 = 6,
CJtag = 7,
Mc2WireJtag = 10,
}
impl Interface {
fn mask(self) -> u32 {
1 << self as u32
}
fn all_mask() -> u32 {
InterfaceIter::new().fold(0, |mask, interface| mask | interface.mask())
}
}
#[derive(Debug)]
pub struct InterfaceIter {
current: Option<Interface>,
}
impl InterfaceIter {
pub fn new() -> Self {
Self {
current: Some(Interface::Jtag),
}
}
}
impl Iterator for InterfaceIter {
type Item = Interface;
fn next(&mut self) -> Option<Self::Item> {
let current = self.current?;
self.current = match current {
Interface::Jtag => Some(Interface::Swd),
Interface::Swd => Some(Interface::Bdm3),
Interface::Bdm3 => Some(Interface::Fine),
Interface::Fine => Some(Interface::Pic32Icsp),
Interface::Pic32Icsp => Some(Interface::Spi),
Interface::Spi => Some(Interface::C2),
Interface::C2 => Some(Interface::CJtag),
Interface::CJtag => Some(Interface::Mc2WireJtag),
Interface::Mc2WireJtag => None,
};
Some(current)
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Interfaces(u32);
impl Interfaces {
pub(crate) fn from_bits_warn(raw: u32) -> Self {
let flags = raw & Interface::all_mask();
if flags != raw {
tracing::debug!(
"unknown bits in interface mask: {raw:#010x} truncated to {flags:#010x}"
);
}
Self(flags)
}
pub(crate) fn single(interface: Interface) -> Self {
Self(interface.mask())
}
pub fn contains(&self, interface: Interface) -> bool {
self.0 & interface.mask() == interface.mask()
}
}
impl IntoIterator for Interfaces {
type Item = Interface;
type IntoIter = InterfacesIter;
fn into_iter(self) -> Self::IntoIter {
InterfacesIter {
interfaces: self,
current: InterfaceIter::new(),
}
}
}
pub struct InterfacesIter {
interfaces: Interfaces,
current: InterfaceIter,
}
impl Iterator for InterfacesIter {
type Item = Interface;
fn next(&mut self) -> Option<Self::Item> {
self.current
.by_ref()
.find(|¤t| self.interfaces.contains(current))
}
}