/*********************************************************************
*
*  ConfigTargetSettings()
*
*  Function description
*    Called before InitTarget(). Mainly used to set some global DLL variables to customize the normal connect procedure.
*    For ARM CoreSight devices this may be specifying the base address of some CoreSight components (ETM, ...)
*    that cannot be automatically detected by J-Link due to erroneous ROM tables etc.
*    May also be used to specify the device name in case debugger does not pass it to the DLL.
*
*  Notes
*    (1) May not, under absolutely NO circumstances, call any API functions that perform target communication.
*    (2) Should only set some global DLL variables
*/

/* JTAG Chaining
*
*    J-Link TDI -> #5(K1986VE9x) -> #4-3(K1901VC1QI) -> #2(K1986VK01GI) -> #1(K1986VE1x) -> #0(K1986VE8T) -> J-Link TDO
*/

int MCU_K1986VE9x          = 0;
int MCU_K1901VC1QI_RISC    = 0;
int MCU_K1986VK01GI        = 0;
int MCU_K1986VE1x          = 1;
int MCU_K1986VE8T          = 0;

void ConfigTargetSettings(void)
{
    JLINK_JTAG_SetDeviceId(0, 0x4BA00477);  // IRLen: 4, K1986VE8T
    JLINK_JTAG_SetDeviceId(1, 0x4BA00477);  // IRLen: 4, K1986VE1x
    JLINK_JTAG_SetDeviceId(2, 0x4BA00477);  // IRLen: 4, K1986VK01GI
    JLINK_JTAG_SetDeviceId(3, 0x0D400FDF);  // IRLen: 8, K1901VC1QI, DSP
    JLINK_JTAG_SetDeviceId(4, 0x4BA00477);  // IRLen: 4, K1901VC1QI, RISC
    JLINK_JTAG_SetDeviceId(5, 0x4BA00477);  // IRLen: 4, K1986VE9x

    if (MCU_K1986VE9x)
    {
        JLINK_SYS_Report("J-Link script file: Manually configuring JTAG chain. Selected K1986VE9x.");
        //
        // Pre-select K1986VE9x RISC to be the one J-Link shall communicate with, for this session
        //
        JLINK_JTAG_IRPre  = 24;  // Sum of IRLen of all JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_DRPre  = 5;   // Number of JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_IRPost = 0;   // Sum of IRLen of all JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_DRPost = 0;   // Number of JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_IRLen  = 4;   // IRLen of device we want to communicate with
    }

    else if (MCU_K1901VC1QI_RISC)
    {
        JLINK_SYS_Report("J-Link script file: Manually configuring JTAG chain. Selected K1901VC1QI RISC.");
        //
        // Pre-select K1901VC1QI RISC to be the one J-Link shall communicate with, for this session
        //
        JLINK_JTAG_IRPre  = 20;  // Sum of IRLen of all JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_DRPre  = 4;   // Number of JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_IRPost = 4;   // Sum of IRLen of all JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_DRPost = 1;   // Number of JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_IRLen  = 4;   // IRLen of device we want to communicate with
    }

    else if (MCU_K1986VK01GI)
    {
        JLINK_SYS_Report("J-Link script file: Manually configuring JTAG chain. Selected K1986VK01GI.");
        //
        // Pre-select K1986VK01GI to be the one J-Link shall communicate with, for this session
        //
        JLINK_JTAG_IRPre  = 8;  // Sum of IRLen of all JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_DRPre  = 2;   // Number of JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_IRPost = 16;  // Sum of IRLen of all JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_DRPost = 3;   // Number of JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_IRLen  = 4;   // IRLen of device we want to communicate with
    }

    else if (MCU_K1986VE1x)
    {
        JLINK_SYS_Report("J-Link script file: Manually configuring JTAG chain. Selected K1986VE1x.");
        //
        // Pre-select K1986VE1x to be the one J-Link shall communicate with, for this session
        //
        JLINK_JTAG_IRPre  = 4;   // Sum of IRLen of all JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_DRPre  = 1;   // Number of JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_IRPost = 20;  // Sum of IRLen of all JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_DRPost = 4;   // Number of JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_IRLen  = 4;   // IRLen of device we want to communicate with
    }

    else if (MCU_K1986VE8T)
    {
        JLINK_SYS_Report("J-Link script file: Manually configuring JTAG chain. Selected K1986VE8T.");
        //
        // Pre-select K1986VE8T to be the one J-Link shall communicate with, for this session
        //
        JLINK_JTAG_IRPre  = 0;   // Sum of IRLen of all JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_DRPre  = 0;   // Number of JTAG TAPs preceding the one we want to communicate with
        JLINK_JTAG_IRPost = 24;  // Sum of IRLen of all JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_DRPost = 5;   // Number of JTAG TAPs following the one we want to communicate with
        JLINK_JTAG_IRLen  = 4;   // IRLen of device we want to communicate with
    }

    return 0;
}

/*************************** End of file ****************************/
