embassy/embassy-stm32/src/spi/v3.rs

157 lines
4.0 KiB
Rust
Raw Normal View History

#![macro_use]
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
use futures::future::join;
2021-12-07 05:45:40 +01:00
use super::*;
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
2021-12-07 05:06:58 +01:00
pub(super) async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
where
Tx: TxDmaChannel<T>,
{
self.set_word_size(WordSize::EightBit);
unsafe {
T::regs().cr1().modify(|w| {
w.set_spe(false);
});
2021-12-07 09:40:45 +01:00
// Flush the read buffer to avoid errornous data from being read
while T::regs().sr().read().rxp() {
let _ = T::regs().rxdr().read();
}
}
let request = self.txdma.request();
2021-12-06 23:33:06 +01:00
let dst = T::regs().tx_ptr();
2021-11-19 19:15:55 +01:00
let f = crate::dma::write(&mut self.txdma, request, write, dst);
unsafe {
T::regs().cfg1().modify(|reg| {
reg.set_txdmaen(true);
});
T::regs().cr1().modify(|w| {
w.set_spe(true);
});
T::regs().cr1().modify(|w| {
w.set_cstart(true);
});
}
f.await;
2021-12-14 23:59:31 +01:00
finish_dma(T::regs());
Ok(())
}
2021-12-07 05:06:58 +01:00
pub(super) async fn read_dma_u8(&mut self, read: &mut [u8]) -> Result<(), Error>
where
Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>,
{
self.set_word_size(WordSize::EightBit);
unsafe {
T::regs().cr1().modify(|w| {
w.set_spe(false);
});
T::regs().cfg1().modify(|reg| {
reg.set_rxdmaen(true);
});
}
2021-07-21 16:42:22 +02:00
let clock_byte_count = read.len();
let rx_request = self.rxdma.request();
2021-12-06 23:33:06 +01:00
let rx_src = T::regs().rx_ptr();
2021-11-19 19:15:55 +01:00
let rx_f = crate::dma::read(&mut self.rxdma, rx_request, rx_src, read);
2021-07-21 16:42:22 +02:00
let tx_request = self.txdma.request();
2021-12-06 23:33:06 +01:00
let tx_dst = T::regs().tx_ptr();
2021-11-19 19:15:55 +01:00
let clock_byte = 0x00u8;
let tx_f = crate::dma::write_repeated(
&mut self.txdma,
tx_request,
clock_byte,
clock_byte_count,
tx_dst,
);
2021-07-21 16:42:22 +02:00
unsafe {
T::regs().cfg1().modify(|reg| {
reg.set_txdmaen(true);
});
T::regs().cr1().modify(|w| {
w.set_spe(true);
});
T::regs().cr1().modify(|w| {
w.set_cstart(true);
2021-07-21 16:42:22 +02:00
});
}
join(tx_f, rx_f).await;
2021-12-14 23:59:31 +01:00
finish_dma(T::regs());
2021-07-21 16:42:22 +02:00
Ok(())
}
2021-12-07 05:45:40 +01:00
pub(super) async fn read_write_dma_u8(
&mut self,
read: &mut [u8],
write: &[u8],
) -> Result<(), Error>
where
Tx: TxDmaChannel<T>,
Rx: RxDmaChannel<T>,
{
assert!(read.len() >= write.len());
self.set_word_size(WordSize::EightBit);
unsafe {
T::regs().cr1().modify(|w| {
w.set_spe(false);
});
T::regs().cfg1().modify(|reg| {
reg.set_rxdmaen(true);
});
2021-12-02 11:38:43 +01:00
// Flush the read buffer to avoid errornous data from being read
while T::regs().sr().read().rxp() {
let _ = T::regs().rxdr().read();
}
}
2021-07-20 21:33:42 +02:00
let rx_request = self.rxdma.request();
2021-12-06 23:33:06 +01:00
let rx_src = T::regs().rx_ptr();
2021-11-19 19:15:55 +01:00
let rx_f = crate::dma::read(
&mut self.rxdma,
rx_request,
rx_src,
&mut read[0..write.len()],
);
let tx_request = self.txdma.request();
2021-12-06 23:33:06 +01:00
let tx_dst = T::regs().tx_ptr();
2021-11-19 19:15:55 +01:00
let tx_f = crate::dma::write(&mut self.txdma, tx_request, write, tx_dst);
unsafe {
T::regs().cfg1().modify(|reg| {
reg.set_txdmaen(true);
});
T::regs().cr1().modify(|w| {
w.set_spe(true);
});
T::regs().cr1().modify(|w| {
w.set_cstart(true);
});
}
join(tx_f, rx_f).await;
2021-12-14 23:59:31 +01:00
finish_dma(T::regs());
Ok(())
}
}