mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-10-02 14:44:42 +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")]
|
#[cfg_attr(feature = "esp32c3", path = "interrupt/riscv.rs")]
|
||||||
pub mod interrupt;
|
pub mod interrupt;
|
||||||
pub mod prelude;
|
pub mod prelude;
|
||||||
|
pub mod rng;
|
||||||
#[cfg(not(feature = "esp32c3"))]
|
#[cfg(not(feature = "esp32c3"))]
|
||||||
pub mod rtc_cntl;
|
pub mod rtc_cntl;
|
||||||
pub mod serial;
|
pub mod serial;
|
||||||
@ -46,6 +47,7 @@ pub use gpio::*;
|
|||||||
pub use interrupt::*;
|
pub use interrupt::*;
|
||||||
use procmacros;
|
use procmacros;
|
||||||
pub use procmacros::ram;
|
pub use procmacros::ram;
|
||||||
|
pub use rng::Rng;
|
||||||
#[cfg(not(feature = "esp32c3"))]
|
#[cfg(not(feature = "esp32c3"))]
|
||||||
pub use rtc_cntl::RtcCntl;
|
pub use rtc_cntl::RtcCntl;
|
||||||
pub use serial::Serial;
|
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]
|
#![no_std]
|
||||||
|
|
||||||
pub use embedded_hal as ehal;
|
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;
|
pub use self::gpio::IO;
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
pub use embedded_hal as ehal;
|
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"))]
|
#[cfg(not(feature = "normalboot"))]
|
||||||
use riscv_rt::pre_init;
|
use riscv_rt::pre_init;
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ pub use esp_hal_common::{
|
|||||||
prelude,
|
prelude,
|
||||||
ram,
|
ram,
|
||||||
Delay,
|
Delay,
|
||||||
|
Rng,
|
||||||
RtcCntl,
|
RtcCntl,
|
||||||
Serial,
|
Serial,
|
||||||
Timer,
|
Timer,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
pub use embedded_hal as ehal;
|
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;
|
pub use self::gpio::IO;
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user