; --------------------------------------------------------------------------------
; @Title: Example for flash declaration of NXP LPC21xx internal flash.
;
; @Description: 
; Script arguments:
;
;   DO lpc21xx [PREPAREONLY]
;
;     PREPAREONLY only declares flash but does not execute flash programming
;
; Example:
;
;   DO ~~/demo/arm/flash/lpc21xx PREPAREONLY
;
; List of LPC21xx derivatives and their configuration:
;
;   CPU-Type  FlashSize  FlashType  UserFlash  RamSize
;                (kB)                 (kB)      (kB)
; --------------------------------------------------------------------------------
;   LPC2101        8.       3.          8.        2.
;   LPC2102       16.       3.         16.        4.
;   LPC2103       32.       3.         32.        8.
;   LPC2104      128.       1.        120.       16.
;   LPC2105      128.       1.        120.       32.
;   LPC2106      128.       1.        120.       64.
;   LPC2109       64.       1.         60.        8.
;   LPC2114      128.       1.        120.       16.
;   LPC2119      128.       1.        120.       16.
;   LPC2124      128.       1.        120.       16.
;   LPC2129      256.       1.        248.       16.
;   LPC2131       32.       2.         32.        8. 
;   LPC2132       64.       2.         64.       16.      
;   LPC2134      128.       2.        128.       16.
;   LPC2136      256.       2.        256.       32.
;   LPC2138      512.       2.        500.       32.      
;   LPC2141       32.       2.         32.        8.
;   LPC2142       64.       2.         64.       16.     
;   LPC2144      128.       2.        128.       16.
;   LPC2146      256.       2.        256.       32.
;   LPC2148      512.       2.        500.       32.     
;   LPC2157      512.       2.        500.       32.     
;   LPC2158      512.       2.        500.       32.     
;   LPC2194      256.       1.        248.       16.
;
; Supported flash types:
;
;   FlashType  SmallBlock  MainBlock  BootFlash  Alignment 
; --------------------------------------------------------------------------------
;       0.        no processor internal flash
;       1.        8 kB       64 kB      8 kB      512 B    
;       2.        4 kB       32 kB     12 kB      256 B    
;       3.        4 kB       32 kB      8 kB      256 B    
;
; HINTS:
;
;   With Trace32 software version build 17761 or higher and most recent
;   flash support package FLASH.CLocK.AUTO can be used for automatic 
;   flash clock measurement. With older Trace32 software flash module 
;   input clock has to be set with FLASH.CLocK <freq> command. Flash 
;   clock has to match System Clock Frequency (CCLK). 
;
;   Boot flash cannot be programmed or erased with builtin flash 
;   algorithm. 
;
;   Data has to be loaded to flash alignment to page boundaries.
;
;   Vector table checksum generation is done by script, so that it 
;   can be used or switched off, as needed.
;
; @Author: WRD
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; @Chip: LPC21*
; --------------------------------------------------------------------------------
; $Rev: 7602 $
; $Id: lpc21xx.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)

  ; ------------------------------------------------------------------------------
  ; Set configuration values out of the CPU table above. If your CPU 
  ; derivate is not listed please check the data sheet for matching 
  ; flash configuration.

  LOCAL &CpuType
  LOCAL &FlashSize
  LOCAL &FlashType
  LOCAL &RAMSize

  ; Example for LPC2106
  &CpuType="LPC2106"
  &FlashSize=128.
  &FlashType=1.
  &RAMSize=64.

  ; ------------------------------------------------------------------------------
  ; Start debugging

  IF SYStem.MODE()<5
  (
    SYStem.RESet
    SYStem.CPU &CpuType
    SYStem.JtagClock RTCK
    IF VERSION.BUILD()<20711.
    (
      SYStem.Option ResBreak OFF
      SYStem.Option NOIRCHECK ON
      SYStem.Option BUGFIXV4 ON
    )
    SYStem.Up
  )

  ; ------------------------------------------------------------------------------
  ; Flash declaration

  FLASH.RESet
  FLASH.CLocK.AUTO
  GOSUB FlashDeclaration &FlashSize &FlashType &RAMSize

  ; Flash script ends here if called with parameter PREPAREONLY
  IF &param_prepareonly
    ENDDO PREPAREDONE 
  
  ; ------------------------------------------------------------------------------
  ; Flash programming example

  IF &FlashType!=0.
  (
    DIALOG.YESNO "Program flash memory?"
    LOCAL &progflash
    ENTRY &progflash
    IF &progflash 
    (
      ; Switch memory mapping to user flash mode, so that on-chip flash is 
      ; mapped to interrupt vectors. Memory Mapping Control Register 
      ; MEMMAP[1:0] = 01b
      Data.Set 0xe01fc040 %Byte 0x01

      ; Example for download 
      FLASH.ReProgram.ALL /Erase
      ; 1. Download file
      Data.LOAD.auto *
      ; 2. Checksum generation
      Data.Set 0x14 %Long 0x0          ;Zero the reserved vector's spot
      Data.SUM 0x0--0x1f /Long         ;Calculate checksum of all (other) vectors
      Data.Set 0x14 %Long -Data.SUM()  ;Write back the 2's complement in reserved vector's spot
      ; 3. Flash programming
      FLASH.ReProgram.off
    )
  )

  ENDDO


