From e527892d89ff14b6af4aad778945c4675423fa77 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 16:29:22 -0700 Subject: [PATCH 01/16] Start work on CRC_v1 --- embassy-stm32/src/crc/mod.rs | 4 ++++ embassy-stm32/src/crc/v1.rs | 21 +++++++++++++++++++++ embassy-stm32/src/crc/v2.rs | 0 embassy-stm32/src/crc/v3.rs | 0 embassy-stm32/src/lib.rs | 2 ++ 5 files changed, 27 insertions(+) create mode 100644 embassy-stm32/src/crc/mod.rs create mode 100644 embassy-stm32/src/crc/v1.rs create mode 100644 embassy-stm32/src/crc/v2.rs create mode 100644 embassy-stm32/src/crc/v3.rs diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs new file mode 100644 index 000000000..79e00be73 --- /dev/null +++ b/embassy-stm32/src/crc/mod.rs @@ -0,0 +1,4 @@ +#[cfg_attr(crc_v1, path = "v1.rs")] +#[cfg_attr(crc_v2, path = "v2.rs")] +#[cfg_attr(crc_v3, path = "v3.rs")] +mod _version; \ No newline at end of file diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs new file mode 100644 index 000000000..4fc23fe39 --- /dev/null +++ b/embassy-stm32/src/crc/v1.rs @@ -0,0 +1,21 @@ +use crate::pac::{CRC as PAC_CRC, RCC}; +use crate::peripherals::CRC; +use crate::rcc::sealed::RccPeripheral; + +pub struct Crc { + _peripheral: CRC +} + +impl Crc{ + pub fn new(peripheral: CRC) -> Self{ + // enable CRC clock in RCC. + CRC::enable(); + // Reset CRC to default values. + CRC::reset(); + Self { _peripheral: peripheral} + } + + pub fn reset() { + unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)) }; + } +} \ No newline at end of file diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs new file mode 100644 index 000000000..e69de29bb diff --git a/embassy-stm32/src/crc/v3.rs b/embassy-stm32/src/crc/v3.rs new file mode 100644 index 000000000..e69de29bb diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 073e79f2f..711c29971 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -49,6 +49,8 @@ pub mod sdmmc; pub mod spi; #[cfg(usart)] pub mod usart; +#[cfg(crc)] +pub mod crc; #[cfg(feature = "subghz")] pub mod subghz; From e18a27eea2aca005f01fdde72162b14e976f358a Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 16:46:17 -0700 Subject: [PATCH 02/16] First pass at CRC_V1 --- embassy-stm32/src/crc/mod.rs | 2 +- embassy-stm32/src/crc/v1.rs | 47 ++++++++++++++++++++++++++++++------ embassy-stm32/src/crc/v2.rs | 1 + embassy-stm32/src/crc/v3.rs | 1 + embassy-stm32/src/lib.rs | 4 +-- 5 files changed, 45 insertions(+), 10 deletions(-) diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs index 79e00be73..02fa1278b 100644 --- a/embassy-stm32/src/crc/mod.rs +++ b/embassy-stm32/src/crc/mod.rs @@ -1,4 +1,4 @@ #[cfg_attr(crc_v1, path = "v1.rs")] #[cfg_attr(crc_v2, path = "v2.rs")] #[cfg_attr(crc_v3, path = "v3.rs")] -mod _version; \ No newline at end of file +mod _version; diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs index 4fc23fe39..52ccca660 100644 --- a/embassy-stm32/src/crc/v1.rs +++ b/embassy-stm32/src/crc/v1.rs @@ -1,21 +1,54 @@ -use crate::pac::{CRC as PAC_CRC, RCC}; +use crate::pac::CRC as PAC_CRC; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; pub struct Crc { - _peripheral: CRC + _peripheral: CRC, } -impl Crc{ - pub fn new(peripheral: CRC) -> Self{ +impl Crc { + /// Instantiates the CRC32 peripheral and initializes it to default values. + pub fn new(peripheral: CRC) -> Self { + // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); // Reset CRC to default values. CRC::reset(); - Self { _peripheral: peripheral} + let mut instance = Self { + _peripheral: peripheral, + }; + instance.init(); + instance } - pub fn reset() { + /// Resets the CRC unit to default value (0xFFFF_FFFF) + pub fn init(&mut self) { unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)) }; } -} \ No newline at end of file + + /// Feeds a word to the peripheral and returns the current CRC value + pub fn feed_word(&mut self, word: u32) -> u32 { + // write a single byte to the device, and return the result + unsafe { + PAC_CRC.dr().write_value(word); + PAC_CRC.dr().read() + } + } + /// Feed a slice of words to the peripheral and return the result. + pub fn feed_words(&mut self, words: &[u32]) -> u32 { + for word in words { + unsafe { + PAC_CRC.dr().write_value(*word); + } + } + + unsafe { PAC_CRC.dr().read() } + } + + /// Reclaims the CRC peripheral. + pub fn release(self) -> CRC { + CRC::disable(); + + self._peripheral + } +} diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs index e69de29bb..8b1378917 100644 --- a/embassy-stm32/src/crc/v2.rs +++ b/embassy-stm32/src/crc/v2.rs @@ -0,0 +1 @@ + diff --git a/embassy-stm32/src/crc/v3.rs b/embassy-stm32/src/crc/v3.rs index e69de29bb..8b1378917 100644 --- a/embassy-stm32/src/crc/v3.rs +++ b/embassy-stm32/src/crc/v3.rs @@ -0,0 +1 @@ + diff --git a/embassy-stm32/src/lib.rs b/embassy-stm32/src/lib.rs index 711c29971..424b1c994 100644 --- a/embassy-stm32/src/lib.rs +++ b/embassy-stm32/src/lib.rs @@ -39,6 +39,8 @@ pub mod exti; #[cfg(i2c)] pub mod i2c; +#[cfg(crc)] +pub mod crc; #[cfg(pwr)] pub mod pwr; #[cfg(rng)] @@ -49,8 +51,6 @@ pub mod sdmmc; pub mod spi; #[cfg(usart)] pub mod usart; -#[cfg(crc)] -pub mod crc; #[cfg(feature = "subghz")] pub mod subghz; From 4760afd9f44738603fee36f49a9e0e29bf784889 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 16:53:32 -0700 Subject: [PATCH 03/16] Update stm32-data --- stm32-data | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stm32-data b/stm32-data index 8cdca8ad5..ded0ac053 160000 --- a/stm32-data +++ b/stm32-data @@ -1 +1 @@ -Subproject commit 8cdca8ad5b65d9a63c5198370ea337ddd03f32b1 +Subproject commit ded0ac053d88db503bc395d8dbdbf6e7550b6859 From 24dea91f5abdd4029c7c912dd01d92313da58df3 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 17:24:48 -0700 Subject: [PATCH 04/16] Fix interface changes --- embassy-stm32/src/crc/v1.rs | 6 +++--- stm32-data | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs index 52ccca660..b95df8838 100644 --- a/embassy-stm32/src/crc/v1.rs +++ b/embassy-stm32/src/crc/v1.rs @@ -1,4 +1,4 @@ -use crate::pac::CRC as PAC_CRC; +use crate::pac::{CRC as PAC_CRC}; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; @@ -23,7 +23,7 @@ impl Crc { /// Resets the CRC unit to default value (0xFFFF_FFFF) pub fn init(&mut self) { - unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)) }; + unsafe { PAC_CRC.cr().write(|w| w.set_reset(true)) }; } /// Feeds a word to the peripheral and returns the current CRC value @@ -38,7 +38,7 @@ impl Crc { pub fn feed_words(&mut self, words: &[u32]) -> u32 { for word in words { unsafe { - PAC_CRC.dr().write_value(*word); + PAC_CRC.dr().write_value(*word) } } diff --git a/stm32-data b/stm32-data index ded0ac053..9e8e34786 160000 --- a/stm32-data +++ b/stm32-data @@ -1 +1 @@ -Subproject commit ded0ac053d88db503bc395d8dbdbf6e7550b6859 +Subproject commit 9e8e34786222b76a7d1bbbc1baf14cd3b33cd2d7 From c892289b2c11b0c707ef2578583a249b05f779b2 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 17:26:33 -0700 Subject: [PATCH 05/16] Actually export CRC --- embassy-stm32/src/crc/mod.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs index 02fa1278b..d8de4bd09 100644 --- a/embassy-stm32/src/crc/mod.rs +++ b/embassy-stm32/src/crc/mod.rs @@ -2,3 +2,5 @@ #[cfg_attr(crc_v2, path = "v2.rs")] #[cfg_attr(crc_v3, path = "v3.rs")] mod _version; + +pub use _version::Crc; From 7899d733590ad373e0dea215d0b084f32412a816 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 17:28:58 -0700 Subject: [PATCH 06/16] Expose read so the value can be obtained without a write. --- embassy-stm32/src/crc/v1.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs index b95df8838..09c8eecb6 100644 --- a/embassy-stm32/src/crc/v1.rs +++ b/embassy-stm32/src/crc/v1.rs @@ -1,4 +1,4 @@ -use crate::pac::{CRC as PAC_CRC}; +use crate::pac::CRC as PAC_CRC; use crate::peripherals::CRC; use crate::rcc::sealed::RccPeripheral; @@ -31,17 +31,18 @@ impl Crc { // write a single byte to the device, and return the result unsafe { PAC_CRC.dr().write_value(word); - PAC_CRC.dr().read() } + self.read() } /// Feed a slice of words to the peripheral and return the result. pub fn feed_words(&mut self, words: &[u32]) -> u32 { for word in words { - unsafe { - PAC_CRC.dr().write_value(*word) - } + unsafe { PAC_CRC.dr().write_value(*word) } } + self.read() + } + pub fn read(&self) -> u32 { unsafe { PAC_CRC.dr().read() } } From afef19d813d7301631e98f432621ceaa963d253e Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 18:26:20 -0700 Subject: [PATCH 07/16] Start work towards CRC_V2 --- embassy-stm32/src/crc/mod.rs | 9 ++-- embassy-stm32/src/crc/v2.rs | 86 ++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs index d8de4bd09..42f131af7 100644 --- a/embassy-stm32/src/crc/mod.rs +++ b/embassy-stm32/src/crc/mod.rs @@ -1,6 +1,9 @@ -#[cfg_attr(crc_v1, path = "v1.rs")] -#[cfg_attr(crc_v2, path = "v2.rs")] +use crate::peripherals::CRC; + + +#[cfg_attr(crc_v2, path = "v1.rs")] +#[cfg_attr(crc_v1, path = "v2.rs")] #[cfg_attr(crc_v3, path = "v3.rs")] mod _version; -pub use _version::Crc; +pub use _version::Crc; \ No newline at end of file diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs index 8b1378917..523bad10f 100644 --- a/embassy-stm32/src/crc/v2.rs +++ b/embassy-stm32/src/crc/v2.rs @@ -1 +1,87 @@ +use crate::pac::CRC as PAC_CRC; +use crate::pac::crc::vals; +use crate::peripherals::CRC; +use crate::rcc::sealed::RccPeripheral; +pub struct Crc { + _peripheral: CRC, + _config: CrcConfig, +} + +pub enum CrcConfigError { + InvalidPolynomial +} + +pub struct CrcConfig { + reverse_in: CrcInputReverseConfig, + reverse_out: bool, + poly_size: PolySize, + crc_init_value: u32, + crc_poly: u32, +} + +pub enum CrcInputReverseConfig { + None, + Byte, + Halfword, + Word, +} + +impl CrcConfig { + pub fn new(reverse_in: CrcInputReverseConfig, reverse_out: bool, poly_size: PolySize, crc_init_value: u32, crc_poly: u32) -> Result { + // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported. + if crc_poly % 2 == 0 { + Err(CrcConfigError::InvalidPolynomial) + } else { + Ok(CrcConfig { reverse_in, reverse_out, poly_size, crc_init_value, crc_poly }) + } + } +} + +pub enum PolySize { + Width7, + Width8, + Width16, + Width32, +} + +impl Crc { + /// Instantiates the CRC32 peripheral and initializes it to default values. + pub fn new(peripheral: CRC, init_value: u32, config: CrcConfig) -> Self { + // Note: enable and reset come from RccPeripheral. + // enable CRC clock in RCC. + CRC::enable(); + // Reset CRC to default values. + CRC::reset(); + let mut instance = Self { + _peripheral: peripheral, + }; + instance.init(); + instance + } + + // Configure device settings + fn configure_revout(&mut self) { + unsafe { + PAC_CRC.cr().modify(|w| {w.set_rev_out( + match self._config.reverse_out { + true => {vals::RevOut::REVERSED} + false => {vals::RevOut::NORMAL} + } + )}) + } + } + + fn configure_revin(&mut self) { + unsafe { + PAC_CRC.cr().modify(|w| {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} + } + )}) + } + } +} \ No newline at end of file From 8fac444c4e54cc13f514a23e36da737d70cf7eea Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 18:39:55 -0700 Subject: [PATCH 08/16] Flesh out v2 config writes --- embassy-stm32/src/crc/v2.rs | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs index 523bad10f..db4a3699a 100644 --- a/embassy-stm32/src/crc/v2.rs +++ b/embassy-stm32/src/crc/v2.rs @@ -55,9 +55,11 @@ impl Crc { CRC::reset(); let mut instance = Self { _peripheral: peripheral, + _config: config }; - instance.init(); - instance + unimplemented!(); + // instance.init(); + // instance } // Configure device settings @@ -84,4 +86,28 @@ impl Crc { )}) } } + + fn configure_polysize(&mut self) { + unsafe { + PAC_CRC.cr().modify(|w| {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} + } + )}) + } + } + + pub fn reset(&mut self) { + unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)); } + } + + fn set_crc_init(&mut self, value: u32) { + unsafe { + PAC_CRC.init().write_value(value) + } + } + } \ No newline at end of file From f9ff5336d4c78e571c755e63ef360393e29a1f51 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 18:46:19 -0700 Subject: [PATCH 09/16] Merge all of the crc_v2 configurations into a single modify call --- embassy-stm32/src/crc/v2.rs | 80 ++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 42 deletions(-) diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs index db4a3699a..b21e6c633 100644 --- a/embassy-stm32/src/crc/v2.rs +++ b/embassy-stm32/src/crc/v2.rs @@ -55,59 +55,55 @@ impl Crc { CRC::reset(); let mut instance = Self { _peripheral: peripheral, - _config: config + _config: config, }; unimplemented!(); // instance.init(); // instance } - // Configure device settings - fn configure_revout(&mut self) { - unsafe { - PAC_CRC.cr().modify(|w| {w.set_rev_out( - match self._config.reverse_out { - true => {vals::RevOut::REVERSED} - false => {vals::RevOut::NORMAL} - } - )}) - } - } - fn configure_revin(&mut self) { - unsafe { - PAC_CRC.cr().modify(|w| {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} - } - )}) - } - } - - fn configure_polysize(&mut self) { - unsafe { - PAC_CRC.cr().modify(|w| {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} - } - )}) - } - } pub fn reset(&mut self) { unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)); } } - fn set_crc_init(&mut self, value: u32) { - unsafe { - PAC_CRC.init().write_value(value) - } - } + fn reconfigure(&mut self) { + unsafe { + // Init CRC value + PAC_CRC.init().write_value(self._config.crc_init_value); + + 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 } + } + ); + // 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 } + } + ); + // 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 } + } + ) + + }) + } + + self.reset(); + } } \ No newline at end of file From 642b0825a62a7d40c3496c32ac6abd8dce3e55bd Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 19:14:08 -0700 Subject: [PATCH 10/16] 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 b21e6c633..3336d3f75 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 8b1378917..000000000 --- a/embassy-stm32/src/crc/v3.rs +++ /dev/null @@ -1 +0,0 @@ - From e67af514e921b3b28b4c20a88554c4d7cbef7951 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 19:15:54 -0700 Subject: [PATCH 11/16] Fix v2/3 module paths --- embassy-stm32/src/crc/mod.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs index 42f131af7..597e7cf3a 100644 --- a/embassy-stm32/src/crc/mod.rs +++ b/embassy-stm32/src/crc/mod.rs @@ -1,9 +1,6 @@ -use crate::peripherals::CRC; - - -#[cfg_attr(crc_v2, path = "v1.rs")] -#[cfg_attr(crc_v1, path = "v2.rs")] -#[cfg_attr(crc_v3, path = "v3.rs")] +#[cfg_attr(crc_v2, path = "v2.rs")] +#[cfg_attr(crc_v1, path = "v1.rs")] +#[cfg_attr(crc_v3, path = "v2.rs")] mod _version; pub use _version::Crc; \ No newline at end of file From 7392e33ad563642453bde20c9d33120af6b7ccbb Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Sun, 26 Sep 2021 19:20:21 -0700 Subject: [PATCH 12/16] cargo fmt --- embassy-stm32/src/crc/mod.rs | 2 +- embassy-stm32/src/crc/v2.rs | 34 ++++++++++++++++------------------ 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs index 597e7cf3a..70c87ab53 100644 --- a/embassy-stm32/src/crc/mod.rs +++ b/embassy-stm32/src/crc/mod.rs @@ -3,4 +3,4 @@ #[cfg_attr(crc_v3, path = "v2.rs")] mod _version; -pub use _version::Crc; \ No newline at end of file +pub use _version::Crc; diff --git a/embassy-stm32/src/crc/v2.rs b/embassy-stm32/src/crc/v2.rs index 3336d3f75..7705b4285 100644 --- a/embassy-stm32/src/crc/v2.rs +++ b/embassy-stm32/src/crc/v2.rs @@ -33,11 +33,9 @@ impl CrcConfig { pub fn new( reverse_in: CrcInputReverseConfig, reverse_out: bool, - #[cfg(crc_v3)] - poly_size: PolySize, + #[cfg(crc_v3)] poly_size: PolySize, crc_init_value: u32, - #[cfg(crc_v3)] - crc_poly: u32, + #[cfg(crc_v3)] crc_poly: u32, ) -> Result { // As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported. #[cfg(crc_v3)] @@ -94,7 +92,7 @@ impl Crc { // 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); + PAC_CRC.pol().write_value(self._config.crc_poly); // configure CR components // (reverse I/O, polysize, poly) @@ -113,7 +111,7 @@ impl Crc { }); // configure the polynomial. #[cfg(crc_v3)] - w.set_polysize(match self._config.poly_size { + w.set_polysize(match self._config.poly_size { PolySize::Width7 => vals::Polysize::POLYSIZE7, PolySize::Width8 => vals::Polysize::POLYSIZE8, PolySize::Width16 => vals::Polysize::POLYSIZE16, @@ -136,11 +134,11 @@ impl Crc { /// 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() + 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 { @@ -152,11 +150,11 @@ impl Crc { /// 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() + 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 { @@ -168,10 +166,10 @@ impl Crc { /// 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() + unsafe { + PAC_CRC.dr().write_value(*byte as u32); + } } + unsafe { PAC_CRC.dr().read() } } } From 43ad28b9f9b25f2b7369be33dfd1f7db5d34e330 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Mon, 27 Sep 2021 10:38:55 -0700 Subject: [PATCH 13/16] 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 70c87ab53..2ebebc935 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 09c8eecb6..8431c07f6 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 7705b4285..91c24215c 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)] From e36d4f460a74ce9cfb55975aa9a382ed53231aca Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Mon, 27 Sep 2021 10:46:09 -0700 Subject: [PATCH 14/16] Fix variable names in crc_v2/v3. removed `reclaim` in crc_v1. used write instead of modify. renamed `init` to `reset` in crc_v1. --- embassy-stm32/src/crc/v1.rs | 10 ++-------- embassy-stm32/src/crc/v2v3.rs | 26 +++++++++++++------------- 2 files changed, 15 insertions(+), 21 deletions(-) diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs index 8431c07f6..fc6d6945d 100644 --- a/embassy-stm32/src/crc/v1.rs +++ b/embassy-stm32/src/crc/v1.rs @@ -22,12 +22,12 @@ impl Crc { let mut instance = Self { _peripheral: peripheral, }; - instance.init(); + instance.reset(); instance } /// Resets the CRC unit to default value (0xFFFF_FFFF) - pub fn init(&mut self) { + pub fn reset(&mut self) { unsafe { PAC_CRC.cr().write(|w| w.set_reset(true)) }; } @@ -51,10 +51,4 @@ impl Crc { unsafe { PAC_CRC.dr().read() } } - /// Reclaims the CRC peripheral. - pub fn release(self) -> CRC { - CRC::disable(); - - self._peripheral - } } diff --git a/embassy-stm32/src/crc/v2v3.rs b/embassy-stm32/src/crc/v2v3.rs index 91c24215c..78334d6cf 100644 --- a/embassy-stm32/src/crc/v2v3.rs +++ b/embassy-stm32/src/crc/v2v3.rs @@ -99,7 +99,7 @@ impl Crc { // configure CR components // (reverse I/O, polysize, poly) - PAC_CRC.cr().modify(|w| { + PAC_CRC.cr().write(|w| { // configure reverse output w.set_rev_out(match self._config.reverse_out { true => vals::RevOut::REVERSED, @@ -144,33 +144,33 @@ impl Crc { 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 { + pub fn feed_halfword(&mut self, halfword: u16) -> u32 { unsafe { - PAC_CRC.dr16().write_value(byte as u32); + PAC_CRC.dr16().write_value(halfword 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 { + pub fn feed_halfwords(&mut self, halfwords: &[u16]) -> u32 { + for halfword in halfwords { unsafe { - PAC_CRC.dr16().write_value(*byte as u32); + PAC_CRC.dr16().write_value(*halfword 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 { + /// Feeds a words into the CRC peripheral. Returns the computed checksum. + pub fn feed_word(&mut self, word: u32) -> u32 { unsafe { - PAC_CRC.dr().write_value(byte as u32); + PAC_CRC.dr().write_value(word 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 { + /// Feeds an slice of words into the CRC peripheral. Returns the computed checksum. + pub fn feed_words(&mut self, words: &[u32]) -> u32 { + for word in words { unsafe { - PAC_CRC.dr().write_value(*byte as u32); + PAC_CRC.dr().write_value(*word as u32); } } unsafe { PAC_CRC.dr().read() } From a26ffeb84bf49af342526241feca83ba41fe5b53 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Mon, 27 Sep 2021 10:49:32 -0700 Subject: [PATCH 15/16] Cargo fmt --- embassy-stm32/src/crc/v1.rs | 6 ++---- embassy-stm32/src/crc/v2v3.rs | 4 ++-- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/embassy-stm32/src/crc/v1.rs b/embassy-stm32/src/crc/v1.rs index fc6d6945d..c33eea316 100644 --- a/embassy-stm32/src/crc/v1.rs +++ b/embassy-stm32/src/crc/v1.rs @@ -1,9 +1,8 @@ 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; - +use embassy_hal_common::unborrow; pub struct Crc { _peripheral: CRC, @@ -11,7 +10,7 @@ pub struct Crc { impl Crc { /// Instantiates the CRC32 peripheral and initializes it to default values. - pub fn new(peripheral: impl Unborrow) -> Self { + pub fn new(peripheral: impl Unborrow) -> Self { // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); @@ -50,5 +49,4 @@ impl Crc { pub fn read(&self) -> u32 { unsafe { PAC_CRC.dr().read() } } - } diff --git a/embassy-stm32/src/crc/v2v3.rs b/embassy-stm32/src/crc/v2v3.rs index 78334d6cf..1ca529568 100644 --- a/embassy-stm32/src/crc/v2v3.rs +++ b/embassy-stm32/src/crc/v2v3.rs @@ -2,8 +2,8 @@ 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; +use embassy_hal_common::unborrow; pub struct Crc { _peripheral: CRC, @@ -66,7 +66,7 @@ pub enum PolySize { impl Crc { /// Instantiates the CRC32 peripheral and initializes it to default values. - pub fn new(peripheral: impl Unborrow, config: Config) -> Self { + pub fn new(peripheral: impl Unborrow, config: Config) -> Self { // Note: enable and reset come from RccPeripheral. // enable CRC clock in RCC. CRC::enable(); From 07e20a74431617c4c4130dec2e70f77a439baad9 Mon Sep 17 00:00:00 2001 From: Joshua Salzedo Date: Mon, 27 Sep 2021 11:17:31 -0700 Subject: [PATCH 16/16] Pub use version-specific CRC symbols, not just the CRC struct. --- embassy-stm32/src/crc/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/embassy-stm32/src/crc/mod.rs b/embassy-stm32/src/crc/mod.rs index 2ebebc935..63f7ad9ba 100644 --- a/embassy-stm32/src/crc/mod.rs +++ b/embassy-stm32/src/crc/mod.rs @@ -3,4 +3,4 @@ #[cfg_attr(crc_v3, path = "v2v3.rs")] mod _version; -pub use _version::Crc; +pub use _version::*;