; --------------------------------------------------------------------------------
; @Title: Example for flash declaration of Atmel AT91SAM3U internal flash. 
; 
; @Description: 
; Script arguments:
;
;   DO at91sam3u [PREPAREONLY]
;
;     PREPAREONLY only declares flash but does not execute flash programming
;
; Example:
;
;   DO ~~/demo/arm/flash/at91sam3u PREPAREONLY
;
; List of AT91SAM3U derivatives and their configuration:
;
;   CPU-Type        Flash size   Page size   SRAM0 size   SRAM1 size 
;                    (Byte)       (Byte)       (Byte)       (Byte)
; --------------------------------------------------------------------------------
;   AT91SAM3U1C       0x10000      0x100       0x2000       0x2000
;   AT91SAM3U1E       0x10000      0x100       0x2000       0x2000
;   AT91SAM3U2C       0x20000      0x100       0x4000       0x4000
;   AT91SAM3U2E       0x20000      0x100       0x4000       0x4000
;   AT91SAM3U4C     2 * 0x20000    0x100       0x8000       0x4000
;   AT91SAM3U4E     2 * 0x20000    0x100       0x8000       0x4000
;
; The flash is divided into pages of 256 Byte. 
;
; The internal flash EFC0 is located at 0x00080000
; The internal flash EFC1 is located at 0x00100000
; The internal SRAM0 is located at      0x20000000
; The internal SRAM1 is located at      0x20080000
; The internal ROM is located at        0x00180000.
;
; The system always boot at address 0x0. If GPNVM bit 1 is set the 
; internal flash is mirrored to address 0x0 before remap command. 
; Otherwise ROM is mapped to address 0x0. If GPNVM bit 2 is set 
; flash 1 is used for boot otherwise flash 0. After remap command 
; internal SRAM is mirrored to address 0x0.
;
; HINTS:
;
;   Locked pages can be unlocked with FLASH.UNLOCK command and can be 
;   locked again with FLASH.LOCK command.
;
;   Watchdog is critical for flash programming. The flash algorithm is 
;   serving the watchdog if the watchdog is not used in window mode.
;
;   If the watchdog is setup to window mode by a running application 
;   the watchdog should be either disabled after first power-up or the 
;   board should be power-up before every flash programming. If the 
;   watchdog is disabled after power-up it cannot be enabled again by 
;   the application because WDT_MR register is write once. Please see 
;   below the command to disable the watchdog.
;
;   GPNVM bit values can be changed via at91sam3-nvm.cmm script.
;
; @Author: WRD
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; @Chip: AT91SAM3U*
; --------------------------------------------------------------------------------
; $Rev: 7616 $
; $Id: at91sam3u.cmm 7616 2019-11-08 07:18:24Z bschroefel $

  LOCAL &parameters &param_prepareonly
  ENTRY %LINE &parameters
  &param_prepareonly=(STRing.SCAN(STRing.UPpeR("&parameters"),"PREPAREONLY",0)!=-1)
  &param_cpu=STRing.SCANAndExtract(STRing.UPpeR("&parameters"),"CPU=","")

  LOCAL &cpu
  LOCAL &efc0flashsize
  LOCAL &efc1flashsize

  ; ------------------------------------------------------------------------------
  ; Setup CPU

  IF SYStem.MODE()<5
  (
    SYStem.RESet
    IF "&param_cpu"!=""
      SYStem.CPU &param_cpu
    IF !CPUIS(AT91SAM3U*)
      SYStem.CPU AT91SAM3U*
    SYStem.JtagClock RTCK
    SYStem.Option.ResBreak OFF
    SYStem.Up
  )

  ; Disable watchdog for flash programming if watchdog may be used in 
  ; window mode.
  ;DATA.SET 0x400E1254 %LONG DATA.LONG(ASD:0x400E1254)|0x00008000

  ; ------------------------------------------------------------------------------
  ; Setup flash configuration depending on selected CPU.

  &cpu=SYStem.CPU()
  IF (("&cpu"=="AT91SAM3U1C")||("&cpu"=="AT91SAM3U1E"))
  (
    &efc0flashsize=0x10000
    &efc1flashsize=0x0
  )
  ELSE IF (("&cpu"=="AT91SAM3U2C")||("&cpu"=="AT91SAM3U2E"))
  (
    &efc0flashsize=0x20000
    &efc1flashsize=0x0
  )
  ELSE IF (("&cpu"=="AT91SAM3U4C")||("&cpu"=="AT91SAM3U4E"))
  (
    &efc0flashsize=0x20000
    &efc1flashsize=0x20000
  )
  ELSE
  (
    PRINT %ERROR "FLASH size of CPU type is unknown"
    ENDDO
  )

  ; Setup EEFC wait states in EEFC_FMR register, FWS = 6
  Data.Set ASD:0x400E0800 %Long (Data.Long(ASD:0x400E0800)&0xFFFFF0FF)|0x00000600  ; EEFC0
  IF &efc1flashsize!=0x0
  (
    Data.Set ASD:0x400E0A00 %Long (Data.Long(ASD:0x400E0A00)&0xFFFFF0FF)|0x00000600  ; EEFC1
  )

  ; ------------------------------------------------------------------------------
  ; Flash declaration

  FLASH.RESet
  ; Bank EFC0
  FLASH.Create 1. 0x00080000++(&efc0flashsize-1) 0x100 TARGET Long 0.
  IF &efc1flashsize!=0x0
  (
    ; Bank EFC1
    FLASH.Create 2. 0x00100000++(&efc1flashsize-1) 0x100 TARGET Long 1.
  )
  FLASH.TARGET 0x20000000 0x20000800 0x400 ~~/demo/arm/flash/long/eefcsam3.bin

  ; Flash script ends here if called with parameter PREPAREONLY
  IF &param_prepareonly
    ENDDO PREPAREDONE

  ; ------------------------------------------------------------------------------
  ; Flash NVM bit programming example

  DIALOG.YESNO "Program flash NVM bits"
  LOCAL &prognvm
  ENTRY &prognvm
  IF &prognvm 
  (
    DO ~~~~/at91sam3-nvm
  )

  ; ------------------------------------------------------------------------------
  ; Flash programming example

  DIALOG.YESNO "Program flash memory?"
  LOCAL &progflash
  ENTRY &progflash
  IF &progflash 
  (
    ; Unlock locked pages if necessary
    ;FLASH.UNLOCK ALL

    ; Because of small sector size and very fast chip erase feature 
    ; FLASH.Erase and FLASH.Program is used instead of FLASH.ReProgram.
    FLASH.Erase.ALL
    FLASH.Program.ALL
    Data.LOAD.auto * /Long
    FLASH.Program.off

    ; Reset device
    SYStem.Down
    SYStem.Up
  )

  ENDDO
