psoc4hv_a0.cfg 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. #
  2. # Copyright (C) <2019-2021>
  3. # <Cypress Semiconductor Corporation (an Infineon company)>
  4. #
  5. # Configuration script for Cypress PSoC4-HV family of microcontrollers.
  6. #
  7. #
  8. # PSoC 4 devices support SWD transports only.
  9. #
  10. source [find target/swj-dp.tcl]
  11. source [find mem_helper.tcl]
  12. adapter speed 2000
  13. if { [info exists CHIPNAME] } {
  14. set _CHIPNAME $CHIPNAME
  15. } else {
  16. set _CHIPNAME psoc4hv
  17. }
  18. # Work-area is a space in RAM used for flash programming
  19. # By default use 2kB
  20. if { [info exists WORKAREASIZE] } {
  21. set _WORKAREASIZE $WORKAREASIZE
  22. } else {
  23. set _WORKAREASIZE 0x800
  24. }
  25. ###############################################################################
  26. # KitProg3 acquire/power control stuff
  27. ###############################################################################
  28. global ${_CHIPNAME}::use_acquire
  29. global ${_CHIPNAME}::enable_power_supply
  30. if { [adapter name] eq "kitprog3" } {
  31. if { [info exists PSOC4_USE_ACQUIRE] } {
  32. if { ( $PSOC4_USE_ACQUIRE != 0 ) && ( $PSOC4_USE_ACQUIRE != 1 ) && ( $PSOC4_USE_ACQUIRE != 2 ) } {
  33. puts stderr "** Invalid PSOC4_USE_ACQUIRE value ($PSOC4_USE_ACQUIRE). Allowed values are:"
  34. puts stderr "** 0 - Test Mode acquisition is disabled"
  35. puts stderr "** 1 - Enable acquisition using XRES method"
  36. puts stderr "** 2 - Enable acquisition using power-cycle method"
  37. terminate
  38. }
  39. if { $PSOC4_USE_ACQUIRE == 2 && ![info exists ENABLE_POWER_SUPPLY] } {
  40. set ENABLE_POWER_SUPPLY default
  41. }
  42. set ${_CHIPNAME}::use_acquire $PSOC4_USE_ACQUIRE
  43. } else {
  44. set ${_CHIPNAME}::use_acquire 1
  45. }
  46. if { [info exists ENABLE_POWER_SUPPLY] } {
  47. set ${_CHIPNAME}::enable_power_supply $ENABLE_POWER_SUPPLY
  48. } else {
  49. set ${_CHIPNAME}::enable_power_supply 0
  50. }
  51. } else {
  52. set ${_CHIPNAME}::use_acquire 0
  53. set ${_CHIPNAME}::enable_power_supply 0
  54. echo "** Test Mode acquire not supported by selected adapter"
  55. }
  56. if { [set ${_CHIPNAME}::use_acquire] } {
  57. echo "** Auto-acquire enabled, use \"set PSOC4_USE_ACQUIRE 0\" to disable"
  58. # set acquire mode: power cycle = 2, reset otherwise
  59. if { [set ${_CHIPNAME}::use_acquire] == 2 } {
  60. kitprog3 acquire_config on 0 1 3
  61. } else {
  62. kitprog3 acquire_config on 0 0 3
  63. }
  64. }
  65. if { [string is integer [set ${_CHIPNAME}::enable_power_supply]]} {
  66. if { [set ${_CHIPNAME}::enable_power_supply] } {
  67. echo "** Enabling target power ([set ${_CHIPNAME}::enable_power_supply] mV) \"set ENABLE_POWER_SUPPLY 0\" to disable"
  68. kitprog3 power_config on [set ${_CHIPNAME}::enable_power_supply]
  69. }
  70. } elseif { [set ${_CHIPNAME}::enable_power_supply]== "default" } {
  71. echo "** Enabling target power (default voltage) \"set ENABLE_POWER_SUPPLY 0\" to disable"
  72. kitprog3 power_config on
  73. } else {
  74. puts stderr "Invalid ENABLE_POWER_SUPPLY value - '[set ${_CHIPNAME}::enable_power_supply]' (integer or 'default' expected)"
  75. terminate
  76. }
  77. swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf
  78. dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
  79. # Order of examination is important
  80. target create $_CHIPNAME.mem_ap mem_ap -dap $_CHIPNAME.dap -ap-num 0 -coreid 255
  81. target create $_CHIPNAME.cpu cortex_m -dap $_CHIPNAME.dap -ap-num 0
  82. $_CHIPNAME.cpu cortex_m reset_config sysresetreq
  83. $_CHIPNAME.cpu configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
  84. $_CHIPNAME.cpu configure -event reset-deassert-post "psoc4_deassert_post $_CHIPNAME.cpu"
  85. proc check_protection {} {
  86. set target [target current]
  87. set dap [$target cget -dap]
  88. for { set i 0 } { $i < 3 } { incr i } {
  89. $dap apreg 0 0x04 0xE000ED00
  90. if { [catch {$dap apreg 0 0x0C}] == 0 } {
  91. return 0
  92. }
  93. }
  94. return 1
  95. }
  96. proc psoc_soft_acquire {} {
  97. echo "** Attempting to soft-acquire the chip in Test Mode..."
  98. local_echo off
  99. scan [debug_level] "debug_level: %d" old_level
  100. scan [adapter speed] "adapter speed: %d kHz" adapter_speed
  101. debug_level -1
  102. reset_config srst_only
  103. adapter speed 4000
  104. adapter assert srst
  105. sleep 25
  106. adapter deassert srst
  107. catch { mww 0x40030014 0x80000000 }
  108. catch { mww 0x40030014 0x80000000 }
  109. catch { mww 0x40030014 0x80000000 }
  110. catch { mww 0x40030014 0x80000000 }
  111. catch { mww 0x40030014 0x80000000 }
  112. catch { mww 0x40030014 0x80000000 }
  113. catch { mww 0x40030014 0x80000000 }
  114. catch { mww 0x40030014 0x80000000 }
  115. catch { mww 0x40030014 0x80000000 }
  116. if { [catch { mww 0x40030014 0x80000000 }] != 0 } {
  117. puts stderr "** Unable to write to SRSS.TST_MODE register"
  118. }
  119. reset_config none
  120. adapter speed $adapter_speed
  121. debug_level $old_level
  122. local_echo on
  123. }
  124. $_CHIPNAME.mem_ap configure -event examine-end {
  125. global _CHIPNAME
  126. global ${_CHIPNAME}::is_locked
  127. if { [info exist ${_CHIPNAME}::is_locked] } return
  128. local_echo off
  129. set ${_CHIPNAME}::is_locked [check_protection]
  130. local_echo on
  131. if { [set ${_CHIPNAME}::is_locked] } {
  132. puts stderr "Warn: Chip's resources access is locked down."
  133. puts stderr " The state can be set back to OPEN but only after completely erasing the flash."
  134. puts stderr " To do so, perform mass erase operation using 'psoc4 mass_erase' command."
  135. if { [set ${_CHIPNAME}::use_acquire] == 0 } { psoc_soft_acquire }
  136. ${_CHIPNAME}.cpu configure -defer-examine
  137. ${_CHIPNAME}.cpu configure -event reset-assert {}
  138. targets ${_CHIPNAME}.mem_ap
  139. catch { psoc4 silicon_info }
  140. } else {
  141. targets $_CHIPNAME.cpu
  142. $_CHIPNAME.cpu arp_examine
  143. catch { psoc4 silicon_info }
  144. }
  145. }
  146. flash bank $_CHIPNAME.mflash psoc4hv 0x00000000 0 4 4 $_CHIPNAME.cpu 0
  147. flash bank $_CHIPNAME.wflash psoc4hv 0x1F000000 0 4 4 $_CHIPNAME.cpu 1
  148. flash bank $_CHIPNAME.sflash0 psoc4hv 0x0FFFF000 0 4 4 $_CHIPNAME.cpu 0
  149. flash bank $_CHIPNAME.sflash1 psoc4hv 0x0FFFF000 0 4 4 $_CHIPNAME.cpu 1
  150. proc init_reset { mode } {
  151. global RESET_MODE
  152. set RESET_MODE $mode
  153. }
  154. proc acquire { target } {
  155. global _CHIPNAME
  156. global ${_CHIPNAME}::is_locked
  157. global ${_CHIPNAME}::use_acquire
  158. if { ![set ${_CHIPNAME}::use_acquire] } {
  159. echo "----------------------------------------------------------------"
  160. echo "Test Mode acquire disabled. Use 'set PSOC4_USE_ACQUIRE 1' to enable"
  161. echo "----------------------------------------------------------------"
  162. error
  163. }
  164. # acquire will leave CPU in running state
  165. # openocd does not expect this
  166. # ignore possible error e.g. when listen window is disabled
  167. catch {[adapter name] acquire_psoc}
  168. # In the PROTECTED mode access is locked for everything except System Calls
  169. if { [set ${_CHIPNAME}::is_locked] } {
  170. echo "** Device acquired successfully"
  171. return
  172. }
  173. # we need to re-examine and halt target manually
  174. ${target} arp_examine
  175. ${target} arp_poll
  176. ${target} arp_poll
  177. # Ensure target has stopped on WFI instruction
  178. set loops 200
  179. while { $loops } {
  180. set sleeping [ expr {[mrw 0xE000EDF0] & 0x00040000} ]
  181. if { $sleeping } break
  182. set loops [ expr {$loops - 1} ]
  183. sleep 10
  184. }
  185. if { $sleeping } {
  186. ${target} arp_halt
  187. ${target} arp_waitstate halted 100
  188. echo "** Device acquired successfully"
  189. return
  190. }
  191. echo "--------------------------------------------"
  192. echo "Failed to acquire PSoC 4 device in Test Mode"
  193. echo "--------------------------------------------"
  194. error
  195. }
  196. # Utility to make 'reset halt' work as reset;halt on a target
  197. # It does not prevent running code after reset
  198. proc psoc4_deassert_post { target } {
  199. global _CHIPNAME
  200. global ${_CHIPNAME}::is_locked
  201. global ${_CHIPNAME}::use_acquire
  202. global RESET_MODE
  203. if { $RESET_MODE ne "run" } {
  204. if { [set ${_CHIPNAME}::use_acquire] } {
  205. acquire $target
  206. }
  207. if { [set ${_CHIPNAME}::is_locked] } return
  208. if { ![set ${_CHIPNAME}::use_acquire] } {
  209. psoc_soft_acquire
  210. }
  211. # PSoC 4 cleares AP registers including TAR during reset
  212. # Force examine to synchronize OpenOCD target status
  213. $target arp_examine
  214. catch {$target arp_poll}
  215. catch {$target arp_poll}
  216. set st [$target curstate]
  217. if { $st eq "reset" } {
  218. # we assume running state follows
  219. # if reset accidentally halts, waiting is useless
  220. catch { $target arp_waitstate running 100 }
  221. set st [$target curstate]
  222. }
  223. if { $st eq "running" } {
  224. echo "** $target: Ran after reset and before halt..."
  225. if { ![set ${_CHIPNAME}::use_acquire] } {
  226. catch { psoc4 reset_halt }
  227. } else {
  228. $target arp_halt
  229. }
  230. $target arp_waitstate halted 100
  231. }
  232. }
  233. }