1 # SPDX-License-Identifier: GPL-2.0-or-later
4 # Nordic nRF52 series: ARM Cortex-M4 @ 64 MHz
7 source [find target/swj-dp.tcl]
9 if { [info exists CHIPNAME] } {
10 set _CHIPNAME $CHIPNAME
15 # Work-area is a space in RAM used for flash programming
17 if { [info exists WORKAREASIZE] } {
18 set _WORKAREASIZE $WORKAREASIZE
20 set _WORKAREASIZE 0x4000
23 if { [info exists CPUTAPID] } {
24 set _CPUTAPID $CPUTAPID
26 set _CPUTAPID 0x2ba01477
29 swj_newdap $_CHIPNAME cpu -expected-id $_CPUTAPID
30 dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
32 set _TARGETNAME $_CHIPNAME.cpu
33 target create $_TARGETNAME cortex_m -dap $_CHIPNAME.dap
37 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
41 echo "nRF52 device has a CTRL-AP dedicated to recover the device from AP lock."
42 echo "A high level adapter (like a ST-Link) you are currently using cannot access"
43 echo "the CTRL-AP so 'nrf52_recover' command will not work."
44 echo "Do not enable UICR APPROTECT."
47 cortex_m reset_config sysresetreq
49 $_TARGETNAME configure -event examine-fail nrf52_check_ap_lock
52 flash bank $_CHIPNAME.flash nrf5 0x00000000 0 1 1 $_TARGETNAME
53 flash bank $_CHIPNAME.uicr nrf5 0x10001000 0 1 1 $_TARGETNAME
55 # Test if MEM-AP is locked by UICR APPROTECT
56 proc nrf52_check_ap_lock {} {
57 set dap [[target current] cget -dap]
58 set err [catch {set APPROTECTSTATUS [$dap apreg 1 0xc]}]
59 if {$err == 0 && $APPROTECTSTATUS != 1} {
60 echo "****** WARNING ******"
61 echo "nRF52 device has AP lock engaged (see UICR APPROTECT register)."
62 echo "Debug access is denied."
63 echo "Use 'nrf52_recover' to erase and unlock the device."
69 # Mass erase and unlock the device using proprietary nRF CTRL-AP (AP #1)
70 # http://www.ebyte.com produces modules with nRF52 locked by default,
71 # use nrf52_recover to enable flashing and debug.
72 proc nrf52_recover {} {
73 set target [target current]
74 set dap [$target cget -dap]
76 set IDR [$dap apreg 1 0xfc]
77 if {$IDR != 0x02880000} {
78 echo "Error: Cannot access nRF52 CTRL-AP!"
84 # Reset and trigger ERASEALL task
88 for {set i 0} {1} {incr i} {
89 set ERASEALLSTATUS [$dap apreg 1 8]
90 if {$ERASEALLSTATUS == 0} {
91 echo "$target device has been successfully erased and unlocked."
95 echo "Waiting for chip erase..."
98 echo "Error: $target recovery failed."
110 # Reset ERASEALL task
118 add_help_text nrf52_recover "Mass erase and unlock nRF52 device"