#![macro_use] use crate::dma::NoDma; use crate::gpio::sealed::AFType; use crate::gpio::sealed::Pin; use crate::gpio::{AnyPin, NoPin}; use crate::pac::spi; use crate::peripherals; use crate::spi::{ ByteOrder, Config, Error, Instance, MisoPin, MosiPin, RxDmaChannel, SckPin, TxDmaChannel, WordSize, }; use crate::time::Hertz; use core::future::Future; use core::marker::PhantomData; use core::ptr; use embassy::util::Unborrow; use embassy_hal_common::unborrow; use embassy_traits::spi as traits; pub use embedded_hal::blocking; pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3}; use futures::future::join3; impl WordSize { fn dff(&self) -> spi::vals::Dff { match self { WordSize::EightBit => spi::vals::Dff::EIGHTBIT, WordSize::SixteenBit => spi::vals::Dff::SIXTEENBIT, } } } macro_rules! impl_nopin { ($inst:ident, $signal:ident) => { impl $signal for NoPin {} impl super::sealed::$signal 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); }; ); pub struct Spi<'d, T: Instance, Tx, Rx> { sck: Option, mosi: Option, miso: Option, txdma: Tx, rxdma: Rx, current_word_size: WordSize, phantom: PhantomData<&'d mut T>, } impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> { pub fn new( _peri: impl Unborrow + 'd, sck: impl Unborrow>, mosi: impl Unborrow>, miso: impl Unborrow>, txdma: impl Unborrow, rxdma: impl Unborrow, freq: F, config: Config, ) -> Self where F: Into, { 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)); } unsafe { T::regs().cr2().modify(|w| { w.set_ssoe(false); }); } let pclk = T::frequency(); let br = Self::compute_baud_rate(pclk, freq.into()); unsafe { T::enable(); T::reset(); 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) { if self.current_word_size == word_size { return; } unsafe { T::regs().cr1().modify(|reg| { reg.set_spe(false); reg.set_dff(word_size.dff()) }); T::regs().cr1().modify(|reg| { reg.set_spe(true); }); self.current_word_size = word_size; } } #[allow(unused)] async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error> where Tx: TxDmaChannel, { unsafe { T::regs().cr1().modify(|w| { w.set_spe(false); }); } self.set_word_size(WordSize::EightBit); let request = self.txdma.request(); let dst = T::regs().dr().ptr() as *mut u8; let f = self.txdma.write(request, write, dst); unsafe { T::regs().cr2().modify(|reg| { reg.set_txdmaen(true); }); T::regs().cr1().modify(|w| { w.set_spe(true); }); } f.await; Ok(()) } #[allow(unused)] async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, { unsafe { T::regs().cr1().modify(|w| { w.set_spe(false); }); T::regs().cr2().modify(|reg| { reg.set_rxdmaen(true); }); } self.set_word_size(WordSize::EightBit); let clock_byte_count = read.len(); let rx_request = self.rxdma.request(); let rx_src = T::regs().dr().ptr() as *mut u8; let rx_f = self.rxdma.read(rx_request, rx_src, read); let tx_request = self.txdma.request(); let tx_dst = T::regs().dr().ptr() as *mut u8; let clock_byte = 0x00; let tx_f = self .txdma .write_x(tx_request, &clock_byte, clock_byte_count, tx_dst); unsafe { T::regs().cr2().modify(|reg| { reg.set_txdmaen(true); }); T::regs().cr1().modify(|w| { w.set_spe(true); }); } join3(tx_f, rx_f, Self::wait_for_idle()).await; unsafe { T::regs().cr2().modify(|reg| { reg.set_txdmaen(false); reg.set_rxdmaen(false); }); T::regs().cr1().modify(|w| { w.set_spe(false); }); } Ok(()) } #[allow(unused)] async fn read_write_dma_u8(&mut self, read: &mut [u8], write: &[u8]) -> Result<(), Error> where Tx: TxDmaChannel, Rx: RxDmaChannel, { assert!(read.len() >= write.len()); unsafe { T::regs().cr1().modify(|w| { w.set_spe(false); }); T::regs().cr2().modify(|reg| { reg.set_rxdmaen(true); }); } self.set_word_size(WordSize::EightBit); let rx_request = self.rxdma.request(); let rx_src = T::regs().dr().ptr() as *mut u8; let rx_f = self .rxdma .read(rx_request, rx_src, &mut read[0..write.len()]); let tx_request = self.txdma.request(); let tx_dst = T::regs().dr().ptr() as *mut u8; let tx_f = self.txdma.write(tx_request, write, tx_dst); unsafe { T::regs().cr2().modify(|reg| { reg.set_txdmaen(true); }); T::regs().cr1().modify(|w| { w.set_spe(true); }); } join3(tx_f, rx_f, Self::wait_for_idle()).await; unsafe { T::regs().cr2().modify(|reg| { reg.set_txdmaen(false); reg.set_rxdmaen(false); }); T::regs().cr1().modify(|w| { w.set_spe(false); }); } Ok(()) } async fn wait_for_idle() { unsafe { while T::regs().sr().read().bsy() { // spin } } } } impl<'d, T: Instance, Tx, Rx> Drop for Spi<'d, T, Tx, Rx> { fn drop(&mut self) { unsafe { self.sck.as_ref().map(|x| x.set_as_analog()); self.mosi.as_ref().map(|x| x.set_as_analog()); self.miso.as_ref().map(|x| x.set_as_analog()); } } } impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T, NoDma, NoDma> { type Error = Error; fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> { self.set_word_size(WordSize::EightBit); let regs = T::regs(); for word in words.iter() { write_word(regs, *word)?; let _: u8 = read_word(regs)?; } Ok(()) } } impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, NoDma, NoDma> { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> { self.set_word_size(WordSize::EightBit); let regs = T::regs(); for word in words.iter_mut() { write_word(regs, *word)?; *word = read_word(regs)?; } Ok(words) } } impl<'d, T: Instance> embedded_hal::blocking::spi::Write for Spi<'d, T, NoDma, NoDma> { type Error = Error; fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> { self.set_word_size(WordSize::SixteenBit); let regs = T::regs(); for word in words.iter() { write_word(regs, *word)?; let _: u8 = read_word(regs)?; } Ok(()) } } impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer for Spi<'d, T, NoDma, NoDma> { type Error = Error; fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> { self.set_word_size(WordSize::SixteenBit); let regs = T::regs(); for word in words.iter_mut() { write_word(regs, *word)?; *word = read_word(regs)?; } Ok(words) } } impl<'d, T: Instance, Tx, Rx> traits::Spi for Spi<'d, T, Tx, Rx> { type Error = super::Error; } impl<'d, T: Instance, Tx: TxDmaChannel, Rx> traits::Write for Spi<'d, T, Tx, Rx> { #[rustfmt::skip] type WriteFuture<'a> where Self: 'a = impl Future> + 'a; fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> { self.write_dma_u8(data) } } impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::Read for Spi<'d, T, Tx, Rx> { #[rustfmt::skip] type ReadFuture<'a> where Self: 'a = impl Future> + 'a; fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> { self.read_dma_u8(data) } } impl<'d, T: Instance, Tx: TxDmaChannel, Rx: RxDmaChannel> traits::FullDuplex for Spi<'d, T, Tx, Rx> { #[rustfmt::skip] type WriteReadFuture<'a> where Self: 'a = impl Future> + 'a; fn read_write<'a>( &'a mut self, read: &'a mut [u8], write: &'a [u8], ) -> Self::WriteReadFuture<'a> { self.read_write_dma_u8(read, write) } } trait Word {} impl Word for u8 {} impl Word for u16 {} fn write_word(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(), Error> { loop { let sr = unsafe { regs.sr().read() }; if sr.ovr() { return Err(Error::Overrun); } else if sr_fre(sr) { return Err(Error::Framing); } else if sr.modf() { return Err(Error::ModeFault); } else if sr.crcerr() { return Err(Error::Crc); } else if sr.txe() { unsafe { let dr = regs.dr().ptr() as *mut W; ptr::write_volatile(dr, word); } return Ok(()); } } } /// Read a single word blocking. Assumes word size have already been set. fn read_word(regs: &'static crate::pac::spi::Spi) -> Result { loop { let sr = unsafe { regs.sr().read() }; if sr.ovr() { return Err(Error::Overrun); } else if sr.modf() { return Err(Error::ModeFault); } else if sr_fre(sr) { return Err(Error::Framing); } else if sr.crcerr() { return Err(Error::Crc); } else if sr.rxne() { unsafe { let dr = regs.dr().ptr() as *const W; return Ok(ptr::read_volatile(dr)); } } } } // SPI on F1 is just V1 without FRE and FRF fields // This driver only uses FRE, so add a simple function here to read fre on v1, // and return false on f1 #[cfg(spi_v1)] fn sr_fre(sr: crate::pac::spi::regs::Sr) -> bool { sr.fre() } #[cfg(spi_f1)] fn sr_fre(_sr: crate::pac::spi::regs::Sr) -> bool { false }