check_error_flags function

This commit is contained in:
Grant Miller 2021-12-06 19:12:34 -06:00
parent 496579b48b
commit 20d2151b1d
4 changed files with 56 additions and 93 deletions

View File

@ -3,7 +3,7 @@
use crate::dma;
use crate::gpio::sealed::{AFType, Pin};
use crate::gpio::{AnyPin, NoPin, OptionalPin};
use crate::pac::spi::vals;
use crate::pac::spi::{regs, vals};
use crate::peripherals;
use crate::rcc::RccPeripheral;
use crate::time::Hertz;
@ -374,6 +374,33 @@ impl RegsExt for crate::pac::spi::Spi {
}
}
fn check_error_flags(sr: regs::Sr) -> Result<(), Error> {
if sr.ovr() {
return Err(Error::Overrun);
}
#[cfg(not(any(spi_f1, spi_v3)))]
if sr.fre() {
return Err(Error::Framing);
}
#[cfg(spi_v3)]
if sr.tifre() {
return Err(Error::Framing);
}
if sr.modf() {
return Err(Error::ModeFault);
}
#[cfg(not(spi_v3))]
if sr.crcerr() {
return Err(Error::Crc);
}
#[cfg(spi_v3)]
if sr.crce() {
return Err(Error::Crc);
}
Ok(())
}
trait Word {}
impl Word for u8 {}

View File

@ -1,7 +1,9 @@
#![macro_use]
use crate::dma::NoDma;
use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize};
use crate::spi::{
check_error_flags, Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize,
};
use core::future::Future;
use core::ptr;
use embassy_traits::spi as traits;
@ -263,19 +265,9 @@ use super::Word;
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);
}
#[cfg(not(spi_f1))]
if sr.fre() {
return Err(Error::Framing);
}
if sr.modf() {
return Err(Error::ModeFault);
}
if sr.crcerr() {
return Err(Error::Crc);
}
check_error_flags(sr)?;
if sr.txe() {
unsafe {
ptr::write_volatile(regs.tx_ptr(), word);
@ -289,19 +281,9 @@ fn write_word<W: Word>(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(
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);
}
#[cfg(not(spi_f1))]
if sr.fre() {
return Err(Error::Framing);
}
if sr.modf() {
return Err(Error::ModeFault);
}
if sr.crcerr() {
return Err(Error::Crc);
}
check_error_flags(sr)?;
if sr.rxne() {
unsafe {
return Ok(ptr::read_volatile(regs.rx_ptr()));

View File

@ -1,7 +1,7 @@
#![macro_use]
use crate::dma::NoDma;
use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize};
use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, check_error_flags};
use core::future::Future;
use core::ptr;
use embassy_traits::spi as traits;
@ -180,15 +180,10 @@ use super::Word;
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);
} else if sr.fre() {
return Err(Error::Framing);
} else if sr.modf() {
return Err(Error::ModeFault);
} else if sr.crcerr() {
return Err(Error::Crc);
} else if sr.txe() {
check_error_flags(sr)?;
if sr.txe() {
unsafe {
ptr::write_volatile(regs.tx_ptr(), word);
}
@ -201,15 +196,10 @@ fn write_word<W: Word>(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(
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);
} else if sr.modf() {
return Err(Error::ModeFault);
} else if sr.fre() {
return Err(Error::Framing);
} else if sr.crcerr() {
return Err(Error::Crc);
} else if sr.rxne() {
check_error_flags(sr)?;
if sr.rxne() {
unsafe {
return Ok(ptr::read_volatile(regs.rx_ptr()));
}

View File

@ -1,7 +1,7 @@
#![macro_use]
use crate::dma::NoDma;
use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize};
use crate::spi::{Error, Instance, RegsExt, RxDmaChannel, TxDmaChannel, WordSize, check_error_flags};
use core::future::Future;
use core::ptr;
use embassy_traits::spi as traits;
@ -249,29 +249,14 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u8> for Spi<'d, T, N
if sr.rxp() {
break;
}
if sr.tifre() {
return Err(Error::Framing);
}
if sr.ovr() {
return Err(Error::Overrun);
}
if sr.crce() {
return Err(Error::Crc);
}
check_error_flags(sr)?;
}
unsafe {
*word = ptr::read_volatile(T::regs().rx_ptr());
}
let sr = unsafe { regs.sr().read() };
if sr.tifre() {
return Err(Error::Framing);
}
if sr.ovr() {
return Err(Error::Overrun);
}
if sr.crce() {
return Err(Error::Crc);
}
check_error_flags(sr)?;
}
Ok(words)
@ -296,15 +281,9 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Write<u16> for Spi<'d, T, NoD
}
loop {
let sr = unsafe { regs.sr().read() };
if sr.tifre() {
return Err(Error::Framing);
}
if sr.ovr() {
return Err(Error::Overrun);
}
if sr.crce() {
return Err(Error::Crc);
}
check_error_flags(sr)?;
if !sr.txp() {
// loop waiting for TXE
continue;
@ -350,15 +329,8 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T,
if sr.rxp() {
break;
}
if sr.tifre() {
return Err(Error::Framing);
}
if sr.ovr() {
return Err(Error::Overrun);
}
if sr.crce() {
return Err(Error::Crc);
}
check_error_flags(sr)?;
}
unsafe {
@ -366,15 +338,7 @@ impl<'d, T: Instance> embedded_hal::blocking::spi::Transfer<u16> for Spi<'d, T,
*word = ptr::read_volatile(rxdr);
}
let sr = unsafe { regs.sr().read() };
if sr.tifre() {
return Err(Error::Framing);
}
if sr.ovr() {
return Err(Error::Overrun);
}
if sr.crce() {
return Err(Error::Crc);
}
check_error_flags(sr)?;
}
Ok(words)