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

53 lines
1.5 KiB
Rust
Raw Normal View History

use crate::pac::CRC as PAC_CRC;
2021-09-27 01:29:22 +02:00
use crate::peripherals::CRC;
use crate::rcc::sealed::RccPeripheral;
use embassy::util::Unborrow;
2021-09-27 19:49:32 +02:00
use embassy_hal_common::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:49:32 +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();
// Unborrow the peripheral
unborrow!(peripheral);
2021-09-27 01:46:17 +02:00
let mut instance = Self {
_peripheral: peripheral,
};
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)
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);
}
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 {
unsafe { PAC_CRC.dr().write_value(*word) }
2021-09-27 01:46:17 +02:00
}
self.read()
}
pub fn read(&self) -> u32 {
2021-09-27 01:46:17 +02:00
unsafe { PAC_CRC.dr().read() }
}
}