From 220f812625985b4dbb4f2a8db7a68c0c04bbe246 Mon Sep 17 00:00:00 2001 From: Jesse Braham Date: Wed, 14 Dec 2022 04:19:53 -0800 Subject: [PATCH] Peripheral ref/sha (#312) * Add SHA to list of peripherals to be created * Refactor SHA peripheral to use PeripheralRef * Update SHA examples to get them building again --- esp-hal-common/src/peripherals/esp32.rs | 1 + esp-hal-common/src/peripherals/esp32c2.rs | 1 + esp-hal-common/src/peripherals/esp32c3.rs | 1 + esp-hal-common/src/peripherals/esp32s2.rs | 1 + esp-hal-common/src/peripherals/esp32s3.rs | 1 + esp-hal-common/src/sha.rs | 20 ++++++++++---------- esp32-hal/examples/sha.rs | 1 - esp32c2-hal/examples/sha.rs | 1 - esp32c3-hal/examples/sha.rs | 1 - esp32s2-hal/examples/sha.rs | 1 - esp32s3-hal/examples/sha.rs | 1 - 11 files changed, 15 insertions(+), 15 deletions(-) diff --git a/esp-hal-common/src/peripherals/esp32.rs b/esp-hal-common/src/peripherals/esp32.rs index e28570b3a..0d22c39a8 100644 --- a/esp-hal-common/src/peripherals/esp32.rs +++ b/esp-hal-common/src/peripherals/esp32.rs @@ -55,6 +55,7 @@ mod peripherals { I2C0, I2C1, RNG, + SHA, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32c2.rs b/esp-hal-common/src/peripherals/esp32c2.rs index 3d833a462..1f175ddce 100644 --- a/esp-hal-common/src/peripherals/esp32c2.rs +++ b/esp-hal-common/src/peripherals/esp32c2.rs @@ -36,6 +36,7 @@ mod peripherals { crate::create_peripherals! { I2C0, RNG, + SHA, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32c3.rs b/esp-hal-common/src/peripherals/esp32c3.rs index 5cc3a97dc..2147b5921 100644 --- a/esp-hal-common/src/peripherals/esp32c3.rs +++ b/esp-hal-common/src/peripherals/esp32c3.rs @@ -47,6 +47,7 @@ mod peripherals { crate::create_peripherals! { I2C0, RNG, + SHA, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32s2.rs b/esp-hal-common/src/peripherals/esp32s2.rs index efd7817eb..7f186bb35 100644 --- a/esp-hal-common/src/peripherals/esp32s2.rs +++ b/esp-hal-common/src/peripherals/esp32s2.rs @@ -53,6 +53,7 @@ mod peripherals { I2C0, I2C1, RNG, + SHA, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/peripherals/esp32s3.rs b/esp-hal-common/src/peripherals/esp32s3.rs index bfc7827c7..e6077ad8a 100644 --- a/esp-hal-common/src/peripherals/esp32s3.rs +++ b/esp-hal-common/src/peripherals/esp32s3.rs @@ -64,6 +64,7 @@ mod peripherals { I2C0, I2C1, RNG, + SHA, SPI0, SPI1, SPI2, diff --git a/esp-hal-common/src/sha.rs b/esp-hal-common/src/sha.rs index 6a5f0cdcf..46084635e 100644 --- a/esp-hal-common/src/sha.rs +++ b/esp-hal-common/src/sha.rs @@ -1,6 +1,9 @@ use core::convert::Infallible; -use crate::pac::SHA; +use crate::{ + peripheral::{Peripheral, PeripheralRef}, + peripherals::SHA, +}; // All the hash algorithms introduced in FIPS PUB 180-4 Spec. // – SHA-1 @@ -157,9 +160,8 @@ impl AlignmentHelper { } } -#[derive(Debug)] -pub struct Sha { - sha: SHA, +pub struct Sha<'d> { + sha: PeripheralRef<'d, SHA>, mode: ShaMode, alignment_helper: AlignmentHelper, cursor: usize, @@ -224,8 +226,10 @@ fn mode_as_bits(mode: ShaMode) -> u8 { // This implementation might fail after u32::MAX/8 bytes, to increase please see // ::finish() length/self.cursor usage -impl Sha { - pub fn new(sha: SHA, mode: ShaMode) -> Self { +impl<'d> Sha<'d> { + pub fn new(sha: impl Peripheral

+ 'd, mode: ShaMode) -> Self { + crate::into_ref!(sha); + // Setup SHA Mode #[cfg(not(esp32))] sha.mode @@ -508,8 +512,4 @@ impl Sha { Ok(()) } - - pub fn free(self) -> SHA { - self.sha - } } diff --git a/esp32-hal/examples/sha.rs b/esp32-hal/examples/sha.rs index 474910f15..92bdbac12 100644 --- a/esp32-hal/examples/sha.rs +++ b/esp32-hal/examples/sha.rs @@ -66,7 +66,6 @@ fn main() -> ! { let hw_time = post_calc - pre_calc; println!("Took {} cycles", hw_time); println!("SHA512 Hash output {:02x?}", output); - let _usha = hasher.free(); let pre_calc = xtensa_lx::timer::get_cycle_count(); let mut hasher = Sha512::new(); diff --git a/esp32c2-hal/examples/sha.rs b/esp32c2-hal/examples/sha.rs index 290770886..8502930b8 100644 --- a/esp32c2-hal/examples/sha.rs +++ b/esp32c2-hal/examples/sha.rs @@ -65,7 +65,6 @@ fn main() -> ! { // let hw_time = post_calc - pre_calc; // println!("Took {} cycles", hw_time); println!("SHA256 Hash output {:02x?}", output); - let _usha = hasher.free(); // let pre_calc = xtensa_lx::timer::get_cycle_count(); let mut hasher = Sha256::new(); diff --git a/esp32c3-hal/examples/sha.rs b/esp32c3-hal/examples/sha.rs index 88d8e6edd..f2f58f756 100644 --- a/esp32c3-hal/examples/sha.rs +++ b/esp32c3-hal/examples/sha.rs @@ -65,7 +65,6 @@ fn main() -> ! { // let hw_time = post_calc - pre_calc; // println!("Took {} cycles", hw_time); println!("SHA256 Hash output {:02x?}", output); - let _usha = hasher.free(); // let pre_calc = xtensa_lx::timer::get_cycle_count(); let mut hasher = Sha256::new(); diff --git a/esp32s2-hal/examples/sha.rs b/esp32s2-hal/examples/sha.rs index c9d53c259..ce1e47ffd 100644 --- a/esp32s2-hal/examples/sha.rs +++ b/esp32s2-hal/examples/sha.rs @@ -65,7 +65,6 @@ fn main() -> ! { let hw_time = post_calc - pre_calc; println!("Took {} cycles", hw_time); println!("SHA512 Hash output {:02x?}", output); - let _usha = hasher.free(); let pre_calc = xtensa_lx::timer::get_cycle_count(); let mut hasher = Sha512::new(); diff --git a/esp32s3-hal/examples/sha.rs b/esp32s3-hal/examples/sha.rs index bd81f660e..a51163c9d 100644 --- a/esp32s3-hal/examples/sha.rs +++ b/esp32s3-hal/examples/sha.rs @@ -65,7 +65,6 @@ fn main() -> ! { let hw_time = post_calc - pre_calc; println!("Took {} cycles", hw_time); println!("SHA512 Hash output {:02x?}", output); - let _usha = hasher.free(); let pre_calc = xtensa_lx::timer::get_cycle_count(); let mut hasher = Sha512::new();