; --------------------------------------------------------------------------------
; @Title: Example for flash declaration of ST A2Fx internal flash
; @Description:
;   Script arguments:
;
;    DO a2f [PREPAREONLY] [CPU=<cpu>]
;
;      PREPAREONLY only declares flash but does not execute flash programming
;
;      CPU=<cpu> selects CPU derivative <cpu>
;
;   Example:
;
;   DO ~~/demo/arm/flash/a2f CPU=A2F200 PREPAREONLY
;
;   List of A2Fx derivatives and their configuration:
;
;     CPU-Type        Flash size   Sector size    SRAM size
;                       [Byte]      [Byte]        [Byte]
; --------------------------------------------------------------------------------
;     A2F500              0x80000    0x1000       0x10000
;     A2F200              0x40000    0x1000       0x10000
;     A2F060                 -          -            -
;
; @Author: FLC
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 7577 $
; $Id: a2f.cmm 7577 2019-10-30 15:30:31Z bschroefel $


  LOCAL &parameters
  ENTRY %LINE &parameters

  LOCAL &param_prepareonly &param_cpu
  &param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)
  &param_cpu=STRing.SCANAndExtract(STRing.UPpeR("&parameters"),"CPU=","")

  ; ------------------------------------------------------------------------------
  ; Setup CPU

  IF SYStem.MODE()<5
  (
    SYStem.RESet

    IF "&param_cpu"!=""
      SYStem.CPU &param_cpu
    IF !CPUIS(A2F*)
      SYStem.CPU A2F*

    SYStem.Option.EnReset OFF
    SYStem.Up
  )

  ; Disable Watchdog
  Data.Set C:0x40006010  %Long 0x4C6E55FA
  
  ; ------------------------------------------------------------------------------
  ; 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
    Data.LOAD.auto *
    FLASH.ReProgram.off

    ; Reset device
    SYStem.Down
    SYStem.Up
  )

  ENDDO


; --------------------------------------------------------------------------------
; Flash declaration depending on selected CPU

FlashDeclaration:
  LOCAL &FlashSize &FlashDriver

  IF CPUIS("A2F200")
  (
    &FlashSize=0x40000
    &FlashDriver="a2f.bin"
  )
  ELSE IF CPUIS("A2F500")
  (
    &FlashSize=0x80000
    &FlashDriver="a2f.bin"
  )
  ELSE
  (
    PRINT %ERROR "FLASH size of CPU type is unknown"
    ENDDO
  )

  IF &FlashSize>=0x40000
  (
    FLASH.Create  1. 0x60000000--0x6003FFFF 0x80 TARGET Long
    FLASH.CreateALIAS 0x00000000--0x0003FFFF 0x60000000
  )
  ELSE IF &FlashSize>=0x80000
  (
    FLASH.Create  1. 0x60000000--0x6007FFFF 0x80 TARGET Long
    FLASH.CreateALIAS 0x00000000--0x0007FFFF 0x60000000
  )

  FLASH.TARGET 0x20000000 0x20001000 0x1000 ~~/demo/arm/flash/long/&FlashDriver

  RETURN
