/***********************************************************************
*                    SEGGER Microcontroller GmbH                       *
*                        The Embedded Experts                          *
************************************************************************
*                                                                      *
*                  (c) SEGGER Microcontroller GmbH                     *
*                        All rights reserved                           *
*                          www.segger.com                              *
*                                                                      *
************************************************************************
*                                                                      *
************************************************************************
*                                                                      *
*                                                                      *
*  Licensing terms                                                     *
*                                                                      *
* This software may be distributed to your customers free of charge.   *
* This grant of redistribution does not entitle YOU or enduser to      *
* receive from SEGGER hard-copy documentation, technical support,      *
* phone assistance, or enhancements or updates to the Software unless  *
* a specific agreement clearly states otherwise.                       *
*                                                                      *
*                                                                      *
* 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: Example showing the JTAG => SWD switching sequence as
         documented by ARM.
Literature:
  [1]  J-Link documentation: https://wiki.segger.com/J-Link_script_files
  [2]  Cortex-M Technical Reference Manual
*/

/*********************************************************************
*
*       Constants
*
**********************************************************************
*/

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

/*********************************************************************
*
*       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) {
  U32 BitPos;
  U32 Id;
  //
  // Read IDCODE via JTAG_Store* and JTAG_Write API
  //
  // In our example, we do only have one device with IRLen == 4 in the setup.
  // Thus, we only have to set the JTAG_IRLen variable to make sure we write to the correct device.
  // alternatively, the
  // JLINK_CORESIGHT_Configure()
  // function should be used.
  //
  JTAG_IRLen = 4;                      // We want to communicate with the device with IRLen == 4.
  //
  // Option 1:
  //   Using JLINK_JTAG_Store*-API:
  //   The commands are not written, but first stored in the J-Link DLL, and written with the first command that forces a write.
  //   Commands that force a write are e.g. the JLINK_JTAG_Write*-Functions or the JTAG_GetU32() function.
  //
  JLINK_JTAG_StoreIR(0xE);             // Store read JTAG-DP IDCODE register
  BitPos = JLINK_JTAG_StoreDR(0, 32);  // Store Read from DR
  Id = JTAG_GetU32(BitPos);            // Force write and Read data.
  JLINK_SYS_Report1("JLINK_JTAG_Store* - IDCode: ", Id);
  //
  // Option 2:
  //   Using JLINK_JTAG_Write*-API:
  //   The commands are not written, but first stored in the J-Link DLL, and written with the first command that forces a write.
  //   Commands that force a write are e.g. the JLINK_JTAG_Write*-Functions or the JTAG_GetU32() function.
  //
  JLINK_JTAG_WriteIR(0xE);             // Read JTAG-DP IDCODE register
  BitPos = JLINK_JTAG_WriteDR(0, 32);  // Get ID
  Id = JTAG_GetU32(BitPos);            // Force write and Read data.
  JLINK_SYS_Report1("JLINK_JTAG_Write* - IDCode: ", Id);
  return 0;
}

/*************************** end of file ****************************/
