2021-09-27 02:28:58 +02:00
|
|
|
use crate::pac::CRC as PAC_CRC;
|
2021-09-27 01:29:22 +02:00
|
|
|
use crate::peripherals::CRC;
|
|
|
|
use crate::rcc::sealed::RccPeripheral;
|
2021-09-27 19:38:55 +02:00
|
|
|
use embassy_hal_common::unborrow;
|
|
|
|
use embassy::util::Unborrow;
|
|
|
|
|
2021-09-27 01:29:22 +02:00
|
|
|
|
|
|
|
pub struct Crc {
|
2021-09-27 01:46:17 +02:00
|
|
|
_peripheral: CRC,
|
2021-09-27 01:29:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 01:46:17 +02:00
|
|
|
impl Crc {
|
|
|
|
/// Instantiates the CRC32 peripheral and initializes it to default values.
|
2021-09-27 19:38:55 +02:00
|
|
|
pub fn new(peripheral: impl Unborrow<Target= CRC>) -> Self {
|
2021-09-27 01:46:17 +02:00
|
|
|
// Note: enable and reset come from RccPeripheral.
|
2021-09-27 01:29:22 +02:00
|
|
|
// enable CRC clock in RCC.
|
|
|
|
CRC::enable();
|
|
|
|
// Reset CRC to default values.
|
|
|
|
CRC::reset();
|
2021-09-27 19:38:55 +02:00
|
|
|
// Unborrow the peripheral
|
|
|
|
unborrow!(peripheral);
|
2021-09-27 01:46:17 +02:00
|
|
|
let mut instance = Self {
|
|
|
|
_peripheral: peripheral,
|
|
|
|
};
|
|
|
|
instance.init();
|
|
|
|
instance
|
2021-09-27 01:29:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 01:46:17 +02:00
|
|
|
/// Resets the CRC unit to default value (0xFFFF_FFFF)
|
|
|
|
pub fn init(&mut self) {
|
2021-09-27 02:24:48 +02:00
|
|
|
unsafe { PAC_CRC.cr().write(|w| w.set_reset(true)) };
|
2021-09-27 01:29:22 +02:00
|
|
|
}
|
2021-09-27 01:46:17 +02:00
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|
2021-09-27 02:28:58 +02:00
|
|
|
self.read()
|
2021-09-27 01:46:17 +02:00
|
|
|
}
|
|
|
|
/// 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 {
|
2021-09-27 02:28:58 +02:00
|
|
|
unsafe { PAC_CRC.dr().write_value(*word) }
|
2021-09-27 01:46:17 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 02:28:58 +02:00
|
|
|
self.read()
|
|
|
|
}
|
|
|
|
pub fn read(&self) -> u32 {
|
2021-09-27 01:46:17 +02:00
|
|
|
unsafe { PAC_CRC.dr().read() }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Reclaims the CRC peripheral.
|
|
|
|
pub fn release(self) -> CRC {
|
|
|
|
CRC::disable();
|
|
|
|
|
|
|
|
self._peripheral
|
|
|
|
}
|
|
|
|
}
|