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::dma;
use crate::gpio::sealed::{AFType, Pin}; use crate::gpio::sealed::{AFType, Pin};
use crate::gpio::{AnyPin, NoPin, OptionalPin}; use crate::gpio::{AnyPin, NoPin, OptionalPin};
use crate::pac::spi::vals; use crate::pac::spi::{regs, vals};
use crate::peripherals; use crate::peripherals;
use crate::rcc::RccPeripheral; use crate::rcc::RccPeripheral;
use crate::time::Hertz; 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 {} trait Word {}
impl Word for u8 {} impl Word for u8 {}

View File

@ -1,7 +1,9 @@
#![macro_use] #![macro_use]
use crate::dma::NoDma; 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::future::Future;
use core::ptr; use core::ptr;
use embassy_traits::spi as traits; 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> { fn write_word<W: Word>(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(), Error> {
loop { loop {
let sr = unsafe { regs.sr().read() }; let sr = unsafe { regs.sr().read() };
if sr.ovr() {
return Err(Error::Overrun); check_error_flags(sr)?;
}
#[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);
}
if sr.txe() { if sr.txe() {
unsafe { unsafe {
ptr::write_volatile(regs.tx_ptr(), word); 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> { fn read_word<W: Word>(regs: &'static crate::pac::spi::Spi) -> Result<W, Error> {
loop { loop {
let sr = unsafe { regs.sr().read() }; let sr = unsafe { regs.sr().read() };
if sr.ovr() {
return Err(Error::Overrun); check_error_flags(sr)?;
}
#[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);
}
if sr.rxne() { if sr.rxne() {
unsafe { unsafe {
return Ok(ptr::read_volatile(regs.rx_ptr())); return Ok(ptr::read_volatile(regs.rx_ptr()));

View File

@ -1,7 +1,7 @@
#![macro_use] #![macro_use]
use crate::dma::NoDma; 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::future::Future;
use core::ptr; use core::ptr;
use embassy_traits::spi as traits; 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> { fn write_word<W: Word>(regs: &'static crate::pac::spi::Spi, word: W) -> Result<(), Error> {
loop { loop {
let sr = unsafe { regs.sr().read() }; let sr = unsafe { regs.sr().read() };
if sr.ovr() {
return Err(Error::Overrun); check_error_flags(sr)?;
} else if sr.fre() {
return Err(Error::Framing); if sr.txe() {
} else if sr.modf() {
return Err(Error::ModeFault);
} else if sr.crcerr() {
return Err(Error::Crc);
} else if sr.txe() {
unsafe { unsafe {
ptr::write_volatile(regs.tx_ptr(), word); 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> { fn read_word<W: Word>(regs: &'static crate::pac::spi::Spi) -> Result<W, Error> {
loop { loop {
let sr = unsafe { regs.sr().read() }; let sr = unsafe { regs.sr().read() };
if sr.ovr() {
return Err(Error::Overrun); check_error_flags(sr)?;
} else if sr.modf() {
return Err(Error::ModeFault); if sr.rxne() {
} else if sr.fre() {
return Err(Error::Framing);
} else if sr.crcerr() {
return Err(Error::Crc);
} else if sr.rxne() {
unsafe { unsafe {
return Ok(ptr::read_volatile(regs.rx_ptr())); return Ok(ptr::read_volatile(regs.rx_ptr()));
} }

View File

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