Remove AF_NUM const from pin traits, only use af_num fn

This commit is contained in:
Dario Nieuwenhuis 2021-05-15 03:07:37 +02:00
parent e63c4bde0b
commit 180ca48d34
3 changed files with 25 additions and 51 deletions

View File

@ -6,6 +6,8 @@ mod spi;
pub use spi::*; pub use spi::*;
use crate::gpio::Pin;
pub enum Error { pub enum Error {
Framing, Framing,
Crc, Crc,
@ -41,32 +43,21 @@ impl Default for Config {
pub(crate) mod sealed { pub(crate) mod sealed {
use super::*; use super::*;
use crate::gpio::Pin;
use embassy::util::AtomicWaker;
pub trait Instance { pub trait Instance {
fn regs() -> &'static crate::pac::spi::Spi; fn regs() -> &'static crate::pac::spi::Spi;
} }
pub trait SckPin<T: Instance>: Pin { pub trait SckPin<T: Instance>: Pin {
const AF: u8; fn af_num(&self) -> u8;
fn af(&self) -> u8 {
Self::AF
}
} }
pub trait MosiPin<T: Instance>: Pin { pub trait MosiPin<T: Instance>: Pin {
const AF: u8; fn af_num(&self) -> u8;
fn af(&self) -> u8 {
Self::AF
}
} }
pub trait MisoPin<T: Instance>: Pin { pub trait MisoPin<T: Instance>: Pin {
const AF: u8; fn af_num(&self) -> u8;
fn af(&self) -> u8 {
Self::AF
}
} }
} }
@ -95,7 +86,9 @@ macro_rules! impl_spi_pin {
impl crate::spi::$pin_func<peripherals::$inst> for peripherals::$pin {} impl crate::spi::$pin_func<peripherals::$inst> for peripherals::$pin {}
impl crate::spi::sealed::$pin_func<peripherals::$inst> for peripherals::$pin { impl crate::spi::sealed::$pin_func<peripherals::$inst> for peripherals::$pin {
const AF: u8 = $af; fn af_num(&self) -> u8 {
$af
}
} }
}; };
} }

View File

@ -43,9 +43,9 @@ impl<'d, T: Instance> Spi<'d, T> {
unborrow!(peri, sck, mosi, miso); unborrow!(peri, sck, mosi, miso);
unsafe { unsafe {
sck.set_as_af(sck.af()); sck.set_as_af(sck.af_num());
mosi.set_as_af(mosi.af()); mosi.set_as_af(mosi.af_num());
miso.set_as_af(miso.af()); miso.set_as_af(miso.af_num());
} }
let sck = sck.degrade(); let sck = sck.degrade();

View File

@ -56,20 +56,20 @@ pub(crate) mod sealed {
pub trait Instance { pub trait Instance {
fn regs(&self) -> Usart; fn regs(&self) -> Usart;
} }
pub trait RxPin<T: Instance>: OptionalPin { pub trait RxPin<T: Instance>: Pin {
const AF_NUM: u8; fn af_num(&self) -> u8;
} }
pub trait TxPin<T: Instance>: OptionalPin { pub trait TxPin<T: Instance>: Pin {
const AF_NUM: u8; fn af_num(&self) -> u8;
} }
pub trait CtsPin<T: Instance>: OptionalPin { pub trait CtsPin<T: Instance>: Pin {
const AF_NUM: u8; fn af_num(&self) -> u8;
} }
pub trait RtsPin<T: Instance>: OptionalPin { pub trait RtsPin<T: Instance>: Pin {
const AF_NUM: u8; fn af_num(&self) -> u8;
} }
pub trait CkPin<T: Instance>: OptionalPin { pub trait CkPin<T: Instance>: Pin {
const AF_NUM: u8; fn af_num(&self) -> u8;
} }
} }
pub trait Instance: sealed::Instance {} pub trait Instance: sealed::Instance {}
@ -79,27 +79,6 @@ pub trait CtsPin<T: Instance>: sealed::CtsPin<T> {}
pub trait RtsPin<T: Instance>: sealed::RtsPin<T> {} pub trait RtsPin<T: Instance>: sealed::RtsPin<T> {}
pub trait CkPin<T: Instance>: sealed::CkPin<T> {} pub trait CkPin<T: Instance>: sealed::CkPin<T> {}
impl<T: Instance> sealed::RxPin<T> for NoPin {
const AF_NUM: u8 = 0;
}
impl<T: Instance> RxPin<T> for NoPin {}
impl<T: Instance> sealed::TxPin<T> for NoPin {
const AF_NUM: u8 = 0;
}
impl<T: Instance> TxPin<T> for NoPin {}
impl<T: Instance> sealed::CtsPin<T> for NoPin {
const AF_NUM: u8 = 0;
}
impl<T: Instance> CtsPin<T> for NoPin {}
impl<T: Instance> sealed::RtsPin<T> for NoPin {
const AF_NUM: u8 = 0;
}
impl<T: Instance> RtsPin<T> for NoPin {}
impl<T: Instance> sealed::CkPin<T> for NoPin {
const AF_NUM: u8 = 0;
}
impl<T: Instance> CkPin<T> for NoPin {}
macro_rules! impl_usart { macro_rules! impl_usart {
($inst:ident) => { ($inst:ident) => {
impl crate::usart::sealed::Instance for peripherals::$inst { impl crate::usart::sealed::Instance for peripherals::$inst {
@ -112,9 +91,11 @@ macro_rules! impl_usart {
} }
macro_rules! impl_usart_pin { macro_rules! impl_usart_pin {
($inst:ident, $func:ident, $pin:ident, $num:expr) => { ($inst:ident, $func:ident, $pin:ident, $af:expr) => {
impl crate::usart::sealed::$func<peripherals::$inst> for peripherals::$pin { impl crate::usart::sealed::$func<peripherals::$inst> for peripherals::$pin {
const AF_NUM: u8 = $num; fn af_num(&self) -> u8 {
$af
}
} }
impl crate::usart::$func<peripherals::$inst> for peripherals::$pin {} impl crate::usart::$func<peripherals::$inst> for peripherals::$pin {}
}; };