From 20d2151b1d59bb4a1ad2aa4f6697fcc2f259cbd7 Mon Sep 17 00:00:00 2001 From: Grant Miller Date: Mon, 6 Dec 2021 19:12:34 -0600 Subject: [PATCH] check_error_flags function --- embassy-stm32/src/spi/mod.rs | 29 ++++++++++++++++++- embassy-stm32/src/spi/v1.rs | 36 ++++++----------------- embassy-stm32/src/spi/v2.rs | 28 ++++++------------ embassy-stm32/src/spi/v3.rs | 56 +++++++----------------------------- 4 files changed, 56 insertions(+), 93 deletions(-) diff --git a/embassy-stm32/src/spi/mod.rs b/embassy-stm32/src/spi/mod.rs index be4061e0..76857ae6 100644 --- a/embassy-stm32/src/spi/mod.rs +++ b/embassy-stm32/src/spi/mod.rs @@ -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 {} diff --git a/embassy-stm32/src/spi/v1.rs b/embassy-stm32/src/spi/v1.rs index c1a9cdbf..3a7d948a 100644 --- a/embassy-stm32/src/spi/v1.rs +++ b/embassy-stm32/src/spi/v1.rs @@ -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(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(regs: &'static crate::pac::spi::Spi, word: W) -> Result<( fn read_word(regs: &'static crate::pac::spi::Spi) -> Result { 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())); diff --git a/embassy-stm32/src/spi/v2.rs b/embassy-stm32/src/spi/v2.rs index 1ca90619..2f02dc86 100644 --- a/embassy-stm32/src/spi/v2.rs +++ b/embassy-stm32/src/spi/v2.rs @@ -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(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(regs: &'static crate::pac::spi::Spi, word: W) -> Result<( fn read_word(regs: &'static crate::pac::spi::Spi) -> Result { 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())); } diff --git a/embassy-stm32/src/spi/v3.rs b/embassy-stm32/src/spi/v3.rs index 052924db..e63dbcb2 100644 --- a/embassy-stm32/src/spi/v3.rs +++ b/embassy-stm32/src/spi/v3.rs @@ -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 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 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 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 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)