2021-05-10 16:17:58 -04:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
use crate::dma::NoDma;
|
2021-10-11 22:50:33 +02:00
|
|
|
use crate::gpio::sealed::Pin;
|
2021-12-06 15:19:24 -06:00
|
|
|
use crate::spi::{Error, Instance, RxDmaChannel, TxDmaChannel, WordSize};
|
2021-07-21 16:45:43 -04:00
|
|
|
use core::future::Future;
|
2021-05-20 14:19:43 -04:00
|
|
|
use core::ptr;
|
2021-07-21 16:45:43 -04:00
|
|
|
use embassy_traits::spi as traits;
|
2021-10-09 22:04:25 +02:00
|
|
|
pub use embedded_hal::blocking;
|
2021-05-14 10:11:43 -04:00
|
|
|
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
|
2021-07-21 16:45:43 -04:00
|
|
|
use futures::future::join3;
|
2021-05-12 14:18:42 -04:00
|
|
|
|
2021-12-06 14:47:50 -06:00
|
|
|
use super::Spi;
|
2021-05-10 15:21:57 -04:00
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
impl<'d, T: Instance, Tx, Rx> Spi<'d, T, Tx, Rx> {
|
2021-05-13 14:28:53 -04:00
|
|
|
fn set_word_size(&mut self, word_size: WordSize) {
|
|
|
|
if self.current_word_size == word_size {
|
2021-05-14 10:11:43 -04:00
|
|
|
return;
|
2021-05-13 14:28:53 -04:00
|
|
|
}
|
2021-05-12 14:18:42 -04:00
|
|
|
unsafe {
|
2021-05-14 10:11:43 -04:00
|
|
|
T::regs().cr1().modify(|reg| {
|
2021-05-13 14:28:53 -04:00
|
|
|
reg.set_spe(false);
|
2021-05-14 10:11:43 -04:00
|
|
|
reg.set_dff(word_size.dff())
|
2021-05-13 14:28:53 -04:00
|
|
|
});
|
2021-05-14 10:11:43 -04:00
|
|
|
T::regs().cr1().modify(|reg| {
|
2021-05-13 14:28:53 -04:00
|
|
|
reg.set_spe(true);
|
|
|
|
});
|
2021-05-12 14:18:42 -04:00
|
|
|
}
|
2021-12-06 14:24:02 -06:00
|
|
|
self.current_word_size = word_size;
|
2021-05-12 14:18:42 -04:00
|
|
|
}
|
2021-07-21 16:45:43 -04:00
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
async fn write_dma_u8(&mut self, write: &[u8]) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
Tx: TxDmaChannel<T>,
|
|
|
|
{
|
|
|
|
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<T>,
|
|
|
|
Rx: RxDmaChannel<T>,
|
|
|
|
{
|
|
|
|
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<T>,
|
|
|
|
Rx: RxDmaChannel<T>,
|
|
|
|
{
|
2021-07-22 09:28:42 -04:00
|
|
|
assert!(read.len() >= write.len());
|
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
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;
|
2021-07-22 09:50:34 -04:00
|
|
|
let rx_f = self
|
|
|
|
.rxdma
|
|
|
|
.read(rx_request, rx_src, &mut read[0..write.len()]);
|
2021-07-21 16:45:43 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 10:46:18 -04:00
|
|
|
}
|
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
impl<'d, T: Instance, Tx, Rx> Drop for Spi<'d, T, Tx, Rx> {
|
2021-05-12 10:46:18 -04:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2021-10-09 22:04:25 +02:00
|
|
|
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());
|
2021-05-12 10:46:18 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-11 11:25:01 -04:00
|
|
|
}
|
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spi<'d, T, NoDma, NoDma> {
|
2021-05-10 15:21:57 -04:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
|
2021-05-13 14:28:53 -04:00
|
|
|
self.set_word_size(WordSize::EightBit);
|
2021-05-10 16:17:58 -04:00
|
|
|
let regs = T::regs();
|
2021-05-10 15:21:57 -04:00
|
|
|
|
2021-05-10 16:17:58 -04:00
|
|
|
for word in words.iter() {
|
2021-09-21 14:50:05 +02:00
|
|
|
write_word(regs, *word)?;
|
|
|
|
let _: u8 = read_word(regs)?;
|
2021-05-10 16:17:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2021-05-10 15:21:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spi<'d, T, NoDma, NoDma> {
|
2021-05-10 16:17:58 -04:00
|
|
|
type Error = Error;
|
2021-05-10 15:21:57 -04:00
|
|
|
|
2021-05-10 16:17:58 -04:00
|
|
|
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
|
2021-05-13 14:28:53 -04:00
|
|
|
self.set_word_size(WordSize::EightBit);
|
2021-05-10 16:17:58 -04:00
|
|
|
let regs = T::regs();
|
2021-05-10 15:21:57 -04:00
|
|
|
|
2021-05-10 16:17:58 -04:00
|
|
|
for word in words.iter_mut() {
|
2021-09-21 14:50:05 +02:00
|
|
|
write_word(regs, *word)?;
|
|
|
|
*word = read_word(regs)?;
|
2021-05-12 14:18:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(words)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u16> for Spi<'d, T, NoDma, NoDma> {
|
2021-05-12 14:18:42 -04:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> {
|
2021-05-13 14:28:53 -04:00
|
|
|
self.set_word_size(WordSize::SixteenBit);
|
2021-05-12 14:18:42 -04:00
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
for word in words.iter() {
|
2021-09-21 14:50:05 +02:00
|
|
|
write_word(regs, *word)?;
|
|
|
|
let _: u8 = read_word(regs)?;
|
2021-05-10 15:21:57 -04:00
|
|
|
}
|
2021-05-10 16:17:58 -04:00
|
|
|
|
2021-05-12 14:18:42 -04:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-21 16:45:43 -04:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T, NoDma, NoDma> {
|
2021-05-12 14:18:42 -04:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> {
|
2021-05-13 14:28:53 -04:00
|
|
|
self.set_word_size(WordSize::SixteenBit);
|
2021-05-12 14:18:42 -04:00
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
for word in words.iter_mut() {
|
2021-09-21 14:50:05 +02:00
|
|
|
write_word(regs, *word)?;
|
|
|
|
*word = read_word(regs)?;
|
2021-05-12 14:18:42 -04:00
|
|
|
}
|
|
|
|
|
2021-05-10 16:17:58 -04:00
|
|
|
Ok(words)
|
2021-05-10 15:21:57 -04:00
|
|
|
}
|
2021-05-10 16:17:58 -04:00
|
|
|
}
|
2021-07-21 16:45:43 -04:00
|
|
|
|
|
|
|
impl<'d, T: Instance, Tx, Rx> traits::Spi<u8> for Spi<'d, T, Tx, Rx> {
|
|
|
|
type Error = super::Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx> traits::Write<u8> for Spi<'d, T, Tx, Rx> {
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
|
|
|
|
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
|
|
|
|
self.write_dma_u8(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance, Tx: TxDmaChannel<T>, Rx: RxDmaChannel<T>> traits::Read<u8>
|
|
|
|
for Spi<'d, T, Tx, Rx>
|
|
|
|
{
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + '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<T>, Rx: RxDmaChannel<T>> traits::FullDuplex<u8>
|
|
|
|
for Spi<'d, T, Tx, Rx>
|
|
|
|
{
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type WriteReadFuture<'a> where Self: 'a = impl Future<Output=Result<(), Self::Error>> + '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)
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 14:50:05 +02:00
|
|
|
|
|
|
|
trait Word {}
|
|
|
|
|
|
|
|
impl Word for u8 {}
|
|
|
|
impl Word for u16 {}
|
|
|
|
|
|
|
|
fn write_word<W: 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);
|
2021-10-11 23:33:32 +02:00
|
|
|
}
|
|
|
|
#[cfg(not(spi_f1))]
|
|
|
|
if sr.fre() {
|
2021-09-21 14:50:05 +02:00
|
|
|
return Err(Error::Framing);
|
2021-10-11 23:33:32 +02:00
|
|
|
}
|
|
|
|
if sr.modf() {
|
2021-09-21 14:50:05 +02:00
|
|
|
return Err(Error::ModeFault);
|
2021-10-11 23:33:32 +02:00
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
2021-09-21 14:50:05 +02:00
|
|
|
return Err(Error::Crc);
|
2021-10-11 23:33:32 +02:00
|
|
|
}
|
|
|
|
if sr.txe() {
|
2021-09-21 14:50:05 +02:00
|
|
|
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<W: Word>(regs: &'static crate::pac::spi::Spi) -> Result<W, Error> {
|
|
|
|
loop {
|
|
|
|
let sr = unsafe { regs.sr().read() };
|
|
|
|
if sr.ovr() {
|
|
|
|
return Err(Error::Overrun);
|
2021-10-11 23:33:32 +02:00
|
|
|
}
|
|
|
|
#[cfg(not(spi_f1))]
|
|
|
|
if sr.fre() {
|
2021-09-21 14:50:05 +02:00
|
|
|
return Err(Error::Framing);
|
2021-10-11 23:33:32 +02:00
|
|
|
}
|
|
|
|
if sr.modf() {
|
|
|
|
return Err(Error::ModeFault);
|
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
2021-09-21 14:50:05 +02:00
|
|
|
return Err(Error::Crc);
|
2021-10-11 23:33:32 +02:00
|
|
|
}
|
|
|
|
if sr.rxne() {
|
2021-09-21 14:50:05 +02:00
|
|
|
unsafe {
|
|
|
|
let dr = regs.dr().ptr() as *const W;
|
|
|
|
return Ok(ptr::read_volatile(dr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|