; ---------------------------------------------------------------------------
; prepare_itm.cmm
; ---------------------------------------------------------------------------
  PMACRO.EXPLICIT

  ; show path and name of this script (present practice file)
  ECHO ""
  ECHO %COLOR.BLUE "### " OS.PPF() " ###"

; ---------------------------------------------------------------------------
  IF COMBIPROBE()||UTRACE()
  (
    SYStem.CONFIG.CONNECTOR MIPI20T
  )

  SYStem.MemAccess DAP
  SYStem.JtagClock CTCK 10MHz

; ---------------------------------------------------------------------------
; disable trace on Cortex-M0 to avoid conflicts
  InterCom M0 Trace.METHOD NONE

; ---------------------------------------------------------------------------
; prepare some generic trace settings
  Trace.DISable
  Trace.METHOD CAnalyzer
  Trace.AutoInit ON
  TRACE.AutoArm ON
  Trace.CLOCK 160.MHz
  Trace.TraceCLOCK 10.MHz

; ---------------------------------------------------------------------------
; prepare ITM
  ITM.DataTrace Data
 ;ITM.DataTrace CorrelatedData
  ITM.PCSampler OFF
 ;ITM.PCSampler 1/256
  ITM.InterruptTrace OFF
  ITM.CycleAccurate ON
  ITM.CLOCK 160MHz
  ITM.ON

 ;# Example:
 ;# ITM ports 0.-31.(dec) = 0y00000-0y11111(binary)
 ;# ITM port 2.       = 0y00010
 ;# ITM port 3.       = 0y00011
 ;# ITM port 2. or 3. = 0y0001X

 ; ITM.PortFilter 0y00010
 ; ITMTrace.FLOWPROCESS

; ---------------------------------------------------------------------------
; prepare ETM
  ETM.Trace OFF
  ETM.OFF

; ---------------------------------------------------------------------------
; prepare trace-clock
  DO "prepare_traceclock.cmm" "SWV"

  GOSUB trigger_via_cti CATRIGGER

  CAnalyzer.AutoFocus

//ITMTrace.STATistic.distance
  ITMTrace.List DEFault TIme.REF TIme.Zero /Track

  Trace

  ENDDO


; --------------------------------------------------------------------------------
trigger_via_cti:
  PRIVATE &trigger &ppd
  ENTRY &trigger

  &ppd=OS.PresentPracticeDirectory()

  ; Normally, trigger signals from the ETM are routed via the ATB, together
  ; with the trace data. However, this does not seem to work on this chip, so
  ; instead we route the trigger signal via the CoreSight cross trigger.

  ; CM4_CM4CTI_CTICONTROL: set GLBEN
  Data.Set AD:0xE0042000 %Long 1.<<0.
  ; CM4_CM4CTI_CTIINEN7: route ETM trigger output to CTM channel 3
  Data.Set AD:0xE004203C %Long 1.<<3.

  ; CM4_TRCCTI_CTICONTROL: set GLBEN
  Data.Set AD:0xE0080000 %Long 1.<<0.
  ; CM4_TRCCTI_CTIOUTEN3: route CTM channel 3 to TPIU trigger input
  Data.Set AD:0xE00800AC %Long 1.<<3.

  ; The Cortex-M TPIU lacks an acknowledge channel to the CTI. This means that
  ; the trigger channel never gets cleared automatically. Therefore, we clear
  ; the trigger manually each time we detect a trigger. This is somewhat
  ; fragile, but works for simple use cases.

  ; Reset the trigger channel from CM4_TRCCTI to TPIU
  ; CM4_TRCCTI_CTIINTACK: acknowledge TPIU trigger
  Data.Set ESD:0xE0080010 %Long 1.<<3.

  RETURN


