// File: PY32F002Axx_DBGMCU.ini
// Version: 1.0.0


/* variable to hold register values */
define unsigned long DbgMCU_CR;

// <h> Debug MCU configuration register (DBGMCU_CR)
//                                   <i> Reserved bits must be kept at reset value
//   <o.6..7> TRACE_MODE             <i> Trace Mode
//      <0=> Asynchronous
//      <1=> Synchronous: TRACEDATA Size 1
//      <2=> Synchronous: TRACEDATA Size 2
//      <3=> Synchronous: TRACEDATA Size 4
//   <o.5>  TRACE_IOEN               <i> Trace I/O Enable
//   <o.2>  DBG_STANDBY              <i> Debug Standby Mode
//   <o.1>  DBG_STOP                 <i> Debug Stop Mode
//   <o.0>  DBG_SLEEP                <i> Debug Sleep Mode
// </h>
DbgMCU_CR = 0x00000007;

// <<< end of configuration section >>>

/*----------------------------------------------------------------------------
  Setup_TracePins()  configure the used trace pins
 *----------------------------------------------------------------------------*/
FUNC void Setup_TracePins (unsigned char trace_mode) {

  if (trace_mode == 0) {           /* asynchronous mode */
          /* configure SWO (PB3) */
          _WDWORD(0x40023830, ( _RDWORD(0x40023830)                | 0x00000002) );  // RCC_AHB1ENR: IO port B clock enable
          _WDWORD(0x40020400, ((_RDWORD(0x40020400) & ~0x000000C0) | 0x00000080) );  // GPIOx_MODER:   Set Mode (Alternate Function)
          _WDWORD(0x40020408, ((_RDWORD(0x40020408)              ) | 0x000000C0) );  // GPIOx_OSPEEDR: Set Speed (Very High Speed)
          _WDWORD(0x4002040C, ((_RDWORD(0x4002040C) & ~0x000000C0)             ) );  // GPIOx_PUPDR:   Set I/O to no pull-up/pull-down
          _WDWORD(0x40020420, ((_RDWORD(0x40020420) & ~0x0000F000)             ) );  // GPIOx_AFRL:    Alternate Function to AF0
  }
  else {                           /* synchronous mode */
          /* configure TRACECLK (PE2) */
          _WDWORD(0x40023830, ( _RDWORD(0x40023830)                | 0x00000010) );  // RCC_AHB1ENR: IO port E clock enable
          _WDWORD(0x40021000, ((_RDWORD(0x40021000) & ~0x00000030) | 0x00000020) );  // GPIOx_MODER:   Set Mode (Alternate Function)
          _WDWORD(0x40021008, ((_RDWORD(0x40021008)              ) | 0x00000030) );  // GPIOx_OSPEEDR: Set Speed (Very High Speed)
          _WDWORD(0x4002100C, ((_RDWORD(0x4002100C) & ~0x00000030)             ) );  // GPIOx_PUPDR:   Set I/O to no pull-up/pull-down
          _WDWORD(0x40021020, ((_RDWORD(0x40021020) & ~0x00000F00)             ) );  // GPIOx_AFRL:    Alternate Function to AF0

    switch (trace_mode) {
      case 3:                      /* TRACEDATA[3..2] */
        /* configure TRACED3 */
          /* configure TRACED3 (PE6) */
       // _WDWORD(0x40023830, ( _RDWORD(0x40023830)                | 0x00000010) );  // RCC_AHB1ENR: IO port E clock enable
          _WDWORD(0x40021000, ((_RDWORD(0x40021000) & ~0x00003000) | 0x00002000) );  // GPIOx_MODER:   Set Mode (Alternate Function)
          _WDWORD(0x40021008, ((_RDWORD(0x40021008)              ) | 0x00003000) );  // GPIOx_OSPEEDR: Set Speed (Very High Speed)
          _WDWORD(0x4002100C, ((_RDWORD(0x4002100C) & ~0x00003000)             ) );  // GPIOx_PUPDR:   Set I/O to no pull-up/pull-down
          _WDWORD(0x40021020, ((_RDWORD(0x40021020) & ~0x0F000000)             ) );  // GPIOx_AFRL:    Alternate Function to AF0

        /* configure TRACED2 */
          /* configure TRACED2 (PE5) */
       // _WDWORD(0x40023830, ( _RDWORD(0x40023830)                | 0x00000010) );  // RCC_AHB1ENR: IO port E clock enable
          _WDWORD(0x40021000, ((_RDWORD(0x40021000) & ~0x00000C00) | 0x00000800) );  // GPIOx_MODER:   Set Mode (Alternate Function)
          _WDWORD(0x40021008, ((_RDWORD(0x40021008)              ) | 0x00000C00) );  // GPIOx_OSPEEDR: Set Speed (Very High Speed)
          _WDWORD(0x4002100C, ((_RDWORD(0x4002100C) & ~0x00000C00)             ) );  // GPIOx_PUPDR:   Set I/O to no pull-up/pull-down
          _WDWORD(0x40021020, ((_RDWORD(0x40021020) & ~0x00F00000)             ) );  // GPIOx_AFRL:    Alternate Function to AF0

      case 2:                      /* TRACEDATA[1] */
        /* configure TRACED1 */
          /* configure TRACED1 (PE4) */
       // _WDWORD(0x40023830, ( _RDWORD(0x40023830)                | 0x00000010) );  // RCC_AHB1ENR: IO port E clock enable
          _WDWORD(0x40021000, ((_RDWORD(0x40021000) & ~0x00000300) | 0x00000200) );  // GPIOx_MODER:   Set Mode (Alternate Function)
          _WDWORD(0x40021008, ((_RDWORD(0x40021008)              ) | 0x00000300) );  // GPIOx_OSPEEDR: Set Speed (Very High Speed)
          _WDWORD(0x4002100C, ((_RDWORD(0x4002100C) & ~0x00000300)             ) );  // GPIOx_PUPDR:   Set I/O to no pull-up/pull-down
          _WDWORD(0x40021020, ((_RDWORD(0x40021020) & ~0x000F0000)             ) );  // GPIOx_AFRL:    Alternate Function to AF0

      case 1:                      /* TRACEDATA[0] */
        /* configure TRACED0 */
          /* configure TRACED0 (PE3) */
       // _WDWORD(0x40023830, ( _RDWORD(0x40023830)                | 0x00000010) );  // RCC_AHB1ENR: IO port E clock enable
          _WDWORD(0x40021000, ((_RDWORD(0x40021000) & ~0x000000C0) | 0x00000080) );  // GPIOx_MODER:   Set Mode (Alternate Function)
          _WDWORD(0x40021008, ((_RDWORD(0x40021008)              ) | 0x000000C0) );  // GPIOx_OSPEEDR: Set Speed (Very High Speed)
          _WDWORD(0x4002100C, ((_RDWORD(0x4002100C) & ~0x000000C0)             ) );  // GPIOx_PUPDR:   Set I/O to no pull-up/pull-down
          _WDWORD(0x40021020, ((_RDWORD(0x40021020) & ~0x0000F000)             ) );  // GPIOx_AFRL:    Alternate Function to AF0
        break;
    }
  }

}

/*----------------------------------------------------------------------------
  Setup_DBGMCU()  configure DBGMCU registers
 *----------------------------------------------------------------------------*/
FUNC void Setup_DBGMCU (void) {

  if (DBGMCU_CR & (1 << 5)){
    Setup_TracePins (((DbgMCU_CR >> 6) & 3));
  }

  _WDWORD(0xE0042004, DbgMCU_CR);                                // Set DBGMCU_CR
  _WDWORD(0xE0042008, DbgMCU_APB1_Fz);                           // Set DBGMCU_APB1_FZ
  _WDWORD(0xE004200C, DbgMCU_APB2_Fz);                           // Set DBGMCU_APB2_FZ
}


/*----------------------------------------------------------------------------
  OnResetExec()  Executed after reset via uVision's 'Reset'-button
 *----------------------------------------------------------------------------*/
FUNC void OnResetExec (void) {
  Setup_DBGMCU();
}

Setup_DBGMCU();                                           // Debugger Setup