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 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;
|
2022-06-11 05:08:57 +02:00
|
|
|
use crate::Unborrow;
|
2021-09-27 01:29:22 +02:00
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
pub struct Crc<'d> {
|
2022-07-23 01:29:35 +02:00
|
|
|
_peri: Unborrowed<'d, CRC>,
|
2021-09-27 01:29:22 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 16:06:42 +01:00
|
|
|
impl<'d> Crc<'d> {
|
2021-09-27 01:46:17 +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) -> Self {
|
2022-07-23 01:29:35 +02:00
|
|
|
unborrow!(peripheral);
|
|
|
|
|
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
|
2022-07-23 01:29:35 +02:00
|
|
|
let mut instance = Self { _peri: peripheral };
|
2021-09-27 19:46:09 +02:00
|
|
|
instance.reset();
|
2021-09-27 01:46:17 +02:00
|
|
|
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)
|
2021-09-27 19:46:09 +02:00
|
|
|
pub fn reset(&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() }
|
|
|
|
}
|
|
|
|
}
|