Implement the embedded-hal Read trait for the RNG peripheral

This commit is contained in:
Jesse Braham 2022-03-04 09:45:45 -08:00
parent c7dfabcefe
commit bad8020abe
6 changed files with 35 additions and 3 deletions

View File

@ -36,6 +36,7 @@ pub mod i2c;
#[cfg_attr(feature = "esp32c3", path = "interrupt/riscv.rs")]
pub mod interrupt;
pub mod prelude;
pub mod rng;
#[cfg(not(feature = "esp32c3"))]
pub mod rtc_cntl;
pub mod serial;
@ -46,6 +47,7 @@ pub use gpio::*;
pub use interrupt::*;
use procmacros;
pub use procmacros::ram;
pub use rng::Rng;
#[cfg(not(feature = "esp32c3"))]
pub use rtc_cntl::RtcCntl;
pub use serial::Serial;

29
esp-hal-common/src/rng.rs Normal file
View File

@ -0,0 +1,29 @@
use core::convert::Infallible;
use embedded_hal::blocking::rng::Read;
use crate::pac::RNG;
#[derive(Debug)]
pub struct Rng {
rng: RNG,
}
impl Rng {
pub fn new(rng: RNG) -> Self {
Self { rng }
}
}
impl Read for Rng {
type Error = Infallible;
fn read(&mut self, buffer: &mut [u8]) -> Result<(), Self::Error> {
for chunk in buffer.chunks_mut(4) {
let bytes = self.rng.data.read().bits().to_le_bytes();
chunk.copy_from_slice(&bytes[..chunk.len()]);
}
Ok(())
}
}

View File

@ -1,7 +1,7 @@
#![no_std]
pub use embedded_hal as ehal;
pub use esp_hal_common::{i2c, pac, prelude, Delay, RtcCntl, Serial, Timer};
pub use esp_hal_common::{i2c, pac, prelude, Delay, Rng, RtcCntl, Serial, Timer};
pub use self::gpio::IO;

View File

@ -1,7 +1,7 @@
#![no_std]
pub use embedded_hal as ehal;
pub use esp_hal_common::{i2c, pac, prelude, Delay, Serial, Timer};
pub use esp_hal_common::{i2c, pac, prelude, Delay, Rng, Serial, Timer};
#[cfg(not(feature = "normalboot"))]
use riscv_rt::pre_init;

View File

@ -7,6 +7,7 @@ pub use esp_hal_common::{
prelude,
ram,
Delay,
Rng,
RtcCntl,
Serial,
Timer,

View File

@ -1,7 +1,7 @@
#![no_std]
pub use embedded_hal as ehal;
pub use esp_hal_common::{i2c, pac, prelude, ram, Delay, RtcCntl, Serial, Timer};
pub use esp_hal_common::{i2c, pac, prelude, ram, Delay, Rng, RtcCntl, Serial, Timer};
pub use self::gpio::IO;