Use unborrow for CRC constructor

sort feature gates
fix repetition in CRC config names
This commit is contained in:
Joshua Salzedo 2021-09-27 10:38:55 -07:00
parent 7392e33ad5
commit 43ad28b9f9
No known key found for this signature in database
GPG Key ID: C3D0EB484493B731
3 changed files with 26 additions and 18 deletions

View File

@ -1,6 +1,6 @@
#[cfg_attr(crc_v2, path = "v2.rs")]
#[cfg_attr(crc_v1, path = "v1.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; mod _version;
pub use _version::Crc; pub use _version::Crc;

View File

@ -1,6 +1,9 @@
use crate::pac::CRC as PAC_CRC; use crate::pac::CRC as PAC_CRC;
use crate::peripherals::CRC; use crate::peripherals::CRC;
use crate::rcc::sealed::RccPeripheral; use crate::rcc::sealed::RccPeripheral;
use embassy_hal_common::unborrow;
use embassy::util::Unborrow;
pub struct Crc { pub struct Crc {
_peripheral: CRC, _peripheral: CRC,
@ -8,12 +11,14 @@ pub struct Crc {
impl Crc { impl Crc {
/// Instantiates the CRC32 peripheral and initializes it to default values. /// Instantiates the CRC32 peripheral and initializes it to default values.
pub fn new(peripheral: CRC) -> Self { pub fn new(peripheral: impl Unborrow<Target= CRC>) -> Self {
// Note: enable and reset come from RccPeripheral. // Note: enable and reset come from RccPeripheral.
// enable CRC clock in RCC. // enable CRC clock in RCC.
CRC::enable(); CRC::enable();
// Reset CRC to default values. // Reset CRC to default values.
CRC::reset(); CRC::reset();
// Unborrow the peripheral
unborrow!(peripheral);
let mut instance = Self { let mut instance = Self {
_peripheral: peripheral, _peripheral: peripheral,
}; };

View File

@ -2,18 +2,20 @@ use crate::pac::crc::vals;
use crate::pac::CRC as PAC_CRC; use crate::pac::CRC as PAC_CRC;
use crate::peripherals::CRC; use crate::peripherals::CRC;
use crate::rcc::sealed::RccPeripheral; use crate::rcc::sealed::RccPeripheral;
use embassy_hal_common::unborrow;
use embassy::util::Unborrow;
pub struct Crc { pub struct Crc {
_peripheral: CRC, _peripheral: CRC,
_config: CrcConfig, _config: Config,
} }
pub enum CrcConfigError { pub enum ConfigError {
InvalidPolynomial, InvalidPolynomial,
} }
pub struct CrcConfig { pub struct Config {
reverse_in: CrcInputReverseConfig, reverse_in: InputReverseConfig,
reverse_out: bool, reverse_out: bool,
#[cfg(crc_v3)] #[cfg(crc_v3)]
poly_size: PolySize, poly_size: PolySize,
@ -22,27 +24,27 @@ pub struct CrcConfig {
crc_poly: u32, crc_poly: u32,
} }
pub enum CrcInputReverseConfig { pub enum InputReverseConfig {
None, None,
Byte, Byte,
Halfword, Halfword,
Word, Word,
} }
impl CrcConfig { impl Config {
pub fn new( pub fn new(
reverse_in: CrcInputReverseConfig, reverse_in: InputReverseConfig,
reverse_out: bool, reverse_out: bool,
#[cfg(crc_v3)] poly_size: PolySize, #[cfg(crc_v3)] poly_size: PolySize,
crc_init_value: u32, crc_init_value: u32,
#[cfg(crc_v3)] crc_poly: u32, #[cfg(crc_v3)] crc_poly: u32,
) -> Result<Self, CrcConfigError> { ) -> Result<Self, ConfigError> {
// As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported. // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported.
#[cfg(crc_v3)] #[cfg(crc_v3)]
if crc_poly % 2 == 0 { if crc_poly % 2 == 0 {
return Err(CrcConfigError::InvalidPolynomial); return Err(ConfigError::InvalidPolynomial);
} }
Ok(CrcConfig { Ok(Config {
reverse_in, reverse_in,
reverse_out, reverse_out,
#[cfg(crc_v3)] #[cfg(crc_v3)]
@ -64,12 +66,13 @@ pub enum PolySize {
impl Crc { impl Crc {
/// Instantiates the CRC32 peripheral and initializes it to default values. /// Instantiates the CRC32 peripheral and initializes it to default values.
pub fn new(peripheral: CRC, config: CrcConfig) -> Self { pub fn new(peripheral: impl Unborrow<Target= CRC>, config: Config) -> Self {
// Note: enable and reset come from RccPeripheral. // Note: enable and reset come from RccPeripheral.
// enable CRC clock in RCC. // enable CRC clock in RCC.
CRC::enable(); CRC::enable();
// Reset CRC to default values. // Reset CRC to default values.
CRC::reset(); CRC::reset();
unborrow!(peripheral);
let mut instance = Self { let mut instance = Self {
_peripheral: peripheral, _peripheral: peripheral,
_config: config, _config: config,
@ -104,10 +107,10 @@ impl Crc {
}); });
// configure reverse input // configure reverse input
w.set_rev_in(match self._config.reverse_in { w.set_rev_in(match self._config.reverse_in {
CrcInputReverseConfig::None => vals::RevIn::NORMAL, InputReverseConfig::None => vals::RevIn::NORMAL,
CrcInputReverseConfig::Byte => vals::RevIn::BYTE, InputReverseConfig::Byte => vals::RevIn::BYTE,
CrcInputReverseConfig::Halfword => vals::RevIn::HALFWORD, InputReverseConfig::Halfword => vals::RevIn::HALFWORD,
CrcInputReverseConfig::Word => vals::RevIn::WORD, InputReverseConfig::Word => vals::RevIn::WORD,
}); });
// configure the polynomial. // configure the polynomial.
#[cfg(crc_v3)] #[cfg(crc_v3)]