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;