Files
2025-10-14 09:52:32 +09:00

227 lines
6.9 KiB
Plaintext

; --------------------------------------------------------------------------------
; @Title: Flash declaration for NXP MKL8?Z128VLK7 internal flash
; @Description:
; Script arguments:
; DO mkl80 [PREPAREONLY] [CPU=<cpu>] [DUALPORT=<0|1>] [MASSERASE]
; PREPAREONLY only declares flash but does not execute flash programming
; CPU=<cpu> selects CPU derivative <cpu>
; DUALPORT=<0|1> use dual port memory access, default 1
; MASSERASE forces mass erase of device before establishing debug
; connection
; Example:
; DO ~~/demo/arm/flash/mkl80 PREPAREONLY
; Note:
; This file must NOT be modified.
; This file is intended to stay within TRACE32 installation.
; Usage examples are available in the ~~/demo/arm/hardware/... subdirectories.
;
; List of MKL8?Z128VLK7 derivatives and their configuration:
; CPU-Type ProgFlash RamSize
; [Byte] [Byte]
; --------------------------------------------------------------------------------
; MKL81Z128CBH7R 128kB 96kB
; MKL81Z128VLK7 128kB 96kB
; MKL81Z128VMC7 128kB 96kB
; MKL81Z128VMC7R 128kB 96kB
; --------------------------------------------------------------------------------
; MKL82Z128VLK7 128kB 96kB
; MKL82Z128VLK7R 128kB 96kB
; MKL82Z128VMC7 128kB 96kB
;
; The range 0x400--0x40f is guarded against modification.
; Address-Range Width Description
; --------------------------------------------------------------------------------
; 0x0400--0x0407 8 Backdoor Comparison Key.
; 0x0408--0x040B 4 Program flash protection bytes (FPROT0-3).
; 0x040F 1 Program flash only devices: Reserved
; FlexNVM devices: Data flash protection byte
; (FDPROT).
; 0x040E 1 Program flash only devices: Reserved
; FlexNVM devices: EEPROM protection byte
; (FEPROT).
; 0x040D 1 Flash nonvolatile option byte (FOPT).
; 0x040C 1 Flash security byte (FSEC).
;
; Modifications can be done using the FLASH.AUTO command with /CENSORSHIP option.
;
; @Chip: MKL81* MKL82*
; @Author: PHI
; @Copyright: (C) 1989-2022 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 10516 $
; $Id: mkl80.cmm 10516 2022-02-02 11:39:30Z bschroefel $
;
PRIVATE &parameters
ENTRY %LINE &parameters
PRIVATE &param_prepareonly &param_cpu &param_dualport &param_masserase
&parameters=STRing.UPpeR("&parameters")
&param_prepareonly=(STRing.SCAN("&parameters","PREPAREONLY",0)!=-1)
&param_cpu=STRing.SCANAndExtract("&parameters","CPU=","")
&param_dualport=STRing.SCANAndExtract("&parameters","DUALPORT=","1")
&param_masserase=(STRing.SCAN("&parameters","MASSERASE",0)!=-1)
; --------------------------------------------------------------------------------
; Initialize and start the debugger
IF !SYStem.Up()||&param_masserase
(
SYStem.RESet
IF "&param_cpu"!=""
SYStem.CPU &param_cpu
IF !CPUIS(MKL8*)
SYStem.CPU MKL8*
SYStem.MemAccess DAP
SYStem.CONFIG DEBUGPORTTYPE SWD
IF &param_masserase
GOSUB Unsecure
SYStem.Up
GOSUB DisableWatchdog
)
; --------------------------------------------------------------------------------
; Flash declaration
FLASH.RESet
GOSUB FlashDeclaration "&param_dualport"
; Flash script ends here if called with parameter PREPAREONLY
IF &param_prepareonly
ENDDO PREPAREDONE
; --------------------------------------------------------------------------------
; Flash programming example
DIALOG.YESNO "Program flash memory?"
PRIVATE &progflash
ENTRY &progflash
IF &progflash
(
FLASH.ReProgram.ALL /Erase
Data.LOAD.auto *
FLASH.ReProgram.off
; Example to modify Censorship range
; FLASH.AUTO 0x400--0x40f /CENSORSHIP
; Data.Set 0x400--0x40f %Byte <value>
; Data.LOAD.auto <file> 0x400--0x40f
; FLASH.AUTO OFF
; Reset device
SYStem.Down
SYStem.Up
; Disable watchdog
GOSUB DisableWatchdog
)
ENDDO
; --------------------------------------------------------------------------------
; SUBROUTINES
; --------------------------------------------------------------------------------
; Flash declaration depending on selected CPU
;
; Please do NOT modify the TRACE32 flash declaration.
;
; Modifications can result in unpredictable behavior.
; Please contact support@lauterbach.com for any changes.
FlashDeclaration: ;(param_dualport)
(
PARAMETERS &param_dualport
PRIVATE &PFlashSize
; Setup configuration values
IF CPUIS("MKL8?Z128V*")
&PFlashSize=0x20000
ELSE
(
PRINT %ERROR "FLASH size of CPU type is not supported by the script"
ENDDO
)
; Program Flash
IF &PFlashSize>=0x20000
FLASH.Create 1. 0x00000000--0x0001FFFF 0x800 TARGET Long /CENSORSHIP 0x400--0x40F
IF (("&param_dualport"!="1")||SYStem.ACCESS.DENIED())
FLASH.TARGET 0x1FFFF000 0x20000000 0x800 ~~/demo/arm/flash/long/ftfa.bin
ELSE
FLASH.TARGET 0x1FFFF000 E:0x20000000 0x800 ~~/demo/arm/flash/long/ftfa.bin /DualPort
RETURN
)
; --------------------------------------------------------------------------------
; Disable watchdog
DisableWatchdog:
(
; disable the Watchdog
PRIVATE &tmp1 &tmp2
&tmp1=Data.Long(ST:0x20000000)
&tmp2=Data.Long(ST:0x20000004)
Register.SWAP
; The watchdog has a restrictive timing. It has to be configured and unlocked within a peripod
; of 20+256 cycles. Therefor the unlock sequence need to be done by a small target program.
Data.Assemble ST:0x20000000 strh r1,[r0] ;SD:0x4005200E = 0xC520 (Key 1)
Data.Assemble , strh r2,[r0] ;SD:0x4005200E = 0xD928 (Key 2)
Data.Assemble , strh r4,[r3] ;SD:0x40052000 = 0x0000 (Config register)
Data.Assemble , bkpt #0
Register.Set PC 0x20000000
Register.Set R0 0x4005200E
Register.Set R1 0xC520
Register.Set R2 0xD928
Register.Set R3 0x40052000
Register.Set R4 0x0
Go.direct
WAIT !STATE.RUN()
Register.SWAP
Data.Set ST:0x20000000 %Long &tmp1
Data.Set ST:0x20000004 %Long &tmp2
RETURN
)
; --------------------------------------------------------------------------------
; Try to unsecure a secured device
Unsecure:
(
; Print security state information
SYStem.Mode.Prepare
PRINT "MDM-AP Status Register is 0x" Data.Long(EDBG:0x40000100)
DIALOG.YESNO "Execute mass erase to unsecure chip?"
PRIVATE &masserase
ENTRY &masserase
IF &masserase
(
ON.ERROR DEFault
; Unsecure device
SYStem.Down
FLASH.UNSECUREerase
SYStem.Mode.Prepare
IF (Data.Long(EDBG:0x40000100)&0x04)==0x04
(
PRINT %ERROR "Unsecure failed, MDM-AP Status Register is 0x" Data.Long(EDBG:0x40000100)
SYStem.Down
ENDDO
)
)
ELSE
ENDDO
SYStem.Up
; Disable watchdog
GOSUB DisableWatchdog
RETURN
)