diff options
| author | Dario Nieuwenhuis <[email protected]> | 2024-02-13 01:36:11 +0100 |
|---|---|---|
| committer | GitHub <[email protected]> | 2024-02-13 01:36:11 +0100 |
| commit | 8c82d1bcbcb8b04f18e9fd2a2d388a316bc50b49 (patch) | |
| tree | 3ef1dcdc7f98476e3febbd1515b1cc7221d1c9b7 /examples | |
| parent | e8c998aad882d766988ac2c0cb0c357c600b28c1 (diff) | |
| parent | 7bf044278e8c85b5ea6dbe3de6b3cdea884c995a (diff) | |
Merge pull request #2528 from caleb-garrett/hash
STM32 Hash Accelerator
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f7/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/stm32f7/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/eth.rs | 2 | ||||
| -rw-r--r-- | examples/stm32f7/src/bin/hash.rs | 56 |
4 files changed, 61 insertions, 4 deletions
diff --git a/examples/stm32f7/.cargo/config.toml b/examples/stm32f7/.cargo/config.toml index 9088eea6e..086da2d78 100644 --- a/examples/stm32f7/.cargo/config.toml +++ b/examples/stm32f7/.cargo/config.toml | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] | 1 | [target.'cfg(all(target_arch = "arm", target_os = "none"))'] |
| 2 | # replace STM32F429ZITx with your chip as listed in `probe-rs chip list` | 2 | # replace STM32F429ZITx with your chip as listed in `probe-rs chip list` |
| 3 | runner = "probe-rs run --chip STM32F767ZITx" | 3 | runner = "probe-rs run --chip STM32F777ZITx" |
| 4 | 4 | ||
| 5 | [build] | 5 | [build] |
| 6 | target = "thumbv7em-none-eabihf" | 6 | target = "thumbv7em-none-eabihf" |
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/eth.rs b/examples/stm32f7/src/bin/eth.rs index 5bff48197..9a608e909 100644 --- a/examples/stm32f7/src/bin/eth.rs +++ b/examples/stm32f7/src/bin/eth.rs | |||
| @@ -19,7 +19,7 @@ use {defmt_rtt as _, panic_probe as _}; | |||
| 19 | 19 | ||
| 20 | bind_interrupts!(struct Irqs { | 20 | bind_interrupts!(struct Irqs { |
| 21 | ETH => eth::InterruptHandler; | 21 | ETH => eth::InterruptHandler; |
| 22 | RNG => rng::InterruptHandler<peripherals::RNG>; | 22 | HASH_RNG => rng::InterruptHandler<peripherals::RNG>; |
| 23 | }); | 23 | }); |
| 24 | 24 | ||
| 25 | type Device = Ethernet<'static, ETH, GenericSMI>; | 25 | type Device = Ethernet<'static, ETH, GenericSMI>; |
diff --git a/examples/stm32f7/src/bin/hash.rs b/examples/stm32f7/src/bin/hash.rs new file mode 100644 index 000000000..96e50f84b --- /dev/null +++ b/examples/stm32f7/src/bin/hash.rs | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use defmt::info; | ||
| 5 | use embassy_executor::Spawner; | ||
| 6 | use embassy_stm32::hash::*; | ||
| 7 | use embassy_stm32::{bind_interrupts, hash, peripherals, Config}; | ||
| 8 | use embassy_time::Instant; | ||
| 9 | use sha2::{Digest, Sha256}; | ||
| 10 | use {defmt_rtt as _, panic_probe as _}; | ||
| 11 | |||
| 12 | bind_interrupts!(struct Irqs { | ||
| 13 | HASH_RNG => hash::InterruptHandler<peripherals::HASH>; | ||
| 14 | }); | ||
| 15 | |||
| 16 | #[embassy_executor::main] | ||
| 17 | async fn main(_spawner: Spawner) -> ! { | ||
| 18 | let config = Config::default(); | ||
| 19 | let p = embassy_stm32::init(config); | ||
| 20 | |||
| 21 | let test_1: &[u8] = b"as;dfhaslfhas;oifvnasd;nifvnhasd;nifvhndlkfghsd;nvfnahssdfgsdafgsasdfasdfasdfasdfasdfghjklmnbvcalskdjghalskdjgfbaslkdjfgbalskdjgbalskdjbdfhsdfhsfghsfghfgh"; | ||
| 22 | let test_2: &[u8] = b"fdhalksdjfhlasdjkfhalskdjfhgal;skdjfgalskdhfjgalskdjfglafgadfgdfgdafgaadsfgfgdfgadrgsyfthxfgjfhklhjkfgukhulkvhlvhukgfhfsrghzdhxyfufynufyuszeradrtydyytserr"; | ||
| 23 | |||
| 24 | let mut hw_hasher = Hash::new(p.HASH, p.DMA2_CH7, Irqs); | ||
| 25 | |||
| 26 | let hw_start_time = Instant::now(); | ||
| 27 | |||
| 28 | // Compute a digest in hardware. | ||
| 29 | let mut context = hw_hasher.start(Algorithm::SHA256, DataType::Width8); | ||
| 30 | hw_hasher.update(&mut context, test_1).await; | ||
| 31 | hw_hasher.update(&mut context, test_2).await; | ||
| 32 | let mut hw_digest: [u8; 32] = [0; 32]; | ||
| 33 | hw_hasher.finish(context, &mut hw_digest).await; | ||
| 34 | |||
| 35 | let hw_end_time = Instant::now(); | ||
| 36 | let hw_execution_time = hw_end_time - hw_start_time; | ||
| 37 | |||
| 38 | let sw_start_time = Instant::now(); | ||
| 39 | |||
| 40 | // Compute a digest in software. | ||
| 41 | let mut sw_hasher = Sha256::new(); | ||
| 42 | sw_hasher.update(test_1); | ||
| 43 | sw_hasher.update(test_2); | ||
| 44 | let sw_digest = sw_hasher.finalize(); | ||
| 45 | |||
| 46 | let sw_end_time = Instant::now(); | ||
| 47 | let sw_execution_time = sw_end_time - sw_start_time; | ||
| 48 | |||
| 49 | info!("Hardware Digest: {:?}", hw_digest); | ||
| 50 | info!("Software Digest: {:?}", sw_digest[..]); | ||
| 51 | info!("Hardware Execution Time: {:?}", hw_execution_time); | ||
| 52 | info!("Software Execution Time: {:?}", sw_execution_time); | ||
| 53 | assert_eq!(hw_digest, sw_digest[..]); | ||
| 54 | |||
| 55 | loop {} | ||
| 56 | } | ||
