tcl/target/nrf52.cfg: detect AP lock and add command to recover
[fw/openocd] / tcl / target / nrf52.cfg
1 #
2 # Nordic nRF52 series: ARM Cortex-M4 @ 64 MHz
3 #
4
5 source [find target/swj-dp.tcl]
6
7 if { [info exists CHIPNAME] } {
8         set _CHIPNAME $CHIPNAME
9 } else {
10         set _CHIPNAME nrf52
11 }
12
13 # Work-area is a space in RAM used for flash programming
14 # By default use 16kB
15 if { [info exists WORKAREASIZE] } {
16    set _WORKAREASIZE $WORKAREASIZE
17 } else {
18    set _WORKAREASIZE 0x4000
19 }
20
21 if { [info exists CPUTAPID] } {
22         set _CPUTAPID $CPUTAPID
23 } else {
24         set _CPUTAPID 0x2ba01477
25 }
26
27 swj_newdap $_CHIPNAME cpu -expected-id $_CPUTAPID
28 dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
29
30 set _TARGETNAME $_CHIPNAME.cpu
31 target create $_TARGETNAME cortex_m -dap $_CHIPNAME.dap
32
33 adapter speed 1000
34
35 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
36
37 if { [using_hla] } {
38         echo ""
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."
43         echo ""
44 } else {
45         cortex_m reset_config sysresetreq
46
47         $_TARGETNAME configure -event examine-fail nrf52_check_ap_lock
48 }
49
50 flash bank $_CHIPNAME.flash nrf5 0x00000000 0 1 1 $_TARGETNAME
51 flash bank $_CHIPNAME.uicr nrf5 0x10001000 0 1 1 $_TARGETNAME
52
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 [ocd_$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."
62                 echo ""
63                 poll off
64         }
65 }
66
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]
73
74         set IDR [ocd_$dap apreg 1 0xfc]
75         if {$IDR != 0x02880000} {
76                 echo "Error: Cannot access nRF52 CTRL-AP!"
77                 return
78         }
79
80         poll off
81
82         # Assert reset
83         $dap apreg 1 0 1
84
85         # Reset ERASEALLSTATUS event
86         $dap apreg 1 8 0
87
88         # Trigger ERASEALL task
89         $dap apreg 1 4 0
90         $dap apreg 1 4 1
91
92         for {set i 0} {1} {incr i} {
93                 set ERASEALLSTATUS [ocd_$dap apreg 1 8]
94                 if {$ERASEALLSTATUS == 1} {
95                         echo "$target device has been successfully erased and unlocked."
96                         break
97                 }
98                 if {$i >= 5} {
99                         echo "Error: $target recovery failed."
100                         break
101                 }
102                 sleep 100
103         }
104
105         # Deassert reset
106         $dap apreg 1 0 0
107
108         if {$ERASEALLSTATUS == 1} {
109                 sleep 100
110                 $target arp_examine
111                 poll on
112         }
113 }
114
115 add_help_text nrf52_recover "Mass erase and unlock nRF52 device"