; --------------------------------------------------------------------------------
; @Title: Generic script for RM42 internal flash
;
; @Description:
;
; Flash declaration of TI RM42 internal flash.
;
; Script arguments:
;
;   DO rm42 [PREPAREONLY] [AUTOECC=0|1] [DUALPORT=0|1]
;
;     PREPAREONLY only declares flash but does not execute flash programming
;
;     AUTOECC default value is 1
;
;     DUALPORT default value is 0 (disabled). If DualPort mode is enabled
;             flash algorithm stays running until flash programming is
;             finished. Data is tranferred via dual-port memory access. 
;
; For example:
;
;   DO ~~/demo/arm/flash/rm42 DUALPORT=1 PREPAREONLY
;
; List of RM42 derivatives and their configuration:
;
;                            EEPROM
;                 Program   emulation
;   CPU-Type       Flash      Flash       RAM
; --------------------------------------------------------------------------------
;   RM42L432       384kB       16kB      32kB
;
; HINTS:
;
;   Flash programming algorithm is linked to address 0x08000000 and is not
;   position independent. Code address of FLASH.TARGET command has not be
;   changed.
;
;   OTP sectors can be programmed using FLASH.Program with /OTP option.
;   For example with:
;
;     FLASH.Program 0xF0000000--0xF0000FFF /OTP
;     Data.Set ...
;     ...
;     FLASH.Program off
;
; @Chip: RM42*
; @Author: WRD
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 6945 $
; $Id: rm42.cmm 6945 2019-05-07 13:57:32Z wdoering $
;

  ; ------------------------------------------------------------------------------
  ; Setup script configuration. If auto ECC generation is disabled then
  ; flash sectors are programmed without generating ECC. ECC sectors can be
  ; programmed separately or ECC sector programming can be omitted.

  LOCAL &parameters
  ENTRY %LINE &parameters

  LOCAL &param_prepareonly &param_cpu &AutoECCGeneration &param_dualport
  &param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)
  &AutoECCGeneration=STRing.SCANAndExtract(STRing.UPpeR("&parameters"),"AUTOECC=","1")
  &param_dualport=0
  IF VERSION.BUILD.BASE()>=43441.
    &param_dualport=STRing.SCANAndExtract(STRing.UPpeR("&parameters"),"DUALPORT=","0")

  LOCAL &flashdriver

  ; ------------------------------------------------------------------------------
  ; Setup CPU

  IF (SYStem.MODE()<5)
  (
    SYStem.RESet
    MAP.RESet

    SYStem.CPU RM42L432
    SYStem.Option.ResBreak OFF
    SYStem.Up

    ; Clear SRAM to prevent ECC faults
    Data.Set 0xFFFFFF5C %Long 0x00000005
    Data.Set 0xFFFFFF5C %Long 0x0000000A
    Data.Set 0xFFFFFF60 %Long 0x00000001    ; init L2 SRAM only
    Data.Set 0xFFFFFF5C %Long 0x00000005
  )

  ; ------------------------------------------------------------------------------
  ; 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 
  (
    ; Disable memory protection unit (MPU) for case internal SRAM is
    ; protected against code execution
    LOCAL &MPU
    &MPU=Data.Long(EC15:0x1)
    Data.Set EC15:0x1 %Long &MPU&0xFFFFFFFE 

    ; Disable ECC for flash programming
    LOCAL &PMNC &ACTLR
    &PMNC=Data.Long(C15:0x0C9)
    &ACTLR=Data.Long(C15:0x101)
    Data.Set C15:0x0C9 %Long &PMNC&~0x10           ; PMNC:X
    Data.Set C15:0x101 %Long &ACTLR&~0x0E000000    ; ACTLR:ATCMPCEN, B0TCMPCEN, B1TCMPCEN

    FLASH.ReProgram.ALL /Erase
    Data.LOAD.auto *
    FLASH.ReProgram.off
    
    ; Restore MPU and ECC setup
    Data.Set C15:0x0C9 %Long &PMNC
    Data.Set C15:0x101 %Long &ACTLR
    Data.Set EC15:0x1  %Long &MPU
  )

  ENDDO


; --------------------------------------------------------------------------------
; Flash declaration depending on selected CPU 

