Skip to content

Commit 3d40f2a

Browse files
kyp44Dan Whitman
authored andcommitted
feat(icm): Migrates the icm::Icm peripheral to use the clock::v2.
* `Icm::new` now requires ownership of its `AhbClk` and `ApbClk`. * Renames `Icm::destroy` to the standard `Icm::free` method to free the `pac::Icm` and bus clocks. * Updates the `icm` module documentation example to include the above changes.
1 parent 8c88ace commit 3d40f2a

File tree

1 file changed

+94
-65
lines changed

1 file changed

+94
-65
lines changed

hal/src/peripherals/icm.rs

Lines changed: 94 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -121,28 +121,35 @@
121121
//!
122122
//! ```no_run
123123
//! # use atsamd_hal::{pac::Peripherals, icm::*};
124-
//!
125124
//! // SHA Test data
126125
//! 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,
129129
//! ];
130130
//!
131131
//! 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,
134135
//! ];
135136
//!
136137
//! // 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,
139141
//! ];
140142
//!
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,
143147
//! ];
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,
146153
//! ];
147154
//! static mut HASH: HashArea = HashArea::default();
148155
//! static mut ICM_REGION_DESC: Regions = Regions::default();
@@ -151,16 +158,21 @@
151158
//! //use cortex_m::singleton;
152159
//! //let hasharea: &'static mut HashArea = singleton!(: HashArea = HashArea::default()).unwrap();
153160
//!
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
160162
//! 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);
161173
//!
162174
//! // Create new ICM
163-
//! let mut icm = Icm::new(peripherals.ICM);
175+
//! let mut icm = Icm::new(peripherals.icm, clocks.ahbs.icm, apb_clk);
164176
//!
165177
//! // Reset the ICM, clearing past error states
166178
//! icm.swrst();
@@ -187,7 +199,9 @@
187199
//! icm_region0.set_rhc_int();
188200
//!
189201
//! // 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());
191205
//!
192206
//! // Configure the RCFG
193207
//!
@@ -203,7 +217,7 @@
203217
//! // in a descriptor of the main or second list
204218
//! icm_region_desc.region0.rcfg.set_rhien(false);
205219
//! // 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);
207221
//!
208222
//! // Get the interface for region1
209223
//! let icm_region1 = icm.enable_region1();
@@ -217,14 +231,16 @@
217231
//! icm_region1.set_rhc_int();
218232
//!
219233
//! // 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());
221237
//!
222238
//! // Configure the RCFG
223239
//! // The RHC flag is set when the field NEXT = 0
224240
//! // in a descriptor of the main or second list
225241
//! icm_region_desc.region1.rcfg.set_rhien(false);
226242
//! // 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);
228244
//!
229245
//! // Get the interface for region2
230246
//! let icm_region2 = icm.enable_region2();
@@ -238,14 +254,16 @@
238254
//! icm_region2.set_rhc_int();
239255
//!
240256
//! // 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());
242260
//!
243261
//! // Configure the RCFG
244262
//! // The RHC flag is set when the field NEXT = 0
245263
//! // in a descriptor of the main or second list
246264
//! icm_region_desc.region2.rcfg.set_rhien(false);
247265
//! // 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);
249267
//!
250268
//! // Get the interface for region3
251269
//! let icm_region3 = icm.enable_region3();
@@ -259,7 +277,9 @@
259277
//! icm_region3.set_rhc_int();
260278
//!
261279
//! // 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());
263283
//!
264284
//! // Configure the RCFG
265285
//! //
@@ -269,22 +289,18 @@
269289
//! // in a descriptor of the main or second list
270290
//! icm_region_desc.region3.rcfg.set_rhien(false);
271291
//! // 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);
288304
//!
289305
//! // Start the ICM calculation
290306
//! icm.enable();
@@ -318,8 +334,11 @@
318334
//! icm_region_desc
319335
//! .region0
320336
//! .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);
323342
//! // Activate Compare Digest (should be true when comparing memory)
324343
//! icm_region_desc.region0.rcfg.set_cdwbn(true);
325344
//! // Digest Mismatch Interrupt Disable (enabled)
@@ -332,8 +351,11 @@
332351
//! icm_region_desc
333352
//! .region1
334353
//! .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);
337359
//! // Activate Compare Digest (should be true when comparing memory)
338360
//! icm_region_desc.region1.rcfg.set_cdwbn(true);
339361
//! // Digest Mismatch Interrupt Disable (enabled)
@@ -346,8 +368,11 @@
346368
//! icm_region_desc
347369
//! .region2
348370
//! .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);
351376
//! // Activate Compare Digest (should be true when comparing memory)
352377
//! icm_region_desc.region2.rcfg.set_cdwbn(true);
353378
//! // Digest Mismatch Interrupt Disable (enabled)
@@ -360,8 +385,11 @@
360385
//! icm_region_desc
361386
//! .region3
362387
//! .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);
365393
//! // Activate Compare Digest (should be true when comparing memory)
366394
//! icm_region_desc.region3.rcfg.set_cdwbn(true);
367395
//! // Digest Mismatch Interrupt Disable (enabled)
@@ -378,7 +406,9 @@
378406
//! message_region2_sha224[5] = 0xDEAD_BEEF;
379407
//! message_region3_sha256[6] = 0xDEAD_BEEF;
380408
//!
381-
//! icm.enable()
409+
//! icm.enable();
410+
//! ```
411+
382412
use crate::pac::icm::uasr::Uratselect;
383413

384414
use paste::paste;
@@ -393,6 +423,9 @@ use core::marker::PhantomData;
393423
/// Reexport the User SHA Algorithm
394424
pub use crate::icm::cfg::Ualgoselect as icm_algorithm;
395425

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+
396429
// Convenient bitflags representing select parts of
397430
// the status interrupt register `ICM->ISR`
398431

@@ -877,23 +910,19 @@ impl<I: RegionNum> Region<I> {
877910
pub struct Icm {
878911
/// ICM pac register providing hardware access
879912
icm: crate::pac::Icm,
913+
_ahb_clk: IcmAhbClk,
914+
_apb_clk: IcmApbClk,
880915
}
881916

882917
impl Icm {
883918
/// 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();`
894919
#[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+
}
897926
}
898927

899928
// Register Interface
@@ -995,10 +1024,10 @@ impl Icm {
9951024
self.ctrl().write(|w| w.swrst().set_bit());
9961025
}
9971026

998-
/// Destroy the ICM peripheral and return the underlying ICM register
1027+
/// Destroy the ICM peripheral and return its resources
9991028
#[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)
10021031
}
10031032

10041033
// Region specifics

0 commit comments

Comments
 (0)