121
121
//!
122
122
//! ```no_run
123
123
//! # use atsamd_hal::{pac::Peripherals, icm::*};
124
- //!
125
124
//! // SHA Test data
126
125
//! static MESSAGE_REF0: [u32; 16] = [
127
- //! 0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777, 0x88888888,
128
- //! 0x99999999, 0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd, 0xeeeeeeee, 0xffffffff, 0x00000000,
126
+ //! 0x11111111, 0x22222222, 0x33333333, 0x44444444, 0x55555555, 0x66666666, 0x77777777,
127
+ //! 0x88888888, 0x99999999, 0xaaaaaaaa, 0xbbbbbbbb, 0xcccccccc, 0xdddddddd, 0xeeeeeeee,
128
+ //! 0xffffffff, 0x00000000,
129
129
//! ];
130
130
//!
131
131
//! static MESSAGE_REF1: [u32; 16] = [
132
- //! 0x80636261, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
133
- //! 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x18000000,
132
+ //! 0x80636261, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
133
+ //! 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000,
134
+ //! 0x00000000, 0x18000000,
134
135
//! ];
135
136
//!
136
137
//! // Expected SHA1 sum result
137
- //! static MESSAGE_SHA1_RES: [u32; 8] = [
138
- //! 0x363e99a9, 0x6a810647, 0x71253eba, 0x6cc25078, 0x9dd8d09c, 0x00000000, 0x00000000, 0x00000000,
138
+ //! static _MESSAGE_SHA1_RES: [u32; 8] = [
139
+ //! 0x363e99a9, 0x6a810647, 0x71253eba, 0x6cc25078, 0x9dd8d09c, 0x00000000, 0x00000000,
140
+ //! 0x00000000,
139
141
//! ];
140
142
//!
141
- //! static MESSAGE_SHA224_RES: [u32; 8] = [
142
- //! 0x227d0923, 0x22d80534, 0x77a44286, 0xb355a2bd, 0xe4bcad2a, 0xf7b3a0bd, 0xa79d6ce3, 0x00000000,
143
+ //! // Expected SHA224 sum result
144
+ //! static _MESSAGE_SHA224_RES: [u32; 8] = [
145
+ //! 0x227d0923, 0x22d80534, 0x77a44286, 0xb355a2bd, 0xe4bcad2a, 0xf7b3a0bd, 0xa79d6ce3,
146
+ //! 0x00000000,
143
147
//! ];
144
- //! static MESSAGE_SHA256_RES: [u32; 8] = [
145
- //! 0xbf1678ba, 0xeacf018f, 0xde404141, 0x2322ae5d, 0xa36103b0, 0x9c7a1796, 0x61ff10b4, 0xad1500f2,
148
+ //!
149
+ //! // Expected SHA256 sum result
150
+ //! static _MESSAGE_SHA256_RES: [u32; 8] = [
151
+ //! 0xbf1678ba, 0xeacf018f, 0xde404141, 0x2322ae5d, 0xa36103b0, 0x9c7a1796, 0x61ff10b4,
152
+ //! 0xad1500f2,
146
153
//! ];
147
154
//! static mut HASH: HashArea = HashArea::default();
148
155
//! static mut ICM_REGION_DESC: Regions = Regions::default();
151
158
//! //use cortex_m::singleton;
152
159
//! //let hasharea: &'static mut HashArea = singleton!(: HashArea = HashArea::default()).unwrap();
153
160
//!
154
- //! // Enable ICM apb clock
155
- //! // Clock v1
156
- //! //mclk.apbcmask.modify(|_, w| w.icm_().set_bit());
157
- //! // Clock v2
158
- //! //tokens.apbs.icm.enable();
159
- //!
161
+ //! // Setup the clocks
160
162
//! let mut peripherals = Peripherals::take().unwrap();
163
+ //! let (mut buses, clocks, tokens) = hal::clock::v2::clock_system_at_reset(
164
+ //! peripherals.oscctrl,
165
+ //! peripherals.osc32kctrl,
166
+ //! peripherals.gclk,
167
+ //! peripherals.mclk,
168
+ //! &mut peripherals.nvmctrl,
169
+ //! );
170
+ //!
171
+ //! // Enable the APB clock
172
+ //! let apb_clk = buses.apb.enable(tokens.apbs.icm);
161
173
//!
162
174
//! // Create new ICM
163
- //! let mut icm = Icm::new(peripherals.ICM );
175
+ //! let mut icm = Icm::new(peripherals.icm, clocks.ahbs.icm, apb_clk );
164
176
//!
165
177
//! // Reset the ICM, clearing past error states
166
178
//! icm.swrst();
187
199
//! icm_region0.set_rhc_int();
188
200
//!
189
201
//! // Region0 raddr
190
- //! icm_region_desc.region0.set_region_address(MESSAGE_REF0.as_ptr());
202
+ //! icm_region_desc
203
+ //! .region0
204
+ //! .set_region_address(MESSAGE_REF0.as_ptr());
191
205
//!
192
206
//! // Configure the RCFG
193
207
//!
203
217
//! // in a descriptor of the main or second list
204
218
//! icm_region_desc.region0.rcfg.set_rhien(false);
205
219
//! // Set Algorithm to SHA1
206
- //! icm_region_desc.region0.rcfg.set_algo(icm_algorithm::SHA1 );
220
+ //! icm_region_desc.region0.rcfg.set_algo(icm_algorithm::Sha1 );
207
221
//!
208
222
//! // Get the interface for region1
209
223
//! let icm_region1 = icm.enable_region1();
217
231
//! icm_region1.set_rhc_int();
218
232
//!
219
233
//! // Region1 raddr
220
- //! icm_region_desc.region1.set_region_address(MESSAGE_REF1.as_ptr());
234
+ //! icm_region_desc
235
+ //! .region1
236
+ //! .set_region_address(MESSAGE_REF1.as_ptr());
221
237
//!
222
238
//! // Configure the RCFG
223
239
//! // The RHC flag is set when the field NEXT = 0
224
240
//! // in a descriptor of the main or second list
225
241
//! icm_region_desc.region1.rcfg.set_rhien(false);
226
242
//! // Set Algorithm to SHA1
227
- //! icm_region_desc.region1.rcfg.set_algo(icm_algorithm::SHA1 );
243
+ //! icm_region_desc.region1.rcfg.set_algo(icm_algorithm::Sha1 );
228
244
//!
229
245
//! // Get the interface for region2
230
246
//! let icm_region2 = icm.enable_region2();
238
254
//! icm_region2.set_rhc_int();
239
255
//!
240
256
//! // Region2 raddr
241
- //! icm_region_desc.region2.set_region_address(MESSAGE_REF1.as_ptr());
257
+ //! icm_region_desc
258
+ //! .region2
259
+ //! .set_region_address(MESSAGE_REF1.as_ptr());
242
260
//!
243
261
//! // Configure the RCFG
244
262
//! // The RHC flag is set when the field NEXT = 0
245
263
//! // in a descriptor of the main or second list
246
264
//! icm_region_desc.region2.rcfg.set_rhien(false);
247
265
//! // Set Algorithm to SHA224
248
- //! icm_region_desc.region2.rcfg.set_algo(icm_algorithm::SHA224 );
266
+ //! icm_region_desc.region2.rcfg.set_algo(icm_algorithm::Sha224 );
249
267
//!
250
268
//! // Get the interface for region3
251
269
//! let icm_region3 = icm.enable_region3();
259
277
//! icm_region3.set_rhc_int();
260
278
//!
261
279
//! // Region3 raddr
262
- //! icm_region_desc.region3.set_region_address(MESSAGE_REF1.as_ptr());
280
+ //! icm_region_desc
281
+ //! .region3
282
+ //! .set_region_address(MESSAGE_REF1.as_ptr());
263
283
//!
264
284
//! // Configure the RCFG
265
285
//! //
269
289
//! // in a descriptor of the main or second list
270
290
//! icm_region_desc.region3.rcfg.set_rhien(false);
271
291
//! // Set Algorithm to SHA256
272
- //! icm_region_desc.region3.rcfg.set_algo(icm_algorithm::SHA256);
273
- //!
274
- //! unsafe {
275
- //! // Hash Area
276
- //! // Set HASH addr to the beginning of the Hash area
277
- //! icm.set_hash_addr(&HASH);
278
- //! }
279
- //!
280
- //! unsafe {
281
- //! // Move the icm_region_desc into static
282
- //! ICM_REGION_DESC = icm_region_desc;
283
- //! // Set DSCR to the beginning of the region descriptor
284
- //! icm.set_dscr_addr(&ICM_REGION_DESC.region0);
285
- //! // the same but via helper function
286
- //! //ICM_REGION_DESC.region0.set_dscr_addr(&icm);
287
- //! }
292
+ //! icm_region_desc.region3.rcfg.set_algo(icm_algorithm::Sha256);
293
+ //!
294
+ //! // Hash Area
295
+ //! // Set HASH addr to the beginning of the Hash area
296
+ //! icm.set_hash_addr(&HASH);
297
+ //!
298
+ //! // Move the icm_region_desc into static
299
+ //! *ICM_REGION_DESC = icm_region_desc;
300
+ //! // Set DSCR to the beginning of the region descriptor
301
+ //! icm.set_dscr_addr(&ICM_REGION_DESC.region0);
302
+ //! // the same but via helper function
303
+ //! //ICM_REGION_DESC.region0.set_dscr_addr(&icm);
288
304
//!
289
305
//! // Start the ICM calculation
290
306
//! icm.enable();
318
334
//! icm_region_desc
319
335
//! .region0
320
336
//! .set_region_address(&message_region0_sha1);
321
- //! icm_region_desc.region0.rcfg.reset_region_configuration_to_default();
322
- //! icm_region_desc.region0.rcfg.set_algo(icm_algorithm::SHA1);
337
+ //! icm_region_desc
338
+ //! .region0
339
+ //! .rcfg
340
+ //! .reset_region_configuration_to_default();
341
+ //! icm_region_desc.region0.rcfg.set_algo(icm_algorithm::Sha1);
323
342
//! // Activate Compare Digest (should be true when comparing memory)
324
343
//! icm_region_desc.region0.rcfg.set_cdwbn(true);
325
344
//! // Digest Mismatch Interrupt Disable (enabled)
332
351
//! icm_region_desc
333
352
//! .region1
334
353
//! .set_region_address(&message_region1_sha1);
335
- //! icm_region_desc.region1.rcfg.reset_region_configuration_to_default();
336
- //! icm_region_desc.region1.rcfg.set_algo(icm_algorithm::SHA1);
354
+ //! icm_region_desc
355
+ //! .region1
356
+ //! .rcfg
357
+ //! .reset_region_configuration_to_default();
358
+ //! icm_region_desc.region1.rcfg.set_algo(icm_algorithm::Sha1);
337
359
//! // Activate Compare Digest (should be true when comparing memory)
338
360
//! icm_region_desc.region1.rcfg.set_cdwbn(true);
339
361
//! // Digest Mismatch Interrupt Disable (enabled)
346
368
//! icm_region_desc
347
369
//! .region2
348
370
//! .set_region_address(&message_region2_sha224);
349
- //! icm_region_desc.region2.rcfg.reset_region_configuration_to_default();
350
- //! icm_region_desc.region2.rcfg.set_algo(icm_algorithm::SHA224);
371
+ //! icm_region_desc
372
+ //! .region2
373
+ //! .rcfg
374
+ //! .reset_region_configuration_to_default();
375
+ //! icm_region_desc.region2.rcfg.set_algo(icm_algorithm::Sha224);
351
376
//! // Activate Compare Digest (should be true when comparing memory)
352
377
//! icm_region_desc.region2.rcfg.set_cdwbn(true);
353
378
//! // Digest Mismatch Interrupt Disable (enabled)
360
385
//! icm_region_desc
361
386
//! .region3
362
387
//! .set_region_address(&message_region3_sha256);
363
- //! icm_region_desc.region3.rcfg.reset_region_configuration_to_default();
364
- //! icm_region_desc.region3.rcfg.set_algo(icm_algorithm::SHA256);
388
+ //! icm_region_desc
389
+ //! .region3
390
+ //! .rcfg
391
+ //! .reset_region_configuration_to_default();
392
+ //! icm_region_desc.region3.rcfg.set_algo(icm_algorithm::Sha256);
365
393
//! // Activate Compare Digest (should be true when comparing memory)
366
394
//! icm_region_desc.region3.rcfg.set_cdwbn(true);
367
395
//! // Digest Mismatch Interrupt Disable (enabled)
378
406
//! message_region2_sha224[5] = 0xDEAD_BEEF;
379
407
//! message_region3_sha256[6] = 0xDEAD_BEEF;
380
408
//!
381
- //! icm.enable()
409
+ //! icm.enable();
410
+ //! ```
411
+
382
412
use crate :: pac:: icm:: uasr:: Uratselect ;
383
413
384
414
use paste:: paste;
@@ -393,6 +423,9 @@ use core::marker::PhantomData;
393
423
/// Reexport the User SHA Algorithm
394
424
pub use crate :: icm:: cfg:: Ualgoselect as icm_algorithm;
395
425
426
+ type IcmAhbClk = crate :: clock:: v2:: ahb:: AhbClk < crate :: clock:: v2:: types:: Icm > ;
427
+ type IcmApbClk = crate :: clock:: v2:: apb:: ApbClk < crate :: clock:: v2:: types:: Icm > ;
428
+
396
429
// Convenient bitflags representing select parts of
397
430
// the status interrupt register `ICM->ISR`
398
431
@@ -877,23 +910,19 @@ impl<I: RegionNum> Region<I> {
877
910
pub struct Icm {
878
911
/// ICM pac register providing hardware access
879
912
icm : crate :: pac:: Icm ,
913
+ _ahb_clk : IcmAhbClk ,
914
+ _apb_clk : IcmApbClk ,
880
915
}
881
916
882
917
impl Icm {
883
918
/// Create the interface for the ICM peripheral
884
- ///
885
- /// Don't forget to enable the `APB` bus for ICM
886
- ///
887
- /// `AHB` bus is on by default at reset
888
- ///
889
- /// Clock::v1
890
- /// `mclk.apbcmask.modify(|_, w| w.icm_().set_bit());`
891
- ///
892
- /// Clock::v2
893
- /// `tokens.apbs.icm.enable();`
894
919
#[ inline]
895
- pub fn new ( icm : crate :: pac:: Icm ) -> Self {
896
- Self { icm }
920
+ pub fn new ( icm : crate :: pac:: Icm , ahb_clk : IcmAhbClk , apb_clk : IcmApbClk ) -> Self {
921
+ Self {
922
+ icm,
923
+ _ahb_clk : ahb_clk,
924
+ _apb_clk : apb_clk,
925
+ }
897
926
}
898
927
899
928
// Register Interface
@@ -995,10 +1024,10 @@ impl Icm {
995
1024
self . ctrl ( ) . write ( |w| w. swrst ( ) . set_bit ( ) ) ;
996
1025
}
997
1026
998
- /// Destroy the ICM peripheral and return the underlying ICM register
1027
+ /// Destroy the ICM peripheral and return its resources
999
1028
#[ inline]
1000
- pub fn destroy ( self ) -> crate :: pac:: Icm {
1001
- self . icm
1029
+ pub fn free ( self ) -> ( crate :: pac:: Icm , IcmAhbClk , IcmApbClk ) {
1030
+ ( self . icm , self . _ahb_clk , self . _apb_clk )
1002
1031
}
1003
1032
1004
1033
// Region specifics
0 commit comments