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
//! CRC calculation unit
//!
//! The hardware cyclic redundancy check (CRC) unit on GD32VF103 has 32-bit data
//! input and 32-bit data output. Calculation period is 4 AHB clock cycles
//! for 32-bit input data size, from data entered to the calculation result
//! available.
//!
//! This unit uses fixed polynomial 0x4C11DB7, which is a common polynomial
//! used in Ethernet.
//!
//! Ref: Section 8, the User Manual; Firmware/Source/gd32vf103_crc.c
//!
//! # Usage
//!
//! ## CRC calculation
//!
//! To use this module, create a [`Crc`] wrapper using [`Crc::crc`] function. This
//! function requires peripheral CRC and ownership of AHB peripheral bus; it turns
//! on the CRC clock and creates an owned `Crc` wrapper.
//! After the wrapper is created, you need to create a [`Digest`] struct.
//! The function [`crc.new_digest()`] will clear the underlying CRC buffer and return
//! an owned Digest struct to get ready for calculation.
//! With the Digest struct, you may keep writing `u32` values with function
//! [`digest.write_u32(value)`]. To read the digest value out, use [`digest.finish()`].
//! Further values can still be written after the digest value is read out.
//! After all the processes, you may stop writing to digests and get the `Crc` wrapper
//! back with function [`digest.free()`].
//! To release and turn off the clock to get CRC peripheral back, use [`crc.release()`].
//!
//! [`Crc`]: struct.Crc.html
//! [`Crc::crc`]: struct.Crc.html#method.crc
//! [`crc.new_digest()`]: struct.Crc.html#method.new_digest
//! [`Digest`]: struct.Digest.html
//! [`digest.write_u32(value)`]: struct.Digest.html#method.write_u32
//! [`digest.finish()`]: struct.Digest.html#method.finish
//! [`digest.free()`]: struct.Digest.html#method.free
//! [`crc.release()`]: struct.Crc.html#method.release
//!
//! ## Free data register `fdata`
//!
//! The `fdata` register provides you with additional 8-bit global storage. You may read
//! from or write to this register using function [`fdata_read`] and [`fdata_write`].
//!
//! [`fdata_read`]: fn.fdata_read.html
//! [`fdata_write`]: fn.fdata_write.html
//!
//! # Example
//!
//! This example is tested on GD32VF103C-START board. It calculated the CRC value of
//! `0xABCD1234`. The desired result is `0xF7018A40`; if the underlying hardware had
//! calculated correctly, PA7 is set high to turn on the LED with anode connected to
//! the MCU.
//!
//! ```
//! #![no_std]
//! #![no_main]
//!
//! use gd32vf103_hal as hal;
//! use hal::{crc::Crc, pac, prelude::*};
//! use panic_halt as _;
//!
//! #[riscv_rt::entry]
//! fn main() -> ! {
//! let dp = pac::Peripherals::take().unwrap();
//! let mut rcu = dp.RCU.constrain();
//! let mut gpioa = dp.GPIOA.split(&mut rcu.apb2);
//! let mut pa7 = gpioa.pa7.into_push_pull_output(&mut gpioa.ctl0);
//!
//! let src: u32 = 0xABCD1234;
//! let crc = Crc::crc(dp.CRC, &mut rcu.ahb);
//! let mut digest = crc.new_digest();
//! digest.write_u32(src);
//!
//! if digest.finish() == 0xF7018A40 {
//! pa7.set_high().unwrap();
//! }
//!
//! loop {}
//! }
//! ```
use crateCRC;
use crateAHB;
/// Read the value of the free data register `fdata`.
/// Write data to the free data register `fdata`.
/// CRC module abstraction
///
/// Owns `CRC_DATA` and `CRC_CTL`.
/// Digest struct for CRC calculation.
///
/// This struct is created using [`Crc::new_digest`] function. Use [`write_u32`]
/// function to write data for calculation; use [`finish`] to read the result.
/// After calculation, use [`free`] to get the Crc wrapper back.
///
/// [`Crc::new_digest`]: ./struct.Crc.html#method.new_digest
/// [`write_u32`]: #method.write_u32
/// [`finish`]: #method.finish
/// [`free`]: #method.free
///
/// # Examples
///
/// Calculate CRC result of single value:
///
/// ```no_run
/// // Write a single value
/// digest.write_u32(0x12345678);
/// // Read its CRC calculation result
/// let ans = digest.finish();
/// ```
///
/// Calculate CRC reuslt of an array of values:
///
/// ```no_run
/// // Write all values of an array
/// for value in array {
/// digest.write_u32(value);
/// }
/// // Read the CRC calculation result of this array
/// let ans = digest.finish();
/// ```