; --------------------------------------------------------------------------------
; @Title: Example for flash declaration of Atmel ATSAM4C internal flash.
;
; @Description:
;
;   Script arguments:
;
;    DO atsamd2 [PREPAREONLY] [CPU=<cpu>]
;
;      PREPAREONLY only declares flash but does not execute flash programming
;
;      CPU=<cpu> selects CPU derivative <cpu>
;
;   Example:
;
;   DO ~~/demo/arm/flash/atsam4c CPU=ATSAM4C16C-CORE0 PREPAREONLY
;
;   List of ATSAM4C* derivatives and their configuration:
;
;     CPU-Type        Flash size   Page size      SRAM size
;                        [Byte]      [Byte]        [Byte]
;     ATSAM4C8C         0x80000       0x200     128+16+8 Kbytes
;     ATSAM4CMP8C       0x80000       0x200     128+16+8 Kbytes
;     ATSAM4CMS8C       0x80000       0x200     128+16+8 Kbytes
;     ATSAM4C16C       0x100000       0x200     128+16+8 Kbytes
;     ATSAM4CMP16C     0x100000       0x200     128+16+8 Kbytes
;     ATSAM4CMS16C     0x100000       0x200     128+16+8 Kbytes
;
; @Author: FLC
; @Chip: ATSAM4C*
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; --------------------------------------------------------------------------------
; $Rev: 7577 $
; $Id: atsam4c.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("ATSAM4C*C-CORE?")
      SYStem.CPU "ATSAM4C*C-CORE?"
    SYStem.Up
  )
  
  ; Disable watchdog
  Data.Set C:0x400E1454 %Long Data.Long(C:0x400E1454)&~8000

  ; ------------------------------------------------------------------------------
  ; 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("ATSAM4C*8C-CORE?")
  (
    &FlashSize=0x80000
    &FlashDriver="atsam4c8.bin"
  )
 ELSE IF CPUIS("ATSAM4C*16C-CORE?")
  (
      &FlashSize=0x100000
      &FlashDriver="atsam4c16.bin"
  )
  ELSE
  (
    PRINT %ERROR "FLASH size of CPU type is unknown"
    ENDDO
  )

  IF &FlashSize>=0x80000
    FLASH.Create  1. 0x00000000--0x00001FFF  0x2000 TARGET Long
    FLASH.Create  1. 0x00002000--0x00003FFF  0x2000 TARGET Long
    FLASH.Create  1. 0x00004000--0x0000FFFF  0xC000 TARGET Long
    FLASH.Create  2. 0x00010000--0x0007FFFF 0x10000 TARGET Long
  IF &FlashSize>=0x100000
    FLASH.Create  2. 0x00080000--0x000FFFFF 0x10000 TARGET Long

  FLASH.TARGET 0x20000000 0x20000800 0x10000 ~~/demo/arm/flash/long/&FlashDriver
  
  RETURN
