Difference between revisions of "InitTarget()"

From SEGGER Wiki
Jump to: navigation, search
(Created page with "This function has "extending nature" meaning if it is in a script file, it is called '''before''' the JTAG/SWD/... and CPU auto-detection mechanisms take place.<br> This gives...")
 
(Defining the JTAG chain)
Line 15: Line 15:
 
* Global DLL variable "CPU" MUST be set when implementing this function, so the DLL knows which CPU module to use internally.
 
* Global DLL variable "CPU" MUST be set when implementing this function, so the DLL knows which CPU module to use internally.
   
==Defining the JTAG chain==
+
==JTAG specific connect==
 
<source lang="c">
 
<source lang="c">
 
/*********************************************************************
 
/*********************************************************************

Revision as of 11:41, 22 April 2024

This function has "extending nature" meaning if it is in a script file, it is called before the JTAG/SWD/... and CPU auto-detection mechanisms take place.
This gives the user a chance to do chip-specific init that needs to be done before the core itself is available for debug.
Example devices are MCUs from TI which have a so-called ICEPick JTAG unit on them that needs to be configured, before the actual core is accessible (or even visible) via JTAG.
If this function is not in a script file or empty (simply returns 0), this has no effect on the connect sequence of J-Link.


_

_

Prototype

int InitTarget(void);

Notes and Limitations

If target interface JTAG is used: JTAG chain has to be specified manually before leaving this function (meaning all devices and their TAP IDs have to be specified by the user). Also appropriate JTAG TAP number to communicate with during the debug session has to be manually specified in this function.

  • MUST NOT use any MEM_ API functions
  • Global DLL variable "CPU" MUST be set when implementing this function, so the DLL knows which CPU module to use internally.

JTAG specific connect

/*********************************************************************
*
*       Constants, fixed
*
**********************************************************************
*/
//
// Bits & Shifts (ARM)
//
__constant U32 _DP_CTRL_STAT_BIT_DBGPWRUPREQ     = (1 << 30);
__constant U32 _DP_CTRL_STAT_BIT_SYSPWRUPREQ     = (1 << 28);
__constant U32 _DP_CTRL_STAT_BIT_STICKYERR       = (1 <<  5);

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

/*********************************************************************
*
*       _InitDAP()
*
*  Function description
*    Initializes DAP, so JLINK_CORESIGHT_ functions can be used.
*/
static int _InitDAP(void) {
  U32 v;
  int r;
  int t;

  JLINK_SYS_Report("-- Initializing DAP...");
  //
  // Clear sticky error flags and power up DAP
  //
  v  = _DP_CTRL_STAT_BIT_DBGPWRUPREQ | _DP_CTRL_STAT_BIT_SYSPWRUPREQ;
  if (JLINK_ActiveTIF == JLINK_TIF_JTAG) {
    v |= _DP_CTRL_STAT_BIT_STICKYERR;
  } else {
    JLINK_CORESIGHT_WriteDP(JLINK_CORESIGHT_DP_REG_ABORT, 0x1E);
  }
  JLINK_CORESIGHT_WriteDP(JLINK_CORESIGHT_DP_REG_CTRL_STAT, v);
  //
  // Wait for DAP power up to complete
  //
  r = -1;
  t = JLINK_GetTime() + 500;
  do {
    r = JLINK_CORESIGHT_ReadDP(JLINK_CORESIGHT_DP_REG_CTRL_STAT);
    if (r == -1) {                    // Any error occurred while reading from the DP? => We are done.
      break;
    }
    v = r;
    if (v & (0xF << 28)) {            // CSYSPWRUPACK and CDBGPWRUPACK set (both ports completely powered)? => We are done.
      r = 0;
      break;
    }
    if ((t - JLINK_GetTime()) <= 0) { // Timeout reached? => We are done.
      break;
    }
  } while(1);
  if (r < 0) {
    return -1;
  }
  //
  // Configure AP
  //
  v = (0 << 4) | (0 << 24);                                    // Select AP[0] (AHB-AP) bank 0
  JLINK_CORESIGHT_WriteDP(JLINK_CORESIGHT_DP_REG_SELECT,  v);
  v = (0 << 4) | (1 << 24) | (1 << 25) | (1 << 29) | (2 << 0); // NO Auto-increment, Private access, HMASTER = DEBUG, Access size: word
  r = JLINK_CORESIGHT_WriteAP(JLINK_CORESIGHT_AP_REG_CTRL, v);
  if (r < 0) {
    return -1;
  }
  JLINK_SYS_Report("-- DAP initialized successfully.");
  return 0;
}

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       InitTarget()
*
*  Function description
*    If present, called right before performing generic connect sequence.
*    Usually used for targets which need a special connect sequence.
*    E.g.: TI devices with ICEPick TAP on them where core TAP needs to be enabled via specific ICEPick sequences first
*
*  Return value
*    >= 0:  O.K.
*     < 0:  Error
*
*  Notes
*    (1) Must not use high-level API functions like JLINK_MEM_ etc.
*    (2) For target interface JTAG, this device has to setup the JTAG chain + JTAG TAP Ids.
*/
int InitTarget(void) {
  int r;
  U32 v;
  //
  // Select specific device in JTAG Chain and init DAP.
  // Does not hurt to do this for SWD as well.
  // If SWD is the target interface: Puts out the JTAG->SWD switching sequence.
  //
  JLINK_SYS_Report("-- Setting up JTAG chain...");
  //
  // Example:
  //   2 devices in chain with IRLen of 4 for each device:
  //     1) Connect to first dev. in chain:
  //r = JLINK_CORESIGHT_Configure("IRPre=0;DRPre=0;IRPost=4;DRPost=1;IRLenDevice=4");
  //     2) Connect to 2nd/last dev. in chain:
  //r = JLINK_CORESIGHT_Configure("IRPre=4;DRPre=1;IRPost=0;DRPost=0;IRLenDevice=4");
  //
  r = JLINK_CORESIGHT_Configure("IRPre=<IRPre>;DRPre=<DRPre>;IRPost=<IRPost>;DRPost=<DRPost>;IRLenDevice=<IRLenDevice>"); // Fill out with correct values for your specific device.
  if (r < 0) {
    JLINK_SYS_Report("-- Failed to setup JTAG Chain.");
    return -1;
  }
  JLINK_SYS_Report("-- Done.");
  r = _InitDAP();
  if (r < 0) {
    return -1;
  }
  //
  // Enter device specific handling here.
  //
  // ...
  //
  return 0;
}