2 # Nordic nRF52 series: ARM Cortex-M4 @ 64 MHz
5 source [find target/swj-dp.tcl]
7 if { [info exists CHIPNAME] } {
8 set _CHIPNAME $CHIPNAME
13 # Work-area is a space in RAM used for flash programming
15 if { [info exists WORKAREASIZE] } {
16 set _WORKAREASIZE $WORKAREASIZE
18 set _WORKAREASIZE 0x4000
21 if { [info exists CPUTAPID] } {
22 set _CPUTAPID $CPUTAPID
24 set _CPUTAPID 0x2ba01477
27 swj_newdap $_CHIPNAME cpu -expected-id $_CPUTAPID
28 dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
30 set _TARGETNAME $_CHIPNAME.cpu
31 target create $_TARGETNAME cortex_m -dap $_CHIPNAME.dap
35 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
39 echo "nRF52 device has a CTRL-AP dedicated to recover the device from AP lock."
40 echo "A high level adapter (like a ST-Link) you are currently using cannot access"
41 echo "the CTRL-AP so 'nrf52_recover' command will not work."
42 echo "Do not enable UICR APPROTECT."
45 cortex_m reset_config sysresetreq
47 $_TARGETNAME configure -event examine-fail nrf52_check_ap_lock
50 flash bank $_CHIPNAME.flash nrf5 0x00000000 0 1 1 $_TARGETNAME
51 flash bank $_CHIPNAME.uicr nrf5 0x10001000 0 1 1 $_TARGETNAME
53 # Test if MEM-AP is locked by UICR APPROTECT
54 proc nrf52_check_ap_lock {} {
55 set dap [[target current] cget -dap]
56 set err [catch {set APPROTECTSTATUS [$dap apreg 1 0xc]}]
57 if {$err == 0 && $APPROTECTSTATUS != 1} {
58 echo "****** WARNING ******"
59 echo "nRF52 device has AP lock engaged (see UICR APPROTECT register)."
60 echo "Debug access is denied."
61 echo "Use 'nrf52_recover' to erase and unlock the device."
67 # Mass erase and unlock the device using proprietary nRF CTRL-AP (AP #1)
68 # http://www.ebyte.com produces modules with nRF52 locked by default,
69 # use nrf52_recover to enable flashing and debug.
70 proc nrf52_recover {} {
71 set target [target current]
72 set dap [$target cget -dap]
74 set IDR [$dap apreg 1 0xfc]
75 if {$IDR != 0x02880000} {
76 echo "Error: Cannot access nRF52 CTRL-AP!"
82 # Reset and trigger ERASEALL task
86 for {set i 0} {1} {incr i} {
87 set ERASEALLSTATUS [$dap apreg 1 8]
88 if {$ERASEALLSTATUS == 0} {
89 echo "$target device has been successfully erased and unlocked."
93 echo "Waiting for chip erase..."
96 echo "Error: $target recovery failed."
108 # Reset ERASEALL task
116 add_help_text nrf52_recover "Mass erase and unlock nRF52 device"