1use crate::peripheral::RccPeripheral;
11use crate::{pac, peripherals, Peri, PeripheralType};
12
13pub struct Crc<'d, T: Instance> {
15 _inner: Peri<'d, T>,
16}
17
18impl<'d, T: Instance> Crc<'d, T> {
19 pub fn new(inner: Peri<'d, T>) -> Self {
21 T::enable_and_reset();
22 let mut crc = Self { _inner: inner };
23 crc.reset();
24 crc
25 }
26
27 pub fn reset(&mut self) {
29 T::regs().ctlr().write(|w| w.set_reset(true));
30 while T::regs().datar().read().dr() != 0xffff_ffff {}
32 }
33
34 pub fn feed_word(&mut self, word: u32) -> u32 {
36 T::regs().datar().write(|w| w.set_dr(word));
37 self.read()
38 }
39
40 pub fn feed_words(&mut self, words: &[u32]) -> u32 {
43 for &word in words {
44 T::regs().datar().write(|w| w.set_dr(word));
45 }
46 self.read()
47 }
48
49 pub fn read(&self) -> u32 {
51 T::regs().datar().read().dr()
52 }
53
54 pub fn set_independent_data(&mut self, value: u8) {
58 T::regs().idatar().write(|w| w.set_idr(value));
59 }
60
61 pub fn independent_data(&self) -> u8 {
63 T::regs().idatar().read().idr()
64 }
65}
66
67trait SealedInstance {
68 fn regs() -> pac::crc::Crc;
69}
70
71#[allow(private_bounds)]
73pub trait Instance: SealedInstance + RccPeripheral + PeripheralType + 'static {}
74
75impl<'d, T: Instance> Drop for Crc<'d, T> {
76 fn drop(&mut self) {
77 T::disable();
78 }
79}
80
81foreach_peripheral!(
82 (crc, $inst:ident) => {
83 impl SealedInstance for peripherals::$inst {
84 fn regs() -> pac::crc::Crc {
85 pac::$inst
86 }
87 }
88
89 impl Instance for peripherals::$inst {}
90 };
91);