;
; @Title: Example script for flash declaration of Atmel ATSAM4N internal flash.
; 
; @Description: 
; Script arguments:
;
;   DO atsam4n [PREPAREONLY] [CPU=<cpu>]
;
;     PREPAREONLY only declares flash but does not execute flash programming
;
;     CPU=<cpu> selects CPU derivative <cpu>
;
; Example:
;
;   DO ~~/demo/arm/flash/atsam4n CPU=ATSAM4N16C PREPAREONLY
;
; List of ATSAM4N derivatives and their configuration:
;
;   CPU-Type        Flash size   Page size    SRAM size
;                      [Byte]      [Byte]       [Byte]
; --------------------------------------------------------------------------------
;   ATSAM4N8A         0x80000      0x200       0x10000   
;   ATSAM4N8B         0x80000      0x200       0x10000   
;   ATSAM4N8C         0x80000      0x200       0x10000   
;   ATSAM4N16B       0x100000      0x200       0x14000   
;   ATSAM4N16C       0x100000      0x200       0x14000   
;
; The internal flash is located at 0x00400000
; The internal ROM is located at   0x00800000.
; The internal SRAM is located at  0x20000000
;
; The system always boot at address 0x0. If GPNVM bit 1 is set the 
; internal flash is mirrored to address 0x0 otherwise ROM is mapped
; 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 atsam4-nvm.cmm script.
;
; @Author: WRD
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; @Chip: ATSAM4N*
; --------------------------------------------------------------------------------
; $Rev: 7577 $
; $Id: atsam4n.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(ATSAM4N*)
      SYStem.CPU ATSAM4N*

    SYStem.Option.ResBreak OFF
    IF CABLE.TWOWIRE()
      SYStem.CONFIG.DEBUGPORTTYPE SWD
    ELSE
      SYStem.CONFIG.DEBUGPORTTYPE JTAG

    SYStem.Up
  )

  ; Disable watchdog for flash programming if watchdog may be used in 
  ; window mode.
  //DATA.SET 0x400E1454 %LONG DATA.LONG(ASD:0x400E1454)|0x00008000

  ; ------------------------------------------------------------------------------
  ; Flash declaration

  FLASH.RESet
  GOSUB FlashDeclaration

  ; Setup EEFC wait states in EEFC_FMR register
  //Data.Set ASD:0x400E0800 %Long (Data.Long(ASD:0x400E0800)&0xFFFFF0FF)|0x00000300

  ; 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 ~~~~/atsam4-nvm
  )

  ; ------------------------------------------------------------------------------
  ; Flash programming example

  DIALOG.YESNO "Program flash memory?"
  LOCAL &progflash
  ENTRY &progflash
  IF &progflash 
  (
    ; Unlock locked pages if necessary
    //FLASH.UNLOCK ALL

    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

  IF CPUIS("ATSAM4N8?")
  (
    &FlashSize=0x80000
  )
  ELSE IF CPUIS("ATSAM4N16?")
  (
    &FlashSize=0x100000
  )
  ELSE
  (
    PRINT %ERROR "FLASH size of CPU type is unknown"
    ENDDO
  )

  FLASH.Create  1. 0x00400000--0x00401FFF 0x1000 TARGET Long
  FLASH.Create  2. 0x00402000--0x00403FFF 0x1000 TARGET Long
  FLASH.Create  3. 0x00404000--0x0040FFFF 0x1000 TARGET Long
  FLASH.Create  4. 0x00410000--0x0041FFFF 0x1000 TARGET Long
  FLASH.Create  5. 0x00420000--0x0042FFFF 0x1000 TARGET Long
  FLASH.Create  6. 0x00430000--0x0043FFFF 0x1000 TARGET Long
  FLASH.Create  7. 0x00440000--0x0044FFFF 0x1000 TARGET Long
  FLASH.Create  8. 0x00450000--0x0045FFFF 0x1000 TARGET Long
  FLASH.Create  9. 0x00460000--0x0046FFFF 0x1000 TARGET Long
  FLASH.Create 10. 0x00470000--0x0047FFFF 0x1000 TARGET Long
  IF &FlashSize>=0x100000
  (
    FLASH.Create 11. 0x00480000--0x0048FFFF 0x1000 TARGET Long
    FLASH.Create 12. 0x00490000--0x0049FFFF 0x1000 TARGET Long
    FLASH.Create 13. 0x004A0000--0x004AFFFF 0x1000 TARGET Long
    FLASH.Create 14. 0x004B0000--0x004BFFFF 0x1000 TARGET Long
    FLASH.Create 15. 0x004C0000--0x004CFFFF 0x1000 TARGET Long
    FLASH.Create 16. 0x004D0000--0x004DFFFF 0x1000 TARGET Long
    FLASH.Create 17. 0x004E0000--0x004EFFFF 0x1000 TARGET Long
    FLASH.Create 18. 0x004F0000--0x004FFFFF 0x1000 TARGET Long
  )

  FLASH.TARGET 0x20000000 0x20000800 0x1000 ~~/demo/arm/flash/long/eefcsam4n.bin

  RETURN
