aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xci.sh4
-rw-r--r--embassy-nrf/Cargo.toml35
-rw-r--r--embassy-nrf/src/chips/nrf52805.rs4
-rw-r--r--embassy-nrf/src/chips/nrf52810.rs4
-rw-r--r--embassy-nrf/src/chips/nrf52811.rs4
-rw-r--r--embassy-nrf/src/chips/nrf52820.rs4
-rw-r--r--embassy-nrf/src/chips/nrf52832.rs8
-rw-r--r--embassy-nrf/src/chips/nrf52833.rs8
-rw-r--r--embassy-nrf/src/chips/nrf52840.rs8
-rw-r--r--embassy-nrf/src/chips/nrf5340_app.rs4
-rw-r--r--embassy-nrf/src/lib.rs208
-rw-r--r--embassy-nrf/src/nvmc.rs12
12 files changed, 284 insertions, 19 deletions
diff --git a/ci.sh b/ci.sh
index 4199f91d3..417937d07 100755
--- a/ci.sh
+++ b/ci.sh
@@ -46,8 +46,8 @@ cargo batch \
46 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52810,gpiote,time-driver-rtc1 \ 46 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52810,gpiote,time-driver-rtc1 \
47 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52811,gpiote,time-driver-rtc1 \ 47 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52811,gpiote,time-driver-rtc1 \
48 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52820,gpiote,time-driver-rtc1 \ 48 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52820,gpiote,time-driver-rtc1 \
49 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52832,gpiote,time-driver-rtc1 \ 49 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52832,gpiote,time-driver-rtc1,reset-pin-as-gpio \
50 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52833,gpiote,time-driver-rtc1,unstable-traits \ 50 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv7em-none-eabi --features nightly,nrf52833,gpiote,time-driver-rtc1,unstable-traits,nfc-pins-as-gpio \
51 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nightly,nrf9160-s,gpiote,time-driver-rtc1 \ 51 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nightly,nrf9160-s,gpiote,time-driver-rtc1 \
52 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nightly,nrf9160-ns,gpiote,time-driver-rtc1,unstable-traits \ 52 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nightly,nrf9160-ns,gpiote,time-driver-rtc1,unstable-traits \
53 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nightly,nrf5340-app-s,gpiote,time-driver-rtc1,unstable-traits \ 53 --- build --release --manifest-path embassy-nrf/Cargo.toml --target thumbv8m.main-none-eabihf --features nightly,nrf5340-app-s,gpiote,time-driver-rtc1,unstable-traits \
diff --git a/embassy-nrf/Cargo.toml b/embassy-nrf/Cargo.toml
index 6b06d5d05..c31ce199b 100644
--- a/embassy-nrf/Cargo.toml
+++ b/embassy-nrf/Cargo.toml
@@ -34,22 +34,30 @@ unstable-pac = []
34# Implement embedded-hal-async traits if `nightly` is set as well. 34# Implement embedded-hal-async traits if `nightly` is set as well.
35unstable-traits = ["embedded-hal-1"] 35unstable-traits = ["embedded-hal-1"]
36 36
37nrf52805 = ["nrf52805-pac", "_ppi"] 37nrf52805 = ["nrf52805-pac", "_nrf52"]
38nrf52810 = ["nrf52810-pac", "_ppi"] 38nrf52810 = ["nrf52810-pac", "_nrf52"]
39nrf52811 = ["nrf52811-pac", "_ppi"] 39nrf52811 = ["nrf52811-pac", "_nrf52"]
40nrf52820 = ["nrf52820-pac", "_ppi"] 40nrf52820 = ["nrf52820-pac", "_nrf52"]
41nrf52832 = ["nrf52832-pac", "_ppi"] 41nrf52832 = ["nrf52832-pac", "_nrf52"]
42nrf52833 = ["nrf52833-pac", "_ppi", "_gpio-p1"] 42nrf52833 = ["nrf52833-pac", "_nrf52", "_gpio-p1"]
43nrf52840 = ["nrf52840-pac", "_ppi", "_gpio-p1"] 43nrf52840 = ["nrf52840-pac", "_nrf52", "_gpio-p1"]
44nrf5340-app-s = ["_nrf5340-app"] 44nrf5340-app-s = ["_nrf5340-app", "_s"]
45nrf5340-app-ns = ["_nrf5340-app"] 45nrf5340-app-ns = ["_nrf5340-app", "_ns"]
46nrf5340-net = ["_nrf5340-net"] 46nrf5340-net = ["_nrf5340-net"]
47nrf9160-s = ["_nrf9160"] 47nrf9160-s = ["_nrf9160", "_s"]
48nrf9160-ns = ["_nrf9160"] 48nrf9160-ns = ["_nrf9160", "_ns"]
49 49
50gpiote = [] 50gpiote = []
51time-driver-rtc1 = ["_time-driver"] 51time-driver-rtc1 = ["_time-driver"]
52 52
53# Allow using the NFC pins as regular GPIO pins (P0_09/P0_10 on nRF52, P0_02/P0_03 on nRF53)
54nfc-pins-as-gpio = []
55
56# Allow using the RST pin as a regular GPIO pin.
57# nrf52805, nrf52810, nrf52811, nrf52832: P0_21
58# nrf52820, nrf52833, nrf52840: P0_18
59reset-pin-as-gpio = []
60
53# Features starting with `_` are for internal use only. They're not intended 61# Features starting with `_` are for internal use only. They're not intended
54# to be enabled by other crates, and are not covered by semver guarantees. 62# to be enabled by other crates, and are not covered by semver guarantees.
55 63
@@ -57,9 +65,14 @@ _nrf5340-app = ["_nrf5340", "nrf5340-app-pac"]
57_nrf5340-net = ["_nrf5340", "nrf5340-net-pac"] 65_nrf5340-net = ["_nrf5340", "nrf5340-net-pac"]
58_nrf5340 = ["_gpio-p1", "_dppi"] 66_nrf5340 = ["_gpio-p1", "_dppi"]
59_nrf9160 = ["nrf9160-pac", "_dppi"] 67_nrf9160 = ["nrf9160-pac", "_dppi"]
68_nrf52 = ["_ppi"]
60 69
61_time-driver = ["dep:embassy-time", "embassy-time?/tick-hz-32_768"] 70_time-driver = ["dep:embassy-time", "embassy-time?/tick-hz-32_768"]
62 71
72# trustzone state.
73_s = []
74_ns = []
75
63_ppi = [] 76_ppi = []
64_dppi = [] 77_dppi = []
65_gpio-p1 = [] 78_gpio-p1 = []
diff --git a/embassy-nrf/src/chips/nrf52805.rs b/embassy-nrf/src/chips/nrf52805.rs
index bf4019c13..3c74a2a63 100644
--- a/embassy-nrf/src/chips/nrf52805.rs
+++ b/embassy-nrf/src/chips/nrf52805.rs
@@ -6,6 +6,8 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 256;
6 6
7pub const FLASH_SIZE: usize = 192 * 1024; 7pub const FLASH_SIZE: usize = 192 * 1024;
8 8
9pub const RESET_PIN: u32 = 21;
10
9embassy_hal_common::peripherals! { 11embassy_hal_common::peripherals! {
10 // RTC 12 // RTC
11 RTC0, 13 RTC0,
@@ -108,6 +110,7 @@ embassy_hal_common::peripherals! {
108 P0_18, 110 P0_18,
109 P0_19, 111 P0_19,
110 P0_20, 112 P0_20,
113 #[cfg(feature="reset-pin-as-gpio")]
111 P0_21, 114 P0_21,
112 P0_22, 115 P0_22,
113 P0_23, 116 P0_23,
@@ -162,6 +165,7 @@ impl_pin!(P0_17, 0, 17);
162impl_pin!(P0_18, 0, 18); 165impl_pin!(P0_18, 0, 18);
163impl_pin!(P0_19, 0, 19); 166impl_pin!(P0_19, 0, 19);
164impl_pin!(P0_20, 0, 20); 167impl_pin!(P0_20, 0, 20);
168#[cfg(feature = "reset-pin-as-gpio")]
165impl_pin!(P0_21, 0, 21); 169impl_pin!(P0_21, 0, 21);
166impl_pin!(P0_22, 0, 22); 170impl_pin!(P0_22, 0, 22);
167impl_pin!(P0_23, 0, 23); 171impl_pin!(P0_23, 0, 23);
diff --git a/embassy-nrf/src/chips/nrf52810.rs b/embassy-nrf/src/chips/nrf52810.rs
index 6c28a3bea..6b5c134b8 100644
--- a/embassy-nrf/src/chips/nrf52810.rs
+++ b/embassy-nrf/src/chips/nrf52810.rs
@@ -6,6 +6,8 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 256;
6 6
7pub const FLASH_SIZE: usize = 192 * 1024; 7pub const FLASH_SIZE: usize = 192 * 1024;
8 8
9pub const RESET_PIN: u32 = 21;
10
9embassy_hal_common::peripherals! { 11embassy_hal_common::peripherals! {
10 // RTC 12 // RTC
11 RTC0, 13 RTC0,
@@ -111,6 +113,7 @@ embassy_hal_common::peripherals! {
111 P0_18, 113 P0_18,
112 P0_19, 114 P0_19,
113 P0_20, 115 P0_20,
116 #[cfg(feature="reset-pin-as-gpio")]
114 P0_21, 117 P0_21,
115 P0_22, 118 P0_22,
116 P0_23, 119 P0_23,
@@ -170,6 +173,7 @@ impl_pin!(P0_17, 0, 17);
170impl_pin!(P0_18, 0, 18); 173impl_pin!(P0_18, 0, 18);
171impl_pin!(P0_19, 0, 19); 174impl_pin!(P0_19, 0, 19);
172impl_pin!(P0_20, 0, 20); 175impl_pin!(P0_20, 0, 20);
176#[cfg(feature = "reset-pin-as-gpio")]
173impl_pin!(P0_21, 0, 21); 177impl_pin!(P0_21, 0, 21);
174impl_pin!(P0_22, 0, 22); 178impl_pin!(P0_22, 0, 22);
175impl_pin!(P0_23, 0, 23); 179impl_pin!(P0_23, 0, 23);
diff --git a/embassy-nrf/src/chips/nrf52811.rs b/embassy-nrf/src/chips/nrf52811.rs
index e7214cf5c..c5de9a447 100644
--- a/embassy-nrf/src/chips/nrf52811.rs
+++ b/embassy-nrf/src/chips/nrf52811.rs
@@ -6,6 +6,8 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 256;
6 6
7pub const FLASH_SIZE: usize = 192 * 1024; 7pub const FLASH_SIZE: usize = 192 * 1024;
8 8
9pub const RESET_PIN: u32 = 21;
10
9embassy_hal_common::peripherals! { 11embassy_hal_common::peripherals! {
10 // RTC 12 // RTC
11 RTC0, 13 RTC0,
@@ -111,6 +113,7 @@ embassy_hal_common::peripherals! {
111 P0_18, 113 P0_18,
112 P0_19, 114 P0_19,
113 P0_20, 115 P0_20,
116 #[cfg(feature="reset-pin-as-gpio")]
114 P0_21, 117 P0_21,
115 P0_22, 118 P0_22,
116 P0_23, 119 P0_23,
@@ -172,6 +175,7 @@ impl_pin!(P0_17, 0, 17);
172impl_pin!(P0_18, 0, 18); 175impl_pin!(P0_18, 0, 18);
173impl_pin!(P0_19, 0, 19); 176impl_pin!(P0_19, 0, 19);
174impl_pin!(P0_20, 0, 20); 177impl_pin!(P0_20, 0, 20);
178#[cfg(feature = "reset-pin-as-gpio")]
175impl_pin!(P0_21, 0, 21); 179impl_pin!(P0_21, 0, 21);
176impl_pin!(P0_22, 0, 22); 180impl_pin!(P0_22, 0, 22);
177impl_pin!(P0_23, 0, 23); 181impl_pin!(P0_23, 0, 23);
diff --git a/embassy-nrf/src/chips/nrf52820.rs b/embassy-nrf/src/chips/nrf52820.rs
index 21d1d16cc..81b07f32c 100644
--- a/embassy-nrf/src/chips/nrf52820.rs
+++ b/embassy-nrf/src/chips/nrf52820.rs
@@ -6,6 +6,8 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
6 6
7pub const FLASH_SIZE: usize = 256 * 1024; 7pub const FLASH_SIZE: usize = 256 * 1024;
8 8
9pub const RESET_PIN: u32 = 18;
10
9embassy_hal_common::peripherals! { 11embassy_hal_common::peripherals! {
10 // USB 12 // USB
11 USBD, 13 USBD,
@@ -106,6 +108,7 @@ embassy_hal_common::peripherals! {
106 P0_15, 108 P0_15,
107 P0_16, 109 P0_16,
108 P0_17, 110 P0_17,
111 #[cfg(feature="reset-pin-as-gpio")]
109 P0_18, 112 P0_18,
110 P0_19, 113 P0_19,
111 P0_20, 114 P0_20,
@@ -168,6 +171,7 @@ impl_pin!(P0_14, 0, 14);
168impl_pin!(P0_15, 0, 15); 171impl_pin!(P0_15, 0, 15);
169impl_pin!(P0_16, 0, 16); 172impl_pin!(P0_16, 0, 16);
170impl_pin!(P0_17, 0, 17); 173impl_pin!(P0_17, 0, 17);
174#[cfg(feature = "reset-pin-as-gpio")]
171impl_pin!(P0_18, 0, 18); 175impl_pin!(P0_18, 0, 18);
172impl_pin!(P0_19, 0, 19); 176impl_pin!(P0_19, 0, 19);
173impl_pin!(P0_20, 0, 20); 177impl_pin!(P0_20, 0, 20);
diff --git a/embassy-nrf/src/chips/nrf52832.rs b/embassy-nrf/src/chips/nrf52832.rs
index 152dad4e3..c2b23fc5b 100644
--- a/embassy-nrf/src/chips/nrf52832.rs
+++ b/embassy-nrf/src/chips/nrf52832.rs
@@ -10,6 +10,8 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 255;
10// nrf52832xxAB = 256kb 10// nrf52832xxAB = 256kb
11pub const FLASH_SIZE: usize = 512 * 1024; 11pub const FLASH_SIZE: usize = 512 * 1024;
12 12
13pub const RESET_PIN: u32 = 21;
14
13embassy_hal_common::peripherals! { 15embassy_hal_common::peripherals! {
14 // RTC 16 // RTC
15 RTC0, 17 RTC0,
@@ -109,7 +111,9 @@ embassy_hal_common::peripherals! {
109 P0_06, 111 P0_06,
110 P0_07, 112 P0_07,
111 P0_08, 113 P0_08,
114 #[cfg(feature = "nfc-pins-as-gpio")]
112 P0_09, 115 P0_09,
116 #[cfg(feature = "nfc-pins-as-gpio")]
113 P0_10, 117 P0_10,
114 P0_11, 118 P0_11,
115 P0_12, 119 P0_12,
@@ -121,6 +125,7 @@ embassy_hal_common::peripherals! {
121 P0_18, 125 P0_18,
122 P0_19, 126 P0_19,
123 P0_20, 127 P0_20,
128 #[cfg(feature="reset-pin-as-gpio")]
124 P0_21, 129 P0_21,
125 P0_22, 130 P0_22,
126 P0_23, 131 P0_23,
@@ -178,7 +183,9 @@ impl_pin!(P0_05, 0, 5);
178impl_pin!(P0_06, 0, 6); 183impl_pin!(P0_06, 0, 6);
179impl_pin!(P0_07, 0, 7); 184impl_pin!(P0_07, 0, 7);
180impl_pin!(P0_08, 0, 8); 185impl_pin!(P0_08, 0, 8);
186#[cfg(feature = "nfc-pins-as-gpio")]
181impl_pin!(P0_09, 0, 9); 187impl_pin!(P0_09, 0, 9);
188#[cfg(feature = "nfc-pins-as-gpio")]
182impl_pin!(P0_10, 0, 10); 189impl_pin!(P0_10, 0, 10);
183impl_pin!(P0_11, 0, 11); 190impl_pin!(P0_11, 0, 11);
184impl_pin!(P0_12, 0, 12); 191impl_pin!(P0_12, 0, 12);
@@ -190,6 +197,7 @@ impl_pin!(P0_17, 0, 17);
190impl_pin!(P0_18, 0, 18); 197impl_pin!(P0_18, 0, 18);
191impl_pin!(P0_19, 0, 19); 198impl_pin!(P0_19, 0, 19);
192impl_pin!(P0_20, 0, 20); 199impl_pin!(P0_20, 0, 20);
200#[cfg(feature = "reset-pin-as-gpio")]
193impl_pin!(P0_21, 0, 21); 201impl_pin!(P0_21, 0, 21);
194impl_pin!(P0_22, 0, 22); 202impl_pin!(P0_22, 0, 22);
195impl_pin!(P0_23, 0, 23); 203impl_pin!(P0_23, 0, 23);
diff --git a/embassy-nrf/src/chips/nrf52833.rs b/embassy-nrf/src/chips/nrf52833.rs
index a99ca6343..95f71ade7 100644
--- a/embassy-nrf/src/chips/nrf52833.rs
+++ b/embassy-nrf/src/chips/nrf52833.rs
@@ -6,6 +6,8 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
6 6
7pub const FLASH_SIZE: usize = 512 * 1024; 7pub const FLASH_SIZE: usize = 512 * 1024;
8 8
9pub const RESET_PIN: u32 = 18;
10
9embassy_hal_common::peripherals! { 11embassy_hal_common::peripherals! {
10 // USB 12 // USB
11 USBD, 13 USBD,
@@ -111,7 +113,9 @@ embassy_hal_common::peripherals! {
111 P0_06, 113 P0_06,
112 P0_07, 114 P0_07,
113 P0_08, 115 P0_08,
116 #[cfg(feature = "nfc-pins-as-gpio")]
114 P0_09, 117 P0_09,
118 #[cfg(feature = "nfc-pins-as-gpio")]
115 P0_10, 119 P0_10,
116 P0_11, 120 P0_11,
117 P0_12, 121 P0_12,
@@ -120,6 +124,7 @@ embassy_hal_common::peripherals! {
120 P0_15, 124 P0_15,
121 P0_16, 125 P0_16,
122 P0_17, 126 P0_17,
127 #[cfg(feature="reset-pin-as-gpio")]
123 P0_18, 128 P0_18,
124 P0_19, 129 P0_19,
125 P0_20, 130 P0_20,
@@ -207,7 +212,9 @@ impl_pin!(P0_05, 0, 5);
207impl_pin!(P0_06, 0, 6); 212impl_pin!(P0_06, 0, 6);
208impl_pin!(P0_07, 0, 7); 213impl_pin!(P0_07, 0, 7);
209impl_pin!(P0_08, 0, 8); 214impl_pin!(P0_08, 0, 8);
215#[cfg(feature = "nfc-pins-as-gpio")]
210impl_pin!(P0_09, 0, 9); 216impl_pin!(P0_09, 0, 9);
217#[cfg(feature = "nfc-pins-as-gpio")]
211impl_pin!(P0_10, 0, 10); 218impl_pin!(P0_10, 0, 10);
212impl_pin!(P0_11, 0, 11); 219impl_pin!(P0_11, 0, 11);
213impl_pin!(P0_12, 0, 12); 220impl_pin!(P0_12, 0, 12);
@@ -216,6 +223,7 @@ impl_pin!(P0_14, 0, 14);
216impl_pin!(P0_15, 0, 15); 223impl_pin!(P0_15, 0, 15);
217impl_pin!(P0_16, 0, 16); 224impl_pin!(P0_16, 0, 16);
218impl_pin!(P0_17, 0, 17); 225impl_pin!(P0_17, 0, 17);
226#[cfg(feature = "reset-pin-as-gpio")]
219impl_pin!(P0_18, 0, 18); 227impl_pin!(P0_18, 0, 18);
220impl_pin!(P0_19, 0, 19); 228impl_pin!(P0_19, 0, 19);
221impl_pin!(P0_20, 0, 20); 229impl_pin!(P0_20, 0, 20);
diff --git a/embassy-nrf/src/chips/nrf52840.rs b/embassy-nrf/src/chips/nrf52840.rs
index 4f7463be2..5e7479e88 100644
--- a/embassy-nrf/src/chips/nrf52840.rs
+++ b/embassy-nrf/src/chips/nrf52840.rs
@@ -6,6 +6,8 @@ pub const FORCE_COPY_BUFFER_SIZE: usize = 512;
6 6
7pub const FLASH_SIZE: usize = 1024 * 1024; 7pub const FLASH_SIZE: usize = 1024 * 1024;
8 8
9pub const RESET_PIN: u32 = 18;
10
9embassy_hal_common::peripherals! { 11embassy_hal_common::peripherals! {
10 // USB 12 // USB
11 USBD, 13 USBD,
@@ -117,7 +119,9 @@ embassy_hal_common::peripherals! {
117 P0_06, 119 P0_06,
118 P0_07, 120 P0_07,
119 P0_08, 121 P0_08,
122 #[cfg(feature = "nfc-pins-as-gpio")]
120 P0_09, 123 P0_09,
124 #[cfg(feature = "nfc-pins-as-gpio")]
121 P0_10, 125 P0_10,
122 P0_11, 126 P0_11,
123 P0_12, 127 P0_12,
@@ -126,6 +130,7 @@ embassy_hal_common::peripherals! {
126 P0_15, 130 P0_15,
127 P0_16, 131 P0_16,
128 P0_17, 132 P0_17,
133 #[cfg(feature="reset-pin-as-gpio")]
129 P0_18, 134 P0_18,
130 P0_19, 135 P0_19,
131 P0_20, 136 P0_20,
@@ -212,7 +217,9 @@ impl_pin!(P0_05, 0, 5);
212impl_pin!(P0_06, 0, 6); 217impl_pin!(P0_06, 0, 6);
213impl_pin!(P0_07, 0, 7); 218impl_pin!(P0_07, 0, 7);
214impl_pin!(P0_08, 0, 8); 219impl_pin!(P0_08, 0, 8);
220#[cfg(feature = "nfc-pins-as-gpio")]
215impl_pin!(P0_09, 0, 9); 221impl_pin!(P0_09, 0, 9);
222#[cfg(feature = "nfc-pins-as-gpio")]
216impl_pin!(P0_10, 0, 10); 223impl_pin!(P0_10, 0, 10);
217impl_pin!(P0_11, 0, 11); 224impl_pin!(P0_11, 0, 11);
218impl_pin!(P0_12, 0, 12); 225impl_pin!(P0_12, 0, 12);
@@ -221,6 +228,7 @@ impl_pin!(P0_14, 0, 14);
221impl_pin!(P0_15, 0, 15); 228impl_pin!(P0_15, 0, 15);
222impl_pin!(P0_16, 0, 16); 229impl_pin!(P0_16, 0, 16);
223impl_pin!(P0_17, 0, 17); 230impl_pin!(P0_17, 0, 17);
231#[cfg(feature = "reset-pin-as-gpio")]
224impl_pin!(P0_18, 0, 18); 232impl_pin!(P0_18, 0, 18);
225impl_pin!(P0_19, 0, 19); 233impl_pin!(P0_19, 0, 19);
226impl_pin!(P0_20, 0, 20); 234impl_pin!(P0_20, 0, 20);
diff --git a/embassy-nrf/src/chips/nrf5340_app.rs b/embassy-nrf/src/chips/nrf5340_app.rs
index c600fcbf6..2b8b55e77 100644
--- a/embassy-nrf/src/chips/nrf5340_app.rs
+++ b/embassy-nrf/src/chips/nrf5340_app.rs
@@ -304,7 +304,9 @@ embassy_hal_common::peripherals! {
304 // GPIO port 0 304 // GPIO port 0
305 P0_00, 305 P0_00,
306 P0_01, 306 P0_01,
307 #[cfg(feature = "nfc-pins-as-gpio")]
307 P0_02, 308 P0_02,
309 #[cfg(feature = "nfc-pins-as-gpio")]
308 P0_03, 310 P0_03,
309 P0_04, 311 P0_04,
310 P0_05, 312 P0_05,
@@ -393,7 +395,9 @@ impl_timer!(TIMER2, TIMER2, TIMER2);
393 395
394impl_pin!(P0_00, 0, 0); 396impl_pin!(P0_00, 0, 0);
395impl_pin!(P0_01, 0, 1); 397impl_pin!(P0_01, 0, 1);
398#[cfg(feature = "nfc-pins-as-gpio")]
396impl_pin!(P0_02, 0, 2); 399impl_pin!(P0_02, 0, 2);
400#[cfg(feature = "nfc-pins-as-gpio")]
397impl_pin!(P0_03, 0, 3); 401impl_pin!(P0_03, 0, 3);
398impl_pin!(P0_04, 0, 4); 402impl_pin!(P0_04, 0, 4);
399impl_pin!(P0_05, 0, 5); 403impl_pin!(P0_05, 0, 5);
diff --git a/embassy-nrf/src/lib.rs b/embassy-nrf/src/lib.rs
index 20e70a248..a9683df44 100644
--- a/embassy-nrf/src/lib.rs
+++ b/embassy-nrf/src/lib.rs
@@ -24,6 +24,12 @@
24)))] 24)))]
25compile_error!("No chip feature activated. You must activate exactly one of the following features: nrf52810, nrf52811, nrf52832, nrf52833, nrf52840"); 25compile_error!("No chip feature activated. You must activate exactly one of the following features: nrf52810, nrf52811, nrf52832, nrf52833, nrf52840");
26 26
27#[cfg(all(feature = "reset-pin-as-gpio", not(feature = "_nrf52")))]
28compile_error!("feature `reset-pin-as-gpio` is only valid for nRF52 series chips.");
29
30#[cfg(all(feature = "nfc-pins-as-gpio", not(any(feature = "_nrf52", feature = "_nrf5340-app"))))]
31compile_error!("feature `nfc-pins-as-gpio` is only valid for nRF52, or nRF53's application core.");
32
27// This mod MUST go first, so that the others see its macros. 33// This mod MUST go first, so that the others see its macros.
28pub(crate) mod fmt; 34pub(crate) mod fmt;
29pub(crate) mod util; 35pub(crate) mod util;
@@ -139,6 +145,19 @@ pub mod config {
139 ExternalFullSwing, 145 ExternalFullSwing,
140 } 146 }
141 147
148 /// SWD access port protection setting.
149 #[non_exhaustive]
150 pub enum Debug {
151 /// Debugging is allowed (APPROTECT is disabled). Default.
152 Allowed,
153 /// Debugging is not allowed (APPROTECT is enabled).
154 Disallowed,
155 /// APPROTECT is not configured (neither to enable it or disable it).
156 /// This can be useful if you're already doing it by other means and
157 /// you don't want embassy-nrf to touch UICR.
158 NotConfigured,
159 }
160
142 /// Configuration for peripherals. Default configuration should work on any nRF chip. 161 /// Configuration for peripherals. Default configuration should work on any nRF chip.
143 #[non_exhaustive] 162 #[non_exhaustive]
144 pub struct Config { 163 pub struct Config {
@@ -152,6 +171,8 @@ pub mod config {
152 /// Time driver interrupt priority. Should be lower priority than softdevice if used. 171 /// Time driver interrupt priority. Should be lower priority than softdevice if used.
153 #[cfg(feature = "_time-driver")] 172 #[cfg(feature = "_time-driver")]
154 pub time_interrupt_priority: crate::interrupt::Priority, 173 pub time_interrupt_priority: crate::interrupt::Priority,
174 /// Enable or disable the debug port.
175 pub debug: Debug,
155 } 176 }
156 177
157 impl Default for Config { 178 impl Default for Config {
@@ -166,17 +187,204 @@ pub mod config {
166 gpiote_interrupt_priority: crate::interrupt::Priority::P0, 187 gpiote_interrupt_priority: crate::interrupt::Priority::P0,
167 #[cfg(feature = "_time-driver")] 188 #[cfg(feature = "_time-driver")]
168 time_interrupt_priority: crate::interrupt::Priority::P0, 189 time_interrupt_priority: crate::interrupt::Priority::P0,
190
191 // In NS mode, default to NotConfigured, assuming the S firmware will do it.
192 #[cfg(feature = "_ns")]
193 debug: Debug::NotConfigured,
194 #[cfg(not(feature = "_ns"))]
195 debug: Debug::Allowed,
169 } 196 }
170 } 197 }
171 } 198 }
172} 199}
173 200
201#[cfg(feature = "_nrf9160")]
202#[allow(unused)]
203mod consts {
204 pub const UICR_APPROTECT: *mut u32 = 0x00FF8000 as *mut u32;
205 pub const UICR_SECUREAPPROTECT: *mut u32 = 0x00FF802C as *mut u32;
206 pub const APPROTECT_ENABLED: u32 = 0x0000_0000;
207}
208
209#[cfg(feature = "_nrf5340-app")]
210#[allow(unused)]
211mod consts {
212 pub const UICR_APPROTECT: *mut u32 = 0x00FF8000 as *mut u32;
213 pub const UICR_SECUREAPPROTECT: *mut u32 = 0x00FF801C as *mut u32;
214 pub const UICR_NFCPINS: *mut u32 = 0x00FF8028 as *mut u32;
215 pub const APPROTECT_ENABLED: u32 = 0x0000_0000;
216 pub const APPROTECT_DISABLED: u32 = 0x50FA50FA;
217}
218
219#[cfg(feature = "_nrf5340-net")]
220#[allow(unused)]
221mod consts {
222 pub const UICR_APPROTECT: *mut u32 = 0x01FF8000 as *mut u32;
223 pub const APPROTECT_ENABLED: u32 = 0x0000_0000;
224 pub const APPROTECT_DISABLED: u32 = 0x50FA50FA;
225}
226
227#[cfg(feature = "_nrf52")]
228#[allow(unused)]
229mod consts {
230 pub const UICR_PSELRESET1: *mut u32 = 0x10001200 as *mut u32;
231 pub const UICR_PSELRESET2: *mut u32 = 0x10001204 as *mut u32;
232 pub const UICR_NFCPINS: *mut u32 = 0x1000120C as *mut u32;
233 pub const UICR_APPROTECT: *mut u32 = 0x10001208 as *mut u32;
234 pub const APPROTECT_ENABLED: u32 = 0x0000_0000;
235 pub const APPROTECT_DISABLED: u32 = 0x0000_005a;
236}
237
238#[derive(Debug, Copy, Clone, Eq, PartialEq)]
239#[cfg_attr(feature = "defmt", derive(defmt::Format))]
240enum WriteResult {
241 /// Word was written successfully, needs reset.
242 Written,
243 /// Word was already set to the value we wanted to write, nothing was done.
244 Noop,
245 /// Word is already set to something else, we couldn't write the desired value.
246 Failed,
247}
248
249unsafe fn uicr_write(address: *mut u32, value: u32) -> WriteResult {
250 let curr_val = address.read_volatile();
251 if curr_val == value {
252 return WriteResult::Noop;
253 }
254
255 // We can only change `1` bits to `0` bits.
256 if curr_val & value != value {
257 return WriteResult::Failed;
258 }
259
260 // Writing to UICR can only change `1` bits to `0` bits.
261 // If this write would change `0` bits to `1` bits, we can't do it.
262 // It is only possible to do when erasing UICR, which is forbidden if
263 // APPROTECT is enabled.
264 if (!curr_val) & value != 0 {
265 panic!("Cannot write UICR address={:08x} value={:08x}", address as u32, value)
266 }
267
268 let nvmc = &*pac::NVMC::ptr();
269 nvmc.config.write(|w| w.wen().wen());
270 while nvmc.ready.read().ready().is_busy() {}
271 address.write_volatile(value);
272 while nvmc.ready.read().ready().is_busy() {}
273 nvmc.config.reset();
274 while nvmc.ready.read().ready().is_busy() {}
275
276 WriteResult::Written
277}
278
174/// Initialize peripherals with the provided configuration. This should only be called once at startup. 279/// Initialize peripherals with the provided configuration. This should only be called once at startup.
175pub fn init(config: config::Config) -> Peripherals { 280pub fn init(config: config::Config) -> Peripherals {
176 // Do this first, so that it panics if user is calling `init` a second time 281 // Do this first, so that it panics if user is calling `init` a second time
177 // before doing anything important. 282 // before doing anything important.
178 let peripherals = Peripherals::take(); 283 let peripherals = Peripherals::take();
179 284
285 let mut needs_reset = false;
286
287 // Setup debug protection.
288 match config.debug {
289 config::Debug::Allowed => {
290 #[cfg(feature = "_nrf52")]
291 unsafe {
292 let variant = (0x1000_0104 as *mut u32).read_volatile();
293 // Get the letter for the build code (b'A' .. b'F')
294 let build_code = (variant >> 8) as u8;
295
296 if build_code >= b'F' {
297 // Chips with build code F and higher (revision 3 and higher) have an
298 // improved APPROTECT ("hardware and software controlled access port protection")
299 // which needs explicit action by the firmware to keep it unlocked
300
301 // UICR.APPROTECT = SwDisabled
302 let res = uicr_write(consts::UICR_APPROTECT, consts::APPROTECT_DISABLED);
303 needs_reset |= res == WriteResult::Written;
304 // APPROTECT.DISABLE = SwDisabled
305 (0x4000_0558 as *mut u32).write_volatile(consts::APPROTECT_DISABLED);
306 } else {
307 // nothing to do on older chips, debug is allowed by default.
308 }
309 }
310
311 #[cfg(feature = "_nrf5340")]
312 unsafe {
313 let p = &*pac::CTRLAP::ptr();
314
315 let res = uicr_write(consts::UICR_APPROTECT, consts::APPROTECT_DISABLED);
316 needs_reset |= res == WriteResult::Written;
317 p.approtect.disable.write(|w| w.bits(consts::APPROTECT_DISABLED));
318
319 #[cfg(feature = "_nrf5340-app")]
320 {
321 let res = uicr_write(consts::UICR_SECUREAPPROTECT, consts::APPROTECT_DISABLED);
322 needs_reset |= res == WriteResult::Written;
323 p.secureapprotect.disable.write(|w| w.bits(consts::APPROTECT_DISABLED));
324 }
325 }
326
327 // nothing to do on the nrf9160, debug is allowed by default.
328 }
329 config::Debug::Disallowed => unsafe {
330 // UICR.APPROTECT = Enabled
331 let res = uicr_write(consts::UICR_APPROTECT, consts::APPROTECT_ENABLED);
332 needs_reset |= res == WriteResult::Written;
333 #[cfg(any(feature = "_nrf5340-app", feature = "_nrf9160"))]
334 {
335 let res = uicr_write(consts::UICR_SECUREAPPROTECT, consts::APPROTECT_ENABLED);
336 needs_reset |= res == WriteResult::Written;
337 }
338 },
339 config::Debug::NotConfigured => {}
340 }
341
342 #[cfg(feature = "_nrf52")]
343 unsafe {
344 let value = if cfg!(feature = "reset-pin-as-gpio") {
345 !0
346 } else {
347 chip::RESET_PIN
348 };
349 let res1 = uicr_write(consts::UICR_PSELRESET1, value);
350 let res2 = uicr_write(consts::UICR_PSELRESET2, value);
351 needs_reset |= res1 == WriteResult::Written || res2 == WriteResult::Written;
352 if res1 == WriteResult::Failed || res2 == WriteResult::Failed {
353 #[cfg(not(feature = "reset-pin-as-gpio"))]
354 warn!(
355 "You have requested enabling chip reset functionality on the reset pin, by not enabling the Cargo feature `reset-pin-as-gpio`.\n\
356 However, UICR is already programmed to some other setting, and can't be changed without erasing it.\n\
357 To fix this, erase UICR manually, for example using `probe-rs-cli erase` or `nrfjprog --eraseuicr`."
358 );
359 #[cfg(feature = "reset-pin-as-gpio")]
360 warn!(
361 "You have requested using the reset pin as GPIO, by enabling the Cargo feature `reset-pin-as-gpio`.\n\
362 However, UICR is already programmed to some other setting, and can't be changed without erasing it.\n\
363 To fix this, erase UICR manually, for example using `probe-rs-cli erase` or `nrfjprog --eraseuicr`."
364 );
365 }
366 }
367
368 #[cfg(any(feature = "_nrf52", feature = "_nrf5340-app"))]
369 unsafe {
370 let value = if cfg!(feature = "nfc-pins-as-gpio") { 0 } else { !0 };
371 let res = uicr_write(consts::UICR_NFCPINS, value);
372 needs_reset |= res == WriteResult::Written;
373 if res == WriteResult::Failed {
374 // with nfc-pins-as-gpio, this can never fail because we're writing all zero bits.
375 #[cfg(not(feature = "nfc-pins-as-gpio"))]
376 warn!(
377 "You have requested to use P0.09 and P0.10 pins for NFC, by not enabling the Cargo feature `nfc-pins-as-gpio`.\n\
378 However, UICR is already programmed to some other setting, and can't be changed without erasing it.\n\
379 To fix this, erase UICR manually, for example using `probe-rs-cli erase` or `nrfjprog --eraseuicr`."
380 );
381 }
382 }
383
384 if needs_reset {
385 cortex_m::peripheral::SCB::sys_reset();
386 }
387
180 let r = unsafe { &*pac::CLOCK::ptr() }; 388 let r = unsafe { &*pac::CLOCK::ptr() };
181 389
182 // Start HFCLK. 390 // Start HFCLK.
diff --git a/embassy-nrf/src/nvmc.rs b/embassy-nrf/src/nvmc.rs
index c1ffa31aa..6f48853d5 100644
--- a/embassy-nrf/src/nvmc.rs
+++ b/embassy-nrf/src/nvmc.rs
@@ -85,23 +85,23 @@ impl<'d> Nvmc<'d> {
85 } 85 }
86 86
87 fn enable_erase(&self) { 87 fn enable_erase(&self) {
88 #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] 88 #[cfg(not(feature = "_ns"))]
89 Self::regs().config.write(|w| w.wen().een()); 89 Self::regs().config.write(|w| w.wen().een());
90 #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] 90 #[cfg(feature = "_ns")]
91 Self::regs().configns.write(|w| w.wen().een()); 91 Self::regs().configns.write(|w| w.wen().een());
92 } 92 }
93 93
94 fn enable_read(&self) { 94 fn enable_read(&self) {
95 #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] 95 #[cfg(not(feature = "_ns"))]
96 Self::regs().config.write(|w| w.wen().ren()); 96 Self::regs().config.write(|w| w.wen().ren());
97 #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] 97 #[cfg(feature = "_ns")]
98 Self::regs().configns.write(|w| w.wen().ren()); 98 Self::regs().configns.write(|w| w.wen().ren());
99 } 99 }
100 100
101 fn enable_write(&self) { 101 fn enable_write(&self) {
102 #[cfg(not(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns")))] 102 #[cfg(not(feature = "_ns"))]
103 Self::regs().config.write(|w| w.wen().wen()); 103 Self::regs().config.write(|w| w.wen().wen());
104 #[cfg(any(feature = "nrf5340-app-ns", feature = "nrf9160-ns"))] 104 #[cfg(feature = "_ns")]
105 Self::regs().configns.write(|w| w.wen().wen()); 105 Self::regs().configns.write(|w| w.wen().wen());
106 } 106 }
107} 107}