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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use core::convert::TryInto;
use core::fmt;
use crate::rcc::{rec, ResetEnable};
use crate::stm32::{crc, CRC};
pub trait CrcExt {
fn crc(self, prec: rec::Crc) -> Crc;
}
impl CrcExt for CRC {
fn crc(self, prec: rec::Crc) -> Crc {
prec.enable().reset();
Crc {
reg: self,
output_xor: 0,
}
}
}
pub struct Crc {
reg: CRC,
output_xor: u32,
}
impl Crc {
pub fn set_config(&mut self, config: &Config) {
self.output_xor = config.output_xor & config.poly.xor_mask();
self.reg.cr.modify(|_, w| {
w.polysize()
.bits(config.poly.polysize())
.rev_in()
.bits(config.get_reverse_input())
.rev_out()
.bit(config.reverse_output)
.reset()
.set_bit()
});
self.reg.pol.write(|w| w.pol().bits(config.poly.pol()));
self.reg.init.write(|w| w.init().bits(config.initial));
}
pub fn update(&mut self, data: &[u8]) {
let mut words = data.chunks_exact(4);
for word in words.by_ref() {
let word = u32::from_be_bytes(word.try_into().unwrap());
self.reg.dr_mut().write(|w| w.dr().bits(word));
}
let mut half_word = words.remainder().chunks_exact(2);
if let Some(half_word) = half_word.next() {
let half_word = u16::from_be_bytes(half_word.try_into().unwrap());
self.reg.dr16_mut().write(|w| w.dr16().bits(half_word));
}
if let Some(byte) = half_word.remainder().first() {
self.reg.dr8_mut().write(|w| w.dr8().bits(*byte));
}
}
#[must_use = "retrieving the CRC takes time, use update() if not needed"]
pub fn update_and_read(&mut self, data: &[u8]) -> u32 {
self.update(data);
self.read_crc()
}
pub fn finish(&mut self) -> u32 {
let result = self.read_crc();
self.reg.cr.modify(|_, w| w.reset().set_bit());
result
}
#[inline]
pub fn read_crc(&self) -> u32 {
self.read_crc_no_xor() ^ self.output_xor
}
pub fn read_state(&self) -> u32 {
let state = self.read_crc_no_xor();
if self.reg.cr.read().rev_out().is_reversed() {
state.reverse_bits()
} else {
state
}
}
#[inline(always)]
fn read_crc_no_xor(&self) -> u32 {
self.reg.dr().read().dr().bits()
}
pub fn set_idr(&mut self, value: u32) {
self.reg.idr.write(|w| w.idr().bits(value));
}
pub fn get_idr(&self) -> u32 {
self.reg.idr.read().idr().bits()
}
}
#[macro_use]
mod macros {
macro_rules! ensure_is_odd {
( $x:expr ) => {
if $x % 2 == 0 {
Err(PolynomialError::EvenPoly)
} else {
Ok($x)
}
};
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Polynomial(Poly);
impl Polynomial {
pub fn bits7(poly: u8) -> Result<Self, PolynomialError> {
if poly <= 0x7F {
Ok(Self(Poly::B7(ensure_is_odd!(poly)?)))
} else {
Err(PolynomialError::TooLarge)
}
}
pub fn bits8(poly: u8) -> Result<Self, PolynomialError> {
Ok(Self(Poly::B8(ensure_is_odd!(poly)?)))
}
pub fn bits16(poly: u16) -> Result<Self, PolynomialError> {
Ok(Self(Poly::B16(ensure_is_odd!(poly)?)))
}
pub fn bits32(poly: u32) -> Result<Self, PolynomialError> {
Ok(Self(Poly::B32(ensure_is_odd!(poly)?)))
}
pub const fn bits7_unchecked(poly: u8) -> Self {
Self(Poly::B7(poly))
}
pub const fn bits8_unchecked(poly: u8) -> Self {
Self(Poly::B8(poly))
}
pub const fn bits16_unchecked(poly: u16) -> Self {
Self(Poly::B16(poly))
}
pub const fn bits32_unchecked(poly: u32) -> Self {
Self(Poly::B32(poly))
}
fn polysize(self) -> u8 {
(match self.0 {
Poly::B7(_) => crc::cr::POLYSIZE_A::POLYSIZE7,
Poly::B8(_) => crc::cr::POLYSIZE_A::POLYSIZE8,
Poly::B16(_) => crc::cr::POLYSIZE_A::POLYSIZE16,
Poly::B32(_) => crc::cr::POLYSIZE_A::POLYSIZE32,
}) as u8
}
fn pol(self) -> u32 {
match self.0 {
Poly::B7(pol) | Poly::B8(pol) => pol as u32,
Poly::B16(pol) => pol as u32,
Poly::B32(pol) => pol,
}
}
fn xor_mask(self) -> u32 {
match self.0 {
Poly::B7(_) => 0x7F,
Poly::B8(_) => 0xFF,
Poly::B16(_) => 0xFFFF,
Poly::B32(_) => 0xFFFF_FFFF,
}
}
}
impl Default for Polynomial {
fn default() -> Self {
Self(Poly::B32(0x04C1_1DB7))
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum PolynomialError {
EvenPoly,
TooLarge,
}
impl fmt::Display for PolynomialError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::EvenPoly => f.write_str("tried to create an even polynomial"),
Self::TooLarge => f.write_str(
"tried to create a 7-bit polynomial with an 8-bit number",
),
}
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
enum Poly {
B7(u8),
B8(u8),
B16(u16),
B32(u32),
}
#[derive(Copy, Clone, Debug, PartialEq)]
#[repr(u8)]
pub enum BitReversal {
Byte = crc::cr::REV_IN_A::BYTE as u8,
HalfWord = crc::cr::REV_IN_A::HALFWORD as u8,
Word = crc::cr::REV_IN_A::WORD as u8,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Config {
poly: Polynomial,
initial: u32,
reverse_input: Option<BitReversal>,
reverse_output: bool,
output_xor: u32,
}
impl Config {
pub const fn new() -> Self {
Self {
poly: Polynomial(Poly::B32(0x04C1_1DB7)),
initial: 0xFFFF_FFFF,
reverse_input: None,
reverse_output: false,
output_xor: 0,
}
}
pub const fn polynomial(mut self, poly: Polynomial) -> Self {
self.poly = poly;
self
}
pub const fn initial_value(mut self, initial: u32) -> Self {
self.initial = initial;
self
}
pub const fn reverse_input(mut self, reverse: Option<BitReversal>) -> Self {
self.reverse_input = reverse;
self
}
fn get_reverse_input(&self) -> u8 {
self.reverse_input
.map(|rev| rev as u8)
.unwrap_or(crc::cr::REV_IN_A::NORMAL as u8)
}
pub const fn reverse_output(mut self, reverse: bool) -> Self {
self.reverse_output = reverse;
self
}
pub fn reflect(self, reflect: bool) -> Self {
if reflect {
self.reverse_input(Some(BitReversal::Byte))
.reverse_output(true)
} else {
self.reverse_input(None).reverse_output(false)
}
}
pub const fn output_xor(mut self, output_xor: u32) -> Self {
self.output_xor = output_xor;
self
}
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}