| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- #
- # Copyright (C) <2019-2021>
- # <Cypress Semiconductor Corporation (an Infineon company)>
- #
- # Configuration script for Cypress PSoC4-HV family of microcontrollers.
- #
- #
- # PSoC 4 devices support SWD transports only.
- #
- source [find target/swj-dp.tcl]
- source [find mem_helper.tcl]
- adapter speed 2000
- if { [info exists CHIPNAME] } {
- set _CHIPNAME $CHIPNAME
- } else {
- set _CHIPNAME psoc4hv
- }
- # Work-area is a space in RAM used for flash programming
- # By default use 2kB
- if { [info exists WORKAREASIZE] } {
- set _WORKAREASIZE $WORKAREASIZE
- } else {
- set _WORKAREASIZE 0x800
- }
- ###############################################################################
- # KitProg3 acquire/power control stuff
- ###############################################################################
- global ${_CHIPNAME}::use_acquire
- global ${_CHIPNAME}::enable_power_supply
- if { [adapter name] eq "kitprog3" } {
- if { [info exists PSOC4_USE_ACQUIRE] } {
- if { ( $PSOC4_USE_ACQUIRE != 0 ) && ( $PSOC4_USE_ACQUIRE != 1 ) && ( $PSOC4_USE_ACQUIRE != 2 ) } {
- puts stderr "** Invalid PSOC4_USE_ACQUIRE value ($PSOC4_USE_ACQUIRE). Allowed values are:"
- puts stderr "** 0 - Test Mode acquisition is disabled"
- puts stderr "** 1 - Enable acquisition using XRES method"
- puts stderr "** 2 - Enable acquisition using power-cycle method"
- terminate
- }
- if { $PSOC4_USE_ACQUIRE == 2 && ![info exists ENABLE_POWER_SUPPLY] } {
- set ENABLE_POWER_SUPPLY default
- }
- set ${_CHIPNAME}::use_acquire $PSOC4_USE_ACQUIRE
- } else {
- set ${_CHIPNAME}::use_acquire 1
- }
- if { [info exists ENABLE_POWER_SUPPLY] } {
- set ${_CHIPNAME}::enable_power_supply $ENABLE_POWER_SUPPLY
- } else {
- set ${_CHIPNAME}::enable_power_supply 0
- }
- } else {
- set ${_CHIPNAME}::use_acquire 0
- set ${_CHIPNAME}::enable_power_supply 0
- echo "** Test Mode acquire not supported by selected adapter"
- }
- if { [set ${_CHIPNAME}::use_acquire] } {
- echo "** Auto-acquire enabled, use \"set PSOC4_USE_ACQUIRE 0\" to disable"
- # set acquire mode: power cycle = 2, reset otherwise
- if { [set ${_CHIPNAME}::use_acquire] == 2 } {
- kitprog3 acquire_config on 0 1 3
- } else {
- kitprog3 acquire_config on 0 0 3
- }
- }
- if { [string is integer [set ${_CHIPNAME}::enable_power_supply]]} {
- if { [set ${_CHIPNAME}::enable_power_supply] } {
- echo "** Enabling target power ([set ${_CHIPNAME}::enable_power_supply] mV) \"set ENABLE_POWER_SUPPLY 0\" to disable"
- kitprog3 power_config on [set ${_CHIPNAME}::enable_power_supply]
- }
- } elseif { [set ${_CHIPNAME}::enable_power_supply]== "default" } {
- echo "** Enabling target power (default voltage) \"set ENABLE_POWER_SUPPLY 0\" to disable"
- kitprog3 power_config on
- } else {
- puts stderr "Invalid ENABLE_POWER_SUPPLY value - '[set ${_CHIPNAME}::enable_power_supply]' (integer or 'default' expected)"
- terminate
- }
- swj_newdap $_CHIPNAME cpu -irlen 4 -ircapture 0x1 -irmask 0xf
- dap create $_CHIPNAME.dap -chain-position $_CHIPNAME.cpu
- # Order of examination is important
- target create $_CHIPNAME.mem_ap mem_ap -dap $_CHIPNAME.dap -ap-num 0 -coreid 255
- target create $_CHIPNAME.cpu cortex_m -dap $_CHIPNAME.dap -ap-num 0
- $_CHIPNAME.cpu cortex_m reset_config sysresetreq
- $_CHIPNAME.cpu configure -work-area-phys 0x20000000 -work-area-size $_WORKAREASIZE -work-area-backup 0
- $_CHIPNAME.cpu configure -event reset-deassert-post "psoc4_deassert_post $_CHIPNAME.cpu"
- proc check_protection {} {
- set target [target current]
- set dap [$target cget -dap]
- for { set i 0 } { $i < 3 } { incr i } {
- $dap apreg 0 0x04 0xE000ED00
- if { [catch {$dap apreg 0 0x0C}] == 0 } {
- return 0
- }
- }
- return 1
- }
- proc psoc_soft_acquire {} {
- echo "** Attempting to soft-acquire the chip in Test Mode..."
- local_echo off
- scan [debug_level] "debug_level: %d" old_level
- scan [adapter speed] "adapter speed: %d kHz" adapter_speed
- debug_level -1
- reset_config srst_only
- adapter speed 4000
- adapter assert srst
- sleep 25
- adapter deassert srst
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- catch { mww 0x40030014 0x80000000 }
- if { [catch { mww 0x40030014 0x80000000 }] != 0 } {
- puts stderr "** Unable to write to SRSS.TST_MODE register"
- }
- reset_config none
- adapter speed $adapter_speed
- debug_level $old_level
- local_echo on
- }
- $_CHIPNAME.mem_ap configure -event examine-end {
- global _CHIPNAME
- global ${_CHIPNAME}::is_locked
- if { [info exist ${_CHIPNAME}::is_locked] } return
- local_echo off
- set ${_CHIPNAME}::is_locked [check_protection]
- local_echo on
- if { [set ${_CHIPNAME}::is_locked] } {
- puts stderr "Warn: Chip's resources access is locked down."
- puts stderr " The state can be set back to OPEN but only after completely erasing the flash."
- puts stderr " To do so, perform mass erase operation using 'psoc4 mass_erase' command."
- if { [set ${_CHIPNAME}::use_acquire] == 0 } { psoc_soft_acquire }
- ${_CHIPNAME}.cpu configure -defer-examine
- ${_CHIPNAME}.cpu configure -event reset-assert {}
- targets ${_CHIPNAME}.mem_ap
- catch { psoc4 silicon_info }
- } else {
- targets $_CHIPNAME.cpu
- $_CHIPNAME.cpu arp_examine
- catch { psoc4 silicon_info }
- }
- }
- flash bank $_CHIPNAME.mflash psoc4hv 0x00000000 0 4 4 $_CHIPNAME.cpu 0
- flash bank $_CHIPNAME.wflash psoc4hv 0x1F000000 0 4 4 $_CHIPNAME.cpu 1
- flash bank $_CHIPNAME.sflash0 psoc4hv 0x0FFFF000 0 4 4 $_CHIPNAME.cpu 0
- flash bank $_CHIPNAME.sflash1 psoc4hv 0x0FFFF000 0 4 4 $_CHIPNAME.cpu 1
- proc init_reset { mode } {
- global RESET_MODE
- set RESET_MODE $mode
- }
- proc acquire { target } {
- global _CHIPNAME
- global ${_CHIPNAME}::is_locked
- global ${_CHIPNAME}::use_acquire
- if { ![set ${_CHIPNAME}::use_acquire] } {
- echo "----------------------------------------------------------------"
- echo "Test Mode acquire disabled. Use 'set PSOC4_USE_ACQUIRE 1' to enable"
- echo "----------------------------------------------------------------"
- error
- }
- # acquire will leave CPU in running state
- # openocd does not expect this
- # ignore possible error e.g. when listen window is disabled
- catch {[adapter name] acquire_psoc}
- # In the PROTECTED mode access is locked for everything except System Calls
- if { [set ${_CHIPNAME}::is_locked] } {
- echo "** Device acquired successfully"
- return
- }
- # we need to re-examine and halt target manually
- ${target} arp_examine
- ${target} arp_poll
- ${target} arp_poll
- # Ensure target has stopped on WFI instruction
- set loops 200
- while { $loops } {
- set sleeping [ expr {[mrw 0xE000EDF0] & 0x00040000} ]
- if { $sleeping } break
- set loops [ expr {$loops - 1} ]
- sleep 10
- }
- if { $sleeping } {
- ${target} arp_halt
- ${target} arp_waitstate halted 100
- echo "** Device acquired successfully"
- return
- }
- echo "--------------------------------------------"
- echo "Failed to acquire PSoC 4 device in Test Mode"
- echo "--------------------------------------------"
- error
- }
- # Utility to make 'reset halt' work as reset;halt on a target
- # It does not prevent running code after reset
- proc psoc4_deassert_post { target } {
- global _CHIPNAME
- global ${_CHIPNAME}::is_locked
- global ${_CHIPNAME}::use_acquire
- global RESET_MODE
- if { $RESET_MODE ne "run" } {
- if { [set ${_CHIPNAME}::use_acquire] } {
- acquire $target
- }
- if { [set ${_CHIPNAME}::is_locked] } return
- if { ![set ${_CHIPNAME}::use_acquire] } {
- psoc_soft_acquire
- }
- # PSoC 4 cleares AP registers including TAR during reset
- # Force examine to synchronize OpenOCD target status
- $target arp_examine
- catch {$target arp_poll}
- catch {$target arp_poll}
- set st [$target curstate]
- if { $st eq "reset" } {
- # we assume running state follows
- # if reset accidentally halts, waiting is useless
- catch { $target arp_waitstate running 100 }
- set st [$target curstate]
- }
- if { $st eq "running" } {
- echo "** $target: Ran after reset and before halt..."
- if { ![set ${_CHIPNAME}::use_acquire] } {
- catch { psoc4 reset_halt }
- } else {
- $target arp_halt
- }
- $target arp_waitstate halted 100
- }
- }
- }
|