Skip to content

Commit 52464b3

Browse files
authored
Merge branch 'main' into i2c-pull-down-fix
2 parents 8aac4aa + 61dbd89 commit 52464b3

File tree

6 files changed

+73
-23
lines changed

6 files changed

+73
-23
lines changed

embassy-stm32/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
<!-- next-header -->
99
## Unreleased - ReleaseDate
1010

11-
- fix: Fix vrefbuf building with log feature
1211
- fix: stm32/i2c: pull-down was enabled instead of pull-none when no internal pull-up was needed.
12+
- feat: Improve blocking hash speed
13+
- fix: Fix vrefbuf building with log feature
14+
- fix: Fix performing a hash after performing a hmac
15+
- chore: Updated stm32-metapac and stm32-data dependencies
1316

1417
## 0.3.0 - 2025-08-12
1518

embassy-stm32/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ cortex-m = "0.7.6"
8080
futures-util = { version = "0.3.30", default-features = false }
8181
sdio-host = "0.9.0"
8282
critical-section = "1.1"
83-
stm32-metapac = { version = "17" }
84-
# stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-9941f338734a2e6c1652267f64b13f7b35d8c9db" }
83+
# stm32-metapac = { version = "17" }
84+
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-ecb93d42a6cbcd9e09cab74873908a2ca22327f7" }
8585

8686
vcell = "0.1.3"
8787
nb = "1.0.0"
@@ -109,8 +109,8 @@ proptest-state-machine = "0.3.0"
109109
proc-macro2 = "1.0.36"
110110
quote = "1.0.15"
111111

112-
stm32-metapac = { version = "17", default-features = false, features = ["metadata"]}
113-
#stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-9941f338734a2e6c1652267f64b13f7b35d8c9db", default-features = false, features = ["metadata"] }
112+
# stm32-metapac = { version = "17", default-features = false, features = ["metadata"]}
113+
stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-ecb93d42a6cbcd9e09cab74873908a2ca22327f7", default-features = false, features = ["metadata"] }
114114

115115
[features]
116116
default = ["rt"]

embassy-stm32/src/hash/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ impl<'d, T: Instance, M: Mode> Hash<'d, T, M> {
198198
if key.len() > 64 {
199199
T::regs().cr().modify(|w| w.set_lkey(true));
200200
}
201+
} else {
202+
T::regs().cr().modify(|w| w.set_mode(false));
201203
}
202204

