embassy/embassy-stm32/src/crc/v1.rs

55 lines
1.4 KiB
Rust
Raw Normal View History

2021-09-27 01:46:17 +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;
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.
pub fn new(peripheral: CRC) -> Self {
// 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 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 01:29:22 +02:00
unsafe { PAC_CRC.cr().modify(|w| w.set_reset(true)) };
}
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);
PAC_CRC.dr().read()
}
}
/// 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 {
unsafe {
PAC_CRC.dr().write_value(*word);
}
}
unsafe { PAC_CRC.dr().read() }
}
/// Reclaims the CRC peripheral.
pub fn release(self) -> CRC {
CRC::disable();
self._peripheral
}
}