1;*******************************************************************************
2;   Copyright 2023 NXP                                                         *
3;   SPDX-License-Identifier: Apache-2.0                                        *
4;                                                                              *
5;   Lauterbach Trace32 start-up script for S32K146 / Cortex-M4F                *
6;                                                                              *
7;   Parameters:                                                                *
8;   - command     operation to execute                                         *
9;                 valid values: flash, debug                                   *
10;   - elfFile     filepath of ELF to load                                      *
11;   - loadTo      if "flash", the application will be downloaded to SoC        *
12;                 program flash by a flash programming routine; if "sram" it   *
13;                 will be downloaded to SoC SRAM.                              *
14;                 valid values: flash, sram                                    *
15;                 default: flash                                               *
16;   - eraseFlash  if set to "yes", the whole content in Flash device will be   *
17;                 erased before the application is downloaded to either Flash  *
18;                 or SRAM. This routine takes time to execute                  *
19;                 default: "no"                                                *
20;   - verifyFlash if set to "yes", verify after program application to Flash   *
21;                 default: "no"                                                *
22;*******************************************************************************
23
24ENTRY %LINE &args
25
26&command=STRing.SCANAndExtract("&args","command=","")
27&elfFile=STRing.SCANAndExtract("&args","elfFile=","")
28&loadTo=STRing.SCANAndExtract("&args","loadTo=","flash")
29&eraseFlash=STRing.SCANAndExtract("&args","eraseFlash=","no")
30&verifyFlash=STRing.SCANAndExtract("&args","verifyFlash=","no")
31
32IF ("&elfFile"=="")
33(
34  AREA.view
35  PRINT %ERROR "Missing ELF file path"
36  PLIST
37  STOP
38  ENDDO
39)
40
41; Initialize debugger
42RESet
43SYStem.RESet
44SYStem.CPU S32K146
45SYStem.CONFIG.DEBUGPORTTYPE SWD
46SYStem.Option DUALPORT ON
47SYStem.MemAccess DAP
48SYStem.JtagClock CTCK 10MHz
49Trace.DISable
50SYStem.Up
51
52GOSUB DisableBootrom
53
54; Only declares flash, does not execute flash programming
55DO ~~/demo/arm/flash/s32k.cmm PREPAREONLY
56
57IF ("&eraseFlash"=="yes")
58(
59  FLASH.Erase ALL
60)
61
62IF ("&loadTo"=="flash")
63(
64  ; Switch target flash to reprogramming state, erase virtual flash programming memory,
65  ; all target non-empty flash sectors are marked as pending, to be reprogrammed.
66  FLASH.ReProgram ALL /Erase
67
68  ; Write contents of the file to virtual Flash programming memory
69  Data.LOAD.Elf &elfFile
70
71  ; Program only changed sectors to target flash and erase obsolete code
72  FLASH.ReProgram off
73
74  IF ("&verifyFlash"=="yes")
75  (
76    Data.LOAD.Elf &elfFile /DIFF
77
78    IF FOUND()
79    (
80      AREA.view
81      PRINT %ERROR "Failed to download the code to flash"
82      Data.LOAD.Elf &elfFile /ComPare
83      ENDDO
84    )
85  )
86
87  ; Reset the processor again
88  SYStem.Up
89  GOSUB DisableBootrom
90)
91ELSE
92(
93  ; Load program to SRAM
94  Data.LOAD.Elf &elfFile
95)
96
97IF ("&command"=="flash")
98(
99  ; Execute the application and quit
100  Go
101  QUIT
102)
103ELSE IF ("&command"=="debug")
104(
105  ; Setup minimal debug environment
106  WinCLEAR
107  SETUP.Var.%SpotLight
108  WinPOS 0. 0. 120. 30.
109  List.auto
110  WinPOS 125. 0. 80. 10.
111  Frame.view
112  WinPOS 125. 18.
113  Register.view /SpotLight
114)
115ELSE
116(
117  AREA.view
118  PRINT %ERROR "Invalid command: &command"
119)
120
121ENDDO
122
123DisableBootrom:
124(
125  Data.Set SD:0x4007F010 %LE %Long 0x6
126  Data.Set SD:0x4007F014 %LE %Long 0x0
127  RETURN
128)
129