mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-10-01 14:20:44 +00:00
Implement the embedded-hal Read trait for the RNG peripheral
This commit is contained in:
parent
c7dfabcefe
commit
bad8020abe
@ -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
29
esp-hal-common/src/rng.rs
Normal 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(())
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -7,6 +7,7 @@ pub use esp_hal_common::{
|
||||
prelude,
|
||||
ram,
|
||||
Delay,
|
||||
Rng,
|
||||
RtcCntl,
|
||||
Serial,
|
||||
Timer,
|
||||
|
@ -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;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user