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

53 lines
1.5 KiB
Rust
Raw Normal View History

2022-07-23 01:29:35 +02:00
use embassy_hal_common::{unborrow, Unborrowed};
2022-06-12 22:15:44 +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;
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();
// Unborrow the peripheral
2022-07-23 01:29:35 +02:00
let mut instance = Self { _peri: 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() }
}
}