blob: 6219e8c07865aa7e4a85d4c563e04792435f1033 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
// Copyright 2019 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: GPL-3.0-or-later
use core::iter::Iterator;
use hal::stm32::CRC;
pub trait Crc {
fn get(&self, data: &[u8]) -> u32 {
self.get_u32(data.chunks(4).map(slice_to_u32))
}
fn get_u32<I: Iterator<Item = u32>>(&self, iter: I) -> u32;
}
pub struct Stm32Crc {
crc: CRC,
}
impl Stm32Crc {
pub fn new(crc: CRC) -> Self {
Self { crc }
}
fn write(&self, val: u32) {
self.crc.dr.write(|w| w.dr().bits(val));
}
fn read(&self) -> u32 {
self.crc.dr.read().dr().bits()
}
fn reset(&self) {
self.crc.cr.write(|w| w.reset().reset());
}
}
impl Crc for Stm32Crc {
fn get_u32<I: Iterator<Item = u32>>(&self, iter: I) -> u32 {
self.reset();
iter.for_each(|val| self.write(val));
self.read()
}
}
fn slice_to_u32(data: &[u8]) -> u32 {
data.iter()
.enumerate()
.fold(0, |acc, (idx, val)| acc + (u32::from(*val) << (idx * 8)))
}
|