Skip to content

Commit 52f6e0f

Browse files
bors[bot]sphwjonas-schievink
authored
Merge #366
366: Add nrf5340-app-hal r=jonas-schievink a=sphw This PR adds a HAL for the nRF5340's app core. It is more or less identical to the nRF9160 HAL that already exists, with some features added or removed for the nRF5340. I have not added support for the net core yet, as I don't need it for my use case. Co-authored-by: Sascha Wise <[email protected]> Co-authored-by: Jonas Schievink <[email protected]>
2 parents 169443f + 6d7cd1c commit 52f6e0f

File tree

24 files changed

+433
-111
lines changed

24 files changed

+433
-111
lines changed

Cargo.ci.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ members = [
77
"nrf52833-hal",
88
"nrf52840-hal",
99
"nrf52840-hal-tests",
10+
"nrf5340-app-hal",
1011
"nrf9160-hal",
1112
"examples/*",
1213
]

nrf-hal-common/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ version = "0.10.1"
6060
optional = true
6161
version = "0.10.1"
6262

63+
[dependencies.nrf5340-app-pac]
64+
optional = true
65+
version = "0.10.1"
66+
6367
[dependencies.nrf9160-pac]
6468
optional = true
6569
version = "0.10.1"
@@ -80,4 +84,5 @@ doc = []
8084
52832 = ["nrf52832-pac"]
8185
52833 = ["nrf52833-pac", "nrf-usbd"]
8286
52840 = ["nrf52840-pac", "nrf-usbd"]
87+
5340-app = ["nrf5340-app-pac"]
8388
9160 = ["nrf9160-pac"]

nrf-hal-common/src/clocks.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Configuration and control of the High and Low Frequency Clock sources.
22
3-
#[cfg(feature = "9160")]
3+
#[cfg(any(feature = "9160", feature = "5340-app"))]
44
use crate::pac::CLOCK_NS as CLOCK;
55

6-
#[cfg(not(feature = "9160"))]
6+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
77
use crate::pac::CLOCK;
88

99
// ZST Type States
@@ -156,7 +156,7 @@ impl<H, L> Clocks<H, L, LfOscStopped> {
156156
}
157157

