; --------------------------------------------------------------------------------
; @Title: Example script for flash declaration of Infineon XMC4100 internal flash.
;
; @Description: 
; Synopsis:
;
;   DO xmc4100 [PREPAREONLY] [CPU=<cpu>]
;
; Description:
;
;   Script arguments are not case sensitive.
;
;   PREPAREONLY only declares flash but does not execute flash programming
;
;   CPU=<cpu> selects CPU derivative <cpu>. <cpu> can be CPU name out of the
;             table listed below. For these derivatives the flash declaration
;             is done by the script.
;
; Example:
;
;   DO ~~/demo/arm/flash/xmc4100 CPU=XMC4100-X128 PREPAREONLY
;
; List of XMC4100 derivatives and their configuration:
;
;   CPU-Type              Flash
;   ----------------------------
;   XMC4100-X128          128kB
;   XMC4104-X64            64kB
;   XMC4104-X128          128kB
;   XMC4108-X64            64kB
;
; Memories:
;
;   00000000--00003FFF BROM (PMU ROM)
;   08000000--0801FFFF PMU/FLASH (cached)
;   0C000000--0C01FFFF PMU/FLASH (uncached)
;   1FFFE000--1FFFFFFF PSRAM (code)
;   20000000--20002FFF DSRAM1 (system)
;
; @Author: WRD
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; @Chip: xmc4100*
; --------------------------------------------------------------------------------
; $Rev: 7602 $
; $Id: xmc4100.cmm 7602 2019-11-05 15:02:25Z bschroefel $
;

  LOCAL &parameters
  ENTRY %LINE &parameters

  LOCAL &param_prepareonly
  &param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)

  LOCAL &param_cpu
  &param_cpu=STRing.SCANAndExtract(STRing.UPpeR("&parameters"),"CPU=","")

  ; --------------------------------------------------------------------------------
  ; Start debugging

  IF SYStem.MODE()<5
  (
    SYStem.RESet

    IF "&param_cpu"!=""
      SYStem.CPU &param_cpu
    IF !CPUIS(XMC410*)
      SYStem.CPU XMC410*

    IF CABLE.TWOWIRE()
      SYStem.CONFIG.DEBUGPORTTYPE SWD
    ELSE
      SYStem.CONFIG.DEBUGPORTTYPE JTAG

    SYStem.CONFIG.CONNECTOR MIPI20T
    SYStem.Option.ResBreak OFF
    SYStem.Up

    ; Initialize internal SRAM
    Data.Set 0x1FFFE000--0x1FFFFFFF %Long 0x0
    Data.Set 0x20000000--0x20002FFF %Long 0x0
  )

  ; --------------------------------------------------------------------------------
  ; Flash declaration

  FLASH.RESet
  GOSUB FlashDeclaration

  ; Flash script ends here if called with parameter PREPAREONLY
  IF &param_prepareonly
    ENDDO PREPAREDONE

  ; --------------------------------------------------------------------------------
  ; Flash programming example

  DIALOG.YESNO "Program flash memory?"
  LOCAL &progflash
  ENTRY &progflash
  IF &progflash 
  (
    FLASH.ReProgram.ALL /Erase
    Data.LOAD.auto *
    FLASH.ReProgram.off

    ; Reset device
    SYStem.Down
    SYStem.Up
  )

  ENDDO


; --------------------------------------------------------------------------------
; Flash declaration depending on selected CPU 

FlashDeclaration:
  LOCAL &FlashSize
  &FlashSize=0x0

  IF CPUIS(XMC410?-X64)
    &FlashSize=0x10000
  ELSE IF CPUIS(XMC410?-X128)
    &FlashSize=0x20000
  ELSE
  (
    IF VERSION.BUILD()<42853.
    (
      PRINT %ERROR "Trace32 software version is to old, please request an update"
      ENDDO
    )

    PRINT %ERROR "FLASH size of CPU type is not supported by the script"
    ENDDO
  )

  IF &FlashSize>=0x10000
    FLASH.Create 1. 0x0C000000--0x0C00FFFF  0x4000 TARGET Long
  IF &FlashSize>=0x20000
    FLASH.Create 2. 0x0C010000--0x0C01FFFF  0x4000 TARGET Long

  FLASH.CreateALIAS 0x08000000--0x0801FFFF 0x0C000000

  FLASH.TARGET 0x1FFFE000 0x20000000 0x1000 ~~/demo/arm/flash/long/xmc4000.bin

  RETURN
