Skip to content

Commit 220f812

Browse files
authored
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
1 parent 7817e27 commit 220f812

File tree

11 files changed

+15
-15
lines changed

11 files changed

+15
-15
lines changed

esp-hal-common/src/peripherals/esp32.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ mod peripherals {
5555
I2C0,
5656
I2C1,
5757
RNG,
58+
SHA,
5859
SPI0,
5960
SPI1,
6061
SPI2,

esp-hal-common/src/peripherals/esp32c2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ mod peripherals {
3636
crate::create_peripherals! {
3737
I2C0,
3838
RNG,
39+
SHA,
3940
SPI0,
4041
SPI1,
4142
SPI2,

esp-hal-common/src/peripherals/esp32c3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ mod peripherals {
4747
crate::create_peripherals! {
4848
I2C0,
4949
RNG,
50+
SHA,
5051
SPI0,
5152
SPI1,
5253
SPI2,

esp-hal-common/src/peripherals/esp32s2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ mod peripherals {
5353
I2C0,
5454
I2C1,
5555
RNG,
56+
SHA,
5657
SPI0,
5758
SPI1,
5859
SPI2,

esp-hal-common/src/peripherals/esp32s3.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ mod peripherals {
6464
I2C0,
6565
I2C1,
6666
RNG,
67+
SHA,
6768
SPI0,
6869
SPI1,
6970
SPI2,

esp-hal-common/src/sha.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use core::convert::Infallible;
22

3-
use crate::pac::SHA;
3+
use crate::{
4+
peripheral::{Peripheral, PeripheralRef},
5+
peripherals::SHA,
6+
};
47

58
// All the hash algorithms introduced in FIPS PUB 180-4 Spec.
69
// – SHA-1
@@ -157,9 +160,8 @@ impl AlignmentHelper {
157160
}
158161
}
159162

160-
#[derive(Debug)]
161-
pub struct Sha {
162-
sha: SHA,
163+
pub struct Sha<'d> {
164+
sha: PeripheralRef<'d, SHA>,
163165
mode: ShaMode,
164166
alignment_helper: AlignmentHelper,
165167
cursor: usize,
@@ -224,8 +226,10 @@ fn mode_as_bits(mode: ShaMode) -> u8 {
224226

225227
// This implementation might fail after u32::MAX/8 bytes, to increase please see
226228
// ::finish() length/self.cursor usage
227-
impl Sha {
228-
pub fn new(sha: SHA, mode: ShaMode) -> Self {
229+
impl<'d> Sha<'d> {
230+
pub fn new(sha: impl Peripheral<P = SHA> + 'd, mode: ShaMode) -> Self {
231+
crate::into_ref!(sha);
232+
229233
// Setup SHA Mode
230234
#[cfg(not(esp32))]
231235
sha.mode
@@ -508,8 +512,4 @@ impl Sha {
508512

509513
Ok(())
510514
}
511-
512-
pub fn free(self) -> SHA {
513-
self.sha
514-
}
515515
}

esp32-hal/examples/sha.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ fn main() -> ! {
6666
let hw_time = post_calc - pre_calc;
6767
println!("Took {} cycles", hw_time);
6868
println!("SHA512 Hash output {:02x?}", output);
69-
let _usha = hasher.free();
7069

7170
let pre_calc = xtensa_lx::timer::get_cycle_count();
7271
let mut hasher = Sha512::new();

esp32c2-hal/examples/sha.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ fn main() -> ! {
6565
// let hw_time = post_calc - pre_calc;
6666
// println!("Took {} cycles", hw_time);
6767
println!("SHA256 Hash output {:02x?}", output);
68-
let _usha = hasher.free();
6968

7069
// let pre_calc = xtensa_lx::timer::get_cycle_count();
7170
let mut hasher = Sha256::new();

esp32c3-hal/examples/sha.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ fn main() -> ! {
6565
// let hw_time = post_calc - pre_calc;
6666
// println!("Took {} cycles", hw_time);
6767
println!("SHA256 Hash output {:02x?}", output);
68-
let _usha = hasher.free();
6968

7069
// let pre_calc = xtensa_lx::timer::get_cycle_count();
7170
let mut hasher = Sha256::new();

esp32s2-hal/examples/sha.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ fn main() -> ! {
6565
let hw_time = post_calc - pre_calc;
6666
println!("Took {} cycles", hw_time);
6767
println!("SHA512 Hash output {:02x?}", output);
68-
let _usha = hasher.free();
6968

7069
let pre_calc = xtensa_lx::timer::get_cycle_count();
7170
let mut hasher = Sha512::new();

0 commit comments

Comments
 (0)