Skip to content

Commit

Permalink
nrf54l: Allow debug access from firmware sid3
Browse files Browse the repository at this point in the history
This adds support for the
  • Loading branch information
Tiwalun committed Dec 25, 2024
1 parent fd2b6c8 commit c306d45
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 10 deletions.
89 changes: 80 additions & 9 deletions embassy-nrf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ pub fn init(config: config::Config) -> Peripherals {
let mut needs_reset = false;

// Setup debug protection.
#[cfg(not(feature = "_nrf54l"))] // TODO
#[cfg(not(feature = "_nrf51"))]
match config.debug {
config::Debug::Allowed => {
Expand Down Expand Up @@ -549,18 +548,90 @@ pub fn init(config: config::Config) -> Peripherals {
}
}

#[cfg(feature = "_nrf54l")]
{
use crate::pac::tampc::regs::DomainDbgenCtrl;
use crate::pac::tampc::regs::NidenCtrl;
use crate::pac::tampc::regs::SpidenCtrl;
use crate::pac::tampc::regs::SpnidenCtrl;
use crate::pac::tampc::vals::DomainDbgenCtrlValue;

// UICR cannot be written here, because it can only be written once after an erase all.
// The erase all value means that debug access is allowed if permitted by the firmware.

// Write to TAMPC to permit debug access
//
// See https://docs.nordicsemi.com/bundle/ps_nrf54L15/page/debug.html#ariaid-title6

let p = pac::TAMPC;

// Unlock dbgen
let mut writeprotect_clear = DomainDbgenCtrl::default();
writeprotect_clear.set_key(crate::pac::tampc::vals::DomainDbgenCtrlKey::KEY);
writeprotect_clear.set_writeprotection(crate::pac::tampc::vals::DomainDbgenCtrlWriteprotection::CLEAR);

let mut debug_enable = DomainDbgenCtrl::default();
debug_enable.set_key(crate::pac::tampc::vals::DomainDbgenCtrlKey::KEY);
debug_enable.set_value(DomainDbgenCtrlValue::HIGH);

p.protect().domain(0).dbgen().ctrl().write_value(writeprotect_clear);
p.protect().domain(0).dbgen().ctrl().write_value(debug_enable);

// Unlock niden
//
// Reuse values from dbgen disable
p.protect()
.domain(0)
.niden()
.ctrl()
.write_value(NidenCtrl(writeprotect_clear.0));
p.protect()
.domain(0)
.niden()
.ctrl()
.write_value(NidenCtrl(debug_enable.0));

p.protect()
.domain(0)
.spiden()
.ctrl()
.write_value(SpidenCtrl(writeprotect_clear.0));
p.protect()
.domain(0)
.spiden()
.ctrl()
.write_value(SpidenCtrl(debug_enable.0));

p.protect()
.domain(0)
.spniden()
.ctrl()
.write_value(SpnidenCtrl(writeprotect_clear.0));
p.protect()
.domain(0)
.spniden()
.ctrl()
.write_value(SpnidenCtrl(debug_enable.0));
}

// nothing to do on the nrf9160, debug is allowed by default.
}
config::Debug::Disallowed => unsafe {
// UICR.APPROTECT = Enabled
let res = uicr_write(consts::UICR_APPROTECT, consts::APPROTECT_ENABLED);
needs_reset |= res == WriteResult::Written;
#[cfg(any(feature = "_nrf5340-app", feature = "_nrf91"))]
{
let res = uicr_write(consts::UICR_SECUREAPPROTECT, consts::APPROTECT_ENABLED);
config::Debug::Disallowed => {
// TODO: Handle nRF54L
// By default, debug access is not allowed if the firmware doesn't allow it.
// Code could be added here to disable debug access in UICR as well.
#[cfg(not(feature = "_nrf54l"))]
unsafe {
// UICR.APPROTECT = Enabled
let res = uicr_write(consts::UICR_APPROTECT, consts::APPROTECT_ENABLED);
needs_reset |= res == WriteResult::Written;
#[cfg(any(feature = "_nrf5340-app", feature = "_nrf91"))]
{
let res = uicr_write(consts::UICR_SECUREAPPROTECT, consts::APPROTECT_ENABLED);
needs_reset |= res == WriteResult::Written;
}
}
},
}
config::Debug::NotConfigured => {}
}

Expand Down
2 changes: 1 addition & 1 deletion examples/nrf54l15/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[target.'cfg(all(target_arch = "arm", target_os = "none"))']
# replace nRF82840_xxAA with your chip as listed in `probe-rs chip list`
runner = "../../sshprobe.sh"
runner = "probe-rs run --chip nrf54l15"

[build]
target = "thumbv8m.main-none-eabihf"
Expand Down

0 comments on commit c306d45

Please sign in to comment.