FlashDeclaration:

  IF VERSION.BUILD()<45210.
  (
    DO ~~~~/rm42simpledecl AUTOECC=&AutoECCGeneration DUALPORT=&param_dualport
    RETURN
  )

  IF &AutoECCGeneration!=0
  (
    ; Program flash
    FLASH.Create  1. 0x00000000--0x00017FFF  0x2000 TARGET Byte 0. /INFO "Bank 0"
    FLASH.Create  1. 0x00018000--0x0001FFFF  0x8000 TARGET Byte 0. /INFO "Bank 0"
    FLASH.Create  1. 0x00020000--0x0005FFFF 0x20000 TARGET Byte 0. /INFO "Bank 0"
    ; Program flash ECC
    FLASH.Create  1. 0xF0400000--0xF0402FFF   0x400 NOP    Byte 0. /INFO "Bank 0 ECC"
    FLASH.Create  1. 0xF0403000--0xF0403FFF  0x1000 NOP    Byte 0. /INFO "Bank 0 ECC"
    FLASH.Create  1. 0xF0404000--0xF040BFFF  0x4000 NOP    Byte 0. /INFO "Bank 0 ECC"

    ; EEPROM emulation flash
    FLASH.Create  8. 0xF0200000--0xF0203FFF  0x1000 TARGET Byte 7. /INFO "EEPROM Bank"
    ; EEPROM emulation flash ECC
    FLASH.Create  8. 0xF0100000--0xF01007FF   0x200 NOP    Byte 7. /INFO "EEPROM Bank-ECC"

    IF VERSION.BUILD()>=41634.
    (
      ; OTP flash
      FLASH.Create 11. 0xF0000000--0xF00007FF   0x800 TARGET Quad 0. /OTP /INFO "Customer OTP, Bank 0"
      FLASH.Create 18. 0xF000E000--0xF000E3FF   0x400 TARGET Quad 7. /OTP /INFO "Customer OTP, EEPROM Bank"
      ; OTP flash ECC
      FLASH.Create 11. 0xF0040000--0xF00400FF   0x100 NOP    Byte 0. /OTP /INFO "Customer OTP-ECC, Bank 0"
      FLASH.Create 18. 0xF0041C00--0xF0041C7F    0x80 NOP    Byte 7. /OTP /INFO "Customer OTP-ECC, EEPROM Bank"
    )

    &flashdriver="f021r4.bin"
  )
  ELSE
  (
    ; Program flash
    FLASH.Create  1. 0x00000000--0x00017FFF  0x2000 TARGET Byte 0. /EraseALIAS 0xF0400000--0xF0402FFF /INFO "Bank 0"
    FLASH.Create  1. 0x00018000--0x0001FFFF  0x8000 TARGET Byte 0. /EraseALIAS 0xF0403000--0xF0403FFF /INFO "Bank 0"
    FLASH.Create  1. 0x00020000--0x0005FFFF 0x20000 TARGET Byte 0. /EraseALIAS 0xF0404000--0xF040BFFF /INFO "Bank 0"
    ; Program flash ECC
    FLASH.Create  1. 0xF0400000--0xF0402FFF   0x400 TARGET Byte 0. /EraseALIAS 0x00000000--0x00017FFF /INFO "Bank 0 ECC"
    FLASH.Create  1. 0xF0403000--0xF0403FFF  0x1000 TARGET Byte 0. /EraseALIAS 0x00018000--0x0001FFFF /INFO "Bank 0 ECC"
    FLASH.Create  1. 0xF0404000--0xF040BFFF  0x4000 TARGET Byte 0. /EraseALIAS 0x00020000--0x0005FFFF /INFO "Bank 0 ECC"

    ; EEPROM emulation flash
    FLASH.Create  8. 0xF0200000--0xF0203FFF  0x1000 TARGET Byte 7. /EraseALIAS 0xF0100000--0xF01007FF /INFO "EEPROM Bank"
    ; EEPROM emulation flash ECC
    FLASH.Create  8. 0xF0100000--0xF01007FF   0x200 TARGET Byte 7. /EraseALIAS 0xF0200000--0xF0203FFF /INFO "EEPROM Bank-ECC"

    IF VERSION.BUILD()>=41634.
    (
      ; OTP flash
      FLASH.Create 11. 0xF0000000--0xF00007FF   0x800 TARGET Quad 0. /OTP /INFO "Customer OTP, Bank 0"
      FLASH.Create 18. 0xF000E000--0xF000E3FF   0x400 TARGET Quad 7. /OTP /INFO "Customer OTP, EEPROM Bank"
      ; OTP flash ECC
      FLASH.Create 11. 0xF0040000--0xF00400FF   0x100 TARGET Byte 0. /OTP /INFO "Customer OTP-ECC, Bank 0"
      FLASH.Create 18. 0xF0041C00--0xF0041C7F    0x80 TARGET Byte 7. /OTP /INFO "Customer OTP-ECC, EEPROM Bank"
    )

    &flashdriver="f021r4noecc.bin"
  )

  FLASH.CreateALIAS 0x20000000--0x202FFFFF 0x0

  IF &param_dualport==0
    FLASH.TARGET 0x08000000 0x08001000 0x4000 ~~/demo/arm/flash/byte/&flashdriver.bin
  ELSE
    FLASH.TARGET 0x08000000 EAHB:0x08001000 0x4000 ~~/demo/arm/flash/byte/&flashdriver.bin /DualPort
  IF FLASH.TARGET.BUILD(~~/demo/arm/flash/byte/&flashdriver)<6891.
    PRINT %ERROR "Flash algorithm is to old for EEPROM programming, please request an update"

  FLASH.CLocK.AUTO

  MAP.BUS16 0xF0100000--0xF013FFFF
  MAP.BUS16 0xF0400000--0xF04FFFFF

  RETURN
