Move Spi::new and Spi::compute_baud_rate to mod

This commit is contained in:
Grant Miller 2021-12-06 15:19:24 -06:00
parent 75374ce7e8
commit a35f337bd6
4 changed files with 191 additions and 329 deletions

View file

@ -1,11 +1,15 @@
#![macro_use] #![macro_use]
use crate::dma; use crate::dma;
use crate::gpio::sealed::{AFType, Pin};
use crate::gpio::{AnyPin, NoPin, OptionalPin}; use crate::gpio::{AnyPin, NoPin, OptionalPin};
use crate::pac::spi::vals; use crate::pac::spi::vals;
use crate::peripherals; use crate::peripherals;
use crate::rcc::RccPeripheral; use crate::rcc::RccPeripheral;
use crate::time::Hertz;
use core::marker::PhantomData; use core::marker::PhantomData;
use embassy::util::Unborrow;
use embassy_hal_common::unborrow;
#[cfg_attr(spi_v1, path = "v1.rs")] #[cfg_attr(spi_v1, path = "v1.rs")]
#[cfg_attr(spi_f1, path = "v1.rs")] #[cfg_attr(spi_f1, path = "v1.rs")]
@ -102,6 +106,190 @@ pub struct Spi<'d, T: Instance, Tx, Rx> {
phantom: PhantomData<&'d mut T>, phantom: PhantomData<&'d mut T>,
} }
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,
}
}
}
pub(crate) mod sealed { pub(crate) mod sealed {
use super::*; use super::*;

View file

@ -1,19 +1,10 @@
#![macro_use] #![macro_use]
use crate::dma::NoDma; use crate::dma::NoDma;
use crate::gpio::sealed::AFType;
use crate::gpio::sealed::Pin; use crate::gpio::sealed::Pin;
use crate::pac::spi; use crate::spi::{Error, Instance, RxDmaChannel, TxDmaChannel, WordSize};
use crate::spi::{
ByteOrder, Config, Error, Instance, MisoPin, MosiPin, RxDmaChannel, SckPin, TxDmaChannel,
WordSize,
};
use crate::time::Hertz;
use core::future::Future; use core::future::Future;
use core::marker::PhantomData;
use core::ptr; use core::ptr;
use embassy::util::Unborrow;
use embassy_hal_common::unborrow;
use embassy_traits::spi as traits; use embassy_traits::spi as traits;
pub use embedded_hal::blocking; pub use embedded_hal::blocking;
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
@ -22,100 +13,6 @@ use futures::future::join3;
use super::Spi; use super::Spi;
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { 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));
mosi.as_ref()
.map(|x| x.set_as_af(mosi_af, AFType::OutputPushPull));
miso.as_ref().map(|x| x.set_as_af(miso_af, AFType::Input));
}
let pclk = T::frequency();
let br = Self::compute_baud_rate(pclk, freq.into());
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 => spi::vals::Cpha::SECONDEDGE,
false => spi::vals::Cpha::FIRSTEDGE,
},
);
w.set_cpol(match config.mode.polarity == Polarity::IdleHigh {
true => spi::vals::Cpol::IDLEHIGH,
false => spi::vals::Cpol::IDLELOW,
});
w.set_mstr(spi::vals::Mstr::MASTER);
w.set_br(spi::vals::Br(br));
w.set_spe(true);
w.set_lsbfirst(match config.byte_order {
ByteOrder::LsbFirst => spi::vals::Lsbfirst::LSBFIRST,
ByteOrder::MsbFirst => spi::vals::Lsbfirst::MSBFIRST,
});
w.set_ssi(true);
w.set_ssm(true);
w.set_crcen(false);
w.set_bidimode(spi::vals::Bidimode::UNIDIRECTIONAL);
if mosi.is_none() {
w.set_rxonly(spi::vals::Rxonly::OUTPUTDISABLED);
}
w.set_dff(WordSize::EightBit.dff())
});
}
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,
}
}
fn set_word_size(&mut self, word_size: WordSize) { fn set_word_size(&mut self, word_size: WordSize) {
if self.current_word_size == word_size { if self.current_word_size == word_size {
return; return;

View file

@ -2,17 +2,9 @@
use crate::dma::NoDma; use crate::dma::NoDma;
use crate::gpio::sealed::Pin; use crate::gpio::sealed::Pin;
use crate::pac::spi; use crate::spi::{Error, Instance, RxDmaChannel, TxDmaChannel, WordSize};
use crate::spi::{
ByteOrder, Config, Error, Instance, MisoPin, MosiPin, RxDmaChannel, SckPin, TxDmaChannel,
WordSize,
};
use crate::time::Hertz;
use core::future::Future; use core::future::Future;
use core::marker::PhantomData;
use core::ptr; use core::ptr;
use embassy::util::Unborrow;
use embassy_hal_common::unborrow;
use embassy_traits::spi as traits; use embassy_traits::spi as traits;
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
use futures::future::{join, join3}; use futures::future::{join, join3};
@ -20,104 +12,6 @@ use futures::future::{join, join3};
use super::Spi; use super::Spi;
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { 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, crate::gpio::sealed::AFType::OutputPushPull);
x.set_speed(crate::gpio::Speed::VeryHigh);
});
mosi.as_ref().map(|x| {
x.set_as_af(mosi_af, crate::gpio::sealed::AFType::OutputPushPull);
x.set_speed(crate::gpio::Speed::VeryHigh);
});
miso.as_ref().map(|x| {
x.set_as_af(miso_af, crate::gpio::sealed::AFType::Input);
x.set_speed(crate::gpio::Speed::VeryHigh);
});
}
let pclk = T::frequency();
let freq = freq.into();
let br = Self::compute_baud_rate(pclk, freq);
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 => spi::vals::Cpha::SECONDEDGE,
false => spi::vals::Cpha::FIRSTEDGE,
},
);
w.set_cpol(match config.mode.polarity == Polarity::IdleHigh {
true => spi::vals::Cpol::IDLEHIGH,
false => spi::vals::Cpol::IDLELOW,
});
w.set_mstr(spi::vals::Mstr::MASTER);
w.set_br(spi::vals::Br(br));
w.set_lsbfirst(match config.byte_order {
ByteOrder::LsbFirst => spi::vals::Lsbfirst::LSBFIRST,
ByteOrder::MsbFirst => spi::vals::Lsbfirst::MSBFIRST,
});
w.set_ssi(true);
w.set_ssm(true);
w.set_crcen(false);
w.set_bidimode(spi::vals::Bidimode::UNIDIRECTIONAL);
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,
}
}
fn set_word_size(&mut self, word_size: WordSize) { fn set_word_size(&mut self, word_size: WordSize) {
if self.current_word_size == word_size { if self.current_word_size == word_size {
return; return;

View file

@ -2,17 +2,9 @@
use crate::dma::NoDma; use crate::dma::NoDma;
use crate::gpio::sealed::Pin; use crate::gpio::sealed::Pin;
use crate::pac::spi; use crate::spi::{Error, Instance, RxDmaChannel, TxDmaChannel, WordSize};
use crate::spi::{
ByteOrder, Config, Error, Instance, MisoPin, MosiPin, RxDmaChannel, SckPin, TxDmaChannel,
WordSize,
};
use crate::time::Hertz;
use core::future::Future; use core::future::Future;
use core::marker::PhantomData;
use core::ptr; use core::ptr;
use embassy::util::Unborrow;
use embassy_hal_common::unborrow;
use embassy_traits::spi as traits; use embassy_traits::spi as traits;
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
@ -21,115 +13,6 @@ use futures::future::join3;
use super::Spi; use super::Spi;
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { 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, crate::gpio::sealed::AFType::OutputPushPull);
x.set_speed(crate::gpio::Speed::VeryHigh);
});
mosi.as_ref().map(|x| {
x.set_as_af(mosi_af, crate::gpio::sealed::AFType::OutputPushPull);
x.set_speed(crate::gpio::Speed::VeryHigh);
});
miso.as_ref().map(|x| {
x.set_as_af(miso_af, crate::gpio::sealed::AFType::Input);
x.set_speed(crate::gpio::Speed::VeryHigh);
});
}
let pclk = T::frequency();
let br = Self::compute_baud_rate(pclk, freq.into());
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 => spi::vals::Cpha::SECONDEDGE,
false => spi::vals::Cpha::FIRSTEDGE,
},
);
w.set_cpol(match config.mode.polarity == Polarity::IdleHigh {
true => spi::vals::Cpol::IDLEHIGH,
false => spi::vals::Cpol::IDLELOW,
});
w.set_lsbfrst(match config.byte_order {
ByteOrder::LsbFirst => spi::vals::Lsbfrst::LSBFIRST,
ByteOrder::MsbFirst => spi::vals::Lsbfrst::MSBFIRST,
});
w.set_ssm(true);
w.set_master(spi::vals::Master::MASTER);
w.set_comm(spi::vals::Comm::FULLDUPLEX);
w.set_ssom(spi::vals::Ssom::ASSERTED);
w.set_midi(0);
w.set_mssi(0);
w.set_afcntr(spi::vals::Afcntr::CONTROLLED);
w.set_ssiop(spi::vals::Ssiop::ACTIVEHIGH);
});
T::regs().cfg1().modify(|w| {
w.set_crcen(false);
w.set_mbr(spi::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,
}
}
fn set_word_size(&mut self, word_size: WordSize) { fn set_word_size(&mut self, word_size: WordSize) {
if self.current_word_size == word_size { if self.current_word_size == word_size {
return; return;