/***********************************************************************
*                    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 script file showing how to switch,
         write and read AHB-APs.
Literature:
  [1]  Reference manual: https://wiki.segger.com/J-Link_script_files
*/

/*********************************************************************
*
*       Constants (similar to defines)
*
**********************************************************************
*/
__constant U32 _DAP_ACC_32BIT_AUTO_INC        = (1 << 29) | (1 << 25) | (1 << 24) | (1 << 4) | (2 << 0);  // HMASTER = DEBUG, Private access, Auto-increment, Access size: word;
__constant U32 _DAP_ACC_8BIT_NO_AUTO_INC      = (1 << 29) | (1 << 25) | (1 << 24) | (0 << 4) | (0 << 0);  // HMASTER = DEBUG, Private access, no Auto-increment, Access size: byte;
__constant U32 _DAP_ACC_16BIT_NO_AUTO_INC     = (1 << 29) | (1 << 25) | (1 << 24) | (0 << 4) | (1 << 0);  // HMASTER = DEBUG, Private access, no Auto-increment, Access size: half word;
__constant U32 _DAP_ACC_32BIT_NO_AUTO_INC     = (1 << 29) | (1 << 25) | (1 << 24) | (0 << 4) | (2 << 0);  // HMASTER = DEBUG, Private access, no Auto-increment, Access size: word;
__constant U32 _DAP_MASS_ERASE                = (0x020C);   
__constant U32 _DAP_FACTORY_RESET             = (0x020A);
__constant U32 _DAP_DATA_EXCHANGE             = (0x00EE);
__constant U32 _DAP_WFD                       = (0x0206);    
__constant U32 _DAP_PWR_AP_DPREC_LPM	      = (0x9 << 16) | (1 << 20) | (1 << 3);
__constant U32 _PASSWORD_1                    = (0x12345678);
__constant U32 _PASSWORD_2                    = (0x12345678);
__constant U32 _PASSWORD_3                    = (0x12345678);
__constant U32 _PASSWORD_4                    = (0x12345678);
__constant U32 _FACTORY_RESET_PROCESSED       = (0x0000000A);
__constant U32 _CLEAR_WFD                     = (1 << 17);
int phase                                     = (0);
/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

/*********************************************************************
*
*       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.
*    (3) This function is called before J-Link performed any communication with the device
*/

int InitTarget(void)
{
    U32 AccessPoint;
    U32 Bank;
    U32 Data;
    U32 DestAddr;
    U32 secap_rxctl_read;
    U32 secap_txctl_read;
    U32 secap_rxdata_read;
    U32 cmd;
    U32 counter;
    U32 secap_ctl_mask;
    U32 secap_txctl_transmit_full_mask;
    U32 accessPoint_response;
    U32 accessPoint_cmd;
    U32 secap_cmdMask;
    U32 dssm_cmd_received;
    U32 empty_buffer;
    U32 command_passed;

    cmd                            = _DAP_MASS_ERASE; // command we wish to use
    dssm_cmd_received              = 0x00000100;
    secap_cmdMask                  = 0x000000FE;
    secap_txctl_transmit_full_mask = 0x00000001;
    secap_ctl_mask                 = 0x0000FFFF;
    empty_buffer                   = 0x00000000;
    counter                        = 0;
    AccessPoint                    = 2; // Device specific AP
    Bank                           = 0;

    counter = 0;

    JLINK_SYS_Report("Beginning sequence");
    JLINK_TIF_ActivateTargetReset();          // Pulling nRST line low
    JLINK_SYS_Sleep(100);                     // Giving JLink some time to pull the nRST line low
    JLINK_CORESIGHT_WriteDP(JLINK_CORESIGHT_DP_REG_SELECT, (AccessPoint << 24) | (Bank << 0));   // Switching the access point to SEC-AP
    JLINK_CORESIGHT_WriteAP(0, 0x00);         // Clearing out the mailbox
    JLINK_CORESIGHT_WriteAP(1, cmd);          // Setting the command to factory reset
    JLINK_SYS_Sleep(100);                     // Giving some time to make sure the command enters the mailbox
    JLINK_TIF_ReleaseTargetReset();
    secap_rxctl_read = JLINK_CORESIGHT_ReadAP(3);
    secap_rxctl_read = secap_rxctl_read & secap_ctl_mask;
    if ((secap_rxctl_read & secap_txctl_transmit_full_mask) == secap_txctl_transmit_full_mask)
    {
        JLINK_SYS_Report("Command successfully sent");
        command_passed = 1;
    }


    phase       = 1;
    AccessPoint = 4;
    JLINK_CORESIGHT_WriteDP(JLINK_CORESIGHT_DP_REG_SELECT, (AccessPoint << 24) | (Bank << 0));               // switching to pwr-ap
    JLINK_CORESIGHT_WriteAP(0x00, _DAP_PWR_AP_DPREC_LPM); // enable low-power mode handling

    return 0;
}