#![allow(clippy::needless_doctest_main)]
use core::marker::PhantomData;
use core::ops;
use crate::interrupt;
#[cfg(not(armv6m))]
pub mod cbp;
#[cfg(armv8m_base)]
pub mod itm;
pub mod mpu;
pub mod nvic;
pub mod scb;
#[cfg(all(not(armv6m), not(armv8m_base)))]
pub use cortex_m_0_7::peripheral::itm;
pub use cortex_m_0_7::peripheral::{cpuid, dcb, dwt, syst};
#[cfg(not(armv6m))]
pub use cortex_m_0_7::peripheral::{fpb, tpiu};
#[cfg(any(has_fpu, target_arch="x86_64"))]
pub use cortex_m_0_7::peripheral::fpu;
#[cfg(test)]
mod test;
#[allow(non_snake_case)]
pub struct Peripherals {
pub CBP: CBP,
pub CPUID: CPUID,
pub DCB: DCB,
pub DWT: DWT,
pub FPB: FPB,
pub FPU: FPU,
pub ITM: ITM,
pub MPU: MPU,
pub NVIC: NVIC,
pub SCB: SCB,
pub SYST: SYST,
pub TPIU: TPIU,
}
impl Peripherals {
#[inline]
pub fn take() -> Option<Self> {
interrupt::free(|_| match cortex_m_0_7::peripheral::Peripherals::take() {
Some(_) => { Some(unsafe { Peripherals::steal() }) },
None => None,
})
}
#[inline]
pub unsafe fn steal() -> Self {
cortex_m_0_7::peripheral::Peripherals::steal();
core::mem::transmute(())
}
}
pub struct CBP {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for CBP {}
#[cfg(not(armv6m))]
impl CBP {
#[inline(always)]
pub(crate) unsafe fn new() -> Self {
CBP {
_marker: PhantomData,
}
}
#[inline(always)]
pub fn ptr() -> *const self::cbp::RegisterBlock {
0xE000_EF50 as *const _
}
}
#[cfg(not(armv6m))]
impl ops::Deref for CBP {
type Target = self::cbp::RegisterBlock;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub use cortex_m_0_7::peripheral::{CPUID, DCB, DWT, FPB, FPU, SYST, TPIU};
#[cfg(not(armv8m_base))]
pub use cortex_m_0_7::peripheral::ITM;
#[cfg(armv8m_base)]
pub struct ITM {
_marker: PhantomData<*const ()>,
}
#[cfg(armv8m_base)]
unsafe impl Send for ITM {}
#[cfg(armv8m_base)]
impl ITM {
#[inline(always)]
pub fn ptr() -> *mut itm::RegisterBlock {
0xE000_0000 as *mut _
}
}
#[cfg(armv8m_base)]
impl ops::Deref for ITM {
type Target = self::itm::RegisterBlock;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
#[cfg(armv8m_base)]
impl ops::DerefMut for ITM {
#[inline(always)]
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 {
#[inline(always)]
pub fn ptr() -> *const mpu::RegisterBlock {
0xE000_ED90 as *const _
}
}
impl ops::Deref for MPU {
type Target = self::mpu::RegisterBlock;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub struct NVIC {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for NVIC {}
impl NVIC {
#[inline(always)]
pub fn ptr() -> *const nvic::RegisterBlock {
0xE000_E100 as *const _
}
}
impl ops::Deref for NVIC {
type Target = self::nvic::RegisterBlock;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}
pub struct SCB {
_marker: PhantomData<*const ()>,
}
unsafe impl Send for SCB {}
impl SCB {
#[inline(always)]
pub fn ptr() -> *const scb::RegisterBlock {
0xE000_ED04 as *const _
}
}
impl ops::Deref for SCB {
type Target = self::scb::RegisterBlock;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*Self::ptr() }
}
}