;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; CPT2 data over STM for TDA4VM
; 04 Feb 23 - Richard Woodruff - Change to central for all CPUs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; INPUT PARAMS: {string: ONCHIP or OFFCHIP , string: 200MHz or 250MHz} 

; Step #1 Connect to a core (CM3/CR5x/A72/C7/C6/ICSS)
; Step #2 Clocks for IPs to trace need to be enabled
; Step #3 Configure below for desired probes
;      - There are 7 aggregators which host probe groups.
;           -- Each has a TraceID
;      - Each probe can emit (latency, throughput, or Transaction) data
;           -- The STM2 MasterID is the probe number in the aggregator (M8 packet)
;           -- The Function determines the function (C8 packet)
; Step #4 If streaming into onchip (TBR) uncomment &TBR corresponding for the source in use

PRIVATE &parameters
ENTRY %LINE &parameters
&parameters=STRing.UPpeR("&parameters")

IF CPUFAMILY()=="PRU"
  ENDDO

LOCAL &param_offchip &contype &param_gtcspeed
&param_offchip=(STRing.SCAN("&parameters","OFFCHIP",0)!=-1)
IF &param_offchip
(
  IF Analyzer()
    &contype="Analyzer"
  ELSE IF CAnalyzer()
    &contype="CAnalyzer"
  ELSE
    &contype="Onchip2"
  &path=OS.PPD()
  DO "&path/../x_gel_to_cmm_public/j7es_offchip_trace.cmm"
)
ELSE
  &contype="Onchip2"

&param_gtcspeed=(STRing.SCAN("&parameters","250MHz",0)!=-1)  
IF &param_gtcspeed
  &GTCSPEED="250MHz"  ; AVV/Bare Metal uses 250MHz
ELSE
  &GTCSPEED="200MHz"  ; SDK software uses 200MHz

;     TraceID        Agg        TBR    APB:Agg base ; ProbeBase: AggBase+0x20000 + Prob#x0x1000
;STM2.TraceID 0x50 ; * SOC    : TBR2 - 0x9E100000
        ;&TBR="TBR2"  ; <-- Step #4
;STM2.TraceID 0x51 ; * MCU    : TBR3 - 0x9E020000         ..
;STM2.TraceID 0x52 ; * AC     : TBR3 - 0x9E140000         ..     
;STM2.TraceID 0x53 ; * HC     : TBR3 - 0x9E180000         ..
        ;&TBR="TBR3"  ; <-- Step #4
;STM2.TraceID 0x54 ; * MSMC_0 : TBR1 - 0x90140000         ..
;STM2.TraceID 0x55 ; * MSMC_1 : TBR1 - 0x90180000         ..
;STM2.TraceID 0x56 ; * C7     : TBR1 - 0x91440000         ..
        &TBR="TBR1"  ; <-- Step #4

STM2Trace.METHOD.&contype
IF !&param_offchip
  onchip2.TraceCONNECT.&TBR

IF (CPUFAMILY()=="ARM")&&!CPUIS("*CM3*")
  ETM.OFF  ; for demo keep it simple and suppress processor trace
ELSE IF CPUIS("*C7*")
  TRC.OFF
ELSE IF CPUIS("*C6*")
  AET.OFF

STM2.TraceID 0x50--0x56
STM2.TimeStampCLOCK &GTCSPEED
STM2.TimeStamps.ON

;**********************************************
; Run the following sequence to "switch" to
; a different aggregator after trace has been
; captured:
; 1) STM2.TraceID <new_id>
; 2) STM2T.flowprocess
; 3) CPTT.flowprocess
;**********************************************

; View STP trace stream
STM2T.L /track

Data.set eaxi:0x00A90000 %LE %Long 0x1       ; ensure GTC on for debug timestamps
CPTracerTrace.CLOCK &GTCSPEED

;****************************************************
; The following channels are available for the
; CPTracerTrace.List window:
; - probe:        Does not work without correct STP master packets
; - cycle:        Message type
; - address:      Address of transaction
; - trans.<xxx>:  Transaction specific fields
; - thru.<xxx>:   Throughput specific fields
; - lat.<xxx>:    Latency specific fields
;*****************************************************
CPTracerTrace.method.&contype
IF !&param_offchip
  CPTracerTrace.TraceCONNECT.&TBR
CPTT.L cycle thru.totalbytes thru.totalmatched time /track
CPTT.profilechart.counter thru.totalbytes
IF VERSION.BUILD()>129006.
(
  CPTT.DRAW.channel.THRU.totalbytes /track                ; raw bytecount graph per sample
 ;CPTT.profilechart.counter %ZeroUp.Long thru.totalbytes  ; frequency chart of BW: deltaBytes/deltatime
)
CPTT

;**************************************************
; Setup SystemTrace
;**************************************************
STM2.state

; Open CPTracer control panel
CPT.state

cptracer.MSMC1.on
cptracer.MSMC1.EMIF0_I.OPeration.THRoUghput
CPTracerTrace.OFF 

; open a dump window into DDR to generate transactions
data.dump eaxi:0x80000000

; ARM and disarm test to see cptt used indicator move.
; CPTracerTrace.arm

; Note: Apparent init issue. Sometimes when starting at Continuous decode is not clean.
tpiu.PortMode.Wrapped
tpiu.portmode.Continuous

enddo
