Skip to content

Commit

Permalink
nrf: port to chiptool-based nrf-pac.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirbaio committed Nov 3, 2024
1 parent 650f979 commit 51f6b81
Show file tree
Hide file tree
Showing 58 changed files with 2,160 additions and 2,297 deletions.
3 changes: 2 additions & 1 deletion embassy-net-nrf91/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ log = [ "dep:log" ]
defmt = { version = "0.3", optional = true }
log = { version = "0.4.14", optional = true }

nrf9160-pac = { version = "0.12.0" }
nrf-pac = { git = "https://github.com/embassy-rs/nrf-pac", rev = "875a29629cc1c87aae00cfea647a956b3807d8be" }
cortex-m = "0.7.7"

embassy-time = { version = "0.3.1", path = "../embassy-time" }
embassy-sync = { version = "0.6.0", path = "../embassy-sync"}
Expand Down
78 changes: 37 additions & 41 deletions embassy-net-nrf91/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ use core::slice;
use core::sync::atomic::{compiler_fence, fence, Ordering};
use core::task::{Poll, Waker};

use cortex_m::peripheral::NVIC;
use embassy_sync::blocking_mutex::raw::NoopRawMutex;
use embassy_sync::pipe;
use embassy_sync::waitqueue::{AtomicWaker, WakerRegistration};
use heapless::Vec;
use pac::NVIC;
use {embassy_net_driver_channel as ch, nrf9160_pac as pac};
use {embassy_net_driver_channel as ch, nrf_pac as pac};

const RX_SIZE: usize = 8 * 1024;
const TRACE_SIZE: usize = 16 * 1024;
Expand All @@ -38,11 +38,9 @@ static WAKER: AtomicWaker = AtomicWaker::new();

/// Call this function on IPC IRQ
pub fn on_ipc_irq() {
let ipc = unsafe { &*pac::IPC_NS::ptr() };

trace!("irq");

ipc.inten.write(|w| w);
pac::IPC_NS.inten().write(|_| ());
WAKER.wake();
}

Expand Down Expand Up @@ -135,31 +133,30 @@ async fn new_internal<'a>(
"shmem must be in the lower 128kb of RAM"
);

