2021-05-11 01:04:59 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-01-18 13:22:55 +00:00
|
|
|
use core::future::Future;
|
2021-03-19 03:08:44 +00:00
|
|
|
use core::marker::PhantomData;
|
2021-01-18 13:22:55 +00:00
|
|
|
use core::sync::atomic::{compiler_fence, Ordering};
|
|
|
|
use core::task::Poll;
|
2021-04-14 14:37:10 +00:00
|
|
|
use embassy::interrupt::InterruptExt;
|
2021-03-18 00:27:30 +00:00
|
|
|
use embassy::traits;
|
2021-04-14 17:59:52 +00:00
|
|
|
use embassy::util::{AtomicWaker, Unborrow};
|
2021-03-21 21:09:06 +00:00
|
|
|
use embassy_extras::unborrow;
|
2021-01-18 13:22:55 +00:00
|
|
|
use futures::future::poll_fn;
|
2021-03-18 00:27:30 +00:00
|
|
|
use traits::spi::FullDuplex;
|
2021-01-18 13:22:55 +00:00
|
|
|
|
2021-03-27 02:20:58 +00:00
|
|
|
use crate::gpio::sealed::Pin as _;
|
|
|
|
use crate::gpio::{OptionalPin, Pin as GpioPin};
|
2021-05-17 09:48:58 +00:00
|
|
|
use crate::interrupt::Interrupt;
|
|
|
|
use crate::{pac, util::slice_in_ram_or};
|
2021-01-18 13:22:55 +00:00
|
|
|
|
2021-03-19 03:08:44 +00:00
|
|
|
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
|
|
|
|
pub use pac::spim0::frequency::FREQUENCY_A as Frequency;
|
2021-01-18 13:22:55 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
|
|
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Error {
|
|
|
|
TxBufferTooLong,
|
|
|
|
RxBufferTooLong,
|
|
|
|
/// EasyDMA can only read from data memory, read only buffers in flash will fail.
|
|
|
|
DMABufferNotInDataMemory,
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:08:44 +00:00
|
|
|
pub struct Spim<'d, T: Instance> {
|
|
|
|
phantom: PhantomData<&'d mut T>,
|
2021-01-18 13:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-11 01:04:59 +00:00
|
|
|
#[non_exhaustive]
|
2021-01-18 13:22:55 +00:00
|
|
|
pub struct Config {
|
|
|
|
pub frequency: Frequency,
|
|
|
|
pub mode: Mode,
|
|
|
|
pub orc: u8,
|
|
|
|
}
|
|
|
|
|
2021-05-11 01:04:59 +00:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
frequency: Frequency::M1,
|
|
|
|
mode: MODE_0,
|
|
|
|
orc: 0x00,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 03:08:44 +00:00
|
|
|
impl<'d, T: Instance> Spim<'d, T> {
|
|
|
|
pub fn new(
|
2021-05-17 10:23:04 +00:00
|
|
|
_spim: impl Unborrow<Target = T> + 'd,
|
2021-04-14 17:59:52 +00:00
|
|
|
irq: impl Unborrow<Target = T::Interrupt> + 'd,
|
|
|
|
sck: impl Unborrow<Target = impl GpioPin> + 'd,
|
|
|
|
miso: impl Unborrow<Target = impl OptionalPin> + 'd,
|
|
|
|
mosi: impl Unborrow<Target = impl OptionalPin> + 'd,
|
2021-03-19 03:08:44 +00:00
|
|
|
config: Config,
|
|
|
|
) -> Self {
|
2021-05-17 10:23:04 +00:00
|
|
|
unborrow!(irq, sck, miso, mosi);
|
2021-03-19 03:08:44 +00:00
|
|
|
|
2021-04-14 14:37:10 +00:00
|
|
|
let r = T::regs();
|
2021-01-18 13:22:55 +00:00
|
|
|
|
2021-03-19 03:08:44 +00:00
|
|
|
// Configure pins
|
2021-03-21 19:52:20 +00:00
|
|
|
sck.conf().write(|w| w.dir().output().drive().h0h1());
|
2021-03-27 02:20:58 +00:00
|
|
|
if let Some(mosi) = mosi.pin_mut() {
|
|
|
|
mosi.conf().write(|w| w.dir().output().drive().h0h1());
|
|
|
|
}
|
|
|
|
if let Some(miso) = miso.pin_mut() {
|
|
|
|
miso.conf().write(|w| w.input().connect().drive().h0h1());
|
|
|
|
}
|
2021-03-19 03:08:44 +00:00
|
|
|
|
|
|
|
match config.mode.polarity {
|
|
|
|
Polarity::IdleHigh => {
|
|
|
|
sck.set_high();
|
2021-03-27 02:20:58 +00:00
|
|
|
if let Some(mosi) = mosi.pin_mut() {
|
|
|
|
mosi.set_high();
|
|
|
|
}
|
2021-03-19 03:08:44 +00:00
|
|
|
}
|
|
|
|
Polarity::IdleLow => {
|
|
|
|
sck.set_low();
|
2021-03-27 02:20:58 +00:00
|
|
|
if let Some(mosi) = mosi.pin_mut() {
|
|
|
|
mosi.set_low();
|
|
|
|
}
|
2021-03-19 03:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 13:22:55 +00:00
|
|
|
// Select pins.
|
2021-03-27 02:20:58 +00:00
|
|
|
// Note: OptionalPin reports 'disabled' for psel_bits when no pin was selected.
|
2021-03-21 19:52:20 +00:00
|
|
|
r.psel.sck.write(|w| unsafe { w.bits(sck.psel_bits()) });
|
|
|
|
r.psel.mosi.write(|w| unsafe { w.bits(mosi.psel_bits()) });
|
|
|
|
r.psel.miso.write(|w| unsafe { w.bits(miso.psel_bits()) });
|
2021-01-18 13:22:55 +00:00
|
|
|
|
|
|
|
// Enable SPIM instance.
|
|
|
|
r.enable.write(|w| w.enable().enabled());
|
|
|
|
|
|
|
|
// Configure mode.
|
|
|
|
let mode = config.mode;
|
|
|
|
r.config.write(|w| {
|
|
|
|
// Can't match on `mode` due to embedded-hal, see https://github.com/rust-embedded/embedded-hal/pull/126
|
|
|
|
if mode == MODE_0 {
|
|
|
|
w.order().msb_first();
|
|
|
|
w.cpol().active_high();
|
|
|
|
w.cpha().leading();
|
|
|
|
} else if mode == MODE_1 {
|
|
|
|
w.order().msb_first();
|
|
|
|
w.cpol().active_high();
|
|
|
|
w.cpha().trailing();
|
|
|
|
} else if mode == MODE_2 {
|
|
|
|
w.order().msb_first();
|
|
|
|
w.cpol().active_low();
|
|
|
|
w.cpha().leading();
|
|
|
|
} else {
|
|
|
|
w.order().msb_first();
|
|
|
|
w.cpol().active_low();
|
|
|
|
w.cpha().trailing();
|
|
|
|
}
|
|
|
|
w
|
|
|
|
});
|
|
|
|
|
|
|
|
// Configure frequency.
|
|
|
|
let frequency = config.frequency;
|
|
|
|
r.frequency.write(|w| w.frequency().variant(frequency));
|
|
|
|
|
|
|
|
// Set over-read character
|
|
|
|
let orc = config.orc;
|
|
|
|
r.orc.write(|w|
|
|
|
|
// The ORC field is 8 bits long, so any u8 is a valid value to write.
|
|
|
|
unsafe { w.orc().bits(orc) });
|
|
|
|
|
|
|
|
// Disable all events interrupts
|
|
|
|
r.intenclr.write(|w| unsafe { w.bits(0xFFFF_FFFF) });
|
|
|
|
|
2021-04-14 14:37:10 +00:00
|
|
|
irq.set_handler(Self::on_interrupt);
|
|
|
|
irq.unpend();
|
|
|
|
irq.enable();
|
|
|
|
|
2021-01-18 13:22:55 +00:00
|
|
|
Self {
|
2021-03-19 03:08:44 +00:00
|
|
|
phantom: PhantomData,
|
2021-01-18 13:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-14 14:37:10 +00:00
|
|
|
|
|
|
|
fn on_interrupt(_: *mut ()) {
|
|
|
|
let r = T::regs();
|
|
|
|
let s = T::state();
|
|
|
|
|
|
|
|
if r.events_end.read().bits() != 0 {
|
|
|
|
s.end_waker.wake();
|
|
|
|
r.intenclr.write(|w| w.end().clear());
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 00:27:30 +00:00
|
|
|
}
|
|
|
|
|
2021-03-19 03:08:44 +00:00
|
|
|
impl<'d, T: Instance> FullDuplex<u8> for Spim<'d, T> {
|
2021-03-18 00:27:30 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type WriteFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type ReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
#[rustfmt::skip]
|
|
|
|
type WriteReadFuture<'a> where Self: 'a = impl Future<Output = Result<(), Self::Error>> + 'a;
|
|
|
|
|
2021-04-14 14:37:10 +00:00
|
|
|
fn read<'a>(&'a mut self, data: &'a mut [u8]) -> Self::ReadFuture<'a> {
|
2021-03-20 02:38:21 +00:00
|
|
|
self.read_write(data, &[])
|
2021-03-18 00:27:30 +00:00
|
|
|
}
|
2021-04-14 14:37:10 +00:00
|
|
|
fn write<'a>(&'a mut self, data: &'a [u8]) -> Self::WriteFuture<'a> {
|
2021-03-20 02:38:21 +00:00
|
|
|
self.read_write(&mut [], data)
|
2021-03-18 00:27:30 +00:00
|
|
|
}
|
2021-01-18 13:22:55 +00:00
|
|
|
|
2021-04-14 14:37:10 +00:00
|
|
|
fn read_write<'a>(&'a mut self, rx: &'a mut [u8], tx: &'a [u8]) -> Self::WriteReadFuture<'a> {
|
2021-01-18 13:22:55 +00:00
|
|
|
async move {
|
|
|
|
slice_in_ram_or(rx, Error::DMABufferNotInDataMemory)?;
|
2021-03-18 00:27:30 +00:00
|
|
|
slice_in_ram_or(tx, Error::DMABufferNotInDataMemory)?;
|
2021-01-18 13:22:55 +00:00
|
|
|
|
2021-03-20 02:38:21 +00:00
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// before any DMA action has started.
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
2021-04-14 14:37:10 +00:00
|
|
|
let r = T::regs();
|
|
|
|
let s = T::state();
|
2021-03-20 02:38:21 +00:00
|
|
|
|
|
|
|
// Set up the DMA write.
|
|
|
|
r.txd
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.ptr().bits(tx.as_ptr() as u32) });
|
|
|
|
r.txd
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.maxcnt().bits(tx.len() as _) });
|
|
|
|
|
|
|
|
// Set up the DMA read.
|
|
|
|
r.rxd
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.ptr().bits(rx.as_mut_ptr() as u32) });
|
|
|
|
r.rxd
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.maxcnt().bits(rx.len() as _) });
|
|
|
|
|
|
|
|
// Reset and enable the event
|
|
|
|
r.events_end.reset();
|
|
|
|
r.intenset.write(|w| w.end().set());
|
|
|
|
|
|
|
|
// Start SPI transaction.
|
|
|
|
r.tasks_start.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// after all possible DMA actions have completed.
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
2021-01-18 13:22:55 +00:00
|
|
|
|
|
|
|
// Wait for 'end' event.
|
|
|
|
poll_fn(|cx| {
|
2021-04-14 14:37:10 +00:00
|
|
|
s.end_waker.register(cx.waker());
|
2021-03-20 02:38:21 +00:00
|
|
|
if r.events_end.read().bits() != 0 {
|
|
|
|
return Poll::Ready(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
2021-01-18 13:22:55 +00:00
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 13:50:28 +00:00
|
|
|
// Blocking functions are provided by implementing `embedded_hal` traits.
|
|
|
|
//
|
|
|
|
// Code could be shared between traits to reduce code size.
|
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spim<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
|
|
|
|
slice_in_ram_or(words, Error::DMABufferNotInDataMemory)?;
|
|
|
|
|
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// before any DMA action has started.
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
// Set up the DMA write.
|
|
|
|
r.txd
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.ptr().bits(words.as_ptr() as u32) });
|
|
|
|
r.txd
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.maxcnt().bits(words.len() as _) });
|
|
|
|
|
|
|
|
// Set up the DMA read.
|
|
|
|
r.rxd
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.ptr().bits(words.as_mut_ptr() as u32) });
|
|
|
|
r.rxd
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.maxcnt().bits(words.len() as _) });
|
|
|
|
|
2021-05-05 17:25:14 +00:00
|
|
|
// Disable the end event since we are busy-polling.
|
2021-05-05 13:50:28 +00:00
|
|
|
r.events_end.reset();
|
|
|
|
|
|
|
|
// Start SPI transaction.
|
|
|
|
r.tasks_start.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
2021-05-05 17:25:14 +00:00
|
|
|
// Wait for 'end' event.
|
|
|
|
while r.events_end.read().bits() == 0 {}
|
|
|
|
|
2021-05-05 13:50:28 +00:00
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// after all possible DMA actions have completed.
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
Ok(words)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spim<'d, T> {
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
|
|
|
|
slice_in_ram_or(words, Error::DMABufferNotInDataMemory)?;
|
2021-05-17 09:48:58 +00:00
|
|
|
let recv: &mut [u8] = &mut [];
|
2021-05-05 13:50:28 +00:00
|
|
|
|
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// before any DMA action has started.
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
let r = T::regs();
|
|
|
|
|
|
|
|
// Set up the DMA write.
|
|
|
|
r.txd
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.ptr().bits(words.as_ptr() as u32) });
|
|
|
|
r.txd
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.maxcnt().bits(words.len() as _) });
|
|
|
|
|
|
|
|
// Set up the DMA read.
|
|
|
|
r.rxd
|
|
|
|
.ptr
|
|
|
|
.write(|w| unsafe { w.ptr().bits(recv.as_mut_ptr() as u32) });
|
|
|
|
r.rxd
|
|
|
|
.maxcnt
|
|
|
|
.write(|w| unsafe { w.maxcnt().bits(recv.len() as _) });
|
|
|
|
|
2021-05-05 18:18:57 +00:00
|
|
|
// Disable the end event since we are busy-polling.
|
2021-05-05 13:50:28 +00:00
|
|
|
r.events_end.reset();
|
|
|
|
|
|
|
|
// Start SPI transaction.
|
|
|
|
r.tasks_start.write(|w| unsafe { w.bits(1) });
|
|
|
|
|
2021-05-05 18:18:57 +00:00
|
|
|
// Wait for 'end' event.
|
|
|
|
while r.events_end.read().bits() == 0 {}
|
|
|
|
|
2021-05-05 13:50:28 +00:00
|
|
|
// Conservative compiler fence to prevent optimizations that do not
|
|
|
|
// take in to account actions by DMA. The fence has been placed here,
|
|
|
|
// after all possible DMA actions have completed.
|
|
|
|
compiler_fence(Ordering::SeqCst);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 01:04:59 +00:00
|
|
|
pub(crate) mod sealed {
|
2021-03-18 19:56:10 +00:00
|
|
|
use super::*;
|
|
|
|
|
2021-04-14 14:37:10 +00:00
|
|
|
pub struct State {
|
|
|
|
pub end_waker: AtomicWaker,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl State {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
end_waker: AtomicWaker::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-18 19:56:10 +00:00
|
|
|
pub trait Instance {
|
2021-04-14 14:37:10 +00:00
|
|
|
fn regs() -> &'static pac::spim0::RegisterBlock;
|
|
|
|
fn state() -> &'static State;
|
2021-03-18 19:56:10 +00:00
|
|
|
}
|
2021-01-18 13:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-14 22:05:32 +00:00
|
|
|
pub trait Instance: Unborrow<Target = Self> + sealed::Instance + 'static {
|
2021-01-18 13:22:55 +00:00
|
|
|
type Interrupt: Interrupt;
|
|
|
|
}
|
|
|
|
|
2021-05-11 01:04:59 +00:00
|
|
|
macro_rules! impl_spim {
|
|
|
|
($type:ident, $pac_type:ident, $irq:ident) => {
|
|
|
|
impl crate::spim::sealed::Instance for peripherals::$type {
|
2021-04-14 14:37:10 +00:00
|
|
|
fn regs() -> &'static pac::spim0::RegisterBlock {
|
2021-05-11 01:04:59 +00:00
|
|
|
unsafe { &*pac::$pac_type::ptr() }
|
2021-03-18 19:56:10 +00:00
|
|
|
}
|
2021-05-11 01:04:59 +00:00
|
|
|
fn state() -> &'static crate::spim::sealed::State {
|
|
|
|
static STATE: crate::spim::sealed::State = crate::spim::sealed::State::new();
|
2021-04-14 14:37:10 +00:00
|
|
|
&STATE
|
|
|
|
}
|
2021-03-18 19:56:10 +00:00
|
|
|
}
|
2021-05-11 01:04:59 +00:00
|
|
|
impl crate::spim::Instance for peripherals::$type {
|
|
|
|
type Interrupt = crate::interrupt::$irq;
|
2021-03-18 19:56:10 +00:00
|
|
|
}
|
|
|
|
};
|
2021-01-18 13:22:55 +00:00
|
|
|
}
|