Difference between revisions of "InitTarget()"

From SEGGER Wiki
Jump to: navigation, search
(Defining the JTAG chain)
(JTAG specific connect)
Line 16: Line 16:
   
 
==JTAG specific connect==
 
==JTAG specific connect==
  +
<div class="toccolours mw-collapsible mw-collapsed" style="overflow:auto;">
<source lang="c">
 
  +
<div style="font-weight:bold;line-height:1.6;">Example code</div>
  +
<div class="mw-collapsible-content">
  +
<syntaxhighlight lang="c" class="collapsible">
  +
/***********************************************************************
  +
* SEGGER Microcontroller GmbH *
  +
* The Embedded Experts *
  +
************************************************************************
  +
* *
  +
* (c) SEGGER Microcontroller GmbH *
  +
* All rights reserved *
  +
* www.segger.com *
  +
* *
  +
************************************************************************
  +
* *
  +
************************************************************************
  +
* *
  +
* *
  +
* Licensing terms *
  +
* *
  +
* The use in source and binary forms, with or without modification, *
  +
* is permitted for internal use only. The redistribution to any *
  +
* third party is prohibited. *
  +
* *
  +
* *
  +
* THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER "AS IS" AND ANY *
  +
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
  +
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR *
  +
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE *
  +
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, *
  +
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
  +
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
  +
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY *
  +
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
  +
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE *
  +
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH *
  +
* DAMAGE. *
  +
* *
  +
************************************************************************
  +
  +
-------------------------- END-OF-HEADER -----------------------------
  +
  +
Purpose: Template showing basic device identification.
  +
Literature:
  +
[1] J-Link User Guide
  +
Additional information:
  +
[2]
  +
// <TargetDev>: The device the user wants to connect to.
  +
// IRPre: Sum of IRLens of all devices closer to TDI than <TargetDev>, where IRLen is the number of bits in the IR (Instruction Register) of one device.
  +
// DRPre: Number of devices closer to TDI than <TargetDev>.
  +
// IRPost: Sum of IRLens of all devices closer to TDO than <TargetDev>, where IRLen is the number of bits in the IR (Instruction Register) of one device.
  +
// DRPost: Number of devices closer to TDO than <TargetDev>.
  +
// IRLenDevice: Instruction register length of <TargetDev>
  +
//
  +
*/
  +
 
/*********************************************************************
 
/*********************************************************************
 
*
 
*
Line 153: Line 208:
 
//
 
//
 
return 0;
 
return 0;
  +
}
}</source>
 
  +
</syntaxhighlight>
  +
</div></div>

Revision as of 11:54, 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

Example code
/***********************************************************************
*                    SEGGER Microcontroller GmbH                       *
*                        The Embedded Experts                          *
************************************************************************
*                                                                      *
*                  (c) SEGGER Microcontroller GmbH                     *
*                        All rights reserved                           *
*                          www.segger.com                              *
*                                                                      *
************************************************************************
*                                                                      *
************************************************************************
*                                                                      *
*                                                                      *
*  Licensing terms                                                     *
*                                                                      *
* The use in source and binary forms, with or without modification,    *
* is permitted for internal use only. The redistribution to any        *
* third party is prohibited.                                           *
*                                                                      *
*                                                                      *
* THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDER "AS IS" AND ANY        *
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE    *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR   *
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER BE        *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,     *
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,             *
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR   *
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY  *
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT         *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    *
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH     *
* DAMAGE.                                                              *
*                                                                      *
************************************************************************

-------------------------- END-OF-HEADER -----------------------------

Purpose: Template showing basic device identification.
Literature:
  [1]  J-Link User Guide
Additional information:
  [2]
  //   <TargetDev>: The device the user wants to connect to.
  //   IRPre:       Sum of IRLens of all devices closer to TDI than <TargetDev>, where IRLen is the number of bits in the IR (Instruction Register) of one device.
  //   DRPre:       Number of devices closer to TDI than <TargetDev>.
  //   IRPost:      Sum of IRLens of all devices closer to TDO than <TargetDev>, where IRLen is the number of bits in the IR (Instruction Register) of one device.
  //   DRPost:      Number of devices closer to TDO than <TargetDev>.
  //   IRLenDevice: Instruction register length of <TargetDev>
  //
*/

/*********************************************************************
*
*       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;
}