;===================================================================================================
;
;    @file        run.cmm
;    @version     1.0.1
;
;    @brief       AUTOSAR - Sample_app - Script for programming internal flash and executing
;    @details     LTB script used for loading the elf into flash.
;
;    Project      : AUTOSAR 4.0 MCAL
;    Platform     : PA
;    Peripheral   : 
;    Dependencies : Base Det Dem Port Dio Mcu Rte Wdg WdgIf CanIf LinIf FrIf EcuM
;
;    Autosar Version       : 4.0.3
;    Autosar Revision      : ASR_REL_4_0_REV_0003
;    Autosar Conf. Variant :
;    Software Version      : 1.0.1
;    Build Version         : MPC5777C_MCAL_1_0_0_RTM_ASR_REL_4_0_REV_0003_20160807
;
;    (c) Copyright 2006-2016 Freescale Semiconductor Inc. & STMicroelectronics
;    All Rights Reserved.
;
;    This file contains sample code only. It is not part of the production code deliverables.
;
;
;===================================================================================================
;;===================================================================================================
;Revision History:
;                             Modification     Tracking
;Author (core ID)              Date D/M/Y       Number     Description of Changes
;---------------------------   ----------    ------------  ------------------------------------------
;Marius Rotaru (rtrm001)        9/07/2009    ENGR119813    File creation
;Nicolae Dobrostomat (b00556)  11/03/2010    ENGR121138    Added support for OS
;---------------------------   ----------    ------------  ------------------------------------------
;',);===================================================================================================


;
; Flashing algorithm is specific to each device
;
;&flashbase=0x01000000
;&rambase=0x40000000
;&flashsize=0x00380000

&optimizeFLSMODE=1
&optimizeRAM=1
&optimizePLL=1

;=========================================================================
; Variables settings  
;=========================================================================
&flashdriver="c55fm5777c.bin"

;=========================================================================
; System settings  
;=========================================================================
; CPU setup
SYStem.RESet
SYStem.BdmClock 4.0MHz
SYStem.CPU MPC5777C
SYStem.Option.WATCHDOG OFF
SYStem.OPTION DUALPORT ON
System.Option.Slowreset ON
SYStem.Up


;**************************************
; PLL initialization 
;**************************************
if &optimizePLL==1
(
   print "Starting PLL:"
   
   ;/* select core clock from IRC clock */    
   ;SIU.SYSDIV.B.SYSCLKSEL=0;
   PER.Set.Field ANC:0xC3F909A0 %LONG 0x00003000 0.
    
   ;/* Disable the PLLs */    
   ; PLLDIG.PLL0CR.R=0;
   ; PLLDIG.PLL1CR.R=0;
   per.set ANC:0xFFE74000 %LONG   0x0 
   per.set ANC:0xFFE74020 %LONG   0x0 
   
   ;/* Wait for XOSC to LOCK  */
   ;while (SIU.RSR.B.XOSC==0) {}; 
   wait (Data.Long(ANC:0xC3F9000C)&0x00000080)==0x80 
   
   ;/* Select XOSC as PLL0 source and PHI1 output of PLL0 as PLL1 source */
   ; SIU.SYSDIV.B.PLL0SEL=0;
   ; SIU.SYSDIV.B.PLL1SEL=1;
   PER.Set.Field ANC:0xC3F909A0 %LONG 0x10000000 0.
   PER.Set.Field ANC:0xC3F909A0 %LONG 0x04000000 1.
   
   ;/* Program PLL0:
   ; # RFDPHI1 = 0x0C
   ; # RFDPHI = 0x03
   ; # PREDIV = 0x5 
   ; # MFD = 0x48
   ;*/    
   ;PLLDIG.PLL0DV.R=0x60035048;
   per.set ANC:0xFFE74008 %LONG   0x60035048 
   
   ;/* Turn on PLL0 and wait for lock */
   ; PLLDIG.PLL0CR.R=0x0000300;
   ; while (PLLDIG.PLL0SR.B.LOCK==0) {};
   per.set ANC:0xFFE74000 %LONG   0x0000300 
   wait (Data.Long(ANC:0xFFE74004)&0x00000004)==0x4 
   
   ;/* Program PLL1:
   ; # RFDPHI = 0x02
   ; # MFD = 0x16
   ;*/    
   ; PLLDIG.PLL1DV.R=0x00020016;
   per.set ANC:0xFFE74028 %LONG   0x00020016 

   ; /* Turn on PLL1 and wait for lock */
   ; PLLDIG.PLL1CR.R=0x0000300;
   ; while (PLLDIG.PLL1SR.B.LOCK==0) {};
   per.set ANC:0xFFE74020 %LONG   0x0000300 
   wait (Data.Long(ANC:0xFFE74024)&0x00000004)==0x4 
    
   ;/* Select clock dividers and sources */    
   ; SIU.SYSDIV.R=0x05002110;  
   PER.Set. ANC:0xC3F909A0 %LONG   0x05002110
   
   SYStem.BdmClock 20.0MHz
    
)

