2021-05-13 20:28:53 +02:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-12-06 21:47:50 +01:00
|
|
|
use crate::dma;
|
2021-12-06 22:19:24 +01:00
|
|
|
use crate::gpio::sealed::{AFType, Pin};
|
2021-12-06 21:47:50 +01:00
|
|
|
use crate::gpio::{AnyPin, NoPin, OptionalPin};
|
|
|
|
use crate::pac::spi::vals;
|
|
|
|
use crate::peripherals;
|
|
|
|
use crate::rcc::RccPeripheral;
|
2021-12-06 22:19:24 +01:00
|
|
|
use crate::time::Hertz;
|
2021-12-06 21:47:50 +01:00
|
|
|
use core::marker::PhantomData;
|
2021-12-06 22:19:24 +01:00
|
|
|
use embassy::util::Unborrow;
|
|
|
|
use embassy_hal_common::unborrow;
|
2021-12-06 21:47:50 +01:00
|
|
|
|
2021-07-21 20:12:50 +02:00
|
|
|
#[cfg_attr(spi_v1, path = "v1.rs")]
|
2021-10-06 21:02:15 +02:00
|
|
|
#[cfg_attr(spi_f1, path = "v1.rs")]
|
2021-07-21 22:50:38 +02:00
|
|
|
#[cfg_attr(spi_v2, path = "v2.rs")]
|
|
|
|
#[cfg_attr(spi_v3, path = "v3.rs")]
|
2021-05-17 02:04:51 +02:00
|
|
|
mod _version;
|
|
|
|
pub use _version::*;
|
2021-05-13 20:28:53 +02:00
|
|
|
|
2021-08-31 14:32:48 +02:00
|
|
|
#[derive(Debug)]
|
2021-05-20 10:54:10 +02:00
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
2021-05-14 16:11:43 +02:00
|
|
|
pub enum Error {
|
|
|
|
Framing,
|
|
|
|
Crc,
|
2021-05-27 23:05:42 +02:00
|
|
|
ModeFault,
|
2021-05-14 16:11:43 +02:00
|
|
|
Overrun,
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:28:53 +02:00
|
|
|
// TODO move upwards in the tree
|
|
|
|
pub enum ByteOrder {
|
|
|
|
LsbFirst,
|
|
|
|
MsbFirst,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialOrd, PartialEq)]
|
|
|
|
enum WordSize {
|
|
|
|
EightBit,
|
|
|
|
SixteenBit,
|
|
|
|
}
|
|
|
|
|
2021-12-06 21:13:25 +01:00
|
|
|
impl WordSize {
|
|
|
|
#[cfg(any(spi_v1, spi_f1))]
|
|
|
|
fn dff(&self) -> vals::Dff {
|
|
|
|
match self {
|
|
|
|
WordSize::EightBit => vals::Dff::EIGHTBIT,
|
|
|
|
WordSize::SixteenBit => vals::Dff::SIXTEENBIT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(spi_v2)]
|
|
|
|
fn ds(&self) -> vals::Ds {
|
|
|
|
match self {
|
|
|
|
WordSize::EightBit => vals::Ds::EIGHTBIT,
|
|
|
|
WordSize::SixteenBit => vals::Ds::SIXTEENBIT,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(spi_v2)]
|
|
|
|
fn frxth(&self) -> vals::Frxth {
|
|
|
|
match self {
|
|
|
|
WordSize::EightBit => vals::Frxth::QUARTER,
|
|
|
|
WordSize::SixteenBit => vals::Frxth::HALF,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(spi_v3)]
|
|
|
|
fn dsize(&self) -> u8 {
|
|
|
|
match self {
|
|
|
|
WordSize::EightBit => 0b0111,
|
|
|
|
WordSize::SixteenBit => 0b1111,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(spi_v3)]
|
|
|
|
fn _frxth(&self) -> vals::Fthlv {
|
|
|
|
match self {
|
|
|
|
WordSize::EightBit => vals::Fthlv::ONEFRAME,
|
|
|
|
WordSize::SixteenBit => vals::Fthlv::ONEFRAME,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-13 20:28:53 +02:00
|
|
|
#[non_exhaustive]
|
|
|
|
pub struct Config {
|
|
|
|
pub mode: Mode,
|
|
|
|
pub byte_order: ByteOrder,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
mode: MODE_0,
|
|
|
|
byte_order: ByteOrder::MsbFirst,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-14 16:11:43 +02:00
|
|
|
|
2021-12-06 21:47:50 +01:00
|
|
|
pub struct Spi<'d, T: Instance, Tx, Rx> {
|
|
|
|
sck: Option<AnyPin>,
|
|
|
|
mosi: Option<AnyPin>,
|
|
|
|
miso: Option<AnyPin>,
|
|
|
|
txdma: Tx,
|
|
|
|
rxdma: Rx,
|
|
|
|
current_word_size: WordSize,
|
|
|
|
phantom: PhantomData<&'d mut T>,
|
|
|
|
}
|
|
|
|
|
2021-12-06 22:19:24 +01:00
|
|
|
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
|
|
|
|
pub fn new<F>(
|
|
|
|
_peri: impl Unborrow<Target = T> + 'd,
|
|
|
|
sck: impl Unborrow<Target = impl SckPin<T>>,
|
|
|
|
mosi: impl Unborrow<Target = impl MosiPin<T>>,
|
|
|
|
miso: impl Unborrow<Target = impl MisoPin<T>>,
|
|
|
|
txdma: impl Unborrow<Target = Tx>,
|
|
|
|
rxdma: impl Unborrow<Target = Rx>,
|
|
|
|
freq: F,
|
|
|
|
config: Config,
|
|
|
|
) -> Self
|
|
|
|
where
|
|
|
|
F: Into<Hertz>,
|
|
|
|
{
|
|
|
|
unborrow!(sck, mosi, miso, txdma, rxdma);
|
|
|
|
|
|
|
|
let sck_af = sck.af_num();
|
|
|
|
let mosi_af = mosi.af_num();
|
|
|
|
let miso_af = miso.af_num();
|
|
|
|
let sck = sck.degrade_optional();
|
|
|
|
let mosi = mosi.degrade_optional();
|
|
|
|
let miso = miso.degrade_optional();
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
sck.as_ref().map(|x| {
|
|
|
|
x.set_as_af(sck_af, AFType::OutputPushPull);
|
|
|
|
#[cfg(any(spi_v2, spi_v3))]
|
|
|
|
x.set_speed(crate::gpio::Speed::VeryHigh);
|
|
|
|
});
|
|
|
|
mosi.as_ref().map(|x| {
|
|
|
|
x.set_as_af(mosi_af, AFType::OutputPushPull);
|
|
|
|
#[cfg(any(spi_v2, spi_v3))]
|
|
|
|
x.set_speed(crate::gpio::Speed::VeryHigh);
|
|
|
|
});
|
|
|
|
miso.as_ref().map(|x| {
|
|
|
|
x.set_as_af(miso_af, AFType::Input);
|
|
|
|
#[cfg(any(spi_v2, spi_v3))]
|
|
|
|
x.set_speed(crate::gpio::Speed::VeryHigh);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let pclk = T::frequency();
|
|
|
|
let br = Self::compute_baud_rate(pclk, freq.into());
|
|
|
|
|
|
|
|
#[cfg(any(spi_v1, spi_f1))]
|
|
|
|
unsafe {
|
|
|
|
T::enable();
|
|
|
|
T::reset();
|
|
|
|
T::regs().cr2().modify(|w| {
|
|
|
|
w.set_ssoe(false);
|
|
|
|
});
|
|
|
|
T::regs().cr1().modify(|w| {
|
|
|
|
w.set_cpha(
|
|
|
|
match config.mode.phase == Phase::CaptureOnSecondTransition {
|
|
|
|
true => vals::Cpha::SECONDEDGE,
|
|
|
|
false => vals::Cpha::FIRSTEDGE,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
w.set_cpol(match config.mode.polarity == Polarity::IdleHigh {
|
|
|
|
true => vals::Cpol::IDLEHIGH,
|
|
|
|
false => vals::Cpol::IDLELOW,
|
|
|
|
});
|
|
|
|
|
|
|
|
w.set_mstr(vals::Mstr::MASTER);
|
|
|
|
w.set_br(vals::Br(br));
|
|
|
|
w.set_spe(true);
|
|
|
|
w.set_lsbfirst(match config.byte_order {
|
|
|
|
ByteOrder::LsbFirst => vals::Lsbfirst::LSBFIRST,
|
|
|
|
ByteOrder::MsbFirst => vals::Lsbfirst::MSBFIRST,
|
|
|
|
});
|
|
|
|
w.set_ssi(true);
|
|
|
|
w.set_ssm(true);
|
|
|
|
w.set_crcen(false);
|
|
|
|
w.set_bidimode(vals::Bidimode::UNIDIRECTIONAL);
|
|
|
|
if mosi.is_none() {
|
|
|
|
w.set_rxonly(vals::Rxonly::OUTPUTDISABLED);
|
|
|
|
}
|
|
|
|
w.set_dff(WordSize::EightBit.dff())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
#[cfg(spi_v2)]
|
|
|
|
unsafe {
|
|
|
|
T::enable();
|
|
|
|
T::reset();
|
|
|
|
T::regs().cr2().modify(|w| {
|
|
|
|
w.set_ssoe(false);
|
|
|
|
});
|
|
|
|
T::regs().cr1().modify(|w| {
|
|
|
|
w.set_cpha(
|
|
|
|
match config.mode.phase == Phase::CaptureOnSecondTransition {
|
|
|
|
true => vals::Cpha::SECONDEDGE,
|
|
|
|
false => vals::Cpha::FIRSTEDGE,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
w.set_cpol(match config.mode.polarity == Polarity::IdleHigh {
|
|
|
|
true => vals::Cpol::IDLEHIGH,
|
|
|
|
false => vals::Cpol::IDLELOW,
|
|
|
|
});
|
|
|
|
|
|
|
|
w.set_mstr(vals::Mstr::MASTER);
|
|
|
|
w.set_br(vals::Br(br));
|
|
|
|
w.set_lsbfirst(match config.byte_order {
|
|
|
|
ByteOrder::LsbFirst => vals::Lsbfirst::LSBFIRST,
|
|
|
|
ByteOrder::MsbFirst => vals::Lsbfirst::MSBFIRST,
|
|
|
|
});
|
|
|
|
w.set_ssi(true);
|
|
|
|
w.set_ssm(true);
|
|
|
|
w.set_crcen(false);
|
|
|
|
w.set_bidimode(vals::Bidimode::UNIDIRECTIONAL);
|
|
|
|
w.set_spe(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
#[cfg(spi_v3)]
|
|
|
|
unsafe {
|
|
|
|
T::enable();
|
|
|
|
T::reset();
|
|
|
|
T::regs().ifcr().write(|w| w.0 = 0xffff_ffff);
|
|
|
|
T::regs().cfg2().modify(|w| {
|
|
|
|
//w.set_ssoe(true);
|
|
|
|
w.set_ssoe(false);
|
|
|
|
w.set_cpha(
|
|
|
|
match config.mode.phase == Phase::CaptureOnSecondTransition {
|
|
|
|
true => vals::Cpha::SECONDEDGE,
|
|
|
|
false => vals::Cpha::FIRSTEDGE,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
w.set_cpol(match config.mode.polarity == Polarity::IdleHigh {
|
|
|
|
true => vals::Cpol::IDLEHIGH,
|
|
|
|
false => vals::Cpol::IDLELOW,
|
|
|
|
});
|
|
|
|
w.set_lsbfrst(match config.byte_order {
|
|
|
|
ByteOrder::LsbFirst => vals::Lsbfrst::LSBFIRST,
|
|
|
|
ByteOrder::MsbFirst => vals::Lsbfrst::MSBFIRST,
|
|
|
|
});
|
|
|
|
w.set_ssm(true);
|
|
|
|
w.set_master(vals::Master::MASTER);
|
|
|
|
w.set_comm(vals::Comm::FULLDUPLEX);
|
|
|
|
w.set_ssom(vals::Ssom::ASSERTED);
|
|
|
|
w.set_midi(0);
|
|
|
|
w.set_mssi(0);
|
|
|
|
w.set_afcntr(vals::Afcntr::CONTROLLED);
|
|
|
|
w.set_ssiop(vals::Ssiop::ACTIVEHIGH);
|
|
|
|
});
|
|
|
|
T::regs().cfg1().modify(|w| {
|
|
|
|
w.set_crcen(false);
|
|
|
|
w.set_mbr(vals::Mbr(br));
|
|
|
|
w.set_dsize(WordSize::EightBit.dsize());
|
|
|
|
});
|
|
|
|
T::regs().cr2().modify(|w| {
|
|
|
|
w.set_tsize(0);
|
|
|
|
w.set_tser(0);
|
|
|
|
});
|
|
|
|
T::regs().cr1().modify(|w| {
|
|
|
|
w.set_ssi(false);
|
|
|
|
w.set_spe(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
sck,
|
|
|
|
mosi,
|
|
|
|
miso,
|
|
|
|
txdma,
|
|
|
|
rxdma,
|
|
|
|
current_word_size: WordSize::EightBit,
|
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn compute_baud_rate(clocks: Hertz, freq: Hertz) -> u8 {
|
|
|
|
match clocks.0 / freq.0 {
|
|
|
|
0 => unreachable!(),
|
|
|
|
1..=2 => 0b000,
|
|
|
|
3..=5 => 0b001,
|
|
|
|
6..=11 => 0b010,
|
|
|
|
12..=23 => 0b011,
|
|
|
|
24..=39 => 0b100,
|
|
|
|
40..=95 => 0b101,
|
|
|
|
96..=191 => 0b110,
|
|
|
|
_ => 0b111,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 16:11:43 +02:00
|
|
|
pub(crate) mod sealed {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
pub trait Instance {
|
|
|
|
fn regs() -> &'static crate::pac::spi::Spi;
|
|
|
|
}
|
|
|
|
|
2021-10-09 22:04:25 +02:00
|
|
|
pub trait SckPin<T: Instance>: OptionalPin {
|
2021-05-15 03:07:37 +02:00
|
|
|
fn af_num(&self) -> u8;
|
2021-05-14 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 22:04:25 +02:00
|
|
|
pub trait MosiPin<T: Instance>: OptionalPin {
|
2021-05-15 03:07:37 +02:00
|
|
|
fn af_num(&self) -> u8;
|
2021-05-14 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2021-10-09 22:04:25 +02:00
|
|
|
pub trait MisoPin<T: Instance>: OptionalPin {
|
2021-05-15 03:07:37 +02:00
|
|
|
fn af_num(&self) -> u8;
|
2021-05-14 16:11:43 +02:00
|
|
|
}
|
2021-07-20 15:19:23 +02:00
|
|
|
|
|
|
|
pub trait TxDmaChannel<T: Instance> {
|
|
|
|
fn request(&self) -> dma::Request;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait RxDmaChannel<T: Instance> {
|
|
|
|
fn request(&self) -> dma::Request;
|
|
|
|
}
|
2021-05-14 16:11:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 19:38:44 +02:00
|
|
|
pub trait Instance: sealed::Instance + RccPeripheral {}
|
|
|
|
pub trait SckPin<T: Instance>: sealed::SckPin<T> {}
|
|
|
|
pub trait MosiPin<T: Instance>: sealed::MosiPin<T> {}
|
|
|
|
pub trait MisoPin<T: Instance>: sealed::MisoPin<T> {}
|
|
|
|
pub trait TxDmaChannel<T: Instance>: sealed::TxDmaChannel<T> + dma::Channel {}
|
|
|
|
pub trait RxDmaChannel<T: Instance>: sealed::RxDmaChannel<T> + dma::Channel {}
|
2021-07-20 15:19:23 +02:00
|
|
|
|
2021-06-03 17:09:29 +02:00
|
|
|
crate::pac::peripherals!(
|
|
|
|
(spi, $inst:ident) => {
|
|
|
|
impl sealed::Instance for peripherals::$inst {
|
2021-05-14 16:11:43 +02:00
|
|
|
fn regs() -> &'static crate::pac::spi::Spi {
|
|
|
|
&crate::pac::$inst
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-03 17:09:29 +02:00
|
|
|
impl Instance for peripherals::$inst {}
|
2021-05-14 16:11:43 +02:00
|
|
|
};
|
2021-06-03 17:09:29 +02:00
|
|
|
);
|
2021-05-14 16:11:43 +02:00
|
|
|
|
2021-06-03 17:31:03 +02:00
|
|
|
macro_rules! impl_pin {
|
|
|
|
($inst:ident, $pin:ident, $signal:ident, $af:expr) => {
|
|
|
|
impl $signal<peripherals::$inst> for peripherals::$pin {}
|
2021-05-14 16:11:43 +02:00
|
|
|
|
2021-06-03 17:31:03 +02:00
|
|
|
impl sealed::$signal<peripherals::$inst> for peripherals::$pin {
|
2021-05-15 03:07:37 +02:00
|
|
|
fn af_num(&self) -> u8 {
|
|
|
|
$af
|
|
|
|
}
|
2021-05-14 16:11:43 +02:00
|
|
|
}
|
|
|
|
};
|
2021-06-03 17:31:03 +02:00
|
|
|
}
|
2021-06-03 17:09:29 +02:00
|
|
|
|
2021-10-09 11:35:05 +02:00
|
|
|
#[cfg(not(rcc_f1))]
|
2021-06-03 17:31:03 +02:00
|
|
|
crate::pac::peripheral_pins!(
|
|
|
|
($inst:ident, spi, SPI, $pin:ident, SCK, $af:expr) => {
|
|
|
|
impl_pin!($inst, $pin, SckPin, $af);
|
|
|
|
};
|
2021-06-03 17:09:29 +02:00
|
|
|
|
2021-06-03 17:31:03 +02:00
|
|
|
($inst:ident, spi, SPI, $pin:ident, MOSI, $af:expr) => {
|
|
|
|
impl_pin!($inst, $pin, MosiPin, $af);
|
2021-06-03 17:09:29 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
($inst:ident, spi, SPI, $pin:ident, MISO, $af:expr) => {
|
2021-06-03 17:31:03 +02:00
|
|
|
impl_pin!($inst, $pin, MisoPin, $af);
|
2021-06-03 17:09:29 +02:00
|
|
|
};
|
|
|
|
);
|
2021-07-20 21:44:13 +02:00
|
|
|
|
2021-10-09 11:35:05 +02:00
|
|
|
#[cfg(rcc_f1)]
|
|
|
|
crate::pac::peripheral_pins!(
|
|
|
|
($inst:ident, spi, SPI, $pin:ident, SCK) => {
|
|
|
|
impl_pin!($inst, $pin, SckPin, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
($inst:ident, spi, SPI, $pin:ident, MOSI) => {
|
|
|
|
impl_pin!($inst, $pin, MosiPin, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
($inst:ident, spi, SPI, $pin:ident, MISO) => {
|
|
|
|
impl_pin!($inst, $pin, MisoPin, 0);
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2021-12-06 21:02:21 +01:00
|
|
|
macro_rules! impl_nopin {
|
|
|
|
($inst:ident, $signal:ident) => {
|
|
|
|
impl $signal<peripherals::$inst> for NoPin {}
|
|
|
|
|
|
|
|
impl sealed::$signal<peripherals::$inst> for NoPin {
|
|
|
|
fn af_num(&self) -> u8 {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
crate::pac::peripherals!(
|
|
|
|
(spi, $inst:ident) => {
|
|
|
|
impl_nopin!($inst, SckPin);
|
|
|
|
impl_nopin!($inst, MosiPin);
|
|
|
|
impl_nopin!($inst, MisoPin);
|
|
|
|
};
|
|
|
|
);
|
|
|
|
|
2021-07-20 21:44:13 +02:00
|
|
|
macro_rules! impl_dma {
|
|
|
|
($inst:ident, {dmamux: $dmamux:ident}, $signal:ident, $request:expr) => {
|
|
|
|
impl<T> sealed::$signal<peripherals::$inst> for T
|
|
|
|
where
|
|
|
|
T: crate::dma::MuxChannel<Mux = crate::dma::$dmamux>,
|
|
|
|
{
|
|
|
|
fn request(&self) -> dma::Request {
|
|
|
|
$request
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> $signal<peripherals::$inst> for T where
|
|
|
|
T: crate::dma::MuxChannel<Mux = crate::dma::$dmamux>
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
($inst:ident, {channel: $channel:ident}, $signal:ident, $request:expr) => {
|
|
|
|
impl sealed::$signal<peripherals::$inst> for peripherals::$channel {
|
|
|
|
fn request(&self) -> dma::Request {
|
|
|
|
$request
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $signal<peripherals::$inst> for peripherals::$channel {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
crate::pac::peripheral_dma_channels! {
|
|
|
|
($peri:ident, spi, $kind:ident, RX, $channel:tt, $request:expr) => {
|
|
|
|
impl_dma!($peri, $channel, RxDmaChannel, $request);
|
|
|
|
};
|
|
|
|
($peri:ident, spi, $kind:ident, TX, $channel:tt, $request:expr) => {
|
|
|
|
impl_dma!($peri, $channel, TxDmaChannel, $request);
|
|
|
|
};
|
|
|
|
}
|