1 # script for Cypress PSoC 4 devices
4 # PSoC 4 devices support SWD transports only.
6 source [find target/swj-dp.tcl]
8 if { [info exists CHIPNAME] } {
9 set _CHIPNAME $CHIPNAME
14 # Work-area is a space in RAM used for flash programming
16 if { [info exists WORKAREASIZE] } {
17 set _WORKAREASIZE $WORKAREASIZE
19 set _WORKAREASIZE 0x1000
22 if { [info exists CPUTAPID] } {
23 set _CPUTAPID $CPUTAPID
25 set _CPUTAPID 0x0bb11477
28 swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf -expected-id $_CPUTAPID
29 dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
31 set _TARGETNAME $_CHIPNAME.cpu
32 target create $_TARGETNAME cortex_m -dap $_CHIPNAME.dap
34 $_TARGETNAME configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
36 set _FLASHNAME $_CHIPNAME.flash
37 flash bank $_FLASHNAME psoc4 0 0 0 0 $_TARGETNAME
41 # Reset, bloody PSoC 4 reset
43 # 1) XRES (nSRST) resets also SWD DP so SWD line reset and DP reinit is needed.
44 # High level adapter stops working after SRST and needs OpenOCD restart.
45 # If your hw does not use SRST for other circuits, use sysresetreq instead
47 # 2) PSoC 4 executes initialization code from system ROM after reset.
48 # This code subsequently jumps to user flash reset vector address.
49 # Unfortunately the system ROM code is protected from reading and debugging.
50 # Protection breaks vector catch VC_CORERESET used for "reset halt" by cortex_m.
52 # Cypress uses TEST_MODE flag to loop CPU in system ROM before executing code
53 # from user flash. Programming specifications states that TEST_MODE flag must be
54 # set in time frame 400 usec delayed about 1 msec from reset.
56 # OpenOCD have no standard way how to set TEST_MODE in specified time frame.
57 # As a workaround the TEST_MODE flag is set before reset instead.
58 # It worked for the oldest family PSoC4100/4200 even though it is not guaranteed
61 # Newer families like PSoC 4000, 4100M, 4200M, 4100L, 4200L and PSoC 4 BLE
62 # clear TEST_MODE flag during device reset so workaround is not possible.
63 # Use a KitProg adapter for theese devices or "reset halt" will not stop
64 # before executing user code.
66 # 3) SWD cannot be connected during system initialization after reset.
67 # This might be a reason for unconnecting ST-Link v2 when deasserting reset.
68 # As a workaround arp_reset deassert is not called for hla
71 # if srst is not fitted use SYSRESETREQ to
72 # perform a soft reset
73 cortex_m reset_config sysresetreq
76 proc psoc4_get_family_id {} {
77 set err [catch "mem2array romtable_pid 32 0xF0000FE0 3"]
81 if { [expr $romtable_pid(0) & 0xffffff00 ]
82 || [expr $romtable_pid(1) & 0xffffff00 ]
83 || [expr $romtable_pid(2) & 0xffffff00 ] } {
84 echo "Unexpected data in ROMTABLE"
87 set designer_id [expr (( $romtable_pid(1) & 0xf0 ) >> 4) | (( $romtable_pid(2) & 0xf ) << 4 ) ]
88 if { $designer_id != 0xb4 } {
89 echo [format "ROMTABLE Designer ID 0x%02x is not Cypress" $designer_id]
92 set family_id [expr ( $romtable_pid(0) & 0xff ) | (( $romtable_pid(1) & 0xf ) << 8 ) ]
96 proc ocd_process_reset_inner { MODE } {
97 global PSOC4_USE_ACQUIRE PSOC4_TEST_MODE_WORKAROUND
100 if { 0 != [string compare $_TARGETNAME [target names]] } {
101 return -code error "PSoC 4 reset can handle only one $_TARGETNAME target";
105 # If this target must be halted...
107 if { 0 == [string compare $MODE halt] } {
110 if { 0 == [string compare $MODE init] } {
113 if { 0 == [string compare $MODE run ] } {
117 return -code error "Invalid mode: $MODE, must be one of: halt, init, or run";
120 if { ! [info exists PSOC4_USE_ACQUIRE] } {
121 if { 0 == [string compare [adapter name] kitprog ] } {
122 set PSOC4_USE_ACQUIRE 1
124 set PSOC4_USE_ACQUIRE 0
127 if { $PSOC4_USE_ACQUIRE } {
128 set PSOC4_TEST_MODE_WORKAROUND 0
129 } elseif { ! [info exists PSOC4_TEST_MODE_WORKAROUND] } {
130 if { [psoc4_get_family_id] == 0x93 } {
131 set PSOC4_TEST_MODE_WORKAROUND 1
133 set PSOC4_TEST_MODE_WORKAROUND 0
137 #$t invoke-event reset-start
138 $t invoke-event reset-assert-pre
140 if { $halt && $PSOC4_USE_ACQUIRE } {
141 catch { [adapter name] acquire_psoc }
144 if { $PSOC4_TEST_MODE_WORKAROUND } {
145 set TEST_MODE 0x40030014
147 catch { mww $TEST_MODE 0x80000000 }
149 catch { mww $TEST_MODE 0 }
153 $t arp_reset assert 0
156 $t invoke-event reset-assert-post
157 $t invoke-event reset-deassert-pre
158 if {![using_hla]} { # workaround ST-Link v2 fails and forcing reconnect
159 $t arp_reset deassert 0
161 $t invoke-event reset-deassert-post
163 # Pass 1 - Now wait for any halt (requested as part of reset
164 # assert/deassert) to happen. Ideally it takes effect without
165 # first executing any instructions.
167 # Now PSoC CPU should loop in system ROM
168 $t arp_waitstate running 200
171 # Catch, but ignore any errors.
172 catch { $t arp_waitstate halted 1000 }
177 if { 0 != [string compare $s "halted" ] } {
178 return -code error [format "TARGET: %s - Not halted" $t]
181 # Check if PSoC CPU is stopped in system ROM
183 regsub {pc[^:]*: } $pc "" pc
184 if { $pc < 0x10000000 || $pc > 0x1000ffff } {
186 set family_id [psoc4_get_family_id]
187 if { $family_id == 0x93 } {
188 set hint ", use 'reset_config none'"
189 } elseif { $family_id > 0x93 } {
190 set hint ", use a KitProg adapter"
192 return -code error [format "TARGET: %s - Not halted in system ROM%s" $t $hint]
195 # Set registers to reset vector values
196 mem2array value 32 0 2
197 reg pc [expr $value(1) & 0xfffffffe ]
200 if { $PSOC4_TEST_MODE_WORKAROUND } {
201 catch { mww $TEST_MODE 0 }
205 #Pass 2 - if needed "init"
206 if { 0 == [string compare init $MODE] } {
207 set err [catch "$t arp_waitstate halted 5000"]
211 $t invoke-event reset-init
215 $t invoke-event reset-end