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