use crate::bit::*;
use crate::clock::peripheral::PeripheralInterrupt;
use crate::gpio::{self, GpioPort};
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum Line {
Line0 = 0,
Line1 = 1,
Line2 = 2,
Line3 = 3,
Line4 = 4,
Line5 = 5,
Line6 = 6,
Line7 = 7,
Line8 = 8,
Line9 = 9,
Line10 = 10,
Line11 = 11,
Line12 = 12,
Line13 = 13,
Line14 = 14,
Line15 = 15,
}
#[derive(Debug, PartialEq)]
pub enum ExitPinSource {
PA = 0,
PB = 1,
PF = 2,
}
impl From<gpio::GpioPort> for ExitPinSource {
fn from(value: GpioPort) -> Self {
match value {
GpioPort::GPIOA => Self::PA,
GpioPort::GPIOB => Self::PB,
GpioPort::GPIOF => Self::PF,
}
}
}
impl PeripheralInterrupt for Line {
fn interrupt(&self) -> crate::pac::interrupt {
match *self {
Line::Line0 | Line::Line1 => PY32f030xx_pac::interrupt::EXTI0_1,
Line::Line2 | Line::Line3 => PY32f030xx_pac::interrupt::EXTI2_3,
_ => PY32f030xx_pac::interrupt::EXTI4_15,
}
}
}
impl From<usize> for Line {
fn from(value: usize) -> Self {
match value {
0 => Self::Line0,
1 => Self::Line1,
2 => Self::Line2,
3 => Self::Line3,
4 => Self::Line4,
5 => Self::Line5,
6 => Self::Line6,
7 => Self::Line7,
8 => Self::Line8,
9 => Self::Line9,
10 => Self::Line10,
11 => Self::Line11,
12 => Self::Line12,
13 => Self::Line13,
14 => Self::Line14,
15 => Self::Line15,
_ => unreachable!(),
}
}
}
#[derive(PartialEq)]
pub enum Edge {
Rising,
Falling,
RisingFalling,
}
impl Edge {
pub fn is_rising(&self) -> bool {
*self == Self::Rising || *self == Self::RisingFalling
}
pub fn is_falling(&self) -> bool {
*self == Self::Falling || *self == Self::RisingFalling
}
}
pub struct BitIter(pub u32);
impl Iterator for BitIter {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> {
match self.0.trailing_zeros() {
32 => None,
idx => {
self.0 = bit_mask_idx_clear::<1>(idx as usize, self.0);
Some(idx)
}
}
}
}