aboutsummaryrefslogtreecommitdiff
path: root/examples/stm32f7
diff options
context:
space:
mode:
authorCaleb Garrett <[email protected]>2024-01-31 21:21:36 -0500
committerCaleb Garrett <[email protected]>2024-01-31 21:21:36 -0500
commit6e9ddd46267fd0fce2333af4f15bfd86f6f17f4d (patch)
treeb5614fec7c6e3e32e13c01dac6e050e32831fd77 /examples/stm32f7
parentdcce40c8a2eefb956ffadbfcc3db6c27cde55dab (diff)
Added hash module with blocking implementation. Included SHA256 example.
Diffstat (limited to 'examples/stm32f7')
-rw-r--r--examples/stm32f7/Cargo.toml5
-rw-r--r--examples/stm32f7/src/bin/hash.rs49
2 files changed, 52 insertions, 2 deletions
diff --git a/examples/stm32f7/Cargo.toml b/examples/stm32f7/Cargo.toml
index 941ba38cd..a612c2554 100644
--- a/examples/stm32f7/Cargo.toml
+++ b/examples/stm32f7/Cargo.toml
@@ -5,8 +5,8 @@ version = "0.1.0"
5license = "MIT OR Apache-2.0" 5license = "MIT OR Apache-2.0"
6 6
7[dependencies] 7[dependencies]
8# Change stm32f767zi to your chip name, if necessary. 8# Change stm32f777zi to your chip name, if necessary.
9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f767zi", "memory-x", "unstable-pac", "time-driver-any", "exti"] } 9embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f777zi", "memory-x", "unstable-pac", "time-driver-any", "exti"] }
10embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } 12embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] }
@@ -28,6 +28,7 @@ rand_core = "0.6.3"
28critical-section = "1.1" 28critical-section = "1.1"
29embedded-storage = "0.3.1" 29embedded-storage = "0.3.1"
30static_cell = "2" 30static_cell = "2"
31sha2 = { version = "0.10.8", default-features = false }
31 32
32[profile.release] 33[profile.release]
33debug = 2 34debug = 2
diff --git a/examples/stm32f7/src/bin/hash.rs b/examples/stm32f7/src/bin/hash.rs
new file mode 100644
index 000000000..1fd0e87eb
--- /dev/null
+++ b/examples/stm32f7/src/bin/hash.rs
@@ -0,0 +1,49 @@
1#![no_std]
2#![no_main]
3
4use defmt::info;
5use embassy_executor::Spawner;
6use embassy_stm32::Config;
7use embassy_time::{Duration, Instant};
8use {defmt_rtt as _, panic_probe as _};
9
10use embassy_stm32::hash::*;
11use sha2::{Digest, Sha256};
12
13const TEST_STRING_1: &[u8] = b"hello world";
14
15#[embassy_executor::main]
16async fn main(_spawner: Spawner) -> ! {
17 let config = Config::default();
18 let p = embassy_stm32::init(config);
19
20 let hw_start_time = Instant::now();
21
22 // Compute a digest in hardware.
23 let mut hw_hasher = Hash::new(p.HASH);
24 let mut context = hw_hasher.start(Algorithm::SHA256, DataType::Width8);
25 hw_hasher.update(&mut context, TEST_STRING_1);
26 let mut buffer: [u8; 32] = [0; 32];
27 let hw_digest = hw_hasher.finish(context, &mut buffer);
28
29 let hw_end_time = Instant::now();
30 let hw_execution_time = hw_end_time - hw_start_time;
31
32 let sw_start_time = Instant::now();
33
34 // Compute a digest in software.
35 let mut sw_hasher = Sha256::new();
36 sw_hasher.update(TEST_STRING_1);
37 let sw_digest = sw_hasher.finalize();
38
39 let sw_end_time = Instant::now();
40 let sw_execution_time = sw_end_time - sw_start_time;
41
42 info!("Hardware Digest: {:?}", hw_digest);
43 info!("Software Digest: {:?}", sw_digest[..]);
44 info!("Hardware Execution Time: {:?}", hw_execution_time);
45 info!("Software Execution Time: {:?}", sw_execution_time);
46 assert_eq!(*hw_digest, sw_digest[..]);
47
48 loop {}
49}