From 43ad28b9f9b25f2b7369be33dfd1f7db5d34e330 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Mon, 27 Sep 2021 10:38:55 -0700 Subject: [PATCH] Use unborrow for CRC constructor sort feature gates fix repetition in CRC config names --- embassy-stm32/src/crc/mod.rs | 4 +-- embassy-stm32/src/crc/v1.rs | 7 ++++- embassy-stm32/src/crc/{v2.rs => v2v3.rs} | 33 +++++++++++++----------- 3 files changed, 26 insertions(+), 18 deletions(-) rename embassy-stm32/src/crc/{v2.rs => v2v3.rs} (85%) diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs index 70c87ab5..2ebebc93 100644 --- a/embassy-stm32/src/crc/mod.rs +++ b/embassy-stm32/src/crc/mod.rs @@ -1,6 +1,6 @@ -#[cfg_attr(crc_v2, path = "v2.rs")] #[cfg_attr(crc_v1, path = "v1.rs")] -#[cfg_attr(crc_v3, path = "v2.rs")] +#[cfg_attr(crc_v2, path = "v2v3.rs")] +#[cfg_attr(crc_v3, path = "v2v3.rs")] mod _version; pub use _version::Crc; diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs index 09c8eecb..8431c07f 100644 --- a/embassy-stm32/src/crc/v1.rs +++ b/embassy-stm32/src/crc/v1.rs @@ -1,6 +1,9 @@ use crate::pac::CRC as PAC_CRC; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; +use embassy_hal_common::unborrow; +use embassy::util::Unborrow; + pub struct Crc { _peripheral: CRC, @@ -8,12 +11,14 @@ pub struct Crc { impl Crc { /// Instantiates the CRC32 peripheral and initializes it to default values. - pub fn new(peripheral: CRC) -> Self { + pub fn new(peripheral: impl Unborrow) -> Self { // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); // Reset CRC to default values. CRC::reset(); + // Unborrow the peripheral + unborrow!(peripheral); let mut instance = Self { _peripheral: peripheral, }; diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2v3.rs similarity index 85% rename from embassy-stm32/src/crc/v2.rs rename to embassy-stm32/src/crc/v2v3.rs index 7705b428..91c24215 100644 --- a/embassy-stm32/src/crc/v2.rs +++ b/embassy-stm32/src/crc/v2v3.rs @@ -2,18 +2,20 @@ use crate::pac::crc::vals; use crate::pac::CRC as PAC_CRC; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; +use embassy_hal_common::unborrow; +use embassy::util::Unborrow; pub struct Crc { _peripheral: CRC, - _config: CrcConfig, + _config: Config, } -pub enum CrcConfigError { +pub enum ConfigError { InvalidPolynomial, } -pub struct CrcConfig { - reverse_in: CrcInputReverseConfig, +pub struct Config { + reverse_in: InputReverseConfig, reverse_out: bool, #[cfg(crc_v3)] poly_size: PolySize, @@ -22,27 +24,27 @@ pub struct CrcConfig { crc_poly: u32, } -pub enum CrcInputReverseConfig { +pub enum InputReverseConfig { None, Byte, Halfword, Word, } -impl CrcConfig { +impl Config { pub fn new( - reverse_in: CrcInputReverseConfig, + reverse_in: InputReverseConfig, reverse_out: bool, #[cfg(crc_v3)] poly_size: PolySize, crc_init_value: u32, #[cfg(crc_v3)] crc_poly: u32, - ) -> Result { + ) -> Result { // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported. #[cfg(crc_v3)] if crc_poly % 2 == 0 { - return Err(CrcConfigError::InvalidPolynomial); + return Err(ConfigError::InvalidPolynomial); } - Ok(CrcConfig { + Ok(Config { reverse_in, reverse_out, #[cfg(crc_v3)] @@ -64,12 +66,13 @@ pub enum PolySize { impl Crc { /// Instantiates the CRC32 peripheral and initializes it to default values. - pub fn new(peripheral: CRC, config: CrcConfig) -> Self { + pub fn new(peripheral: impl Unborrow, config: Config) -> Self { // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); // Reset CRC to default values. CRC::reset(); + unborrow!(peripheral); let mut instance = Self { _peripheral: peripheral, _config: config, @@ -104,10 +107,10 @@ impl Crc { }); // configure reverse input w.set_rev_in(match self._config.reverse_in { - CrcInputReverseConfig::None => vals::RevIn::NORMAL, - CrcInputReverseConfig::Byte => vals::RevIn::BYTE, - CrcInputReverseConfig::Halfword => vals::RevIn::HALFWORD, - CrcInputReverseConfig::Word => vals::RevIn::WORD, + InputReverseConfig::None => vals::RevIn::NORMAL, + InputReverseConfig::Byte => vals::RevIn::BYTE, + InputReverseConfig::Halfword => vals::RevIn::HALFWORD, + InputReverseConfig::Word => vals::RevIn::WORD, }); // configure the polynomial. #[cfg(crc_v3)]