Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peripheral ref/sha #312

Merged
merged 3 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod peripherals {
I2C0,
I2C1,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ mod peripherals {
crate::create_peripherals! {
I2C0,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32c3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod peripherals {
crate::create_peripherals! {
I2C0,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32s2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod peripherals {
I2C0,
I2C1,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
Expand Down
1 change: 1 addition & 0 deletions esp-hal-common/src/peripherals/esp32s3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ mod peripherals {
I2C0,
I2C1,
RNG,
SHA,
SPI0,
SPI1,
SPI2,
Expand Down
20 changes: 10 additions & 10 deletions esp-hal-common/src/sha.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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<P = SHA> + 'd, mode: ShaMode) -> Self {
crate::into_ref!(sha);

// Setup SHA Mode
#[cfg(not(esp32))]
sha.mode
Expand Down Expand Up @@ -508,8 +512,4 @@ impl Sha {

Ok(())
}

pub fn free(self) -> SHA {
self.sha
}
}
1 change: 0 additions & 1 deletion esp32-hal/examples/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion esp32c2-hal/examples/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion esp32c3-hal/examples/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion esp32s2-hal/examples/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
1 change: 0 additions & 1 deletion esp32s3-hal/examples/sha.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down