; --------------------------------------------------------------------------------
; @Title: Example for flash declaration of TI Stellaris LM4F110 internal flash. 
;
; @Description:
; Synopsis:
;
;   do ~~/demo/arm/flash/lm4f110 [<cpu>] [PREPAREONLY]
;
; Description:
;
;   Script arguments are not case sensitive.
;
;   <cpu>
;     is the CPU name argument out of the table listed below. If debug 
;     connection is not already established CPU name argument is used for
;     CPU selection. Without CPU name argument and no LM4F110 CPU is 
;     selected the script is using default CPU selection LM4F112H5QD.
;
;   PREPAREONLY only declares flash but does not execute flash programming
;
; Example:
;
;   do ~~/demo/arm/flash/lm4f110 LM4F110B2QR PREPAREONLY
;
; List of LM4F110 derivatives and their configuration:
;
;   CPU-Type       ProgFlash    EEPROM   RamSize
; --------------------------------------------------------------------------------
;   LM4F110B2QR       32kB        2kB      12kB
;   LM4F110C4QR       64kB        2kB      24kB
;   LM4F110E5QR      128kB        2kB      32kB
;   LM4F110H5QR      256kB        2kB      32kB
;   LM4F111B2QR       32kB        2kB      12kB
;   LM4F111C4QR       64kB        2kB      24kB
;   LM4F111E5QR      128kB        2kB      32kB
;   LM4F111H5QR      256kB        2kB      32kB
;   LM4F112C4QC       64kB        2kB      24kB
;   LM4F112E5QC      128kB        2kB      32kB
;   LM4F112H5QC      256kB        2kB      32kB
;   LM4F112H5QD      256kB        2kB      32kB
;
; @Author: WRD
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; @Chip: LM4F11*
; --------------------------------------------------------------------------------
; $Rev: 7616 $
; $Id: lm4f110.cmm 7616 2019-11-08 07:18:24Z bschroefel $

  LOCAL &parameters
  ENTRY %LINE &parameters
  
  LOCAL &param_prepareonly
  &param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)

  LOCAL &cpu
  &cpu="LM4F11"+STRing.SCANAndExtract(STRing.UPpeR("&parameters"),"LM4F11","")

  ; Setup CPU selection for the case script is called without argument.
  IF "&cpu"=="LM4F11"
  (
    IF CPUIS(LM4F11*)
      &cpu=SYStem.CPU()
    ELSE
      &cpu="LM4F112H5QD"
  )

  ; ------------------------------------------------------------------------------
  ; Start debugging

  IF SYStem.MODE()<5
  (
    SYStem.RESet
    SYStem.CPU &cpu
    IF CABLE.TWOWIRE()
    (
      SYStem.CONFIG.DEBUGPORTTYPE SWD
    )
    ELSE
    (
      SYStem.CONFIG.DEBUGPORTTYPE JTAG
    )
    SYStem.Up
  )

  ; ------------------------------------------------------------------------------
  ; Flash declaration

  FLASH.RESet
  GOSUB FlashDeclaration

  ; In case of empty flash disable RMCTL:BA (ROM Control register Boot 
  ; Alias bit) to show flash contents at address 0x0
  IF (Data.Long(ASD:0x400FE0F0)&0x01)==0x01
    Data.Set ASD:0x400FE0F0 %Long 0x01

  ; 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 CPU selection

FlashDeclaration:
  LOCAL &FlashSize

  IF CPUIS("LM4F11?B?Q?")
  (
    &FlashSize=0x8000
  )
  ELSE IF CPUIS("LM4F11?C?Q?")
  (
    &FlashSize=0x10000
  )
  ELSE IF CPUIS("LM4F11?E?Q?")
  (
    &FlashSize=0x20000
  )
  ELSE IF CPUIS("LM4F11?H?Q?")
  (
    &FlashSize=0x40000
  )
  ELSE
  (
    PRINT %ERROR "FLASH size of CPU type is not supported by the script"
    ENDDO
  )

  ;IF (Data.Long(SD:0x400FE358)&0x01)==0x01
  ;  PRINT "EEPROM module is present"

  FLASH.Create 1. 0x0++(&FlashSize-1) 0x400 TARGET Long
  FLASH.TARGET 0x20000000 0x20000800 0x400 ~~/demo/arm/flash/long/lm4f.bin

  RETURN
