Skip to content

Commit 25ca58a

Browse files
author
James Munns
committed
Address review comments
1 parent af75383 commit 25ca58a

File tree

1 file changed

+17
-45
lines changed

1 file changed

+17
-45
lines changed

src/crc32.rs

Lines changed: 17 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ pub struct Crc32 {
1919
impl Crc32 {
2020
/// Create a new Crc32 HAL peripheral
2121
pub fn new(crc: CRC) -> Self {
22-
crc.cr.write(|w| w.reset().reset());
23-
Self { periph: crc }
22+
let mut new = Self { periph: crc };
23+
new.init();
24+
new
2425
}
2526

26-
/// Feed words into the CRC engine without pre-clearing the CRC state.
27-
///
28-
/// The resulting calculated CRC (including this and prior data)
29-
/// is returned.
27+
/// Reset the internal CRC32 state to the default value (0xFFFF_FFFF)
28+
#[inline(always)]
29+
pub fn init(&mut self) {
30+
self.periph.cr.write(|w| w.reset().reset());
31+
}
32+
33+
/// Feed words into the CRC engine.
3034
///
31-
/// This is useful when using the engine to process multi-part data.
32-
pub fn feed_without_clear(&mut self, data: &[u32]) -> u32 {
35+
/// The resulting calculated CRC (including this and prior data
36+
/// since the last call to `init()` is returned.
37+
pub fn update(&mut self, data: &[u32]) -> u32 {
3338
// Feed each word into the engine
3439
for word in data {
3540
self.periph.dr.write(|w| unsafe { w.bits(*word) });
@@ -39,18 +44,10 @@ impl Crc32 {
3944
self.periph.dr.read().bits()
4045
}
4146

42-
/// Feed words into the CRC engine, first clearing the CRC state.
43-
///
44-
/// The resulting calculated CRC (of ONLY the given data) is returned.
45-
pub fn feed(&mut self, data: &[u32]) -> u32 {
46-
self.periph.cr.write(|w| w.reset().reset());
47-
self.feed_without_clear(data)
48-
}
49-
50-
/// Feed bytes into the CRC engine without pre-clearing the CRC state.
47+
/// Feed bytes into the CRC engine.
5148
///
52-
/// The resulting calculated CRC (including this and prior data)
53-
/// is returned.
49+
/// The resulting calculated CRC (including this and prior data
50+
/// since the last call to `init()` is returned.
5451
///
5552
/// NOTE: Each four-byte chunk will be copied into a scratch buffer. This
5653
/// is done to ensure alignment of the data (the CRC engine only processes
@@ -71,9 +68,7 @@ impl Crc32 {
7168
///
7269
/// 1. `0x4433_2211`
7370
/// 2. `0x0077_6655`
74-
///
75-
/// This is useful when using the engine to process multi-part data.
76-
pub fn feed_bytes_without_clear(&mut self, data: &[u8]) -> u32 {
71+
pub fn update_bytes(&mut self, data: &[u8]) -> u32 {
7772
let chunks = data.chunks_exact(4);
7873
let remainder = chunks.remainder();
7974

@@ -114,29 +109,6 @@ impl Crc32 {
114109
self.periph.dr.read().bits()
115110
}
116111

117-
/// Feed words into the CRC engine, first clearing the CRC state.
118-
///
119-
/// NOTE: Each four-byte chunk will be copied into a scratch buffer. This
120-
/// is done to ensure alignment of the data (the CRC engine only processes
121-
/// full words at a time). If the number of bytes passed in are not a
122-
/// multiple of four, the MOST significant bytes of the remaining word will
123-
/// be zeroes.
124-
///
125-
/// Example: Given the following 7 bytes:
126-
///
127-
/// `[0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77]`
128-
///
129-
/// The following two words will be fed into the CRC engine:
130-
///
131-
/// 1. `0x4433_2211`
132-
/// 2. `0x0077_6655`
133-
///
134-
/// The resulting calculated CRC (of ONLY the given data) is returned.
135-
pub fn feed_bytes(&mut self, data: &[u8]) -> u32 {
136-
self.periph.cr.write(|w| w.reset().reset());
137-
self.feed_bytes_without_clear(data)
138-
}
139-
140112
/// Consume the HAL peripheral, returning the PAC peripheral
141113
pub fn free(self) -> CRC {
142114
self.periph

0 commit comments

Comments
 (0)