;**************************************
; RAM initialization
;**************************************
print "Starting RAM initialization:"
if &optimizeRAM==1
(    
    ; Initialize internal SRAM (16KB)
    Data.Set A:0x40000000--0x40003FFF %Quad 0
)
else
(
   ; Initialize internal SRAM (384kb)
    Data.Set A:0x40000000--0x4007ffff %Quad 0
)

; setup MMU for FLASH, RAM and peripheral access
MMU.Set TLB1 0. 0xC0000580 0xFFE0000A 0xFFE0003F
MMU.Set TLB1 1. 0xC0000700 0x00000000 0x0000003F
MMU.Set TLB1 2. 0xC0000700 0x20000000 0x0000003F
MMU.Set TLB1 3. 0xC0000400 0x40000028 0x4000003F
MMU.Set TLB1 4. 0xC0000400 0x40040000 0x4004003F
MMU.Set TLB1 5. 0xC0000580 0xC3E0000A 0xC3E0003F

;========================================================================
; Flash declaration

  FLASH.RESet
  
; Low address space
  FLASH.Create 1. 0x00000000--0x0000FFFF TARGET Quad 0x0000    ; Low Flash Block 0 (Data Flash)
  FLASH.Create 1. 0x00010000--0x0001FFFF TARGET Quad 0x0001    ; Low Flash Block 1 (Data Flash)
  
  ; Mid address space
  FLASH.Create 2. 0x00020000--0x0002FFFF TARGET Quad 0x0100    ; Mid Flash Block 0 (Data Flash)
  FLASH.Create 2. 0x00030000--0x0003FFFF TARGET Quad 0x0101    ; Mid Flash Block 1 (Data Flash)
  
  ; High address space
  FLASH.Create 3. 0x00600000--0x00603FFF NOP    Quad 0x0200    ; High Flash Block 0 (CSE)
  FLASH.Create 3. 0x00604000--0x00607FFF NOP    Quad 0x0201    ; High Flash Block 1 (CSE)
  
  ; Large address space
  FLASH.Create 4. 0x00800000--0x0083FFFF TARGET Quad 0x0300    ; Large Flash Block 0 (Boot)
  FLASH.Create 4. 0x00840000--0x0087FFFF TARGET Quad 0x0301    ; Large Flash Block 1 (Calibration)
  FLASH.Create 4. 0x00880000--0x008BFFFF TARGET Quad 0x0302    ; Large Flash Block 2 (Calibration)
  FLASH.Create 4. 0x008C0000--0x008FFFFF TARGET Quad 0x0303    ; Large Flash Block 3 (Calibration)
  FLASH.Create 4. 0x00900000--0x0093FFFF TARGET Quad 0x0304    ; Large Flash Block 4 (Calibration)
  FLASH.Create 4. 0x00940000--0x0097FFFF TARGET Quad 0x0305    ; Large Flash Block 5
  FLASH.Create 4. 0x00980000--0x009BFFFF TARGET Quad 0x0306    ; Large Flash Block 6
  FLASH.Create 4. 0x009C0000--0x009FFFFF TARGET Quad 0x0307    ; Large Flash Block 7
  FLASH.Create 4. 0x00A00000--0x00A3FFFF TARGET Quad 0x0308    ; Large Flash Block 8
  FLASH.Create 4. 0x00A40000--0x00A7FFFF TARGET Quad 0x0309    ; Large Flash Block 9
  FLASH.Create 4. 0x00A80000--0x00ABFFFF TARGET Quad 0x030A    ; Large Flash Block 10
  FLASH.Create 4. 0x00AC0000--0x00AFFFFF TARGET Quad 0x030B    ; Large Flash Block 11
  FLASH.Create 4. 0x00B00000--0x00B3FFFF TARGET Quad 0x030C    ; Large Flash Block 12
  FLASH.Create 4. 0x00B40000--0x00B7FFFF TARGET Quad 0x030D    ; Large Flash Block 13
  FLASH.Create 4. 0x00B80000--0x00BBFFFF TARGET Quad 0x030E    ; Large Flash Block 14
  FLASH.Create 4. 0x00BC0000--0x00BFFFFF TARGET Quad 0x030F    ; Large Flash Block 15
  FLASH.Create 4. 0x00C00000--0x00C3FFFF TARGET Quad 0x0310    ; Large Flash Block 16
  FLASH.Create 4. 0x00C40000--0x00C7FFFF TARGET Quad 0x0311    ; Large Flash Block 17
  FLASH.Create 4. 0x00C80000--0x00CBFFFF TARGET Quad 0x0312    ; Large Flash Block 18
  FLASH.Create 4. 0x00CC0000--0x00CFFFFF TARGET Quad 0x0313    ; Large Flash Block 19
  FLASH.Create 4. 0x00D00000--0x00D3FFFF TARGET Quad 0x0314    ; Large Flash Block 20
  FLASH.Create 4. 0x00D40000--0x00D7FFFF TARGET Quad 0x0315    ; Large Flash Block 21
  FLASH.Create 4. 0x00D80000--0x00DBFFFF TARGET Quad 0x0316    ; Large Flash Block 22
  FLASH.Create 4. 0x00DC0000--0x00DFFFFF TARGET Quad 0x0317    ; Large Flash Block 23
  FLASH.Create 4. 0x00E00000--0x00E3FFFF TARGET Quad 0x0318    ; Large Flash Block 24
  FLASH.Create 4. 0x00E40000--0x00E7FFFF TARGET Quad 0x0319    ; Large Flash Block 25
  FLASH.Create 4. 0x00E80000--0x00EBFFFF TARGET Quad 0x031A    ; Large Flash Block 26
  FLASH.Create 4. 0x00EC0000--0x00EFFFFF TARGET Quad 0x031B    ; Large Flash Block 27
  FLASH.Create 4. 0x00F00000--0x00F3FFFF TARGET Quad 0x031C    ; Large Flash Block 28
  FLASH.Create 4. 0x00F40000--0x00F7FFFF TARGET Quad 0x031D    ; Large Flash Block 29
  FLASH.Create 4. 0x00F80000--0x00FBFFFF TARGET Quad 0x031E    ; Large Flash Block 30
  FLASH.Create 4. 0x00FC0000--0x00FFFFFF TARGET Quad 0x031F    ; Large Flash Block 31
  
  ; UTEST address space
  FLASH.Create 6. 0x00400000--0x00403FFF TARGET Quad 0x0500 /OTP /INFO "UTEST"

  ; Overlay enabled mapping
  FLASH.CreateALIAS 0x08A00000--0x08FFFFFF 0x00A00000   ; Small & medium flash blocks
  FLASH.CreateALIAS 0x09000000--0x09FFFFFF 0x01000000   ; Large flash blocks

  FLASH.TARGET E:0x40000000 E:0x40002000 0x1000 ~~/demo/powerpc/flash/quad/c55fm5777c.bin /STACKSIZE 0x0200 /DualPort



