d0c52fdabce7959426fe1f0515123f55eacf5a71
[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 [$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 [$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         # Reset and trigger ERASEALL task
83         $dap apreg 1 4 0
84         $dap apreg 1 4 1
85
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."
90                         break
91                 }
92                 if {$i == 0} {
93                         echo "Waiting for chip erase..."
94                 }
95                 if {$i >= 150} {
96                         echo "Error: $target recovery failed."
97                         break
98                 }
99                 sleep 100
100         }
101
102         # Assert reset
103         $dap apreg 1 0 1
104
105         # Deassert reset
106         $dap apreg 1 0 0
107
108         # Reset ERASEALL task
109         $dap apreg 1 4 0
110
111         sleep 100
112         $target arp_examine
113         poll on
114 }
115
116 add_help_text nrf52_recover "Mass erase and unlock nRF52 device"