2022-07-23 01:29:35 +02:00
|
|
|
use embassy_hal_common::{unborrow, Unborrowed};
|
2022-06-12 22:15:44 +02:00
|
|
|
|
2021-09-27 03:26:20 +02:00
|
|
|
use crate::pac::crc::vals;
|
2021-09-27 04:14:08 +02:00
|
|
|
use crate::pac::CRC as PAC_CRC;
|
2021-09-27 03:26:20 +02:00
|
|
|
use crate::peripherals::CRC;
|
|
|
|
use crate::rcc::sealed::RccPeripheral;
|
2022-06-11 05:08:57 +02:00
|
|
|
use crate::Unborrow;
|
2021-09-27 01:46:17 +02:00
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
pub struct Crc<'d> {
|
2022-07-23 01:29:35 +02:00
|
|
|
_peripheral: Unborrowed<'d, CRC>,
|
2021-09-27 19:38:55 +02:00
|
|
|
_config: Config,
|
2021-09-27 03:26:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 19:38:55 +02:00
|
|
|
pub enum ConfigError {
|
2021-09-27 04:14:08 +02:00
|
|
|
InvalidPolynomial,
|
2021-09-27 03:26:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 19:38:55 +02:00
|
|
|
pub struct Config {
|
|
|
|
reverse_in: InputReverseConfig,
|
2021-09-27 03:26:20 +02:00
|
|
|
reverse_out: bool,
|
2021-09-27 04:14:08 +02:00
|
|
|
#[cfg(crc_v3)]
|
2021-09-27 03:26:20 +02:00
|
|
|
poly_size: PolySize,
|
|
|
|
crc_init_value: u32,
|
2021-09-27 04:14:08 +02:00
|
|
|
#[cfg(crc_v3)]
|
2021-09-27 03:26:20 +02:00
|
|
|
crc_poly: u32,
|
|
|
|
}
|
|
|
|
|
2021-09-27 19:38:55 +02:00
|
|
|
pub enum InputReverseConfig {
|
2021-09-27 03:26:20 +02:00
|
|
|
None,
|
|
|
|
Byte,
|
|
|
|
Halfword,
|
|
|
|
Word,
|
|
|
|
}
|
|
|
|
|
2021-09-27 19:38:55 +02:00
|
|
|
impl Config {
|
2021-09-27 04:14:08 +02:00
|
|
|
pub fn new(
|
2021-09-27 19:38:55 +02:00
|
|
|
reverse_in: InputReverseConfig,
|
2021-09-27 04:14:08 +02:00
|
|
|
reverse_out: bool,
|
2021-09-27 04:20:21 +02:00
|
|
|
#[cfg(crc_v3)] poly_size: PolySize,
|
2021-09-27 04:14:08 +02:00
|
|
|
crc_init_value: u32,
|
2021-09-27 04:20:21 +02:00
|
|
|
#[cfg(crc_v3)] crc_poly: u32,
|
2021-09-27 19:38:55 +02:00
|
|
|
) -> Result<Self, ConfigError> {
|
2021-09-27 03:26:20 +02:00
|
|
|
// As Per RM0091 (DocID018940 Rev 9), Even polynomials are not supported.
|
2021-09-27 04:14:08 +02:00
|
|
|
#[cfg(crc_v3)]
|
2021-09-27 03:26:20 +02:00
|
|
|
if crc_poly % 2 == 0 {
|
2021-09-27 19:38:55 +02:00
|
|
|
return Err(ConfigError::InvalidPolynomial);
|
2021-09-27 03:26:20 +02:00
|
|
|
}
|
2021-09-27 19:38:55 +02:00
|
|
|
Ok(Config {
|
2021-09-27 04:14:08 +02:00
|
|
|
reverse_in,
|
|
|
|
reverse_out,
|
|
|
|
#[cfg(crc_v3)]
|
|
|
|
poly_size,
|
|
|
|
crc_init_value,
|
|
|
|
#[cfg(crc_v3)]
|
|
|
|
crc_poly,
|
|
|
|
})
|
2021-09-27 03:26:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 04:14:08 +02:00
|
|
|
#[cfg(crc_v3)]
|
2021-09-27 03:26:20 +02:00
|
|
|
pub enum PolySize {
|
|
|
|
Width7,
|
|
|
|
Width8,
|
|
|
|
Width16,
|
|
|
|
Width32,
|
|
|
|
}
|
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
impl<'d> Crc<'d> {
|
2021-09-27 03:26:20 +02:00
|
|
|
/// Instantiates the CRC32 peripheral and initializes it to default values.
|
2022-02-10 16:06:42 +01:00
|
|
|
pub fn new(peripheral: impl Unborrow<Target = CRC> + 'd, config: Config) -> Self {
|
2021-09-27 03:26:20 +02:00
|
|
|
// Note: enable and reset come from RccPeripheral.
|
|
|
|
// enable CRC clock in RCC.
|
|
|
|
CRC::enable();
|
|
|
|
// Reset CRC to default values.
|
|
|
|
CRC::reset();
|
2021-09-27 19:38:55 +02:00
|
|
|
unborrow!(peripheral);
|
2021-09-27 03:26:20 +02:00
|
|
|
let mut instance = Self {
|
|
|
|
_peripheral: peripheral,
|
2021-09-27 03:46:19 +02:00
|
|
|
_config: config,
|
2021-09-27 03:26:20 +02:00
|
|
|
};
|
2021-09-27 04:14:08 +02:00
|
|
|
CRC::reset();
|
|
|
|
instance.reconfigure();
|
|
|
|
instance.reset();
|
|
|
|
instance
|
2021-09-27 03:26:20 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 03:39:55 +02:00
|
|
|
pub fn reset(&mut self) {
|
2021-09-27 04:14:08 +02:00
|
|
|
unsafe {
|
|
|
|
PAC_CRC.cr().modify(|w| w.set_reset(true));
|
|
|
|
}
|
2021-09-27 03:39:55 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 04:14:08 +02:00
|
|
|
/// Reconfigures the CRC peripheral. Doesn't reset.
|
2021-09-27 03:46:19 +02:00
|
|
|
fn reconfigure(&mut self) {
|
2021-09-27 03:39:55 +02:00
|
|
|
unsafe {
|
2021-09-27 03:46:19 +02:00
|
|
|
// Init CRC value
|
|
|
|
PAC_CRC.init().write_value(self._config.crc_init_value);
|
2021-09-27 04:14:08 +02:00
|
|
|
#[cfg(crc_v3)]
|
2021-09-27 04:20:21 +02:00
|
|
|
PAC_CRC.pol().write_value(self._config.crc_poly);
|
2021-09-27 03:46:19 +02:00
|
|
|
|
2021-09-27 04:14:08 +02:00
|
|
|
// configure CR components
|
|
|
|
// (reverse I/O, polysize, poly)
|
2021-09-27 19:46:09 +02:00
|
|
|
PAC_CRC.cr().write(|w| {
|
2021-09-27 03:46:19 +02:00
|
|
|
// configure reverse output
|
2021-09-27 04:14:08 +02:00
|
|
|
w.set_rev_out(match self._config.reverse_out {
|
|
|
|
true => vals::RevOut::REVERSED,
|
|
|
|
false => vals::RevOut::NORMAL,
|
|
|
|
});
|
2021-09-27 03:46:19 +02:00
|
|
|
// configure reverse input
|
2021-09-27 04:14:08 +02:00
|
|
|
w.set_rev_in(match self._config.reverse_in {
|
2021-09-27 19:38:55 +02:00
|
|
|
InputReverseConfig::None => vals::RevIn::NORMAL,
|
|
|
|
InputReverseConfig::Byte => vals::RevIn::BYTE,
|
|
|
|
InputReverseConfig::Halfword => vals::RevIn::HALFWORD,
|
|
|
|
InputReverseConfig::Word => vals::RevIn::WORD,
|
2021-09-27 04:14:08 +02:00
|
|
|
});
|
2021-09-27 03:46:19 +02:00
|
|
|
// configure the polynomial.
|
2021-09-27 04:14:08 +02:00
|
|
|
#[cfg(crc_v3)]
|
2021-09-27 04:20:21 +02:00
|
|
|
w.set_polysize(match self._config.poly_size {
|
2021-09-27 04:14:08 +02:00
|
|
|
PolySize::Width7 => vals::Polysize::POLYSIZE7,
|
|
|
|
PolySize::Width8 => vals::Polysize::POLYSIZE8,
|
|
|
|
PolySize::Width16 => vals::Polysize::POLYSIZE16,
|
|
|
|
PolySize::Width32 => vals::Polysize::POLYSIZE32,
|
|
|
|
});
|
2021-09-27 03:46:19 +02:00
|
|
|
})
|
2021-09-27 03:39:55 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 03:46:19 +02:00
|
|
|
self.reset();
|
|
|
|
}
|
2021-09-27 04:14:08 +02:00
|
|
|
|
|
|
|
/// Feeds a byte into the CRC peripheral. Returns the computed checksum.
|
|
|
|
pub fn feed_byte(&mut self, byte: u8) -> u32 {
|
|
|
|
unsafe {
|
2022-06-07 00:28:26 +02:00
|
|
|
PAC_CRC.dr8().write_value(byte);
|
2021-09-27 04:14:08 +02:00
|
|
|
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 {
|
2021-09-27 04:20:21 +02:00
|
|
|
unsafe {
|
2022-06-07 00:28:26 +02:00
|
|
|
PAC_CRC.dr8().write_value(*byte);
|
2021-09-27 04:20:21 +02:00
|
|
|
}
|
2021-09-27 04:14:08 +02:00
|
|
|
}
|
2021-09-27 04:20:21 +02:00
|
|
|
unsafe { PAC_CRC.dr().read() }
|
2021-09-27 04:14:08 +02:00
|
|
|
}
|
|
|
|
/// Feeds a halfword into the CRC peripheral. Returns the computed checksum.
|
2021-09-27 19:46:09 +02:00
|
|
|
pub fn feed_halfword(&mut self, halfword: u16) -> u32 {
|
2021-09-27 04:14:08 +02:00
|
|
|
unsafe {
|
2022-06-07 00:28:26 +02:00
|
|
|
PAC_CRC.dr16().write_value(halfword);
|
2021-09-27 04:14:08 +02:00
|
|
|
PAC_CRC.dr().read()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/// Feeds an slice of halfwords into the CRC peripheral. Returns the computed checksum.
|
2021-09-27 19:46:09 +02:00
|
|
|
pub fn feed_halfwords(&mut self, halfwords: &[u16]) -> u32 {
|
|
|
|
for halfword in halfwords {
|
2021-09-27 04:20:21 +02:00
|
|
|
unsafe {
|
2022-06-07 00:28:26 +02:00
|
|
|
PAC_CRC.dr16().write_value(*halfword);
|
2021-09-27 04:20:21 +02:00
|
|
|
}
|
2021-09-27 04:14:08 +02:00
|
|
|
}
|
2021-09-27 04:20:21 +02:00
|
|
|
unsafe { PAC_CRC.dr().read() }
|
2021-09-27 04:14:08 +02:00
|
|
|
}
|
2021-09-27 19:46:09 +02:00
|
|
|
/// Feeds a words into the CRC peripheral. Returns the computed checksum.
|
|
|
|
pub fn feed_word(&mut self, word: u32) -> u32 {
|
2021-09-27 04:14:08 +02:00
|
|
|
unsafe {
|
2021-09-27 19:46:09 +02:00
|
|
|
PAC_CRC.dr().write_value(word as u32);
|
2021-09-27 04:14:08 +02:00
|
|
|
PAC_CRC.dr().read()
|
|
|
|
}
|
|
|
|
}
|
2021-09-27 19:46:09 +02:00
|
|
|
/// 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 {
|
2021-09-27 04:20:21 +02:00
|
|
|
unsafe {
|
2021-09-27 19:46:09 +02:00
|
|
|
PAC_CRC.dr().write_value(*word as u32);
|
2021-09-27 04:20:21 +02:00
|
|
|
}
|
2021-09-27 04:14:08 +02:00
|
|
|
}
|
2021-09-27 04:20:21 +02:00
|
|
|
unsafe { PAC_CRC.dr().read() }
|
2021-09-27 04:14:08 +02:00
|
|
|
}
|
|
|
|
}
|