Skip to main content

esp_hal/peripherals/
overlay_h2.rs

1use core::ops::Deref;
2
3use super::*;
4use crate::efuse::ChipRevision;
5
6// RNG must be marked `virtual` for this to work.
7impl RNG<'_> {
8    /// Return a reference to the register block
9    #[inline(always)]
10    #[instability::unstable]
11    pub const fn regs<'a>() -> &'a RngRegisterBlock {
12        &RngRegisterBlock
13    }
14
15    /// Return a reference to the register block
16    #[inline(always)]
17    #[instability::unstable]
18    pub fn register_block(&self) -> &RngRegisterBlock {
19        &RngRegisterBlock
20    }
21}
22
23/// Register block overlay for the RNG peripheral
24#[instability::unstable]
25pub struct RngRegisterBlock;
26
27// This Deref makes it possible to use the rest of the registers.
28impl Deref for RngRegisterBlock {
29    type Target = pac::rng::RegisterBlock;
30
31    fn deref(&self) -> &Self::Target {
32        unsafe { &*pac::RNG::ptr() }
33    }
34}
35
36impl RngRegisterBlock {
37    /// Random number data
38    #[instability::unstable]
39    pub fn data(&self) -> &pac::rng::DATA {
40        let ptr = unsafe { pac::RNG::steal().data() as *const pac::rng::DATA };
41        if crate::soc::chip_revision_above(ChipRevision::from_combined(102)) {
42            // On H2-ECO5+ the LPPERI peripherals contains an additional register inserted before
43            // the `rng_data` register.
44            // https://github.com/espressif/esp-idf/commit/4c5e1a03414a6d55be4b42ba071b30ad228414f6#diff-bc8f2eca37e32ee4ba21ac812e4934998e764132a400479c4d091eb6f7e2e444
45            unsafe { &*ptr.add(1) }
46        } else {
47            unsafe { &*ptr }
48        }
49    }
50}