2020-09-22 16:03:43 +00:00
#![ no_std ]
2023-11-29 16:23:48 +00:00
#![ allow(async_fn_in_trait) ]
2024-01-11 22:43:17 +00:00
#![ cfg_attr(
docsrs ,
doc = " <div style='padding:30px;background:#810;color:#fff;text-align:center;'><p>You might want to <a href='https://docs.embassy.dev/embassy-nrf'>browse the `embassy-nrf` documentation on the Embassy website</a> instead.</p><p>The documentation here on `docs.rs` is built for a single chip only (nRF52840 in particular), while on the Embassy website you can pick your exact chip from the top menu. Available peripherals and their APIs change depending on the chip.</p></div> \n \n "
) ]
2023-01-31 23:48:33 +00:00
#![ doc = include_str!( " ../README.md " ) ]
#![ warn(missing_docs) ]
2020-09-22 16:03:43 +00:00
2023-12-22 22:37:29 +00:00
//! ## Feature flags
#![ doc = document_features::document_features!(feature_label = r# " <span class= " stab portability " ><code>{feature}</code></span> " #) ]
2020-09-22 16:03:43 +00:00
#[ cfg(not(any(
2021-05-11 01:04:59 +00:00
feature = " nrf51 " ,
feature = " nrf52805 " ,
feature = " nrf52810 " ,
feature = " nrf52811 " ,
feature = " nrf52820 " ,
feature = " nrf52832 " ,
feature = " nrf52833 " ,
feature = " nrf52840 " ,
2021-10-28 01:07:06 +00:00
feature = " nrf5340-app-s " ,
feature = " nrf5340-app-ns " ,
2021-05-11 01:04:59 +00:00
feature = " nrf5340-net " ,
2021-10-26 15:11:51 +00:00
feature = " nrf9160-s " ,
feature = " nrf9160-ns " ,
2020-09-22 16:03:43 +00:00
) ) ) ]
2021-05-11 01:04:59 +00:00
compile_error! ( " No chip feature activated. You must activate exactly one of the following features: nrf52810, nrf52811, nrf52832, nrf52833, nrf52840 " ) ;
2021-01-18 13:22:55 +00:00
2023-01-23 00:48:35 +00:00
#[ cfg(all(feature = " reset-pin-as-gpio " , not(feature = " _nrf52 " ))) ]
compile_error! ( " feature `reset-pin-as-gpio` is only valid for nRF52 series chips. " ) ;
#[ cfg(all(feature = " nfc-pins-as-gpio " , not(any(feature = " _nrf52 " , feature = " _nrf5340-app " )))) ]
compile_error! ( " feature `nfc-pins-as-gpio` is only valid for nRF52, or nRF53's application core. " ) ;
2020-12-01 16:46:56 +00:00
// This mod MUST go first, so that the others see its macros.
pub ( crate ) mod fmt ;
2021-05-11 01:04:59 +00:00
pub ( crate ) mod util ;
2020-12-01 16:46:56 +00:00
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " _time-driver " ) ]
2021-08-03 20:08:13 +00:00
mod time_driver ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2020-12-28 22:57:50 +00:00
pub mod buffered_uarte ;
2021-03-19 03:08:44 +00:00
pub mod gpio ;
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " gpiote " ) ]
2020-09-22 22:32:49 +00:00
pub mod gpiote ;
2022-11-12 17:48:57 +00:00
#[ cfg(any(feature = " nrf52832 " , feature = " nrf52833 " , feature = " nrf52840 " )) ]
2022-11-09 18:14:43 +00:00
pub mod i2s ;
2021-10-22 00:09:55 +00:00
pub mod nvmc ;
2022-07-10 18:12:25 +00:00
#[ cfg(any(
feature = " nrf52810 " ,
feature = " nrf52811 " ,
2023-03-05 19:50:45 +00:00
feature = " nrf52832 " ,
2022-07-10 18:12:25 +00:00
feature = " nrf52833 " ,
feature = " nrf52840 " ,
2023-03-05 19:50:45 +00:00
feature = " _nrf5340-app " ,
2022-07-10 18:12:25 +00:00
feature = " _nrf9160 "
) ) ]
pub mod pdm ;
2021-03-27 03:40:05 +00:00
pub mod ppi ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(any(
feature = " nrf51 " ,
feature = " nrf52805 " ,
feature = " nrf52820 " ,
feature = " _nrf5340-net "
) ) ) ]
2021-05-12 02:56:11 +00:00
pub mod pwm ;
2023-03-05 20:28:13 +00:00
#[ cfg(not(any(feature = " nrf51 " , feature = " _nrf9160 " , feature = " _nrf5340-net " ))) ]
2022-05-06 22:46:36 +00:00
pub mod qdec ;
2023-02-23 21:36:10 +00:00
#[ cfg(any(feature = " nrf52840 " , feature = " _nrf5340-app " )) ]
2020-09-22 16:03:43 +00:00
pub mod qspi ;
2023-03-05 20:50:15 +00:00
#[ cfg(not(any(feature = " _nrf5340-app " , feature = " _nrf9160 " ))) ]
2021-06-29 07:26:16 +00:00
pub mod rng ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(any(feature = " nrf51 " , feature = " nrf52820 " , feature = " _nrf5340-net " ))) ]
2021-03-24 17:33:17 +00:00
pub mod saadc ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2021-01-18 13:22:55 +00:00
pub mod spim ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2022-11-04 23:15:43 +00:00
pub mod spis ;
2021-10-28 01:07:06 +00:00
#[ cfg(not(any(feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
2021-10-19 06:37:19 +00:00
pub mod temp ;
2021-03-28 20:40:41 +00:00
pub mod timer ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2021-05-11 01:07:37 +00:00
pub mod twim ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2022-11-13 21:15:19 +00:00
pub mod twis ;
2024-01-19 22:13:46 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2020-12-23 15:18:29 +00:00
pub mod uarte ;
2021-12-15 17:11:00 +00:00
#[ cfg(any(
feature = " _nrf5340-app " ,
feature = " nrf52820 " ,
feature = " nrf52833 " ,
feature = " nrf52840 "
) ) ]
2021-12-14 00:50:08 +00:00
pub mod usb ;
2021-10-28 01:07:06 +00:00
#[ cfg(not(feature = " _nrf5340 " )) ]
2021-08-07 12:26:28 +00:00
pub mod wdt ;
2021-03-21 20:58:59 +00:00
2021-05-11 01:04:59 +00:00
// This mod MUST go last, so that it sees all the `impl_foo!` macros
2024-01-19 22:13:46 +00:00
#[ cfg_attr(feature = " nrf51 " , path = " chips/nrf51.rs " ) ]
2021-10-26 15:11:51 +00:00
#[ cfg_attr(feature = " nrf52805 " , path = " chips/nrf52805.rs " ) ]
#[ cfg_attr(feature = " nrf52810 " , path = " chips/nrf52810.rs " ) ]
#[ cfg_attr(feature = " nrf52811 " , path = " chips/nrf52811.rs " ) ]
#[ cfg_attr(feature = " nrf52820 " , path = " chips/nrf52820.rs " ) ]
#[ cfg_attr(feature = " nrf52832 " , path = " chips/nrf52832.rs " ) ]
#[ cfg_attr(feature = " nrf52833 " , path = " chips/nrf52833.rs " ) ]
#[ cfg_attr(feature = " nrf52840 " , path = " chips/nrf52840.rs " ) ]
2021-10-28 01:07:06 +00:00
#[ cfg_attr(feature = " _nrf5340-app " , path = " chips/nrf5340_app.rs " ) ]
#[ cfg_attr(feature = " _nrf5340-net " , path = " chips/nrf5340_net.rs " ) ]
2021-10-26 15:11:51 +00:00
#[ cfg_attr(feature = " _nrf9160 " , path = " chips/nrf9160.rs " ) ]
2021-10-11 08:39:38 +00:00
mod chip ;
2021-05-11 01:04:59 +00:00
2023-06-08 14:08:40 +00:00
/// Macro to bind interrupts to handlers.
///
/// This defines the right interrupt handlers, and creates a unit struct (like `struct Irqs;`)
/// and implements the right [`Binding`]s for it. You can pass this struct to drivers to
/// prove at compile-time that the right interrupts have been bound.
2023-12-08 20:26:28 +00:00
///
/// Example of how to bind one interrupt:
///
/// ```rust,ignore
/// use embassy_nrf::{bind_interrupts, spim, peripherals};
///
/// bind_interrupts!(struct Irqs {
/// SPIM3 => spim::InterruptHandler<peripherals::SPI3>;
/// });
/// ```
///
/// Example of how to bind multiple interrupts in a single macro invocation:
///
/// ```rust,ignore
/// use embassy_nrf::{bind_interrupts, spim, twim, peripherals};
///
/// bind_interrupts!(struct Irqs {
/// SPIM3 => spim::InterruptHandler<peripherals::SPI3>;
/// SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0 => twim::InterruptHandler<peripherals::TWISPI0>;
/// });
/// ```
2023-07-28 11:23:22 +00:00
// developer note: this macro can't be in `embassy-hal-internal` due to the use of `$crate`.
2023-06-08 14:08:40 +00:00
#[ macro_export ]
macro_rules ! bind_interrupts {
2023-02-26 21:42:22 +00:00
( $vis :vis struct $name :ident { $( $irq :ident = > $( $handler :ty ) , * ; ) * } ) = > {
2023-09-25 20:34:41 +00:00
#[ derive(Copy, Clone) ]
2023-02-26 21:42:22 +00:00
$vis struct $name ;
$(
#[ allow(non_snake_case) ]
#[ no_mangle ]
unsafe extern " C " fn $irq ( ) {
$(
2023-06-08 14:08:40 +00:00
< $handler as $crate ::interrupt ::typelevel ::Handler < $crate ::interrupt ::typelevel ::$irq > > ::on_interrupt ( ) ;
2023-02-26 21:42:22 +00:00
) *
}
$(
2023-06-08 14:08:40 +00:00
unsafe impl $crate ::interrupt ::typelevel ::Binding < $crate ::interrupt ::typelevel ::$irq , $handler > for $name { }
2023-02-26 21:42:22 +00:00
) *
) *
} ;
}
2022-06-11 03:08:57 +00:00
// Reexports
2021-07-14 20:19:04 +00:00
#[ cfg(feature = " unstable-pac " ) ]
pub use chip ::pac ;
#[ cfg(not(feature = " unstable-pac " )) ]
2021-05-11 01:04:59 +00:00
pub ( crate ) use chip ::pac ;
2023-02-26 21:42:22 +00:00
pub use chip ::{ peripherals , Peripherals , EASY_DMA_SIZE } ;
2023-07-28 11:23:22 +00:00
pub use embassy_hal_internal ::{ into_ref , Peripheral , PeripheralRef } ;
2021-05-11 23:57:01 +00:00
2023-06-08 14:39:05 +00:00
pub use crate ::chip ::interrupt ;
pub use crate ::pac ::NVIC_PRIO_BITS ;
2021-05-11 23:57:01 +00:00
pub mod config {
2022-06-23 10:59:18 +00:00
//! Configuration options used when initializing the HAL.
/// High frequency clock source.
2021-05-11 23:57:01 +00:00
pub enum HfclkSource {
2022-06-23 10:59:18 +00:00
/// Internal source
2021-05-11 23:57:01 +00:00
Internal ,
2022-06-23 10:59:18 +00:00
/// External source from xtal.
2021-05-11 23:57:01 +00:00
ExternalXtal ,
}
2022-06-23 10:59:18 +00:00
/// Low frequency clock source
2021-05-11 23:57:01 +00:00
pub enum LfclkSource {
2022-06-23 10:59:18 +00:00
/// Internal RC oscillator
2021-05-11 23:57:01 +00:00
InternalRC ,
2022-06-23 10:59:18 +00:00
/// Synthesized from the high frequency clock source.
2021-10-28 01:07:06 +00:00
#[ cfg(not(any(feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
2021-05-11 23:57:01 +00:00
Synthesized ,
2022-06-23 10:59:18 +00:00
/// External source from xtal.
2021-05-11 23:57:01 +00:00
ExternalXtal ,
2022-06-23 10:59:18 +00:00
/// External source from xtal with low swing applied.
2021-10-28 01:07:06 +00:00
#[ cfg(not(any(feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
2021-05-11 23:57:01 +00:00
ExternalLowSwing ,
2022-06-23 10:59:18 +00:00
/// External source from xtal with full swing applied.
2021-10-28 01:07:06 +00:00
#[ cfg(not(any(feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
2021-05-11 23:57:01 +00:00
ExternalFullSwing ,
}
2023-01-23 00:48:35 +00:00
/// SWD access port protection setting.
#[ non_exhaustive ]
pub enum Debug {
/// Debugging is allowed (APPROTECT is disabled). Default.
Allowed ,
/// Debugging is not allowed (APPROTECT is enabled).
Disallowed ,
/// APPROTECT is not configured (neither to enable it or disable it).
/// This can be useful if you're already doing it by other means and
/// you don't want embassy-nrf to touch UICR.
NotConfigured ,
}
2023-04-26 18:03:43 +00:00
/// Settings for enabling the built in DCDC converters.
#[ cfg(not(any(feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
pub struct DcdcConfig {
/// Config for the first stage DCDC (VDDH -> VDD), if disabled LDO will be used.
#[ cfg(feature = " nrf52840 " ) ]
pub reg0 : bool ,
/// Config for the second stage DCDC (VDD -> DEC4), if disabled LDO will be used.
pub reg1 : bool ,
}
/// Settings for enabling the built in DCDC converters.
#[ cfg(feature = " _nrf5340-app " ) ]
pub struct DcdcConfig {
/// Config for the high voltage stage, if disabled LDO will be used.
pub regh : bool ,
/// Config for the main rail, if disabled LDO will be used.
pub regmain : bool ,
/// Config for the radio rail, if disabled LDO will be used.
pub regradio : bool ,
}
/// Settings for enabling the built in DCDC converter.
#[ cfg(feature = " _nrf9160 " ) ]
pub struct DcdcConfig {
/// Config for the main rail, if disabled LDO will be used.
pub regmain : bool ,
}
2022-06-23 10:59:18 +00:00
/// Configuration for peripherals. Default configuration should work on any nRF chip.
2021-05-11 23:57:01 +00:00
#[ non_exhaustive ]
pub struct Config {
2022-06-23 10:59:18 +00:00
/// High frequency clock source.
2021-05-11 23:57:01 +00:00
pub hfclk_source : HfclkSource ,
2022-06-23 10:59:18 +00:00
/// Low frequency clock source.
2021-05-11 23:57:01 +00:00
pub lfclk_source : LfclkSource ,
2023-04-26 18:03:43 +00:00
#[ cfg(not(feature = " _nrf5340-net " )) ]
/// DCDC configuration.
pub dcdc : DcdcConfig ,
2022-06-23 10:59:18 +00:00
/// GPIOTE interrupt priority. Should be lower priority than softdevice if used.
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " gpiote " ) ]
2021-05-17 19:27:27 +00:00
pub gpiote_interrupt_priority : crate ::interrupt ::Priority ,
2022-06-23 10:59:18 +00:00
/// Time driver interrupt priority. Should be lower priority than softdevice if used.
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " _time-driver " ) ]
2021-08-18 20:12:36 +00:00
pub time_interrupt_priority : crate ::interrupt ::Priority ,
2023-01-23 00:48:35 +00:00
/// Enable or disable the debug port.
pub debug : Debug ,
2021-05-11 23:57:01 +00:00
}
impl Default for Config {
fn default ( ) -> Self {
Self {
// There are hobby nrf52 boards out there without external XTALs...
// Default everything to internal so it Just Works. User can enable external
// xtals if they know they have them.
hfclk_source : HfclkSource ::Internal ,
lfclk_source : LfclkSource ::InternalRC ,
2023-04-26 18:03:43 +00:00
#[ cfg(not(any(feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
dcdc : DcdcConfig {
#[ cfg(feature = " nrf52840 " ) ]
reg0 : false ,
reg1 : false ,
} ,
#[ cfg(feature = " _nrf5340-app " ) ]
dcdc : DcdcConfig {
regh : false ,
regmain : false ,
regradio : false ,
} ,
#[ cfg(feature = " _nrf9160 " ) ]
dcdc : DcdcConfig { regmain : false } ,
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " gpiote " ) ]
2021-05-17 19:27:27 +00:00
gpiote_interrupt_priority : crate ::interrupt ::Priority ::P0 ,
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " _time-driver " ) ]
2021-08-18 20:12:36 +00:00
time_interrupt_priority : crate ::interrupt ::Priority ::P0 ,
2023-01-23 00:48:35 +00:00
// In NS mode, default to NotConfigured, assuming the S firmware will do it.
#[ cfg(feature = " _ns " ) ]
debug : Debug ::NotConfigured ,
#[ cfg(not(feature = " _ns " )) ]
debug : Debug ::Allowed ,
2021-05-11 23:57:01 +00:00
}
}
}
}
2023-01-23 00:48:35 +00:00
#[ cfg(feature = " _nrf9160 " ) ]
#[ allow(unused) ]
mod consts {
pub const UICR_APPROTECT : * mut u32 = 0x00FF8000 as * mut u32 ;
pub const UICR_SECUREAPPROTECT : * mut u32 = 0x00FF802C as * mut u32 ;
pub const APPROTECT_ENABLED : u32 = 0x0000_0000 ;
}
#[ cfg(feature = " _nrf5340-app " ) ]
#[ allow(unused) ]
mod consts {
pub const UICR_APPROTECT : * mut u32 = 0x00FF8000 as * mut u32 ;
pub const UICR_SECUREAPPROTECT : * mut u32 = 0x00FF801C as * mut u32 ;
pub const UICR_NFCPINS : * mut u32 = 0x00FF8028 as * mut u32 ;
pub const APPROTECT_ENABLED : u32 = 0x0000_0000 ;
pub const APPROTECT_DISABLED : u32 = 0x50FA50FA ;
}
#[ cfg(feature = " _nrf5340-net " ) ]
#[ allow(unused) ]
mod consts {
pub const UICR_APPROTECT : * mut u32 = 0x01FF8000 as * mut u32 ;
pub const APPROTECT_ENABLED : u32 = 0x0000_0000 ;
pub const APPROTECT_DISABLED : u32 = 0x50FA50FA ;
}
#[ cfg(feature = " _nrf52 " ) ]
#[ allow(unused) ]
mod consts {
pub const UICR_PSELRESET1 : * mut u32 = 0x10001200 as * mut u32 ;
pub const UICR_PSELRESET2 : * mut u32 = 0x10001204 as * mut u32 ;
pub const UICR_NFCPINS : * mut u32 = 0x1000120C as * mut u32 ;
pub const UICR_APPROTECT : * mut u32 = 0x10001208 as * mut u32 ;
pub const APPROTECT_ENABLED : u32 = 0x0000_0000 ;
pub const APPROTECT_DISABLED : u32 = 0x0000_005a ;
}
2024-01-25 07:38:28 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2023-02-20 00:29:12 +00:00
#[ derive(Debug, Copy, Clone, Eq, PartialEq) ]
#[ cfg_attr(feature = " defmt " , derive(defmt::Format)) ]
enum WriteResult {
/// Word was written successfully, needs reset.
Written ,
/// Word was already set to the value we wanted to write, nothing was done.
Noop ,
/// Word is already set to something else, we couldn't write the desired value.
Failed ,
}
2024-01-25 07:38:28 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2023-02-20 00:29:12 +00:00
unsafe fn uicr_write ( address : * mut u32 , value : u32 ) -> WriteResult {
2023-03-07 14:28:27 +00:00
uicr_write_masked ( address , value , 0xFFFF_FFFF )
}
2024-01-25 07:38:28 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2023-03-07 14:28:27 +00:00
unsafe fn uicr_write_masked ( address : * mut u32 , value : u32 , mask : u32 ) -> WriteResult {
2023-01-23 00:48:35 +00:00
let curr_val = address . read_volatile ( ) ;
2023-03-07 14:28:27 +00:00
if curr_val & mask = = value & mask {
2023-02-20 00:29:12 +00:00
return WriteResult ::Noop ;
}
// We can only change `1` bits to `0` bits.
2023-03-07 14:28:27 +00:00
if curr_val & value & mask ! = value & mask {
2023-02-20 00:29:12 +00:00
return WriteResult ::Failed ;
2023-01-23 00:48:35 +00:00
}
let nvmc = & * pac ::NVMC ::ptr ( ) ;
nvmc . config . write ( | w | w . wen ( ) . wen ( ) ) ;
while nvmc . ready . read ( ) . ready ( ) . is_busy ( ) { }
2023-03-07 14:28:27 +00:00
address . write_volatile ( value | ! mask ) ;
2023-01-23 00:48:35 +00:00
while nvmc . ready . read ( ) . ready ( ) . is_busy ( ) { }
nvmc . config . reset ( ) ;
while nvmc . ready . read ( ) . ready ( ) . is_busy ( ) { }
2023-02-20 00:29:12 +00:00
WriteResult ::Written
2023-01-23 00:48:35 +00:00
}
2023-12-18 23:05:54 +00:00
/// Initialize the `embassy-nrf` HAL with the provided configuration.
///
/// This returns the peripheral singletons that can be used for creating drivers.
///
/// This should only be called once at startup, otherwise it panics.
2021-05-11 23:57:01 +00:00
pub fn init ( config : config ::Config ) -> Peripherals {
// Do this first, so that it panics if user is calling `init` a second time
// before doing anything important.
let peripherals = Peripherals ::take ( ) ;
2024-01-25 07:38:28 +00:00
#[ allow(unused_mut) ]
2023-01-23 00:48:35 +00:00
let mut needs_reset = false ;
// Setup debug protection.
2024-01-19 22:13:46 +00:00
#[ cfg(not(feature = " nrf51 " )) ]
2023-01-23 00:48:35 +00:00
match config . debug {
config ::Debug ::Allowed = > {
#[ cfg(feature = " _nrf52 " ) ]
unsafe {
let variant = ( 0x1000_0104 as * mut u32 ) . read_volatile ( ) ;
// Get the letter for the build code (b'A' .. b'F')
let build_code = ( variant > > 8 ) as u8 ;
2023-12-03 18:47:26 +00:00
if build_code > = chip ::APPROTECT_MIN_BUILD_CODE {
// Chips with a certain chip type-specific build code or higher have an
2023-01-23 00:48:35 +00:00
// improved APPROTECT ("hardware and software controlled access port protection")
// which needs explicit action by the firmware to keep it unlocked
2023-12-03 18:47:26 +00:00
// See https://devzone.nordicsemi.com/nordic/nordic-blog/b/blog/posts/working-with-the-nrf52-series-improved-approtect
2023-01-23 00:48:35 +00:00
// UICR.APPROTECT = SwDisabled
2023-02-20 00:29:12 +00:00
let res = uicr_write ( consts ::UICR_APPROTECT , consts ::APPROTECT_DISABLED ) ;
needs_reset | = res = = WriteResult ::Written ;
2023-01-23 00:48:35 +00:00
// APPROTECT.DISABLE = SwDisabled
( 0x4000_0558 as * mut u32 ) . write_volatile ( consts ::APPROTECT_DISABLED ) ;
} else {
// nothing to do on older chips, debug is allowed by default.
}
}
#[ cfg(feature = " _nrf5340 " ) ]
unsafe {
let p = & * pac ::CTRLAP ::ptr ( ) ;
2023-02-20 00:29:12 +00:00
let res = uicr_write ( consts ::UICR_APPROTECT , consts ::APPROTECT_DISABLED ) ;
needs_reset | = res = = WriteResult ::Written ;
2023-01-23 00:48:35 +00:00
p . approtect . disable . write ( | w | w . bits ( consts ::APPROTECT_DISABLED ) ) ;
#[ cfg(feature = " _nrf5340-app " ) ]
{
2023-02-20 00:29:12 +00:00
let res = uicr_write ( consts ::UICR_SECUREAPPROTECT , consts ::APPROTECT_DISABLED ) ;
needs_reset | = res = = WriteResult ::Written ;
2023-01-23 00:48:35 +00:00
p . secureapprotect . disable . write ( | w | w . bits ( consts ::APPROTECT_DISABLED ) ) ;
}
}
// nothing to do on the nrf9160, debug is allowed by default.
}
config ::Debug ::Disallowed = > unsafe {
// UICR.APPROTECT = Enabled
2023-02-20 00:29:12 +00:00
let res = uicr_write ( consts ::UICR_APPROTECT , consts ::APPROTECT_ENABLED ) ;
needs_reset | = res = = WriteResult ::Written ;
2023-01-23 00:48:35 +00:00
#[ cfg(any(feature = " _nrf5340-app " , feature = " _nrf9160 " )) ]
{
2023-02-20 00:29:12 +00:00
let res = uicr_write ( consts ::UICR_SECUREAPPROTECT , consts ::APPROTECT_ENABLED ) ;
needs_reset | = res = = WriteResult ::Written ;
2023-01-23 00:48:35 +00:00
}
} ,
config ::Debug ::NotConfigured = > { }
}
2023-02-20 00:29:12 +00:00
#[ cfg(feature = " _nrf52 " ) ]
2023-01-23 00:48:35 +00:00
unsafe {
2023-02-20 00:29:12 +00:00
let value = if cfg! ( feature = " reset-pin-as-gpio " ) {
! 0
} else {
chip ::RESET_PIN
} ;
let res1 = uicr_write ( consts ::UICR_PSELRESET1 , value ) ;
let res2 = uicr_write ( consts ::UICR_PSELRESET2 , value ) ;
needs_reset | = res1 = = WriteResult ::Written | | res2 = = WriteResult ::Written ;
if res1 = = WriteResult ::Failed | | res2 = = WriteResult ::Failed {
#[ cfg(not(feature = " reset-pin-as-gpio " )) ]
warn! (
" You have requested enabling chip reset functionality on the reset pin, by not enabling the Cargo feature `reset-pin-as-gpio`. \n \
However , UICR is already programmed to some other setting , and can ' t be changed without erasing it . \ n \
2023-06-29 00:39:28 +00:00
To fix this , erase UICR manually , for example using ` probe - rs erase ` or ` nrfjprog - - eraseuicr ` . "
2023-02-20 00:29:12 +00:00
) ;
#[ cfg(feature = " reset-pin-as-gpio " ) ]
warn! (
" You have requested using the reset pin as GPIO, by enabling the Cargo feature `reset-pin-as-gpio`. \n \
However , UICR is already programmed to some other setting , and can ' t be changed without erasing it . \ n \
2023-06-29 00:39:28 +00:00
To fix this , erase UICR manually , for example using ` probe - rs erase ` or ` nrfjprog - - eraseuicr ` . "
2023-02-20 00:29:12 +00:00
) ;
}
2023-01-23 00:48:35 +00:00
}
2023-02-20 00:29:12 +00:00
#[ cfg(any(feature = " _nrf52 " , feature = " _nrf5340-app " )) ]
2023-01-23 00:48:35 +00:00
unsafe {
2023-03-07 14:28:27 +00:00
let value = if cfg! ( feature = " nfc-pins-as-gpio " ) { 0 } else { 1 } ;
let res = uicr_write_masked ( consts ::UICR_NFCPINS , value , 1 ) ;
2023-02-20 00:29:12 +00:00
needs_reset | = res = = WriteResult ::Written ;
if res = = WriteResult ::Failed {
// with nfc-pins-as-gpio, this can never fail because we're writing all zero bits.
#[ cfg(not(feature = " nfc-pins-as-gpio " )) ]
warn! (
" You have requested to use P0.09 and P0.10 pins for NFC, by not enabling the Cargo feature `nfc-pins-as-gpio`. \n \
However , UICR is already programmed to some other setting , and can ' t be changed without erasing it . \ n \
2023-06-29 00:39:28 +00:00
To fix this , erase UICR manually , for example using ` probe - rs erase ` or ` nrfjprog - - eraseuicr ` . "
2023-02-20 00:29:12 +00:00
) ;
}
2023-01-23 00:48:35 +00:00
}
if needs_reset {
cortex_m ::peripheral ::SCB ::sys_reset ( ) ;
}
2021-10-28 01:07:06 +00:00
let r = unsafe { & * pac ::CLOCK ::ptr ( ) } ;
2021-05-11 23:57:01 +00:00
// Start HFCLK.
match config . hfclk_source {
config ::HfclkSource ::Internal = > { }
config ::HfclkSource ::ExternalXtal = > {
// Datasheet says this is likely to take 0.36ms
r . events_hfclkstarted . write ( | w | unsafe { w . bits ( 0 ) } ) ;
r . tasks_hfclkstart . write ( | w | unsafe { w . bits ( 1 ) } ) ;
while r . events_hfclkstarted . read ( ) . bits ( ) = = 0 { }
}
}
// Configure LFCLK.
2024-01-19 22:13:46 +00:00
#[ cfg(not(any(feature = " nrf51 " , feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
2021-05-11 23:57:01 +00:00
match config . lfclk_source {
config ::LfclkSource ::InternalRC = > r . lfclksrc . write ( | w | w . src ( ) . rc ( ) ) ,
config ::LfclkSource ::Synthesized = > r . lfclksrc . write ( | w | w . src ( ) . synth ( ) ) ,
config ::LfclkSource ::ExternalXtal = > r . lfclksrc . write ( | w | w . src ( ) . xtal ( ) ) ,
config ::LfclkSource ::ExternalLowSwing = > r . lfclksrc . write ( | w | {
w . src ( ) . xtal ( ) ;
w . external ( ) . enabled ( ) ;
w . bypass ( ) . disabled ( ) ;
w
} ) ,
config ::LfclkSource ::ExternalFullSwing = > r . lfclksrc . write ( | w | {
w . src ( ) . xtal ( ) ;
w . external ( ) . enabled ( ) ;
w . bypass ( ) . enabled ( ) ;
w
} ) ,
}
2021-10-26 15:11:51 +00:00
#[ cfg(feature = " _nrf9160 " ) ]
2021-10-11 08:39:38 +00:00
match config . lfclk_source {
config ::LfclkSource ::InternalRC = > r . lfclksrc . write ( | w | w . src ( ) . lfrc ( ) ) ,
config ::LfclkSource ::ExternalXtal = > r . lfclksrc . write ( | w | w . src ( ) . lfxo ( ) ) ,
}
2021-05-11 23:57:01 +00:00
// Start LFCLK.
// Datasheet says this could take 100us from synth source
// 600us from rc source, 0.25s from an external source.
r . events_lfclkstarted . write ( | w | unsafe { w . bits ( 0 ) } ) ;
r . tasks_lfclkstart . write ( | w | unsafe { w . bits ( 1 ) } ) ;
while r . events_lfclkstarted . read ( ) . bits ( ) = = 0 { }
2023-04-26 18:03:43 +00:00
#[ cfg(not(any(feature = " _nrf5340 " , feature = " _nrf9160 " ))) ]
{
// Setup DCDCs.
let pwr = unsafe { & * pac ::POWER ::ptr ( ) } ;
#[ cfg(feature = " nrf52840 " ) ]
if config . dcdc . reg0 {
pwr . dcdcen0 . write ( | w | w . dcdcen ( ) . set_bit ( ) ) ;
}
if config . dcdc . reg1 {
pwr . dcdcen . write ( | w | w . dcdcen ( ) . set_bit ( ) ) ;
}
}
#[ cfg(feature = " _nrf9160 " ) ]
{
// Setup DCDC.
let reg = unsafe { & * pac ::REGULATORS ::ptr ( ) } ;
if config . dcdc . regmain {
reg . dcdcen . write ( | w | w . dcdcen ( ) . set_bit ( ) ) ;
}
}
#[ cfg(feature = " _nrf5340-app " ) ]
{
// Setup DCDC.
let reg = unsafe { & * pac ::REGULATORS ::ptr ( ) } ;
if config . dcdc . regh {
reg . vregh . dcdcen . write ( | w | w . dcdcen ( ) . set_bit ( ) ) ;
}
if config . dcdc . regmain {
reg . vregmain . dcdcen . write ( | w | w . dcdcen ( ) . set_bit ( ) ) ;
}
if config . dcdc . regradio {
reg . vregradio . dcdcen . write ( | w | w . dcdcen ( ) . set_bit ( ) ) ;
}
}
2021-05-11 23:57:01 +00:00
// Init GPIOTE
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " gpiote " ) ]
2021-08-03 20:08:13 +00:00
gpiote ::init ( config . gpiote_interrupt_priority ) ;
// init RTC time driver
2021-08-20 13:42:42 +00:00
#[ cfg(feature = " _time-driver " ) ]
2021-08-18 20:12:36 +00:00
time_driver ::init ( config . time_interrupt_priority ) ;
2021-05-11 23:57:01 +00:00
2022-11-22 01:21:06 +00:00
// Disable UARTE (enabled by default for some reason)
#[ cfg(feature = " _nrf9160 " ) ]
unsafe {
( * pac ::UARTE0 ::ptr ( ) ) . enable . write ( | w | w . enable ( ) . disabled ( ) ) ;
( * pac ::UARTE1 ::ptr ( ) ) . enable . write ( | w | w . enable ( ) . disabled ( ) ) ;
}
2021-05-11 23:57:01 +00:00
peripherals
}