;========================================================================
; Flash programming example
;
; Flash programming speed is about three times faster when memory class E:
; is used for data buffer (DUALPORT memory access). For DUALPORT access it
; is required to setup MemAccess NEXUS for both, NEXUS and JTAG debugger.

print "Flash Programming start"

   FLASH.Auto ALL
   Data.Load.Elf ../bin/PlatformIntegration.elf /Quad 
   FLASH.Auto OFF
print "Flash Programming finish"
;set JTAG clock back to default
SYStem.BdmClock 4MHz


;========================================================================

;Batch Job for debugging in internal FLASH

;clear the TRACE32 screen
WinCLEAR

; reset
SYStem.RESet

;select CPU type
SYSTem.CPU MPC5777C
;set JTAG clock back to default
SYStem.BdmClock 4MHz

;inform debugger about FLASH memory address range
Map.BOnchip 0x00000000--0x01000000
SYStem.OPTION DUALPORT ON
SYStem.CONFIG.CORE 1. 1. 
wait 1s
;restart the CPU with debug mode enable
System.Option.Slowreset ON
SYSTem.attach



; setup MMU for FLASH, RAM and peripheral access
MMU.Set TLB1 0. 0xC0000580 0xFFE0000A 0xFFE0003F
MMU.Set TLB1 1. 0xC0000700 0x00000020 0x0000003F
MMU.Set TLB1 2. 0xC0000700 0x20000000 0x0000003F
MMU.Set TLB1 3. 0xC0000400 0x40000028 0x4000003F
MMU.Set TLB1 4. 0xC0000400 0x40040000 0x4004003F
MMU.Set TLB1 5. 0xC0000580 0xC3E0000A 0xC3E0003F


;DisableWatchdog:
d.s 0xfff34010 %long 0xc520
d.s 0xfff34010 %long 0xd928
d.s 0xfff34000 %long 0xFF00010A

d.s 0xfff38010 %long 0xc520
d.s 0xfff38010 %long 0xd928
d.s 0xfff38000 %long 0xFF00010A


;load program (if FLASH selected than load only debug symbols)

Data.Load.Elf ../bin/PlatformIntegration.elf  /GLOBTYPES /NOCODE

;display source
Data.List

; Include test execution, timeout and dump processing


;clear all breakpoints
Break.delete /ALL

print "[BEART]: Setting PC to _start"
R.S IP _start


;set breakpoint at main

Break.SET main
;Go

;Executing test application with timeout " 60 " s"
;wait for timeout or until the emulation is stopped
;wait !run() 60.s

;stop the emulation if it is runnning
IF run()
    (
        Break
    )

;quit

