2022-06-12 16:42:27 -05:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
2015-11-17 04:59:15 -06:00
|
|
|
#
|
|
|
|
# Nordic nRF52 series: ARM Cortex-M4 @ 64 MHz
|
|
|
|
#
|
|
|
|
|
|
|
|
source [find target/swj-dp.tcl]
|
2023-09-17 13:20:04 -05:00
|
|
|
source [find mem_helper.tcl]
|
2015-11-17 04:59:15 -06:00
|
|
|
|
|
|
|
if { [info exists CHIPNAME] } {
|
|
|
|
set _CHIPNAME $CHIPNAME
|
|
|
|
} else {
|
|
|
|
set _CHIPNAME nrf52
|
|
|
|
}
|
|
|
|
|
2017-08-31 15:34:16 -05:00
|
|
|
# Work-area is a space in RAM used for flash programming
|
|
|
|
# By default use 16kB
|
|
|
|
if { [info exists WORKAREASIZE] } {
|
|
|
|
set _WORKAREASIZE $WORKAREASIZE
|
|
|
|
} else {
|
|
|
|
set _WORKAREASIZE 0x4000
|
|
|
|
}
|
|
|
|
|
2015-11-17 04:59:15 -06:00
|
|
|
if { [info exists CPUTAPID] } {
|
|
|
|
set _CPUTAPID $CPUTAPID
|
|
|
|
} else {
|
|
|
|
set _CPUTAPID 0x2ba01477
|
|
|
|
}
|
|
|
|
|
|
|
|
swj_newdap $_CHIPNAME cpu -expected-id $_CPUTAPID
|
2018-03-23 15:17:29 -05:00
|
|
|
dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
|
2015-11-17 04:59:15 -06:00
|
|
|
|
|
|
|
set _TARGETNAME $_CHIPNAME.cpu
|
2018-03-23 15:17:29 -05:00
|
|
|
target create $_TARGETNAME cortex_m -dap $_CHIPNAME.dap
|
2015-11-17 04:59:15 -06:00
|
|
|
|
2019-08-23 08:51:00 -05:00
|
|
|
adapter speed 1000
|
2017-08-31 15:34:16 -05:00
|
|
|
|
|
|
|
$_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
|
2015-11-17 04:59:15 -06:00
|
|
|
|
2019-03-01 06:35:31 -06:00
|
|
|
if { [using_hla] } {
|
|
|
|
echo ""
|
|
|
|
echo "nRF52 device has a CTRL-AP dedicated to recover the device from AP lock."
|
|
|
|
echo "A high level adapter (like a ST-Link) you are currently using cannot access"
|
|
|
|
echo "the CTRL-AP so 'nrf52_recover' command will not work."
|
|
|
|
echo "Do not enable UICR APPROTECT."
|
|
|
|
echo ""
|
|
|
|
} else {
|
2015-11-17 04:59:15 -06:00
|
|
|
cortex_m reset_config sysresetreq
|
2019-03-01 06:35:31 -06:00
|
|
|
|
|
|
|
$_TARGETNAME configure -event examine-fail nrf52_check_ap_lock
|
2015-11-17 04:59:15 -06:00
|
|
|
}
|
2017-08-31 15:34:16 -05:00
|
|
|
|
|
|
|
flash bank $_CHIPNAME.flash nrf5 0x00000000 0 1 1 $_TARGETNAME
|
|
|
|
flash bank $_CHIPNAME.uicr nrf5 0x10001000 0 1 1 $_TARGETNAME
|
2019-03-01 06:35:31 -06:00
|
|
|
|
|
|
|
# Test if MEM-AP is locked by UICR APPROTECT
|
|
|
|
proc nrf52_check_ap_lock {} {
|
|
|
|
set dap [[target current] cget -dap]
|
2020-10-01 05:36:00 -05:00
|
|
|
set err [catch {set APPROTECTSTATUS [$dap apreg 1 0xc]}]
|
2019-03-01 06:35:31 -06:00
|
|
|
if {$err == 0 && $APPROTECTSTATUS != 1} {
|
|
|
|
echo "****** WARNING ******"
|
|
|
|
echo "nRF52 device has AP lock engaged (see UICR APPROTECT register)."
|
|
|
|
echo "Debug access is denied."
|
|
|
|
echo "Use 'nrf52_recover' to erase and unlock the device."
|
|
|
|
echo ""
|
|
|
|
poll off
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Mass erase and unlock the device using proprietary nRF CTRL-AP (AP #1)
|
|
|
|
# http://www.ebyte.com produces modules with nRF52 locked by default,
|
|
|
|
# use nrf52_recover to enable flashing and debug.
|
|
|
|
proc nrf52_recover {} {
|
|
|
|
set target [target current]
|
|
|
|
set dap [$target cget -dap]
|
|
|
|
|
2020-10-01 05:36:00 -05:00
|
|
|
set IDR [$dap apreg 1 0xfc]
|
2019-03-01 06:35:31 -06:00
|
|
|
if {$IDR != 0x02880000} {
|
|
|
|
echo "Error: Cannot access nRF52 CTRL-AP!"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
poll off
|
|
|
|
|
2020-10-01 05:36:00 -05:00
|
|
|
# Reset and trigger ERASEALL task
|
2019-03-01 06:35:31 -06:00
|
|
|
$dap apreg 1 4 0
|
|
|
|
$dap apreg 1 4 1
|
|
|
|
|
|
|
|
for {set i 0} {1} {incr i} {
|
2020-10-01 05:36:00 -05:00
|
|
|
set ERASEALLSTATUS [$dap apreg 1 8]
|
|
|
|
if {$ERASEALLSTATUS == 0} {
|
2019-03-01 06:35:31 -06:00
|
|
|
echo "$target device has been successfully erased and unlocked."
|
|
|
|
break
|
|
|
|
}
|
2020-10-01 05:36:00 -05:00
|
|
|
if {$i == 0} {
|
|
|
|
echo "Waiting for chip erase..."
|
|
|
|
}
|
|
|
|
if {$i >= 150} {
|
2019-03-01 06:35:31 -06:00
|
|
|
echo "Error: $target recovery failed."
|
|
|
|
break
|
|
|
|
}
|
|
|
|
sleep 100
|
|
|
|
}
|
|
|
|
|
2020-10-01 05:36:00 -05:00
|
|
|
# Assert reset
|
|
|
|
$dap apreg 1 0 1
|
|
|
|
|
2019-03-01 06:35:31 -06:00
|
|
|
# Deassert reset
|
|
|
|
$dap apreg 1 0 0
|
|
|
|
|
2020-10-01 05:36:00 -05:00
|
|
|
# Reset ERASEALL task
|
|
|
|
$dap apreg 1 4 0
|
|
|
|
|
|
|
|
sleep 100
|
|
|
|
$target arp_examine
|
|
|
|
poll on
|
2019-03-01 06:35:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
add_help_text nrf52_recover "Mass erase and unlock nRF52 device"
|
2023-09-17 13:20:04 -05:00
|
|
|
|
|
|
|
tpiu create $_CHIPNAME.tpiu -dap $_CHIPNAME.dap -ap-num 0 -baseaddr 0xE0040000
|
|
|
|
|
|
|
|
lappend _telnet_autocomplete_skip _proc_pre_enable_$_CHIPNAME.tpiu
|
|
|
|
proc _proc_pre_enable_$_CHIPNAME.tpiu {_targetname _chipname} {
|
|
|
|
targets $_targetname
|
|
|
|
|
|
|
|
# Read FICR.INFO.PART
|
|
|
|
set PART [mrw 0x10000100]
|
|
|
|
|
|
|
|
switch $PART {
|
|
|
|
0x52840 -
|
|
|
|
0x52833 -
|
|
|
|
0x52832 {
|
2024-05-05 08:28:20 -05:00
|
|
|
# Configuration values for all supported trace port speeds, see
|
|
|
|
# TRACECONFIG.TRACEPORTSPEED
|
|
|
|
set trace_port_speeds {
|
|
|
|
32000000 0
|
|
|
|
16000000 1
|
|
|
|
8000000 2
|
|
|
|
4000000 3
|
|
|
|
}
|
|
|
|
|
|
|
|
# Note that trace port clock stands for what is referred to as
|
|
|
|
# TRACECLKIN in the Arm CoreSight documentation.
|
|
|
|
set trace_port_clock [$_chipname.tpiu cget -traceclk]
|
|
|
|
|
|
|
|
if { ![dict exists $trace_port_speeds $trace_port_clock] } {
|
|
|
|
error "Trace clock speed is not supported"
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set TRACECONFIG.TRACEPORTSPEED
|
|
|
|
mmw 0x4000055C [dict get $trace_port_speeds $trace_port_clock] 0x3
|
|
|
|
|
2023-09-17 13:20:04 -05:00
|
|
|
if { [$_chipname.tpiu cget -protocol] eq "sync" } {
|
|
|
|
if { [$_chipname.tpiu cget -port-width] != 4 } {
|
2024-05-05 08:20:07 -05:00
|
|
|
error "Device only supports 4-bit sync traces"
|
2023-09-17 13:20:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
# Set TRACECONFIG.TRACEMUX to enable synchronous trace
|
|
|
|
mmw 0x4000055C 0x00020000 0x00010000
|
|
|
|
$_targetname configure -event reset-end {
|
|
|
|
mmw 0x4000055C 0x00020000 0x00010000
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
# Set TRACECONFIG.TRACEMUX to enable SWO
|
|
|
|
mmw 0x4000055C 0x00010000 0x00020000
|
|
|
|
$_targetname configure -event reset-end {
|
|
|
|
mmw 0x4000055C 0x00010000 0x00020000
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
0x52820 -
|
|
|
|
0x52811 -
|
|
|
|
0x52810 -
|
|
|
|
0x52805 {
|
2024-05-05 08:20:07 -05:00
|
|
|
error "Device does not support TPIU"
|
2023-09-17 13:20:04 -05:00
|
|
|
}
|
|
|
|
default {
|
2024-05-05 08:20:07 -05:00
|
|
|
error "Unknown device, cannot configure TPIU"
|
2023-09-17 13:20:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$_CHIPNAME.tpiu configure -event pre-enable "_proc_pre_enable_$_CHIPNAME.tpiu $_TARGETNAME $_CHIPNAME"
|