From 642b0825a62a7d40c3496c32ac6abd8dce3e55bd Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 19:14:08 -0700 Subject: [PATCH] V3 is just an extension of V2, merge modules. --- embassy-stm32/src/crc/v2.rs | 144 ++++++++++++++++++++++++++---------- embassy-stm32/src/crc/v3.rs | 1 - 2 files changed, 106 insertions(+), 39 deletions(-) delete mode 100644 embassy-stm32/src/crc/v3.rs diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs index b21e6c63..3336d3f7 100644 --- a/embassy-stm32/src/crc/v2.rs +++ b/embassy-stm32/src/crc/v2.rs @@ -1,5 +1,5 @@ -use crate::pac::CRC as PAC_CRC; use crate::pac::crc::vals; +use crate::pac::CRC as PAC_CRC; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; @@ -9,14 +9,16 @@ pub struct Crc { } pub enum CrcConfigError { - InvalidPolynomial + InvalidPolynomial, } pub struct CrcConfig { reverse_in: CrcInputReverseConfig, reverse_out: bool, + #[cfg(crc_v3)] poly_size: PolySize, crc_init_value: u32, + #[cfg(crc_v3)] crc_poly: u32, } @@ -28,16 +30,33 @@ pub enum CrcInputReverseConfig { } impl CrcConfig { - pub fn new(reverse_in: CrcInputReverseConfig, reverse_out: bool, poly_size: PolySize, crc_init_value: u32, crc_poly: u32) -> Result { + pub fn new( + reverse_in: CrcInputReverseConfig, + reverse_out: bool, + #[cfg(crc_v3)] + poly_size: PolySize, + crc_init_value: u32, + #[cfg(crc_v3)] + crc_poly: u32, + ) -> Result { // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported. + #[cfg(crc_v3)] if crc_poly % 2 == 0 { - Err(CrcConfigError::InvalidPolynomial) - } else { - Ok(CrcConfig { reverse_in, reverse_out, poly_size, crc_init_value, crc_poly }) + return Err(CrcConfigError::InvalidPolynomial); } + Ok(CrcConfig { + reverse_in, + reverse_out, + #[cfg(crc_v3)] + poly_size, + crc_init_value, + #[cfg(crc_v3)] + crc_poly, + }) } } +#[cfg(crc_v3)] pub enum PolySize { Width7, Width8, @@ -47,7 +66,7 @@ pub enum PolySize { impl Crc { /// Instantiates the CRC32 peripheral and initializes it to default values. - pub fn new(peripheral: CRC, init_value: u32, config: CrcConfig) -> Self { + pub fn new(peripheral: CRC, config: CrcConfig) -> Self { // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); @@ -57,53 +76,102 @@ impl Crc { _peripheral: peripheral, _config: config, }; - unimplemented!(); - // instance.init(); - // instance + CRC::reset(); + instance.reconfigure(); + instance.reset(); + instance } - - pub fn reset(&mut self) { - unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)); } + unsafe { + PAC_CRC.cr().modify(|w| w.set_reset(true)); + } } - + /// Reconfigures the CRC peripheral. Doesn't reset. fn reconfigure(&mut self) { unsafe { // Init CRC value PAC_CRC.init().write_value(self._config.crc_init_value); + #[cfg(crc_v3)] + PAC_CRC.pol().write_value(self._config.crc_poly); + // configure CR components + // (reverse I/O, polysize, poly) PAC_CRC.cr().modify(|w| { // configure reverse output - w.set_rev_out( - match self._config.reverse_out { - true => { vals::RevOut::REVERSED } - false => { vals::RevOut::NORMAL } - } - ); + w.set_rev_out(match self._config.reverse_out { + true => vals::RevOut::REVERSED, + false => vals::RevOut::NORMAL, + }); // 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 } - } - ); + 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, + }); // configure the polynomial. - w.set_polysize( - match self._config.poly_size { - PolySize::Width7 => { vals::Polysize::POLYSIZE7 } - PolySize::Width8 => { vals::Polysize::POLYSIZE8 } - PolySize::Width16 => { vals::Polysize::POLYSIZE16 } - PolySize::Width32 => { vals::Polysize::POLYSIZE32 } - } - ) - + #[cfg(crc_v3)] + w.set_polysize(match self._config.poly_size { + PolySize::Width7 => vals::Polysize::POLYSIZE7, + PolySize::Width8 => vals::Polysize::POLYSIZE8, + PolySize::Width16 => vals::Polysize::POLYSIZE16, + PolySize::Width32 => vals::Polysize::POLYSIZE32, + }); }) } self.reset(); } -} \ No newline at end of file + + /// Feeds a byte into the CRC peripheral. Returns the computed checksum. + pub fn feed_byte(&mut self, byte: u8) -> u32 { + unsafe { + PAC_CRC.dr8().write_value(byte as u32); + PAC_CRC.dr().read() + } + } + + /// Feeds an slice of bytes into the CRC peripheral. Returns the computed checksum. + pub fn feed_bytes(&mut self, bytes: &[u8]) -> u32 { + for byte in bytes { + unsafe { PAC_CRC.dr8().write_value(*byte as u32); } + } + unsafe { + PAC_CRC.dr().read() + } + } + /// Feeds a halfword into the CRC peripheral. Returns the computed checksum. + pub fn feed_halfword(&mut self, byte: u16) -> u32 { + unsafe { + PAC_CRC.dr16().write_value(byte as u32); + PAC_CRC.dr().read() + } + } + /// Feeds an slice of halfwords into the CRC peripheral. Returns the computed checksum. + pub fn feed_halfwords(&mut self, bytes: &[u16]) -> u32 { + for byte in bytes { + unsafe { PAC_CRC.dr16().write_value(*byte as u32); } + } + unsafe { + PAC_CRC.dr().read() + } + } + /// Feeds a halfword into the CRC peripheral. Returns the computed checksum. + pub fn feed_word(&mut self, byte: u32) -> u32 { + unsafe { + PAC_CRC.dr().write_value(byte as u32); + PAC_CRC.dr().read() + } + } + /// Feeds an slice of halfwords into the CRC peripheral. Returns the computed checksum. + pub fn feed_words(&mut self, bytes: &[u32]) -> u32 { + for byte in bytes { + unsafe { PAC_CRC.dr().write_value(*byte as u32); } + } + unsafe { + PAC_CRC.dr().read() + } + } +} diff --git a/embassy-stm32/src/crc/v3.rs b/embassy-stm32/src/crc/v3.rs deleted file mode 100644 index 8b137891..00000000 --- a/embassy-stm32/src/crc/v3.rs +++ /dev/null @@ -1 +0,0 @@ -