diff options
| author | Caleb Garrett <[email protected]> | 2024-01-31 21:21:36 -0500 |
|---|---|---|
| committer | Caleb Garrett <[email protected]> | 2024-01-31 21:21:36 -0500 |
| commit | 6e9ddd46267fd0fce2333af4f15bfd86f6f17f4d (patch) | |
| tree | b5614fec7c6e3e32e13c01dac6e050e32831fd77 /examples/stm32f7 | |
| parent | dcce40c8a2eefb956ffadbfcc3db6c27cde55dab (diff) | |
Added hash module with blocking implementation. Included SHA256 example.
Diffstat (limited to 'examples/stm32f7')
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/hash.rs | 49 |
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" | |||
| 5 | license = "MIT OR Apache-2.0" | 5 | license = "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. |
| 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f767zi", "memory-x", "unstable-pac", "time-driver-any", "exti"] } | 9 | embassy-stm32 = { version = "0.1.0", path = "../../embassy-stm32", features = ["defmt", "stm32f777zi", "memory-x", "unstable-pac", "time-driver-any", "exti"] } |
| 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.5.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.5.0", path = "../../embassy-executor", features = ["task-arena-size-32768", "arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.3.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime", "tick-hz-32_768"] } | 12 | embassy-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" | |||
| 28 | critical-section = "1.1" | 28 | critical-section = "1.1" |
| 29 | embedded-storage = "0.3.1" | 29 | embedded-storage = "0.3.1" |
| 30 | static_cell = "2" | 30 | static_cell = "2" |
| 31 | sha2 = { version = "0.10.8", default-features = false } | ||
| 31 | 32 | ||
| 32 | [profile.release] | 33 | [profile.release] |
| 33 | debug = 2 | 34 | debug = 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 | |||
| 4 | use defmt::info; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::Config; | ||
| 7 | use embassy_time::{Duration, Instant}; | ||
| 8 | use {defmt_rtt as _, panic_probe as _}; | ||
| 9 | |||
| 10 | use embassy_stm32::hash::*; | ||
| 11 | use sha2::{Digest, Sha256}; | ||
| 12 | |||
| 13 | const TEST_STRING_1: &[u8] = b"hello world"; | ||
| 14 | |||
| 15 | #[embassy_executor::main] | ||
| 16 | async 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 | } | ||
