; --------------------------------------------------------------------------------
; @Title: Example for flash declaration of Atmel AT91SAM9XE internal flash. 
; 
; @Description: 
; Script arguments:
;
; DO at91sam9xe [PREPAREONLY]
;
;  PREPAREONLY only declares flash but does not execute flash programming
;
; Example:
;
;   DO ~~/demo/arm/flash/at91sam9xe PREPAREONLY
;
; List of AT91SAM9XE derivatives and their configuration:
;
; CPU-Type        FlashSize    Pagesize  SRAM size  Algorithm
;                   (Byte)      (Byte)     (Byte)
; --------------------------------------------------------------------------------
; AT91SAM9XE128    0x20000      0x200      0x8000   eefc.bin
; AT91SAM9XE256    0x40000      0x200      0x8000   eefc.bin
; AT91SAM9XE512    0x80000      0x200      0x8000   eefc.bin
;
; The flash is divided into pages of 512 Byte. 
;
; The internal flash is located at 0x00200000.
; The internal SRAM is located at  0x00300000.
; The internal ROM is located at   0x00100000.
;
; If GPNVM bit 3 has value of 1 the internal flash is mirrored to 
; address 0x0 before remap command. Otherwise ROM is mapped to 
; address 0x0. 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 disbaled 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 at91sam9-nvm.cmm script.
;
; @Author: WRD
; @Copyright: (C) 1989-2019 Lauterbach GmbH, licensed for use with TRACE32(R) only
; @Chip: AT91SAM9XE*
; --------------------------------------------------------------------------------
; $Rev: 7616 $
; $Id: at91sam9xe.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)
  
LOCAL &cpu
LOCAL &flashsize

; --------------------------------------------------------------------------------
; Setup CPU
; Example for AT91SAM9XE512
IF SYStem.MODE()<5
(
  SYStem.RESet
  SYStem.CPU AT91SAM9XE512
  SYStem.JtagClock RTCK
  SYStem.Option ResBreak OFF
  SYStem.Up
)
  
; Setup PLL A
;   CKGR_MOR: OSCOUNT=0x40, MOSCEN=enabled
Data.Set ASD:0xFFFFFC20 %Long 0x00004001
;   wait for PMC_SR:MOSCS
WHILE (Data.Long(ASD:0xFFFFFC68)&0x01)!=0x01
(
)
;   PMC_PLLICPR: ICPPLLA=1
;   CKGR_PLLAR: Bit 29=1, MULA=0x6d, OUTA=190-220MHz, DIVA=0x09
Data.Set ASD:0xFFFFFC80 %Long 0x00000001
Data.Set ASD:0xFFFFFC28 %Long 0x206dbf09
;   wait for PMC_SR:MOSCS
WHILE (Data.Long(ASD:0xFFFFFC68)&0x02)!=0x02
(
)
;   PMC_MCKR: MDIV=Clock/2, CSS=PLL-A
Data.Set ASD:0xFFFFFC30 %Long 0x00000102
WHILE (Data.Long(ASD:0xFFFFFC68)&0x08)!=0x08
(
)

; Setup EEFC wait states
Data.Set ASD:0xFFFFFA00 %Long 0x00000300

; Disable watchdog for flash programming if watchdog may be used in 
; window mode.
;DATA.SET 0xFFFFFD44 %LONG DATA.LONG(ASD:0xFFFFFD44)|0x00008000

; --------------------------------------------------------------------------------
; Setup flash configuration depending on selected CPU.
&cpu=SYStem.CPU()
IF ("&cpu"=="AT91SAM9XE128")
(
  &flashsize=0x20000
)
ELSE IF ("&cpu"=="AT91SAM9XE256")
(
  &flashsize=0x40000
)
ELSE IF ("&cpu"=="AT91SAM9XE512")
(
  &flashsize=0x80000
)
ELSE
(
  PRINT %ERROR "FLASH size of CPU type is unknown"
  ENDDO
)

; --------------------------------------------------------------------------------
; Flash declaration
FLASH.RESet
FLASH.Create 1. 0x00200000++(&flashsize-1) 0x200 TARGET Long
FLASH.TARGET 0x00300000 0x00301000 0x400 ~~/demo/arm/flash/long/eefc.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"
ENTRY &prognvm
IF &prognvm 
(
  DO ~~~~/at91sam9-nvm
)

; --------------------------------------------------------------------------------
; Flash programming example
DIALOG.YESNO "Program flash memory?"
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
)

ENDDO