let spu = unsafe { &*pac::SPU_S::ptr() };
let spu = pac::SPU_S;
debug!("Setting IPC RAM as nonsecure...");
let region_start = (shmem_ptr as usize - 0x2000_0000) / SPU_REGION_SIZE;
let region_end = region_start + shmem_len / SPU_REGION_SIZE;
for i in region_start..region_end {
spu.ramregion[i].perm.write(|w| {
w.execute().set_bit();
w.write().set_bit();
w.read().set_bit();
w.secattr().clear_bit();
w.lock().clear_bit();
w
spu.ramregion(i).perm().write(|w| {
w.set_execute(true);
w.set_write(true);
w.set_read(true);
w.set_secattr(false);
w.set_lock(false);
})
}

spu.periphid[42].perm.write(|w| w.secattr().non_secure());
spu.periphid(42).perm().write(|w| w.set_secattr(false));

let mut alloc = Allocator {
start: shmem_ptr,
end: unsafe { shmem_ptr.add(shmem_len) },
_phantom: PhantomData,
};

let ipc = unsafe { &*pac::IPC_NS::ptr() };
let power = unsafe { &*pac::POWER_S::ptr() };
let ipc = pac::IPC_NS;
let power = pac::POWER_S;

let cb: &mut ControlBlock = alloc.alloc().write(unsafe { mem::zeroed() });
let rx = alloc.alloc_bytes(RX_SIZE);
Expand All @@ -177,20 +174,20 @@ async fn new_internal<'a>(
cb.trace.base = trace.as_mut_ptr() as _;
cb.trace.size = TRACE_SIZE;

ipc.gpmem[0].write(|w| unsafe { w.bits(cb as *mut _ as u32) });
ipc.gpmem[1].write(|w| unsafe { w.bits(0) });
ipc.gpmem(0).write_value(cb as *mut _ as u32);
ipc.gpmem(1).write_value(0);

// connect task/event i to channel i
for i in 0..8 {
ipc.send_cnf[i].write(|w| unsafe { w.bits(1 << i) });
ipc.receive_cnf[i].write(|w| unsafe { w.bits(1 << i) });
ipc.send_cnf(i).write(|w| w.0 = 1 << i);
ipc.receive_cnf(i).write(|w| w.0 = 1 << i);
}

compiler_fence(Ordering::SeqCst);

// POWER.LTEMODEM.STARTN = 0
// The reg is missing in the PAC??
let startn = unsafe { (power as *const _ as *mut u32).add(0x610 / 4) };
let startn = unsafe { (power.as_ptr() as *mut u32).add(0x610 / 4) };
unsafe { startn.write_volatile(0) }

unsafe { NVIC::unmask(pac::Interrupt::IPC) };
Expand Down Expand Up @@ -322,15 +319,15 @@ struct StateInner {
impl StateInner {
fn poll(&mut self, trace_writer: &mut Option<TraceWriter<'_>>, ch: &mut ch::Runner<MTU>) {
trace!("poll!");
let ipc = unsafe { &*pac::IPC_NS::ptr() };
let ipc = pac::IPC_NS;

if ipc.events_receive[0].read().bits() != 0 {
ipc.events_receive[0].reset();
if ipc.events_receive(0).read() != 0 {
ipc.events_receive(0).write_value(0);
trace!("ipc 0");
}

if ipc.events_receive[2].read().bits() != 0 {
ipc.events_receive[2].reset();
if ipc.events_receive(2).read() != 0 {
ipc.events_receive(2).write_value(0);
trace!("ipc 2");

if !self.init {
Expand All @@ -353,8 +350,8 @@ impl StateInner {
}
}

if ipc.events_receive[4].read().bits() != 0 {
ipc.events_receive[4].reset();
if ipc.events_receive(4).read() != 0 {
ipc.events_receive(4).write_value(0);
trace!("ipc 4");

loop {
Expand All @@ -368,13 +365,13 @@ impl StateInner {
}
}

if ipc.events_receive[6].read().bits() != 0 {
ipc.events_receive[6].reset();
if ipc.events_receive(6).read() != 0 {
ipc.events_receive(6).write_value(0);
trace!("ipc 6");
}

if ipc.events_receive[7].read().bits() != 0 {
ipc.events_receive[7].reset();
if ipc.events_receive(7).read() != 0 {
ipc.events_receive(7).write_value(0);
trace!("ipc 7: trace");

let msg = unsafe { addr_of!((*self.cb).trace.rx_state).read_volatile() };
Expand Down Expand Up @@ -437,13 +434,12 @@ impl StateInner {
}
}

ipc.intenset.write(|w| {
w.receive0().set_bit();
w.receive2().set_bit();
w.receive4().set_bit();
w.receive6().set_bit();
w.receive7().set_bit();
w
ipc.intenset().write(|w| {
w.set_receive0(true);
w.set_receive2(true);
w.set_receive4(true);
w.set_receive6(true);
w.set_receive7(true);
});
}

Expand Down Expand Up @@ -546,8 +542,8 @@ impl StateInner {
unsafe { addr_of_mut!((*list_item).state).write_volatile((self.tx_seq_no as u32) << 16 | 0x01) }
self.tx_seq_no = self.tx_seq_no.wrapping_add(1);

let ipc = unsafe { &*pac::IPC_NS::ptr() };
ipc.tasks_send[ipc_ch].write(|w| unsafe { w.bits(1) });
let ipc = pac::IPC_NS;
ipc.tasks_send(ipc_ch).write_value(1);
Ok(())
}

Expand Down
55 changes: 16 additions & 39 deletions embassy-nrf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ src_base_git = "https://github.com/embassy-rs/embassy/blob/$COMMIT/embassy-nrf/s

features = ["time", "defmt", "unstable-pac", "gpiote", "time-driver-rtc1"]
flavors = [
{ regex_feature = "nrf51", target = "thumbv6m-none-eabi" },
{ regex_feature = "_nrf51", target = "thumbv6m-none-eabi" },
{ regex_feature = "nrf52.*", target = "thumbv7em-none-eabihf" },
{ regex_feature = "nrf53.*", target = "thumbv8m.main-none-eabihf" },
{ regex_feature = "nrf91.*", target = "thumbv8m.main-none-eabihf" },
Expand All @@ -28,20 +28,7 @@ rustdoc-args = ["--cfg", "docsrs"]
[features]
default = ["rt"]
## Cortex-M runtime (enabled by default)
rt = [
"nrf51-pac?/rt",
"nrf52805-pac?/rt",
"nrf52810-pac?/rt",
"nrf52811-pac?/rt",
"nrf52820-pac?/rt",
"nrf52832-pac?/rt",
"nrf52833-pac?/rt",
"nrf52840-pac?/rt",
"nrf5340-app-pac?/rt",
"nrf5340-net-pac?/rt",
"nrf9160-pac?/rt",
"nrf9120-pac?/rt",
]
rt = ["nrf-pac/rt"]

## Enable features requiring `embassy-time`
time = ["dep:embassy-time"]
Expand Down Expand Up @@ -75,21 +62,21 @@ qspi-multiwrite-flash = []

#! ### Chip selection features
## nRF51
nrf51 = ["nrf51-pac", "_nrf51"]
nrf51 = ["nrf-pac/nrf51", "_nrf51"]
## nRF52805
nrf52805 = ["nrf52805-pac", "_nrf52"]
nrf52805 = ["nrf-pac/nrf52805", "_nrf52"]
## nRF52810
nrf52810 = ["nrf52810-pac", "_nrf52"]
nrf52810 = ["nrf-pac/nrf52810", "_nrf52"]
## nRF52811
nrf52811 = ["nrf52811-pac", "_nrf52"]
nrf52811 = ["nrf-pac/nrf52811", "_nrf52"]
## nRF52820
nrf52820 = ["nrf52820-pac", "_nrf52"]
nrf52820 = ["nrf-pac/nrf52820", "_nrf52"]
## nRF52832
nrf52832 = ["nrf52832-pac", "_nrf52", "_nrf52832_anomaly_109"]
nrf52832 = ["nrf-pac/nrf52832", "_nrf52", "_nrf52832_anomaly_109"]
## nRF52833
nrf52833 = ["nrf52833-pac", "_nrf52", "_gpio-p1"]
nrf52833 = ["nrf-pac/nrf52833", "_nrf52", "_gpio-p1"]
## nRF52840
nrf52840 = ["nrf52840-pac", "_nrf52", "_gpio-p1"]
nrf52840 = ["nrf-pac/nrf52840", "_nrf52", "_gpio-p1"]
## nRF5340 application core in Secure mode
nrf5340-app-s = ["_nrf5340-app", "_s"]
## nRF5340 application core in Non-Secure mode
Expand All @@ -113,11 +100,11 @@ nrf9161-ns = ["nrf9120-ns"]
# Features starting with `_` are for internal use only. They're not intended
# to be enabled by other crates, and are not covered by semver guarantees.

_nrf5340-app = ["_nrf5340", "nrf5340-app-pac"]
_nrf5340-net = ["_nrf5340", "nrf5340-net-pac"]
_nrf5340-app = ["_nrf5340", "nrf-pac/nrf5340-app"]
_nrf5340-net = ["_nrf5340", "nrf-pac/nrf5340-net"]
_nrf5340 = ["_gpio-p1", "_dppi"]
_nrf9160 = ["nrf9160-pac", "_dppi"]
_nrf9120 = ["nrf9120-pac", "_dppi"]
_nrf9160 = ["nrf-pac/nrf9160", "_dppi"]
_nrf9120 = ["nrf-pac/nrf9120", "_dppi"]
_nrf52 = ["_ppi"]
_nrf51 = ["_ppi"]
_nrf91 = []
Expand Down Expand Up @@ -149,6 +136,8 @@ embedded-hal-async = { version = "1.0" }
embedded-io = { version = "0.6.0" }
embedded-io-async = { version = "0.6.1" }

nrf-pac = { git = "https://github.com/embassy-rs/nrf-pac", rev = "875a29629cc1c87aae00cfea647a956b3807d8be" }

defmt = { version = "0.3", optional = true }
bitflags = "2.4.2"
log = { version = "0.4.14", optional = true }
Expand All @@ -162,15 +151,3 @@ embedded-storage-async = "0.4.1"
cfg-if = "1.0.0"
document-features = "0.2.7"

nrf51-pac = { version = "0.12.0", optional = true }
nrf52805-pac = { version = "0.12.0", optional = true }
nrf52810-pac = { version = "0.12.0", optional = true }
nrf52811-pac = { version = "0.12.0", optional = true }
nrf52820-pac = { version = "0.12.0", optional = true }
nrf52832-pac = { version = "0.12.0", optional = true }
nrf52833-pac = { version = "0.12.0", optional = true }
nrf52840-pac = { version = "0.12.0", optional = true }
nrf5340-app-pac = { version = "0.12.0", optional = true }
nrf5340-net-pac = { version = "0.12.0", optional = true }
nrf9160-pac = { version = "0.12.0", optional = true }
nrf9120-pac = { version = "0.12.0", optional = true }
Loading

0 comments on commit 51f6b81

Please sign in to comment.