; --------------------------------------------------------------------------------
; Flash declaration depending on configuration 

FlashDeclaration:
  ENTRY  &FlashSize  &FlashType &RAMSize

  IF &FlashType==1.
  (
    IF &FlashSize==64.
    (
      FLASH.Create 1. 0x00000--0x0dfff 0x02000 TARGET Long
    )
    ELSE IF &FlashSize==128.
    (
      FLASH.Create 1. 0x00000--0x1dfff 0x02000 TARGET Long
    )
    ELSE IF &FlashSize==256.
    (
      FLASH.Create 1. 0x00000--0x0ffff 0x02000 TARGET Long
      FLASH.Create 1. 0x10000--0x2ffff 0x10000 TARGET Long 0x10000
      FLASH.Create 1. 0x30000--0x3dfff 0x02000 TARGET Long
    )
    ELSE
    (
      PRINT %ERROR "FLASH configuration is not supported by the script"
      ENDDO
    )
    
    FLASH.TARGET 0x40000000 0x40000800 0x1000 ~~/demo/arm/flash/long/lpc2100.bin
  )
  ELSE IF &FlashType==2.
  (
    IF &FlashSize==32.
    (
      FLASH.Create 1. 0x00000--0x07fff 0x1000 TARGET Long
    )
    ELSE IF &FlashSize==64.
    (
      FLASH.Create 1. 0x00000--0x07fff 0x1000 TARGET Long
      FLASH.Create 1. 0x08000--0x0ffff 0x8000 TARGET Long 0x8000
    )
    ELSE IF &FlashSize==128.
    (
      FLASH.Create 1. 0x00000--0x07fff 0x1000 TARGET Long
      FLASH.Create 1. 0x08000--0x1ffff 0x8000 TARGET Long 0x8000
    )
    ELSE IF &FlashSize==256.
    (
      FLASH.Create 1. 0x00000--0x07fff 0x1000 TARGET Long
      FLASH.Create 1. 0x08000--0x3ffff 0x8000 TARGET Long 0x8000
    )
    ELSE IF &FlashSize==512.
    (
      FLASH.Create 1. 0x00000--0x07fff 0x1000 TARGET Long
      FLASH.Create 1. 0x08000--0x77fff 0x8000 TARGET Long 0x8000
      FLASH.Create 1. 0x78000--0x7cfff 0x1000 TARGET Long
    )
    ELSE
    (
      PRINT %ERROR "FLASH configuration is not supported by the script"
      ENDDO
    )

    FLASH.TARGET 0x40000000 0x40000800 0x1000 ~~/demo/arm/flash/long/lpc2130.bin
  )
  ELSE IF &FlashType==3.
  (
    IF &FlashSize==8.
    (
      FLASH.Create 1. 0x00000--0x1fff 0x01000 TARGET Long
    )
    ELSE IF &FlashSize==16.
    (
      FLASH.Create 1. 0x00000--0x3fff 0x01000 TARGET Long
    )
    ELSE IF &FlashSize==32.
    (
      FLASH.Create 1. 0x00000--0x07fff 0x1000 TARGET Long
    )
    ELSE
    (
      PRINT %ERROR "FLASH configuration is not supported by the script"
      ENDDO
    )

    IF &RAMSize>=4.
    (
      FLASH.TARGET 0x40000000 0x40000600 0x800 ~~/demo/arm/flash/long/lpc2103.bin
    ) 
    ELSE IF &RAMSize>=2.
    (
      FLASH.TARGET 0x40000000 0x40000480 0x200 ~~/demo/arm/flash/long/lpc2103.bin
    ) 
  )

  RETURN
