use embassy_hal_internal::{into_ref, PeripheralRef};
use crate::pac::crc::vals;
use crate::pac::CRC as PAC_CRC;
use crate::peripherals::CRC;
use crate::rcc::sealed::RccPeripheral;
use crate::Peripheral;
pub struct Crc<'d> {
_peripheral: PeripheralRef<'d, CRC>,
_config: Config,
}
pub enum ConfigError {
InvalidPolynomial,
}
pub struct Config {
reverse_in: InputReverseConfig,
reverse_out: bool,
#[cfg(crc_v3)]
poly_size: PolySize,
crc_init_value: u32,
#[cfg(crc_v3)]
crc_poly: u32,
}
pub enum InputReverseConfig {
None,
Byte,
Halfword,
Word,
}
impl Config {
pub fn new(
reverse_in: InputReverseConfig,
reverse_out: bool,
#[cfg(crc_v3)] poly_size: PolySize,
crc_init_value: u32,
#[cfg(crc_v3)] crc_poly: u32,
) -> Result<Self, ConfigError> {
#[cfg(crc_v3)]
if crc_poly % 2 == 0 {
return Err(ConfigError::InvalidPolynomial);
}
Ok(Config {
reverse_in,
reverse_out,
#[cfg(crc_v3)]
poly_size,
crc_init_value,
#[cfg(crc_v3)]
crc_poly,
})
}
}
#[cfg(crc_v3)]
#[allow(missing_docs)]
pub enum PolySize {
Width7,
Width8,
Width16,
Width32,
}
impl<'d> Crc<'d> {
pub fn new(peripheral: impl Peripheral<P = CRC> + 'd, config: Config) -> Self {
CRC::enable_and_reset();
into_ref!(peripheral);
let mut instance = Self {
_peripheral: peripheral,
_config: config,
};
instance.reconfigure();
instance.reset();
instance
}
pub fn reset(&mut self) {
PAC_CRC.cr().modify(|w| w.set_reset(true));
}
fn reconfigure(&mut self) {
PAC_CRC.init().write_value(self._config.crc_init_value);
#[cfg(crc_v3)]
PAC_CRC.pol().write_value(self._config.crc_poly);
PAC_CRC.cr().write(|w| {
w.set_rev_out(match self._config.reverse_out {
true => vals::RevOut::REVERSED,
false => vals::RevOut::NORMAL,
});
w.set_rev_in(match self._config.reverse_in {
InputReverseConfig::None => vals::RevIn::NORMAL,
InputReverseConfig::Byte => vals::RevIn::BYTE,
InputReverseConfig::Halfword => vals::RevIn::HALFWORD,
InputReverseConfig::Word => vals::RevIn::WORD,
});
#[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();
}
pub fn feed_byte(&mut self, byte: u8) -> u32 {
PAC_CRC.dr8().write_value(byte);
PAC_CRC.dr().read()
}
pub fn feed_bytes(&mut self, bytes: &[u8]) -> u32 {
for byte in bytes {
PAC_CRC.dr8().write_value(*byte);
}
PAC_CRC.dr().read()
}
pub fn feed_halfword(&mut self, halfword: u16) -> u32 {
PAC_CRC.dr16().write_value(halfword);
PAC_CRC.dr().read()
}
pub fn feed_halfwords(&mut self, halfwords: &[u16]) -> u32 {
for halfword in halfwords {
PAC_CRC.dr16().write_value(*halfword);
}
PAC_CRC.dr().read()
}
pub fn feed_word(&mut self, word: u32) -> u32 {
PAC_CRC.dr().write_value(word as u32);
PAC_CRC.dr().read()
}
pub fn feed_words(&mut self, words: &[u32]) -> u32 {
for word in words {
PAC_CRC.dr().write_value(*word as u32);
}
PAC_CRC.dr().read()
}
}