Skip to content

Commit 166bd76

Browse files
committed
Added reset_with_initial_value, better docs
1 parent 2cbd0f8 commit 166bd76

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/crc.rs

+18-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::rcc;
2020
use crate::stm32::CRC;
2121
use core::ptr;
2222

23-
/// Extension trait to constrain the FLASH peripheral
23+
/// Extension trait to constrain the FLASH peripheral.
2424
pub trait CrcExt {
2525
/// Constrains the CRC peripheral to play nicely with the other abstractions
2626
fn constrain(self, ahb1: &mut rcc::AHB1) -> Config;
@@ -141,19 +141,32 @@ impl Config {
141141
}
142142
}
143143

144-
/// Constrained Crc peripheral.
144+
/// Constrained CRC peripheral.
145145
pub struct Crc {}
146146

147147
impl Crc {
148-
/// This will reset the Crc to its initial condition.
148+
/// This will reset the CRC to its initial condition.
149149
#[inline]
150150
pub fn reset(&mut self) {
151151
let crc = unsafe { &(*CRC::ptr()) };
152152

153153
crc.cr.modify(|_, w| w.reset().set_bit());
154154
}
155155

156-
/// Feed the Crc with data, this will internally optimize for word writes.
156+
/// This will reset the CRC to its initial condition, however with a specific initial value.
157+
/// This is very useful if many task are sharing the CRC peripheral, as one can read out the
158+
/// intermediate result, store it until the next time a task runs, and initialize with the
159+
/// intermediate result to continue where the task left off.
160+
#[inline]
161+
pub fn reset_with_inital_value(&mut self, initial_value: u32) {
162+
let crc = unsafe { &(*CRC::ptr()) };
163+
164+
crc.init.write(|w| unsafe { w.crc_init().bits(initial_value) });
165+
crc.cr.modify(|_, w| w.reset().set_bit());
166+
}
167+
168+
/// Feed the CRC with data, this will internally optimize for word writes.
169+
#[inline]
157170
pub fn feed(&mut self, data: &[u8]) {
158171
let crc = unsafe { &(*CRC::ptr()) };
159172
for byte in data {
@@ -166,7 +179,7 @@ impl Crc {
166179
}
167180

168181
/// Get the result of the CRC, depending on the polynomial chosen only a certain amount of the
169-
/// bits are the result. This will reset the Crc peripheral after use.
182+
/// bits are the result. This will reset the CRC peripheral after use.
170183
#[inline]
171184
pub fn result(&mut self) -> u32 {
172185
let ret = self.peek_result();

0 commit comments

Comments
 (0)