Merge pull request #4255 from kpfleming/improve-stm32-crc-hal

stm32: Improvements to CRC HAL.
This commit is contained in:
Ulf Lilleengen 2025-05-27 05:53:07 +00:00 committed by GitHub
commit 1c9de3491d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 27 additions and 32 deletions

View File

@ -23,22 +23,16 @@ impl<'d> Crc<'d> {
PAC_CRC.cr().write(|w| w.set_reset(true)); PAC_CRC.cr().write(|w| w.set_reset(true));
} }
/// Feeds a word to the peripheral and returns the current CRC value /// Feeds a word into the CRC peripheral. Returns the computed CRC.
pub fn feed_word(&mut self, word: u32) -> u32 { pub fn feed_word(&mut self, word: u32) -> u32 {
// write a single byte to the device, and return the result // write a single byte to the device, and return the result
#[cfg(not(crc_v1))]
PAC_CRC.dr32().write_value(word);
#[cfg(crc_v1)]
PAC_CRC.dr().write_value(word); PAC_CRC.dr().write_value(word);
self.read() self.read()
} }
/// Feed a slice of words to the peripheral and return the result. /// Feeds a slice of words into the CRC peripheral. Returns the computed CRC.
pub fn feed_words(&mut self, words: &[u32]) -> u32 { pub fn feed_words(&mut self, words: &[u32]) -> u32 {
for word in words { for word in words {
#[cfg(not(crc_v1))]
PAC_CRC.dr32().write_value(*word);
#[cfg(crc_v1)]
PAC_CRC.dr().write_value(*word); PAC_CRC.dr().write_value(*word);
} }
@ -46,12 +40,6 @@ impl<'d> Crc<'d> {
} }
/// Read the CRC result value. /// Read the CRC result value.
#[cfg(not(crc_v1))]
pub fn read(&self) -> u32 {
PAC_CRC.dr32().read()
}
/// Read the CRC result value.
#[cfg(crc_v1)]
pub fn read(&self) -> u32 { pub fn read(&self) -> u32 {
PAC_CRC.dr().read() PAC_CRC.dr().read()
} }

View File

@ -9,7 +9,7 @@ pub struct Crc<'d> {
_config: Config, _config: Config,
} }
/// CRC configuration errlr /// CRC configuration error
#[derive(Debug)] #[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum ConfigError { pub enum ConfigError {
@ -34,9 +34,9 @@ pub enum InputReverseConfig {
None, None,
/// Reverse bytes /// Reverse bytes
Byte, Byte,
/// Reverse 16-bit halfwords. /// Reverse 16-bit halfwords
Halfword, Halfword,
/// Reverse 32-bit words. /// Reverse 32-bit words
Word, Word,
} }
@ -127,45 +127,52 @@ impl<'d> Crc<'d> {
PolySize::Width32 => vals::Polysize::POLYSIZE32, PolySize::Width32 => vals::Polysize::POLYSIZE32,
}); });
}); });
self.reset();
} }
/// Feeds a byte into the CRC peripheral. Returns the computed checksum. /// Read the CRC result value.
pub fn feed_byte(&mut self, byte: u8) -> u32 { pub fn read(&self) -> u32 {
PAC_CRC.dr8().write_value(byte);
PAC_CRC.dr32().read() PAC_CRC.dr32().read()
} }
/// Feeds an slice of bytes into the CRC peripheral. Returns the computed checksum. /// Feeds a byte into the CRC peripheral. Returns the computed CRC.
pub fn feed_byte(&mut self, byte: u8) -> u32 {
PAC_CRC.dr8().write_value(byte);
self.read()
}
/// Feeds a slice of bytes into the CRC peripheral. Returns the computed CRC.
pub fn feed_bytes(&mut self, bytes: &[u8]) -> u32 { pub fn feed_bytes(&mut self, bytes: &[u8]) -> u32 {
for byte in bytes { for byte in bytes {
PAC_CRC.dr8().write_value(*byte); PAC_CRC.dr8().write_value(*byte);
} }
PAC_CRC.dr32().read() self.read()
} }
/// Feeds a halfword into the CRC peripheral. Returns the computed checksum.
/// Feeds a halfword into the CRC peripheral. Returns the computed CRC.
pub fn feed_halfword(&mut self, halfword: u16) -> u32 { pub fn feed_halfword(&mut self, halfword: u16) -> u32 {
PAC_CRC.dr16().write_value(halfword); PAC_CRC.dr16().write_value(halfword);
PAC_CRC.dr32().read() self.read()
} }
/// Feeds an slice of halfwords into the CRC peripheral. Returns the computed checksum.
/// Feeds a slice of halfwords into the CRC peripheral. Returns the computed CRC.
pub fn feed_halfwords(&mut self, halfwords: &[u16]) -> u32 { pub fn feed_halfwords(&mut self, halfwords: &[u16]) -> u32 {
for halfword in halfwords { for halfword in halfwords {
PAC_CRC.dr16().write_value(*halfword); PAC_CRC.dr16().write_value(*halfword);
} }
PAC_CRC.dr32().read() self.read()
} }
/// Feeds a words into the CRC peripheral. Returns the computed checksum.
/// Feeds a word into the CRC peripheral. Returns the computed CRC.
pub fn feed_word(&mut self, word: u32) -> u32 { pub fn feed_word(&mut self, word: u32) -> u32 {
PAC_CRC.dr32().write_value(word as u32); PAC_CRC.dr32().write_value(word as u32);
PAC_CRC.dr32().read() self.read()
} }
/// Feeds an slice of words into the CRC peripheral. Returns the computed checksum.
/// Feeds a slice of words into the CRC peripheral. Returns the computed CRC.
pub fn feed_words(&mut self, words: &[u32]) -> u32 { pub fn feed_words(&mut self, words: &[u32]) -> u32 {
for word in words { for word in words {
PAC_CRC.dr32().write_value(*word as u32); PAC_CRC.dr32().write_value(*word as u32);
} }
PAC_CRC.dr32().read() self.read()
} }
} }