@@ -19,17 +19,22 @@ pub struct Crc32 {
19
19
impl Crc32 {
20
20
/// Create a new Crc32 HAL peripheral
21
21
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
24
25
}
25
26
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.
30
34
///
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 {
33
38
// Feed each word into the engine
34
39
for word in data {
35
40
self . periph . dr . write ( |w| unsafe { w. bits ( * word) } ) ;
@@ -39,18 +44,10 @@ impl Crc32 {
39
44
self . periph . dr . read ( ) . bits ( )
40
45
}
41
46
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.
51
48
///
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.
54
51
///
55
52
/// NOTE: Each four-byte chunk will be copied into a scratch buffer. This
56
53
/// is done to ensure alignment of the data (the CRC engine only processes
@@ -71,9 +68,7 @@ impl Crc32 {
71
68
///
72
69
/// 1. `0x4433_2211`
73
70
/// 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 {
77
72
let chunks = data. chunks_exact ( 4 ) ;
78
73
let remainder = chunks. remainder ( ) ;
79
74
@@ -114,29 +109,6 @@ impl Crc32 {
114
109
self . periph . dr . read ( ) . bits ( )
115
110
}
116
111
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
-
140
112
/// Consume the HAL peripheral, returning the PAC peripheral
141
113
pub fn free ( self ) -> CRC {
142
114
self . periph
0 commit comments