203205
T::regs().cr().modify(|w| w.set_init(true));
@@ -351,13 +353,17 @@ impl<'d, T: Instance, M: Mode> Hash<'d, T, M> {
351353
let num_valid_bits: u8 = (8 * (input.len() % 4)) as u8;
352354
T::regs().str().modify(|w| w.set_nblw(num_valid_bits));
353355

354-
let mut i = 0;
355-
while i < input.len() {
356+
let mut chunks = input.chunks_exact(4);
357+
for chunk in &mut chunks {
358+
T::regs()
359+
.din()
360+
.write_value(u32::from_ne_bytes(chunk.try_into().unwrap()));
361+
}
362+
let rem = chunks.remainder();
363+
if !rem.is_empty() {
356364
let mut word: [u8; 4] = [0; 4];
357-
let copy_idx = min(i + 4, input.len());
358-
word[0..copy_idx - i].copy_from_slice(&input[i..copy_idx]);
365+
word[0..rem.len()].copy_from_slice(rem);
359366
T::regs().din().write_value(u32::from_ne_bytes(word));
360-
i += 4;
361367
}
362368
}
363369

embassy-time/src/delay.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use core::future::Future;
2+
13
use super::{Duration, Instant};
24
use crate::Timer;
35

@@ -32,16 +34,16 @@ impl embedded_hal_1::delay::DelayNs for Delay {
3234
}
3335

3436
impl embedded_hal_async::delay::DelayNs for Delay {
35-
async fn delay_ns(&mut self, ns: u32) {
36-
Timer::after_nanos(ns as _).await
37+
fn delay_ns(&mut self, ns: u32) -> impl Future<Output = ()> {
38+
Timer::after_nanos(ns as _)
3739
}
3840

39-
async fn delay_us(&mut self, us: u32) {
40-
Timer::after_micros(us as _).await
41+
fn delay_us(&mut self, us: u32) -> impl Future<Output = ()> {
42+
Timer::after_micros(us as _)
4143
}
4244

43-
async fn delay_ms(&mut self, ms: u32) {
44-
Timer::after_millis(ms as _).await
45+
fn delay_ms(&mut self, ms: u32) -> impl Future<Output = ()> {
46+
Timer::after_millis(ms as _)
4547
}
4648
}
4749

tests/stm32/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ stm32f446re = ["embassy-stm32/stm32f446re", "spi-v1", "chrono", "stop", "can", "
1616
stm32f767zi = ["embassy-stm32/stm32f767zi", "chrono", "not-gpdma", "eth", "rng", "single-bank"]
1717
stm32g071rb = ["embassy-stm32/stm32g071rb", "cm0", "not-gpdma", "dac", "ucpd"]
1818
stm32g491re = ["embassy-stm32/stm32g491re", "chrono", "stop", "not-gpdma", "rng", "fdcan", "cordic"]
19-
stm32h563zi = ["embassy-stm32/stm32h563zi", "spi-v345", "chrono", "eth", "rng", "fdcan", "hash", "cordic", "stop"]
19+
stm32h563zi = ["embassy-stm32/stm32h563zi", "spi-v345", "chrono", "eth", "rng", "fdcan", "hash-v34", "cordic", "stop"]
2020
stm32h753zi = ["embassy-stm32/stm32h753zi", "spi-v345", "chrono", "not-gpdma", "eth", "rng", "fdcan", "hash", "cryp"]
2121
stm32h755zi = ["embassy-stm32/stm32h755zi-cm7", "spi-v345", "chrono", "not-gpdma", "eth", "dac", "rng", "fdcan", "hash", "cryp"]
2222
stm32h7a3zi = ["embassy-stm32/stm32h7a3zi", "spi-v345", "not-gpdma", "rng", "fdcan"]
@@ -33,13 +33,14 @@ stm32wba52cg = ["embassy-stm32/stm32wba52cg", "spi-v345", "chrono", "rng", "hash
3333
stm32wl55jc = ["embassy-stm32/stm32wl55jc-cm4", "not-gpdma", "rng", "chrono"]
3434
stm32f091rc = ["embassy-stm32/stm32f091rc", "cm0", "not-gpdma", "chrono"]
3535
stm32h503rb = ["embassy-stm32/stm32h503rb", "spi-v345", "rng", "stop"]
36-
stm32h7s3l8 = ["embassy-stm32/stm32h7s3l8", "spi-v345", "rng", "cordic", "hash"] # TODO: fdcan crashes, cryp dma hangs.
36+
stm32h7s3l8 = ["embassy-stm32/stm32h7s3l8", "spi-v345", "rng", "cordic", "hash-v34"] # TODO: fdcan crashes, cryp dma hangs.
3737
stm32u083rc = ["embassy-stm32/stm32u083rc", "cm0", "rng", "chrono"]
3838

3939
spi-v1 = []
4040
spi-v345 = []
4141
cryp = []
4242
hash = []
43+
hash-v34 = ["hash"]
4344
eth = []
4445
rng = []
4546
sdmmc = []

tests/stm32/src/bin/hash.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod common;
77
use common::*;
88
use embassy_executor::Spawner;
99
use embassy_stm32::hash::*;
10+
use embassy_stm32::mode::Blocking;
1011
use embassy_stm32::{bind_interrupts, hash, peripherals};
1112
use hmac::{Hmac, Mac};
1213
use sha2::{Digest, Sha224, Sha256};
@@ -32,11 +33,7 @@ bind_interrupts!(struct Irqs {
3233
HASH => hash::InterruptHandler<peripherals::HASH>;
3334
});
3435

35-
#[embassy_executor::main]
36-
async fn main(_spawner: Spawner) {
37-
let p: embassy_stm32::Peripherals = init();
38-
let mut hw_hasher = Hash::new_blocking(p.HASH, Irqs);
39-
36+
fn test_interrupt(hw_hasher: &mut Hash<'_, peripherals::HASH, Blocking>) {
4037
let test_1: &[u8] = b"as;dfhaslfhas;oifvnasd;nifvnhasd;nifvhndlkfghsd;nvfnahssdfgsdafgsasdfasdfasdfasdfasdfghjklmnbvcalskdjghalskdjgfbaslkdjfgbalskdjgbalskdjbdfhsdfhsfghsfghfgh";
4138
let test_2: &[u8] = b"fdhalksdjfhlasdjkfhalskdjfhgal;skdjfgalskdhfjgalskdjfglafgadfgdfgdafgaadsfgfgdfgadrgsyfthxfgjfhklhjkfgukhulkvhlvhukgfhfsrghzdhxyfufynufyuszeradrtydyytserr";
4239
let test_3: &[u8] = b"a.ewtkluGWEBR.KAJRBTA,RMNRBG,FDMGB.kger.tkasjrbt.akrjtba.krjtba.ktmyna,nmbvtyliasd;gdrtba,sfvs.kgjzshd.gkbsr.tksejb.SDkfBSE.gkfgb>ESkfbSE>gkJSBESE>kbSE>fk";
@@ -95,6 +92,47 @@ async fn main(_spawner: Spawner) {
9592
info!("Hardware HMAC: {:?}", hw_hmac);
9693
info!("Software HMAC: {:?}", sw_hmac[..]);
9794
defmt::assert!(hw_hmac == sw_hmac[..]);
95+
}
96+
97+
// This uses sha512, so only supported on hash_v3 and up
98+
#[cfg(feature = "hash-v34")]
99+
fn test_sizes(hw_hasher: &mut Hash<'_, peripherals::HASH, Blocking>) {
100+
let in1 = b"4BPuGudaDK";
101+
let in2 = b"cfFIGf0XSNhFBQ5LaIqzjnRKDRkoWweJI06HLUcicIUGjpuDNfOTQNSrRxDoveDPlazeZtt06SIYO5CvHvsJ98XSfO9yJEMHoDpDAmNQtwZOPlKmdiagRXsJ7w7IjdKpQH6I2t";
102+
103+
for i in 1..10 {
104+
// sha512 block size is 128, so test around there
105+
for j in [1, 1, 2, 3, 4, 5, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133] {
106+
info!("test_sizes i {} j {}", i, j);
107+
let mut sw = sha2::Sha512::new();
108+
let mut ctx = hw_hasher.start(Algorithm::SHA512, DataType::Width8, None);
109+
110+
sw.update(&in1[..i]);
111+
sw.update(&in2[..j]);
112+
hw_hasher.update_blocking(&mut ctx, &in1[..i]);
113+
hw_hasher.update_blocking(&mut ctx, &in2[..j]);
114+
115+
let sw_digest = sw.finalize();
116+
let mut hw_digest = [0u8; 64];
117+
hw_hasher.finish_blocking(ctx, &mut hw_digest);
118+
info!("Hardware: {:?}", hw_digest);
119+
info!("Software: {:?}", sw_digest[..]);
120+
defmt::assert!(hw_digest == *sw_digest);
121+
}
122+
}
123+
}
124+
125+
#[embassy_executor::main]
126+
async fn main(_spawner: Spawner) {
127+
let p: embassy_stm32::Peripherals = init();
128+
let mut hw_hasher = Hash::new_blocking(p.HASH, Irqs);
129+
130+
test_interrupt(&mut hw_hasher);
131+
// Run it a second time to check hash-after-hmac
132+
test_interrupt(&mut hw_hasher);
133+
134+
#[cfg(feature = "hash-v34")]
135+
test_sizes(&mut hw_hasher);
98136

99137
info!("Test OK");
100138
cortex_m::asm::bkpt();

0 commit comments

Comments
 (0)