2020-09-22 16:03:43 +00:00
#![ no_std ]
#![ feature(generic_associated_types) ]
#![ feature(asm) ]
#![ feature(type_alias_impl_trait) ]
#[ 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 " ,
feature = " nrf5340-app " ,
feature = " nrf5340-net " ,
feature = " nrf9160 " ,
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
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 ;
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 ;
2021-03-27 03:40:05 +00:00
pub mod ppi ;
2021-05-12 02:56:11 +00:00
#[ cfg(not(any(feature = " nrf52805 " , feature = " nrf52820 " ))) ]
pub mod pwm ;
2021-05-11 01:04:59 +00:00
#[ cfg(feature = " nrf52840 " ) ]
2020-09-22 16:03:43 +00:00
pub mod qspi ;
2021-10-11 08:39:38 +00:00
#[ cfg(not(feature = " nrf9160 " )) ]
2021-06-29 07:26:16 +00:00
pub mod rng ;
2021-05-11 01:04:59 +00:00
#[ cfg(not(feature = " nrf52820 " )) ]
2021-03-24 17:33:17 +00:00
pub mod saadc ;
2021-01-18 13:22:55 +00:00
pub mod spim ;
2021-10-19 06:37:19 +00:00
#[ cfg(not(feature = " nrf9160 " )) ]
pub mod temp ;
2021-03-28 20:40:41 +00:00
pub mod timer ;
2021-05-11 01:07:37 +00:00
pub mod twim ;
2020-12-23 15:18:29 +00:00
pub mod uarte ;
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
#[ cfg(feature = " nrf52805 " ) ]
#[ path = " chips/nrf52805.rs " ]
mod chip ;
#[ cfg(feature = " nrf52810 " ) ]
#[ path = " chips/nrf52810.rs " ]
mod chip ;
#[ cfg(feature = " nrf52811 " ) ]
#[ path = " chips/nrf52811.rs " ]
mod chip ;
#[ cfg(feature = " nrf52820 " ) ]
#[ path = " chips/nrf52820.rs " ]
mod chip ;
#[ cfg(feature = " nrf52832 " ) ]
#[ path = " chips/nrf52832.rs " ]
mod chip ;
#[ cfg(feature = " nrf52833 " ) ]
#[ path = " chips/nrf52833.rs " ]
mod chip ;
#[ cfg(feature = " nrf52840 " ) ]
#[ path = " chips/nrf52840.rs " ]
mod chip ;
2021-10-11 08:39:38 +00:00
#[ cfg(feature = " nrf9160 " ) ]
#[ path = " chips/nrf9160.rs " ]
mod chip ;
2021-05-11 01:04:59 +00:00
2021-08-21 23:23:03 +00:00
pub use chip ::EASY_DMA_SIZE ;
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 ;
2021-07-14 20:19:04 +00:00
2021-10-11 08:39:38 +00:00
use crate ::pac ::CLOCK ;
2021-10-12 09:43:57 +00:00
pub use chip ::{ peripherals , Peripherals } ;
2021-10-11 08:39:38 +00:00
2021-05-11 01:04:59 +00:00
pub mod interrupt {
pub use crate ::chip ::irqs ::* ;
pub use cortex_m ::interrupt ::{ CriticalSection , Mutex } ;
pub use embassy ::interrupt ::{ declare , take , Interrupt } ;
2021-07-29 11:44:51 +00:00
pub use embassy_hal_common ::interrupt ::Priority3 as Priority ;
2021-03-21 20:58:59 +00:00
}
2021-05-11 23:00:43 +00:00
pub use embassy_macros ::interrupt ;
2021-05-11 23:57:01 +00:00
pub mod config {
pub enum HfclkSource {
Internal ,
ExternalXtal ,
}
pub enum LfclkSource {
InternalRC ,
2021-10-11 08:39:38 +00:00
#[ cfg(not(feature = " nrf9160 " )) ]
2021-05-11 23:57:01 +00:00
Synthesized ,
ExternalXtal ,
2021-10-11 08:39:38 +00:00
#[ cfg(not(feature = " nrf9160 " )) ]
2021-05-11 23:57:01 +00:00
ExternalLowSwing ,
2021-10-11 08:39:38 +00:00
#[ cfg(not(feature = " nrf9160 " )) ]
2021-05-11 23:57:01 +00:00
ExternalFullSwing ,
}
#[ non_exhaustive ]
pub struct Config {
pub hfclk_source : HfclkSource ,
pub lfclk_source : LfclkSource ,
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 ,
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 ,
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 ,
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 ,
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 ( ) ;
2021-10-11 08:39:38 +00:00
let r = unsafe { & * 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.
2021-10-11 08:39:38 +00:00
#[ cfg(not(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-11 08:39:38 +00:00
#[ cfg(feature = " nrf9160 " ) ]
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 { }
// 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
peripherals
}