158158
/// Use the internal RC Oscillator for the low frequency clock source.
159-
#[cfg(not(any(feature = "9160", feature = "51")))]
159+
#[cfg(not(any(feature = "9160", feature = "5340-app", feature = "51")))]
160160
pub fn set_lfclk_src_rc(self) -> Clocks<H, Internal, LfOscStopped> {
161161
self.periph
162162
.lfclksrc
@@ -170,7 +170,7 @@ impl<H, L> Clocks<H, L, LfOscStopped> {
170170
}
171171

172172
/// Generate the Low Frequency clock from the high frequency clock source.
173-
#[cfg(not(any(feature = "9160", feature = "51")))]
173+
#[cfg(not(any(feature = "9160", feature = "5340-app", feature = "51")))]
174174
pub fn set_lfclk_src_synth(self) -> Clocks<H, LfOscSynthesized, LfOscStopped> {
175175
self.periph
176176
.lfclksrc
@@ -184,7 +184,7 @@ impl<H, L> Clocks<H, L, LfOscStopped> {
184184
}
185185

186186
/// Use an external crystal to drive the low frequency clock.
187-
#[cfg(not(any(feature = "9160", feature = "51")))]
187+
#[cfg(not(any(feature = "9160", feature = "5340-app", feature = "51")))]
188188
pub fn set_lfclk_src_external(
189189
self,
190190
cfg: LfOscConfiguration,

nrf-hal-common/src/comp.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
//! Vin can be derived from an analog input pin (AIN0-AIN7).
55
//! Vref can be derived from multiple sources depending on the operation mode of the comparator.
66
7-
use {
8-
crate::gpio::{p0::*, Floating, Input},
9-
crate::pac::{
10-
comp::{extrefsel::EXTREFSEL_A, psel::PSEL_A, EVENTS_CROSS, EVENTS_DOWN, EVENTS_UP},
11-
COMP,
12-
},
7+
use crate::gpio::{p0::*, Floating, Input};
8+
#[cfg(not(feature = "5340-app"))]
9+
use crate::pac::comp::{
10+
extrefsel::EXTREFSEL_A, psel::PSEL_A, EVENTS_CROSS, EVENTS_DOWN, EVENTS_UP,
1311
};
12+
use crate::pac::COMP;
1413

14+
#[cfg(feature = "5340-app")]
15+
use crate::pac::comp_ns::{
16+
extrefsel::EXTREFSEL_A, psel::PSEL_A, EVENTS_CROSS, EVENTS_DOWN, EVENTS_UP,
17+
};
1518
/// A safe wrapper around the `COMP` peripheral.
1619
pub struct Comp {
1720
comp: COMP,

nrf-hal-common/src/gpio.rs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub enum Level {
3737
pub enum Port {
3838
/// Port 0, available on all nRF52 and nRF51 MCUs.
3939
Port0,
40+
/// Port 0 Secure, available on nRF53
41+
#[cfg(any(feature = "5340-app"))]
42+
Port0Secure,
4043

4144
/// Port 1, only available on some nRF52 MCUs.
4245
#[cfg(any(feature = "52833", feature = "52840"))]
@@ -60,10 +63,13 @@ pub struct Pin<MODE> {
6063
#[cfg(feature = "51")]
6164
use crate::pac::{gpio, GPIO as P0};
6265

63-
#[cfg(feature = "9160")]
66+
#[cfg(any(feature = "5340-app", feature = "9160"))]
6467
use crate::pac::{p0_ns as gpio, P0_NS as P0};
6568

66-
#[cfg(not(any(feature = "9160", feature = "51")))]
69+
#[cfg(feature = "5340-app")]
70+
use crate::pac::P0_S;
71+
72+
#[cfg(not(any(feature = "9160", feature = "5340-app", feature = "51")))]
6773
use crate::pac::{p0 as gpio, P0};
6874

6975
#[cfg(any(feature = "52833", feature = "52840"))]
@@ -76,6 +82,8 @@ impl<MODE> Pin<MODE> {
7682
fn new(port: Port, pin: u8) -> Self {
7783
let port_bits = match port {
7884
Port::Port0 => 0x00,
85+
#[cfg(any(feature = "5340-app"))]
86+
Port::Port0Secure => 0x20,
7987
#[cfg(any(feature = "52833", feature = "52840"))]
8088
Port::Port1 => 0x20,
8189
};
@@ -94,12 +102,12 @@ impl<MODE> Pin<MODE> {
94102

95103
#[inline]
96104
pub fn pin(&self) -> u8 {
97-
#[cfg(any(feature = "52833", feature = "52840"))]
105+
#[cfg(any(feature = "52833", feature = "52840", feature = "5340-app"))]
98106
{
99107
self.pin_port & 0x1f
100108
}
101109

102-
#[cfg(not(any(feature = "52833", feature = "52840")))]
110+
#[cfg(not(any(feature = "52833", feature = "52840", feature = "5340-app")))]
103111
{
104112
self.pin_port
105113
}
@@ -116,7 +124,16 @@ impl<MODE> Pin<MODE> {
116124
}
117125
}
118126

119-
#[cfg(not(any(feature = "52833", feature = "52840")))]
127+
#[cfg(any(feature = "5340-app"))]
128+
{
129+
if self.pin_port & 0x20 == 0 {
130+
Port::Port0
131+
} else {
132+
Port::Port0Secure
133+
}
134+
}
135+
136+
#[cfg(not(any(feature = "52833", feature = "52840", feature = "5340-app")))]
120137
{
121138
Port::Port0
122139
}
@@ -130,6 +147,8 @@ impl<MODE> Pin<MODE> {
130147
fn block(&self) -> &gpio::RegisterBlock {
131148
let ptr = match self.port() {
132149
Port::Port0 => P0::ptr(),
150+
#[cfg(any(feature = "5340-app"))]
151+
Port::Port0Secure => P0_S::ptr(),
133152
#[cfg(any(feature = "52833", feature = "52840"))]
134153
Port::Port1 => P1::ptr(),
135154
};
@@ -320,10 +339,10 @@ pub enum OpenDrainConfig {
320339
#[cfg(feature = "51")]
321340
use crate::pac::gpio::pin_cnf;
322341

323-
#[cfg(feature = "9160")]
342+
#[cfg(any(feature = "5340-app", feature = "9160"))]
324343
use crate::pac::p0_ns::pin_cnf;
325344

326-
#[cfg(not(any(feature = "9160", feature = "51")))]
345+
#[cfg(not(any(feature = "9160", feature = "5340-app", feature = "51")))]
327346
use crate::pac::p0::pin_cnf;
328347

329348
impl OpenDrainConfig {
@@ -647,3 +666,39 @@ gpio!(P1, p0, p1, Port::Port1, [
647666
P1_14: (p1_14, 14, Disconnected),
648667
P1_15: (p1_15, 15, Disconnected),
649668
]);
669+
670+
#[cfg(any(feature = "5340-app"))]
671+
gpio!(P0_S, p0, p0_s, Port::Port0Secure, [
672+
P0_00: (p0_00, 0, Disconnected),
673+
P0_01: (p0_01, 1, Disconnected),
674+
P0_02: (p0_02, 2, Disconnected),
675+
P0_03: (p0_03, 3, Disconnected),
676+
P0_04: (p0_04, 4, Disconnected),
677+
P0_05: (p0_05, 5, Disconnected),
678+
P0_06: (p0_06, 6, Disconnected),
679+
P0_07: (p0_07, 7, Disconnected),
680+
P0_08: (p0_08, 8, Disconnected),
681+
P0_09: (p0_09, 9, Disconnected),
682+
P0_10: (p0_10, 10, Disconnected),
683+
P0_11: (p0_11, 11, Disconnected),
684+
P0_12: (p0_12, 12, Disconnected),
685+
P0_13: (p0_13, 13, Disconnected),
686+
P0_14: (p0_14, 14, Disconnected),
687+
P0_15: (p0_15, 15, Disconnected),
688+
P0_16: (p0_16, 16, Disconnected),
689+
P0_17: (p0_17, 17, Disconnected),
690+
P0_18: (p0_18, 18, Disconnected),
691+
P0_19: (p0_19, 19, Disconnected),
692+
P0_20: (p0_20, 20, Disconnected),
693+
P0_21: (p0_21, 21, Disconnected),
694+
P0_22: (p0_22, 22, Disconnected),
695+
P0_23: (p0_23, 23, Disconnected),
696+
P0_24: (p0_24, 24, Disconnected),
697+
P0_25: (p0_25, 25, Disconnected),
698+
P0_26: (p0_26, 26, Disconnected),
699+
P0_27: (p0_27, 27, Disconnected),
700+
P0_28: (p0_28, 28, Disconnected),
701+
P0_29: (p0_29, 29, Disconnected),
702+
P0_30: (p0_30, 30, Disconnected),
703+
P0_31: (p0_31, 31, Disconnected),
704+
]);

nrf-hal-common/src/i2s.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! HAL interface for the I2S peripheral.
22
//!
33
4-
#[cfg(not(feature = "9160"))]
4+
#[cfg(not(any(feature = "5340-app", feature = "9160")))]
55
use crate::pac::{i2s, I2S as I2S_PAC};
6+
#[cfg(feature = "5340-app")]
7+
use crate::pac::{i2s0_ns as i2s, I2S0_NS as I2S_PAC};
68
#[cfg(feature = "9160")]
79
use crate::pac::{i2s_ns as i2s, I2S_NS as I2S_PAC};
810
use crate::{
@@ -198,7 +200,12 @@ impl I2S {
198200
self.i2s
199201
.config
200202
.swidth
201-
.write(|w| unsafe { w.swidth().bits(width.into()) });
203+
.write(|w| {
204+
#[cfg(not(feature = "5340-app"))]
205+
unsafe { w.swidth().bits(width.into()) }
206+
#[cfg(feature = "5340-app")]
207+
w.swidth().bits(width.into())
208+
});
202209
self
203210
}
204211

nrf-hal-common/src/lib.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,49 +24,57 @@ pub use nrf52833_pac as pac;
2424
#[cfg(feature = "52840")]
2525
pub use nrf52840_pac as pac;
2626

27+
#[cfg(feature = "5340-app")]
28+
pub use nrf5340_app_pac as pac;
29+
2730
#[cfg(feature = "9160")]
2831
pub use nrf9160_pac as pac;
2932

3033
#[cfg(feature = "51")]
3134
pub mod adc;
32-
#[cfg(not(feature = "9160"))]
35+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
3336
pub mod ccm;
3437
pub mod clocks;
35-
#[cfg(not(any(feature = "51", feature = "9160")))]
38+
#[cfg(not(any(feature = "51", feature = "9160", feature = "5340-app")))]
3639
pub mod comp;
3740
#[cfg(not(feature = "51"))]
3841
pub mod delay;
39-
#[cfg(not(feature = "9160"))]
42+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
4043
pub mod ecb;
4144
pub mod gpio;
42-
#[cfg(not(feature = "9160"))]
45+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
4346
pub mod gpiote;
4447
#[cfg(not(any(feature = "51", feature = "52810", feature = "52811")))]
4548
pub mod i2s;
4649
#[cfg(any(feature = "52833", feature = "52840"))]
4750
pub mod ieee802154;
48-
#[cfg(not(any(feature = "52811", feature = "52810", feature = "9160")))]
51+
#[cfg(not(any(
52+
feature = "52811",
53+
feature = "52810",
54+
feature = "9160",
55+
feature = "5340-app"
56+
)))]
4957
pub mod lpcomp;
5058
#[cfg(not(feature = "51"))]
5159
pub mod nvmc;
52-
#[cfg(not(feature = "9160"))]
60+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
5361
pub mod ppi;
5462
#[cfg(not(feature = "51"))]
5563
pub mod pwm;
56-
#[cfg(not(any(feature = "51", feature = "9160")))]
64+
#[cfg(not(any(feature = "51", feature = "9160", feature = "5340-app")))]
5765
pub mod qdec;
58-
#[cfg(not(feature = "9160"))]
66+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
5967
pub mod rng;
6068
pub mod rtc;
6169
#[cfg(not(feature = "51"))]
6270
pub mod saadc;
63-
#[cfg(not(feature = "9160"))]
71+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
6472
pub mod spi;
6573
#[cfg(not(feature = "51"))]
6674
pub mod spim;
6775
#[cfg(not(feature = "51"))]
6876
pub mod spis;
69-
#[cfg(not(feature = "9160"))]
77+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
7078
pub mod temp;
7179
pub mod time;
7280
pub mod timer;
@@ -80,7 +88,7 @@ pub mod twis;
8088
pub mod uart;
8189
#[cfg(not(feature = "51"))]
8290
pub mod uarte;
83-
#[cfg(not(feature = "9160"))]
91+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
8492
pub mod uicr;
8593
#[cfg(feature = "nrf-usbd")]
8694
pub mod usbd;
@@ -90,7 +98,7 @@ pub mod prelude {
9098
pub use crate::hal::digital::v2::*;
9199
pub use crate::hal::prelude::*;
92100

93-
#[cfg(not(feature = "9160"))]
101+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
94102
pub use crate::ppi::{ConfigurablePpi, Ppi};
95103
pub use crate::time::U32Ext;
96104
}
@@ -113,7 +121,7 @@ pub mod target_constants {
113121
pub const EASY_DMA_SIZE: usize = (1 << 16) - 1;
114122
#[cfg(feature = "52840")]
115123
pub const EASY_DMA_SIZE: usize = (1 << 16) - 1;
116-
#[cfg(feature = "5340")]
124+
#[cfg(feature = "5340-app")]
117125
pub const EASY_DMA_SIZE: usize = (1 << 16) - 1;
118126
#[cfg(feature = "9160")]
119127
pub const EASY_DMA_SIZE: usize = (1 << 12) - 1;
@@ -172,7 +180,7 @@ impl DmaSlice {
172180
pub use crate::clocks::Clocks;
173181
#[cfg(not(feature = "51"))]
174182
pub use crate::delay::Delay;
175-
#[cfg(not(feature = "9160"))]
183+
#[cfg(not(any(feature = "9160", feature = "5340-app")))]
176184
pub use crate::rng::Rng;
177185
pub use crate::rtc::Rtc;
178186
pub use crate::timer::Timer;

0 commit comments

Comments
 (0)