/***********************************************************************
*                    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: Script which shows how to enable the additional SRAM1 area of the Renesas RZ/G3E.

Literature:
  [1]  J-Link User Guide
*/

/*********************************************************************
*
*       Constants (similar to defines)
*
**********************************************************************
*/

__constant U32 _CPG_BUS_12_MSTOP = 0x40420D2C;  // CPG_BUS_12_MSTOP Register
__constant U32 _INDEX_AHB_AP     = 2;           // Cortex-M33

/*********************************************************************
*
*       Static code
*
**********************************************************************
*/

/*********************************************************************
*
*       _WriteViaAP()
*
*  Function description
*    This function writes the AP_ADDR and AP_DATA register.
*    It can be used to write a value to a specific address via the DAP
*
*  Notes
*    (1) This function assumes that the correct AP / bank has been selected in the JLINK_CORESIGHT_DP_REG_SELECT register
*    (2) This function rlies on that the correct access type has been configured in the JLINK_CORESIGHT_AP_REG_CTRL register
*/
static void _WriteViaAP(U32 Addr, U32 Data) {
  JLINK_CORESIGHT_WriteDAP(JLINK_CORESIGHT_AP_REG_ADDR, _INDEX_AHB_AP, Addr);
  JLINK_CORESIGHT_WriteDAP(JLINK_CORESIGHT_AP_REG_DATA, _INDEX_AHB_AP, Data);
}

/*********************************************************************
*
*       Global functions
*
**********************************************************************
*/

/*********************************************************************
*
*       SetupTarget()
*
*  Function description
*    https://wiki.segger.com/J-Link_script_files#SetupTarget.28.29
*
*  Return value
*    >= 0  O.K.
*     < 0  Error
*
*  Notes
*    https://wiki.segger.com/J-Link_script_files#SetupTarget.28.29
*/
int SetupTarget(void) {
  _WriteViaAP(_CPG_BUS_12_MSTOP, 0x02000000);  // Set 0x02000000 to CPG_BUS_12_MSTOP to enable SRAM1 area. 
  return 0;
}
