tcl/board: Add J721s2 EVM basic support
[fw/openocd] / tcl / target / nrf52.cfg
index c29adbdd632c18fef594db04c5891b6e04a3db19..d0c52fdabce7959426fe1f0515123f55eacf5a71 100644 (file)
@@ -30,13 +30,87 @@ dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
 set _TARGETNAME $_CHIPNAME.cpu
 target create $_TARGETNAME cortex_m -dap $_CHIPNAME.dap
 
-adapter_khz 1000
+adapter speed 1000
 
 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
 
-if { ![using_hla] } {
+if { [using_hla] } {
+       echo ""
+       echo "nRF52 device has a CTRL-AP dedicated to recover the device from AP lock."
+       echo "A high level adapter (like a ST-Link) you are currently using cannot access"
+       echo "the CTRL-AP so 'nrf52_recover' command will not work."
+       echo "Do not enable UICR APPROTECT."
+       echo ""
+} else {
        cortex_m reset_config sysresetreq
+
+       $_TARGETNAME configure -event examine-fail nrf52_check_ap_lock
 }
 
 flash bank $_CHIPNAME.flash nrf5 0x00000000 0 1 1 $_TARGETNAME
 flash bank $_CHIPNAME.uicr nrf5 0x10001000 0 1 1 $_TARGETNAME
+
+# Test if MEM-AP is locked by UICR APPROTECT
+proc nrf52_check_ap_lock {} {
+       set dap [[target current] cget -dap]
+       set err [catch {set APPROTECTSTATUS [$dap apreg 1 0xc]}]
+       if {$err == 0 && $APPROTECTSTATUS != 1} {
+               echo "****** WARNING ******"
+               echo "nRF52 device has AP lock engaged (see UICR APPROTECT register)."
+               echo "Debug access is denied."
+               echo "Use 'nrf52_recover' to erase and unlock the device."
+               echo ""
+               poll off
+       }
+}
+
+# Mass erase and unlock the device using proprietary nRF CTRL-AP (AP #1)
+# http://www.ebyte.com produces modules with nRF52 locked by default,
+# use nrf52_recover to enable flashing and debug.
+proc nrf52_recover {} {
+       set target [target current]
+       set dap [$target cget -dap]
+
+       set IDR [$dap apreg 1 0xfc]
+       if {$IDR != 0x02880000} {
+               echo "Error: Cannot access nRF52 CTRL-AP!"
+               return
+       }
+
+       poll off
+
+       # Reset and trigger ERASEALL task
+       $dap apreg 1 4 0
+       $dap apreg 1 4 1
+
+       for {set i 0} {1} {incr i} {
+               set ERASEALLSTATUS [$dap apreg 1 8]
+               if {$ERASEALLSTATUS == 0} {
+                       echo "$target device has been successfully erased and unlocked."
+                       break
+               }
+               if {$i == 0} {
+                       echo "Waiting for chip erase..."
+               }
+               if {$i >= 150} {
+                       echo "Error: $target recovery failed."
+                       break
+               }
+               sleep 100
+       }
+
+       # Assert reset
+       $dap apreg 1 0 1
+
+       # Deassert reset
+       $dap apreg 1 0 0
+
+       # Reset ERASEALL task
+       $dap apreg 1 4 0
+
+       sleep 100
+       $target arp_examine
+       poll on
+}
+
+add_help_text nrf52_recover "Mass erase and unlock nRF52 device"