#![allow(private_no_mangle_statics)]
use core::marker::PhantomData;
use core::ops;
use interrupt;
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub mod cbp;
pub mod cpuid;
pub mod dcb;
pub mod dwt;
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub mod fpb;
#[cfg(any(has_fpu, target_arch = "x86_64"))]
pub mod fpu;
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub mod itm;
pub mod mpu;
pub mod nvic;
pub mod scb;
pub mod syst;
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub mod tpiu;
#[cfg(test)]
mod test;
#[allow(non_snake_case)]
pub struct Peripherals {
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub CBP: CBP,
pub CPUID: CPUID,
pub DCB: DCB,
pub DWT: DWT,
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub FPB: FPB,
#[cfg(any(has_fpu, target_arch = "x86_64"))]
pub FPU: FPU,
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub ITM: ITM,
pub MPU: MPU,
pub NVIC: NVIC,
pub SCB: SCB,
pub SYST: SYST,
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub TPIU: TPIU,
}
#[no_mangle]
static mut CORE_PERIPHERALS: bool = false;
impl Peripherals {
#[inline]
pub fn take() -> Option<Self> {
interrupt::free(|_| {
if unsafe { CORE_PERIPHERALS } {
None
} else {
Some(unsafe { Peripherals::steal() })
}
})
}
pub unsafe fn steal() -> Self {
debug_assert!(!CORE_PERIPHERALS);
CORE_PERIPHERALS = true;
Peripherals {
#[cfg(any(armv7m, target_arch = "x86_64"))]
CBP: CBP {
_marker: PhantomData,
},
CPUID: CPUID {
_marker: PhantomData,
},
DCB: DCB {
_marker: PhantomData,
},
DWT: DWT {
_marker: PhantomData,
},
#[cfg(any(armv7m, target_arch = "x86_64"))]
FPB: FPB {
_marker: PhantomData,
},
#[cfg(any(has_fpu, target_arch = "x86_64"))]
FPU: FPU {
_marker: PhantomData,
},
#[cfg(any(armv7m, target_arch = "x86_64"))]
ITM: ITM {
_marker: PhantomData,
},
MPU: MPU {
_marker: PhantomData,
},
NVIC: NVIC {
_marker: PhantomData,
},
SCB: SCB {
_marker: PhantomData,
},
SYST: SYST {
_marker: PhantomData,
},
#[cfg(any(armv7m, target_arch = "x86_64"))]
TPIU: TPIU {
_marker: PhantomData,
},
}
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub struct CBP {
_marker: PhantomData<*const ()>,
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
unsafe impl Send for CBP {}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl CBP {
pub(crate) unsafe fn new() -> Self {
CBP {
_marker: PhantomData,
}
}
pub fn ptr() -> *const self::cbp::RegisterBlock {
0xE000_EF50 as *const _
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl ops::Deref for CBP {
type Target = self::cbp::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub struct CPUID {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for CPUID {}
impl CPUID {
pub fn ptr() -> *const self::cpuid::RegisterBlock {
0xE000_ED00 as *const _
}
}
impl ops::Deref for CPUID {
type Target = self::cpuid::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub struct DCB {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for DCB {}
impl DCB {
pub fn ptr() -> *const dcb::RegisterBlock {
0xE000_EDF0 as *const _
}
}
impl ops::Deref for DCB {
type Target = self::dcb::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*DCB::ptr() }
}
}
pub struct DWT {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for DWT {}
impl DWT {
pub fn ptr() -> *const dwt::RegisterBlock {
0xE000_1000 as *const _
}
}
impl ops::Deref for DWT {
type Target = self::dwt::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub struct FPB {
_marker: PhantomData<*const ()>,
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
unsafe impl Send for FPB {}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl FPB {
pub fn ptr() -> *const fpb::RegisterBlock {
0xE000_2000 as *const _
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl ops::Deref for FPB {
type Target = self::fpb::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
#[cfg(any(has_fpu, target_arch = "x86_64"))]
pub struct FPU {
_marker: PhantomData<*const ()>,
}
#[cfg(any(has_fpu, target_arch = "x86_64"))]
unsafe impl Send for FPU {}
#[cfg(any(has_fpu, target_arch = "x86_64"))]
impl FPU {
pub fn ptr() -> *const fpu::RegisterBlock {
0xE000_EF30 as *const _
}
}
#[cfg(any(has_fpu, target_arch = "x86_64"))]
impl ops::Deref for FPU {
type Target = self::fpu::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub struct ITM {
_marker: PhantomData<*const ()>,
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
unsafe impl Send for ITM {}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl ITM {
pub fn ptr() -> *mut itm::RegisterBlock {
0xE000_0000 as *mut _
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl ops::Deref for ITM {
type Target = self::itm::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl ops::DerefMut for ITM {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *Self::ptr() }
}
}
pub struct MPU {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for MPU {}
impl MPU {
pub fn ptr() -> *const mpu::RegisterBlock {
0xE000_ED90 as *const _
}
}
impl ops::Deref for MPU {
type Target = self::mpu::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub struct NVIC {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for NVIC {}
impl NVIC {
pub fn ptr() -> *const nvic::RegisterBlock {
0xE000_E100 as *const _
}
}
impl ops::Deref for NVIC {
type Target = self::nvic::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub struct SCB {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for SCB {}
impl SCB {
pub fn ptr() -> *const scb::RegisterBlock {
0xE000_ED04 as *const _
}
}
impl ops::Deref for SCB {
type Target = self::scb::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub struct SYST {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for SYST {}
impl SYST {
pub fn ptr() -> *const syst::RegisterBlock {
0xE000_E010 as *const _
}
}
impl ops::Deref for SYST {
type Target = self::syst::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
pub struct TPIU {
_marker: PhantomData<*const ()>,
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
unsafe impl Send for TPIU {}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl TPIU {
pub fn ptr() -> *const tpiu::RegisterBlock {
0xE004_0000 as *const _
}
}
#[cfg(any(armv7m, target_arch = "x86_64"))]
impl ops::Deref for TPIU {
type Target = self::tpiu::RegisterBlock;
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}