nrf52_hal_common/
lib.rs

1#![no_std]
2
3use embedded_hal as hal;
4
5#[cfg(feature = "52810")]
6pub use nrf52810_pac as target;
7
8#[cfg(feature = "52832")]
9pub use nrf52832_pac as target;
10
11#[cfg(feature = "52840")]
12pub use nrf52840_pac as target;
13
14pub mod clocks;
15pub mod delay;
16pub mod gpio;
17pub mod rng;
18pub mod rtc;
19pub mod saadc;
20pub mod spim;
21pub mod temp;
22pub mod time;
23pub mod timer;
24pub mod twim;
25pub mod uarte;
26
27pub mod prelude {
28    pub use crate::hal::prelude::*;
29
30    pub use crate::clocks::ClocksExt;
31    pub use crate::gpio::GpioExt;
32    pub use crate::rng::RngExt;
33    pub use crate::rtc::RtcExt;
34    pub use crate::saadc::SaadcExt;
35    pub use crate::spim::SpimExt;
36    pub use crate::time::U32Ext;
37    pub use crate::timer::TimerExt;
38    pub use crate::twim::TwimExt;
39    pub use crate::uarte::UarteExt;
40}
41
42/// Length of Nordic EasyDMA differs for MCUs
43#[cfg(any(feature = "52810", feature = "52832"))]
44pub mod target_constants {
45    // NRF52832 8 bits1..0xFF
46    pub const EASY_DMA_SIZE: usize = 255;
47    // Easy DMA can only read from data ram
48    pub const SRAM_LOWER: usize = 0x2000_0000;
49    pub const SRAM_UPPER: usize = 0x3000_0000;
50    pub const FORCE_COPY_BUFFER_SIZE: usize = 255;
51}
52#[cfg(feature = "52840")]
53pub mod target_constants {
54    // NRF52840 16 bits 1..0xFFFF
55    pub const EASY_DMA_SIZE: usize = 65535;
56    // Limits for Easy DMA - it can only read from data ram
57    pub const SRAM_LOWER: usize = 0x2000_0000;
58    pub const SRAM_UPPER: usize = 0x3000_0000;
59    pub const FORCE_COPY_BUFFER_SIZE: usize = 1024;
60}
61
62/// Does this slice reside entirely within RAM?
63pub(crate) fn slice_in_ram(slice: &[u8]) -> bool {
64    let ptr = slice.as_ptr() as usize;
65    ptr >= target_constants::SRAM_LOWER &&
66        (ptr + slice.len()) < target_constants::SRAM_UPPER
67}
68
69/// A handy structure for converting rust slices into ptr and len pairs
70/// for use with EasyDMA. Care must be taken to make sure mutability
71/// guarantees are respected
72pub(crate) struct DmaSlice {
73    ptr: u32,
74    len: u32,
75}
76
77impl DmaSlice {
78    pub fn null() -> Self {
79        Self {
80            ptr: 0,
81            len: 0,
82        }
83    }
84
85    pub fn from_slice(slice: &[u8]) -> Self {
86        Self {
87            ptr: slice.as_ptr() as u32,
88            len: slice.len() as u32,
89        }
90    }
91}
92
93pub use crate::clocks::Clocks;
94pub use crate::delay::Delay;
95pub use crate::rtc::Rtc;
96pub use crate::saadc::Saadc;
97pub use crate::spim::Spim;
98pub use crate::timer::Timer;
99pub use crate::twim::Twim;
100pub use crate::uarte::Uarte;