2021-05-10 20:17:58 +00:00
|
|
|
#![macro_use]
|
|
|
|
|
2021-05-15 00:39:08 +00:00
|
|
|
use crate::gpio::{sealed::Pin, AnyPin};
|
2021-05-14 14:11:43 +00:00
|
|
|
use crate::pac::spi;
|
|
|
|
use crate::spi::{ByteOrder, Config, Error, Instance, MisoPin, MosiPin, SckPin, WordSize};
|
2021-05-12 14:46:18 +00:00
|
|
|
use crate::time::Hertz;
|
2021-05-14 14:11:43 +00:00
|
|
|
use core::marker::PhantomData;
|
2021-05-20 18:19:43 +00:00
|
|
|
use core::ptr;
|
2021-05-14 14:11:43 +00:00
|
|
|
use embassy::util::Unborrow;
|
|
|
|
use embassy_extras::unborrow;
|
|
|
|
pub use embedded_hal::spi::{Mode, Phase, Polarity, MODE_0, MODE_1, MODE_2, MODE_3};
|
2021-05-12 18:18:42 +00:00
|
|
|
|
|
|
|
impl WordSize {
|
2021-05-13 18:28:53 +00:00
|
|
|
fn dff(&self) -> spi::vals::Dff {
|
2021-05-12 18:18:42 +00:00
|
|
|
match self {
|
2021-05-13 18:28:53 +00:00
|
|
|
WordSize::EightBit => spi::vals::Dff::EIGHTBIT,
|
|
|
|
WordSize::SixteenBit => spi::vals::Dff::SIXTEENBIT,
|
2021-05-12 18:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 14:46:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:10:58 +00:00
|
|
|
pub struct Spi<'d, T: Instance> {
|
2021-05-11 15:25:01 +00:00
|
|
|
sck: AnyPin,
|
|
|
|
mosi: AnyPin,
|
|
|
|
miso: AnyPin,
|
2021-05-13 18:28:53 +00:00
|
|
|
current_word_size: WordSize,
|
2021-05-10 19:21:57 +00:00
|
|
|
phantom: PhantomData<&'d mut T>,
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:10:58 +00:00
|
|
|
impl<'d, T: Instance> Spi<'d, T> {
|
2021-05-14 14:11:43 +00:00
|
|
|
pub fn new<F>(
|
|
|
|
pclk: Hertz,
|
2021-05-20 08:54:10 +00:00
|
|
|
_peri: impl Unborrow<Target = T> + 'd,
|
2021-05-14 14:11:43 +00:00
|
|
|
sck: impl Unborrow<Target = impl SckPin<T>>,
|
|
|
|
mosi: impl Unborrow<Target = impl MosiPin<T>>,
|
|
|
|
miso: impl Unborrow<Target = impl MisoPin<T>>,
|
|
|
|
freq: F,
|
|
|
|
config: Config,
|
2021-05-12 14:46:18 +00:00
|
|
|
) -> Self
|
2021-05-14 14:11:43 +00:00
|
|
|
where
|
|
|
|
F: Into<Hertz>,
|
2021-05-12 14:46:18 +00:00
|
|
|
{
|
2021-05-20 08:54:10 +00:00
|
|
|
unborrow!(sck, mosi, miso);
|
2021-05-11 15:25:01 +00:00
|
|
|
|
|
|
|
unsafe {
|
2021-05-15 01:07:37 +00:00
|
|
|
sck.set_as_af(sck.af_num());
|
|
|
|
mosi.set_as_af(mosi.af_num());
|
|
|
|
miso.set_as_af(miso.af_num());
|
2021-05-11 15:25:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let sck = sck.degrade();
|
|
|
|
let mosi = mosi.degrade();
|
|
|
|
let miso = miso.degrade();
|
|
|
|
|
2021-05-12 14:46:18 +00:00
|
|
|
unsafe {
|
2021-05-20 18:14:31 +00:00
|
|
|
T::regs().cr2().modify(|w| {
|
2021-05-14 14:11:43 +00:00
|
|
|
w.set_ssoe(false);
|
|
|
|
});
|
2021-05-12 14:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let br = Self::compute_baud_rate(pclk, freq.into());
|
|
|
|
|
|
|
|
unsafe {
|
2021-06-08 09:04:44 +00:00
|
|
|
T::enable();
|
|
|
|
T::reset();
|
2021-05-20 18:14:31 +00:00
|
|
|
T::regs().cr1().modify(|w| {
|
2021-05-12 14:46:18 +00:00
|
|
|
w.set_cpha(
|
2021-05-12 18:18:42 +00:00
|
|
|
match config.mode.phase == Phase::CaptureOnSecondTransition {
|
2021-05-12 14:46:18 +00:00
|
|
|
true => spi::vals::Cpha::SECONDEDGE,
|
|
|
|
false => spi::vals::Cpha::FIRSTEDGE,
|
2021-05-14 14:11:43 +00:00
|
|
|
},
|
2021-05-12 14:46:18 +00:00
|
|
|
);
|
2021-05-12 18:18:42 +00:00
|
|
|
w.set_cpol(match config.mode.polarity == Polarity::IdleHigh {
|
2021-05-12 14:46:18 +00:00
|
|
|
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);
|
2021-05-14 14:11:43 +00:00
|
|
|
w.set_lsbfirst(match config.byte_order {
|
|
|
|
ByteOrder::LsbFirst => spi::vals::Lsbfirst::LSBFIRST,
|
|
|
|
ByteOrder::MsbFirst => spi::vals::Lsbfirst::MSBFIRST,
|
|
|
|
});
|
2021-05-12 14:46:18 +00:00
|
|
|
w.set_ssi(true);
|
|
|
|
w.set_ssm(true);
|
|
|
|
w.set_crcen(false);
|
|
|
|
w.set_bidimode(spi::vals::Bidimode::UNIDIRECTIONAL);
|
2021-05-14 14:11:43 +00:00
|
|
|
w.set_dff(WordSize::EightBit.dff())
|
2021-05-12 14:46:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-11 15:25:01 +00:00
|
|
|
Self {
|
|
|
|
sck,
|
|
|
|
mosi,
|
|
|
|
miso,
|
2021-05-13 18:28:53 +00:00
|
|
|
current_word_size: WordSize::EightBit,
|
2021-05-11 15:25:01 +00:00
|
|
|
phantom: PhantomData,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 14:46:18 +00:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 18:18:42 +00:00
|
|
|
|
2021-05-13 18:28:53 +00:00
|
|
|
fn set_word_size(&mut self, word_size: WordSize) {
|
|
|
|
if self.current_word_size == word_size {
|
2021-05-14 14:11:43 +00:00
|
|
|
return;
|
2021-05-13 18:28:53 +00:00
|
|
|
}
|
2021-05-12 18:18:42 +00:00
|
|
|
unsafe {
|
2021-05-14 14:11:43 +00:00
|
|
|
T::regs().cr1().modify(|reg| {
|
2021-05-13 18:28:53 +00:00
|
|
|
reg.set_spe(false);
|
2021-05-14 14:11:43 +00:00
|
|
|
reg.set_dff(word_size.dff())
|
2021-05-13 18:28:53 +00:00
|
|
|
});
|
2021-05-14 14:11:43 +00:00
|
|
|
T::regs().cr1().modify(|reg| {
|
2021-05-13 18:28:53 +00:00
|
|
|
reg.set_spe(true);
|
|
|
|
});
|
|
|
|
self.current_word_size = word_size;
|
2021-05-12 18:18:42 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-12 14:46:18 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:10:58 +00:00
|
|
|
impl<'d, T: Instance> Drop for Spi<'d, T> {
|
2021-05-12 14:46:18 +00:00
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe {
|
2021-05-15 00:39:08 +00:00
|
|
|
self.sck.set_as_analog();
|
|
|
|
self.mosi.set_as_analog();
|
|
|
|
self.miso.set_as_analog();
|
2021-05-12 14:46:18 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-11 15:25:01 +00:00
|
|
|
}
|
|
|
|
|
2021-06-08 11:10:58 +00:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u8> for Spi<'d, T> {
|
2021-05-10 19:21:57 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn write(&mut self, words: &[u8]) -> Result<(), Self::Error> {
|
2021-05-13 18:28:53 +00:00
|
|
|
self.set_word_size(WordSize::EightBit);
|
2021-05-10 20:17:58 +00:00
|
|
|
let regs = T::regs();
|
2021-05-10 19:21:57 +00:00
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
for word in words.iter() {
|
|
|
|
while unsafe { !regs.sr().read().txe() } {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
unsafe {
|
2021-05-20 18:24:40 +00:00
|
|
|
let dr = regs.dr().ptr() as *mut u8;
|
2021-05-20 18:19:43 +00:00
|
|
|
ptr::write_volatile(dr, *word);
|
2021-05-10 20:17:58 +00:00
|
|
|
}
|
|
|
|
loop {
|
|
|
|
let sr = unsafe { regs.sr().read() };
|
|
|
|
if sr.fre() {
|
|
|
|
return Err(Error::Framing);
|
|
|
|
}
|
|
|
|
if sr.ovr() {
|
|
|
|
return Err(Error::Overrun);
|
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
|
|
|
return Err(Error::Crc);
|
|
|
|
}
|
|
|
|
if !sr.txe() {
|
|
|
|
// loop waiting for TXE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:10:58 +00:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spi<'d, T> {
|
2021-05-10 20:17:58 +00:00
|
|
|
type Error = Error;
|
2021-05-10 19:21:57 +00:00
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
fn transfer<'w>(&mut self, words: &'w mut [u8]) -> Result<&'w [u8], Self::Error> {
|
2021-05-13 18:28:53 +00:00
|
|
|
self.set_word_size(WordSize::EightBit);
|
2021-05-10 20:17:58 +00:00
|
|
|
let regs = T::regs();
|
2021-05-10 19:21:57 +00:00
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
for word in words.iter_mut() {
|
|
|
|
while unsafe { !regs.sr().read().txe() } {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
unsafe {
|
2021-05-20 18:24:40 +00:00
|
|
|
let dr = regs.dr().ptr() as *mut u8;
|
2021-05-20 18:19:43 +00:00
|
|
|
ptr::write_volatile(dr, *word);
|
2021-05-10 20:17:58 +00:00
|
|
|
}
|
2021-05-20 18:13:45 +00:00
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
while unsafe { !regs.sr().read().rxne() } {
|
|
|
|
// spin waiting for inbound to shift in.
|
|
|
|
}
|
2021-05-20 18:13:45 +00:00
|
|
|
|
|
|
|
unsafe {
|
|
|
|
let dr = regs.dr().ptr() as *const u8;
|
2021-05-20 18:19:43 +00:00
|
|
|
*word = ptr::read_volatile(dr);
|
2021-05-20 18:13:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 18:18:42 +00:00
|
|
|
let sr = unsafe { regs.sr().read() };
|
|
|
|
if sr.fre() {
|
|
|
|
return Err(Error::Framing);
|
|
|
|
}
|
|
|
|
if sr.ovr() {
|
|
|
|
return Err(Error::Overrun);
|
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
|
|
|
return Err(Error::Crc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(words)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:10:58 +00:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u16> for Spi<'d, T> {
|
2021-05-12 18:18:42 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn write(&mut self, words: &[u16]) -> Result<(), Self::Error> {
|
2021-05-13 18:28:53 +00:00
|
|
|
self.set_word_size(WordSize::SixteenBit);
|
2021-05-12 18:18:42 +00:00
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
for word in words.iter() {
|
|
|
|
while unsafe { !regs.sr().read().txe() } {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
unsafe {
|
2021-05-20 18:24:40 +00:00
|
|
|
let dr = regs.dr().ptr() as *mut u16;
|
2021-05-20 18:19:43 +00:00
|
|
|
ptr::write_volatile(dr, *word);
|
2021-05-12 18:18:42 +00:00
|
|
|
}
|
2021-05-10 20:17:58 +00:00
|
|
|
loop {
|
|
|
|
let sr = unsafe { regs.sr().read() };
|
|
|
|
if sr.fre() {
|
|
|
|
return Err(Error::Framing);
|
|
|
|
}
|
|
|
|
if sr.ovr() {
|
|
|
|
return Err(Error::Overrun);
|
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
|
|
|
return Err(Error::Crc);
|
|
|
|
}
|
|
|
|
if !sr.txe() {
|
|
|
|
// loop waiting for TXE
|
|
|
|
}
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-10 20:17:58 +00:00
|
|
|
|
2021-05-12 18:18:42 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 11:10:58 +00:00
|
|
|
impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T> {
|
2021-05-12 18:18:42 +00:00
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn transfer<'w>(&mut self, words: &'w mut [u16]) -> Result<&'w [u16], Self::Error> {
|
2021-05-13 18:28:53 +00:00
|
|
|
self.set_word_size(WordSize::SixteenBit);
|
2021-05-12 18:18:42 +00:00
|
|
|
let regs = T::regs();
|
|
|
|
|
|
|
|
for word in words.iter_mut() {
|
|
|
|
while unsafe { !regs.sr().read().txe() } {
|
|
|
|
// spin
|
|
|
|
}
|
|
|
|
unsafe {
|
2021-05-20 18:24:40 +00:00
|
|
|
let dr = regs.dr().ptr() as *mut u16;
|
2021-05-20 18:19:43 +00:00
|
|
|
ptr::write_volatile(dr, *word);
|
2021-05-12 18:18:42 +00:00
|
|
|
}
|
|
|
|
while unsafe { !regs.sr().read().rxne() } {
|
|
|
|
// spin waiting for inbound to shift in.
|
|
|
|
}
|
2021-05-20 18:13:45 +00:00
|
|
|
unsafe {
|
|
|
|
let dr = regs.dr().ptr() as *const u16;
|
2021-05-20 18:19:43 +00:00
|
|
|
*word = ptr::read_volatile(dr);
|
2021-05-20 18:13:45 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 18:18:42 +00:00
|
|
|
let sr = unsafe { regs.sr().read() };
|
|
|
|
if sr.fre() {
|
|
|
|
return Err(Error::Framing);
|
|
|
|
}
|
|
|
|
if sr.ovr() {
|
|
|
|
return Err(Error::Overrun);
|
|
|
|
}
|
|
|
|
if sr.crcerr() {
|
|
|
|
return Err(Error::Crc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-10 20:17:58 +00:00
|
|
|
Ok(words)
|
2021-05-10 19:21:57 +00:00
|
|
|
}
|
2021-05-10 20:17:58 +00:00
|
|
|
}
|